-
Notifications
You must be signed in to change notification settings - Fork 2
/
source.py
321 lines (266 loc) · 10.6 KB
/
source.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import requests
from bs4 import BeautifulSoup
import sys, select
from urlparse import urlparse
class Item:
def __init__(self, company, url, notif_p):
self.company = company
self.url = url
self.notif_p = notif_p
self.actual_p = 0.0
self.prod_name = ""
if company == "Snapdeal":
self.get_sd_title()
self.get_sd_price()
elif company == "Amazon":
self.get_amazon_title()
self.get_amazon_price()
else:
self.get_fk_title()
self.get_fk_price()
def get_sd_price(self):
try:
r = requests.get(self.url)
soup = BeautifulSoup(r.content, "html.parser")
data = soup.find_all("div", {"class":"row pdp-e-i-PAY"})[0]
span_data = data.find_all("span",{"class":"payBlkBig"})[0]
price_text = span_data.text.strip()
self.actual_p = float(price_text.replace(',',''))
#print self.actual_p
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
def get_sd_title(self):
try:
r = requests.get(self.url)
soup = BeautifulSoup(r.content, "html.parser")
title_data = soup.find_all("meta", {"name":"og_title"})[0]
#print title_data
temp_title = title_data.get("content")
title_words = temp_title.split()
if len(title_words) > 3:
self.prod_name = title_words[0]+' '+title_words[1]+' '+title_words[2]
else:
self.prod_name = temp_title
#print self.prod_name
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
def get_amazon_price(self):
r = requests.get(self.url)
soup = BeautifulSoup(r.content, "html.parser")
data_obtained = 0
if data_obtained == 0 and len(soup.find_all("div", {"id":"price_feature_div"})) > 0: #to fetch banner data
try:
data = soup.find_all("div", {"id":"price_feature_div"})[0]
span_data = data.find_all("span", {"class":"a-size-medium a-color-price"})[0]
self.actual_p = float(span_data.text.strip().replace(',',''))
data_obtained = 1
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
if len(soup.find_all("div", {"class":"sims-fbt-rows"})) > 0: #to get data from this item
try:
data = soup.find_all("div", {"class":"sims-fbt-rows"})
span_data = data[0].find_all("span", {"class":"a-color-price"})[0]
price_text = span_data.text.strip()
self.actual_p = float(price_text.split()[1].replace(',',''))
data_obtained = 1
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
if data_obtained == 0 and len(soup.find_all("div",{"id":"tmmSwatches"})) > 0: #to get data from swatch, mainly in books
try:
data = soup.find_all("div", {"id":"tmmSwatches"})[0]
li_data = soup.find_all("li", {"class":"swatchElement selected"})[0]
span_data = soup.find_all("span",{"class":"a-color-price"})[0]
self.actual_p = float(span_data.text.strip().replace(',',''))
data_obtained = 1
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
if data_obtained == 0 and len(soup.find_all("div", {"id":"buybox"})) > 0: #to fetch data from buybox
try:
data = soup.find_all("div", {"id":"buybox"})[0]
div_data = soup.find_all("div", {"id":"buyNewSection"})[0]
self.actual_p = float(div_data.text.strip().replace(',',''))
data_obtained = 1
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
#print self.actual_p
def get_amazon_title(self):
r = requests.get(self.url)
soup = BeautifulSoup(r.content, "html.parser")
try:
if len(soup.find_all("span",{"id":"productTitle"})) > 0:
title_data = soup.find_all("span",{"id":"productTitle"})[0]
title = title_data.text.strip()
temp_title = title.split()
if len(temp_title) > 4:
self.prod_name = temp_title[0]+' '+temp_title[1]+' '+temp_title[2]+' '+temp_title[3]
else:
self.prod_name = title
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
#print self.prod_name
def get_fk_price(self):
try:
r = requests.get(self.url)
soup = BeautifulSoup(r.content, "html.parser")
r = requests.get(self.url)
soup = BeautifulSoup(r.content, "html.parser")
data = soup.find_all("div", {"class":"prices"})
for item in data:
span_data = item.find_all("span", {"class":"selling-price omniture-field"})[0]
price_text = span_data.text.strip()
self.actual_p = float(price_text.split()[1].replace(',',''))
#print self.actual_p
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
pass
def get_fk_title(self):
try:
r = requests.get(self.url)
soup = BeautifulSoup(r.content, "html.parser")
title_data = soup.find_all("meta",{"name":"Keywords"})[0]
temp_title = title_data.get("content")
title_words = temp_title.split()
if len(title_words) > 3:
self.prod_name = title_words[0]+' '+title_words[1]+' '+title_words[2]
else:
self.prod_name = temp_title
#print self.prod_name
except:
print "\n---------------Incorrect URL or site has been recently modified. Try different item---------------"
pass
def add_item_to_track(items):
try:
print "\nPaste URL of product from Snapdeal/Amazon/Flipkart:"
url = raw_input().strip()
parsed_uri = urlparse(url)
domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
if str.find(domain, "amazon") > -1:
company = "Amazon"
elif str.find(domain, "snapdeal") > -1:
company = "Snapdeal"
elif str.find(domain, "flipkart"):
company = "Flipkart"
else:
print "Not a valid url"
print "\nEnter price below which you want to be notified:"
notif_p = float(raw_input())
temp_item = Item(company, url, notif_p)
if temp_item.actual_p < temp_item.notif_p:
print "\n---------------Item price is already less than notification price---------------"
return
else:
items.append(temp_item)
except:
print "An error occured"
pass
def delete_tracked_item(items):
if len(items) == 0:
print "\n---------------No item to delete---------------"
return
print_item_list(items)
print "\nEnter index of item to be deleted:"
index = int(raw_input())
if index > len(items):
print "\n---------------Number of items is less than given index-------------"
return
elif index < 1:
print "\n---------------Item index can't be less than 1-------------"
return
del_desc = items[index-1]
items.pop(index-1)
print "\nFollowing item successfully deleted."
print del_desc.company, " ",del_desc.prod_name
def edit_notification_price(items):
if len(items) == 0:
print "\n---------------No item to edit---------------"
return
print_item_list(items)
print "\nEnter index of item to be edited:"
index = int(raw_input())
if index > len(items):
print "\n---------------Number of items is less than given index-------------"
return
elif index < 1:
print "\n---------------Item index can't be less than 1---------------"
return
print "\nEnter new notification price:"
new_price = float(raw_input())
if new_price >= items[index-1].actual_p:
print "\n---------------Entered price is already in notification range---------------"
return
items[index-1].notif_p = new_price
def print_item_list(items):
print "\n%-12s %-45s %-18s %-18s" %("Company", "Product", "Present Price", "Notification Price")
for item in items:
print "%-12s %-45s %-18.1f %-18.1f" %(item.company, item.prod_name, item.actual_p, item.notif_p)
def save_item_list(items):
record_file = open("saved_items.txt","w")
for item in items:
record_file.write(item.company+" "+str(item.notif_p)+" "+item.url+"\n")
record_file.close()
def process_input(my_input, items):
my_input = raw_input().strip()
if my_input.lower() == 'q' or my_input.lower() == 'quit':
print "\nDo you want to save item list (\'Y\' or \'N\')"
is_save = raw_input()
if is_save.lower() == 'y':
save_item_list(items)
#write to file here
exit()
elif my_input.lower() == 'h' or my_input.lower() == 'help':
#print help
pass
elif my_input == '1':
#add item
add_item_to_track(items)
elif my_input == '2':
#print item
delete_tracked_item(items)
elif my_input == '3':
print_item_list(items)
elif my_input == '4':
#edit
edit_notification_price(items)
elif my_input == '5':
save_item_list(items)
else:
print "\nInput not recognised. Please enter correct value"
print "\nEnter...\n1 To add item to track\n2 To delete tracked item\n3 To print tracked item list\n4 To edit notification price of item\n5 To save item list\n\'Q\' or \'quit\' to exit from program\n\'h\' or \'help\' to view operating instructions"
def update_notif_p(items):
notify_items = []
for item in items:
if item.company == "Snapdeal":
item.get_sd_price()
elif item.company == "Amazon":
item.get_amazon_price()
else:
item.get_fk_price()
if item.actual_p < item.notif_p:
notify_items.append(item)
if len(notify_items) > 0:
print "\n**********Notification Alert !!! **********\nFollowing items are below their notification price"
print_item_list(notify_items)
if __name__ == "__main__":
items = []
print "Checking for previously saved items..."
try:
saved_items = open("saved_items.txt","r")
all_items = saved_items.readlines()
for item in all_items:
item_values = item.split()
temp_item = Item(item_values[0], item_values[2], float(item_values[1]))
items.append(temp_item)
if len(items) > 0:
print "\nFollowing ",len(items)," items have been loaded from previous save:"
print_item_list(items)
else:
print "\nNo item has been saved previously"
except:
pass
print "\nEnter...\n1 To add item to track\n2 To delete tracked item\n3 To print tracked item list\n4 To edit notification price of item\n5 To save item list\n\'Q\' or \'quit\' to exit from program\n\'h\' or \'help\' to view operating instructions"
while(1):
i, o, e = select.select([sys.stdin], [], [], 20)
if (i):
process_input(i, items)
elif len(items) > 0:
update_notif_p(items)