-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteepandcheap.sh
executable file
·155 lines (136 loc) · 3.29 KB
/
steepandcheap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# You will need to create the directory $CONF_DIR
# and the filters file $FILTERS_FILE
SAC=steepandcheap
URL=http://www.steepandcheap.com
CONF_DIR=~/.sac
WORKDIR=/tmp/$SAC
OUTPUT_FILE=$WORKDIR/$SAC.html
IMG_FILE=$WORKDIR/$SAC.item.jpg
USER_AGENT="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080311 Iceweasel/2.0.0.13 (Debian-2.0.0.13-0etch1)"
WGET="wget -o /dev/null --user-agent=\"$USER_AGENT\""
FILTERS_FILE=$CONF_DIR/filters
# colors
NO_COLOUR="\033[0m"
RED="\033[1;31m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
YELLOW="\033[1;33m"
# will kill all the instances of the program
kill_all_instances()
{
local pids=`ps fax | grep $SAC | grep -v vim | cut -d ' ' -f 1 | sort -n`
for i in $pids
do
if [[ $i != $$ && $i != $PPID ]] ; then
kill $i
fi
done
}
get_image()
{
local url=`grep "images\[1\] = .http://images.steepandcheap.com/images/items/large" $OUTPUT_FILE | cut -d '"' -f 2`
$WGET --output-document=$IMG_FILE $url
}
get_title()
{
local title=`grep '<title>' $OUTPUT_FILE | cut -d '>' -f 2 | cut -d '<' -f 1 | cut -d ':' -f 2`
echo $title
}
get_price()
{
local price=`grep '<div id="price">' $OUTPUT_FILE | cut -d '>' -f 2 | cut -d '<' -f 1`
echo $price
}
get_percent_off()
{
local price=`grep '<div id="percent_off">' $OUTPUT_FILE | cut -d '>' -f 2 | cut -d '<' -f 1`
echo $price
}
get_page()
{
rm -f $OUTPUT_FILE 2>&1 > /dev/null
$WGET --output-document=$OUTPUT_FILE $URL
}
create_workdir()
{
rm -Rf $WORKDIR
mkdir $WORKDIR
}
print_info()
{
local title=`get_title`
local price=`get_price`
local percent=`get_percent_off`
local color_msg="$YELLOW$title $BLUE $price $NO_COLOUR $percent"
local msg="$title $price $percent"
clear
if [[ $1 == "with color" ]]; then
echo -e -n $color_msg
else
echo -n $msg
fi
}
show_picture()
{
gqview -t $IMG_FILE
}
filter_terms()
{
local ret=0
local title=`get_title`
for i in `cat $FILTERS_FILE`
do
echo $title | grep -i $i
if [[ -z $? ]]; then
ret=1
fi
done
return $ret
}
main()
{
kill_all_instances
create_workdir
cd $WORKDIR
local last_title=
while [[ true ]]
do
get_page
local title=`get_title`
local time_passed=
if [[ $last_title != $title ]]; then
# new item
last_title=$title
get_image
if [[ -z `filter_terms` ]]; then
print_info
else
print_info "with color"
fi
local start_show=`date +%s`
show_picture
local end_show=`date +%s`
time_passed=$((end_show-start_show))
fi
# sleep for random time between 60 to 120
# if the picture took to show more then 5 minutes, then
# we'll not sleep and go again to fetch the page
if [[ $time_passed < $((5*60)) ]]; then
sleep $(( 60 + $(( $RANDOM % 60 )) ))
fi
done
}
main