-
Notifications
You must be signed in to change notification settings - Fork 3
/
Product.py
85 lines (69 loc) · 2.63 KB
/
Product.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
from all_imports import *
class Product:
def __init__(self, pid):
self.pid = pid
pass
def giveNameToSelf(self):
self.pname = getProductNameFromId(pid)
def getProductNameFromId(self):
url = URL('http://www.amazon.in/gp/product/' + self.pid + '/')
html = url.fetch()
if html == -1:
sys.exit('~\n~\n~\n~\nUNABLE TO FETCH URL')
preg_name = re.compile(
'<span id="productTitle" class="a-size-large">(.*)?<\/span>')
pname = preg_name.findall(html)
pname = pname[0]
re.sub(r"<.*?>", "", pname)
self.pname = pname
def updatePrice(self):
url = URL('http://www.amazon.in/gp/product/' + self.pid)
html = url.fetch()
preg_price = re.compile('<span id="priceblock_(.*)?<\/span>')
priceSpans = preg_price.findall(html)
priceDict = {
'saleprice': 0,
'dealprice': 0,
'ourprice': 0,
'bookprice': 0}
ebook = True
for span in priceSpans:
priceType = span[:span.find('"')]
price = span[span.rfind('>') + 1:].replace(',', '').strip()
priceDict[priceType] = float(price)
ebook = False
# special case for ebooks when all the above price are 0
bookprice = ''
if ebook:
rebooks = 'currencyINR"> <\/span>[ ]+[\d,]+\.[\d]+<\/span>'
preg_name = re.compile(rebooks)
pricespans = preg_name.findall(html)
bookprice = []
for span in pricespans:
bookprice.append(re.findall('\d+', span)[0])
priceDict['bookprice'] = ','.join(bookprice)
print self.pid, priceDict
db = DB()
db.insert_price_table(
pid=self.pid,
saleprice=priceDict['saleprice'],
dealprice=priceDict['dealprice'],
ourprice=priceDict['ourprice'],
bookprice=priceDict['bookprice'])
def isTracked(self):
db = DB()
db.cursor.execute(
"SELECT COUNT(*) FROM tracking WHERE id = ?", [self.pid])
return (db.cursor.fetchone()[0] == 1)
def startTracking(self):
db = DB()
if self.isTracked():
print 'Tracked already : ' + self.pid
elif not self.isTracked():
print 'Untracked Product : ' + self.pid
print 'Fetching Details.'
self.getProductNameFromId()
print 'Product Name : ' + self.pname
print 'Starting to track.'
db.insert_tracking_table(pid=self.pid, name=self.pname)
print 'Product added successfully.\n'