-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice-tracker.py
68 lines (53 loc) · 2.35 KB
/
price-tracker.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
import bottlenose
import MySQLdb
import xmltodict
import ConfigParser
def getKey(item,keys):
cnt=len(keys)
for idx in range(0,cnt):
if keys[idx] in item.keys():
item=item[keys[idx]]
else:
return ''
return item
config=ConfigParser.ConfigParser()
config.read("amazon.yaml")
amazon=bottlenose.Amazon(config.get('Amazon','access_key'),config.get('Amazon','secret_key'),config.get('Amazon','associate_tag'),Region="IN",MaxQPS=0.9)
db=MySQLdb.connect("localhost","root","","ameb")
cursor=db.cursor()
sql="SELECT ft_product.id,id_asin,asin,shipping,ebay_fee_percentage,paisapay_fee_percentage from ft_product,ft_asin WHERE active=1 AND id_asin=ft_asin.id AND date_last_scanned<(NOW()- INTERVAL 2 MINUTE)"
#sql="SELECT ft_product.id,id_asin,asin from ft_product,ft_asin WHERE id_asin=ft_asin.id "
cursor.execute(sql)
results=cursor.fetchall()
for row in results:
id=row[0]
asin=row[2]
shipping=row[3]
e_fp=row[4]
p_fp=row[5]
response = amazon.ItemLookup(ItemId=asin, ResponseGroup="Offers")
print response
xml = xmltodict.parse(response)
items = xml['ItemLookupResponse']['Items']['Item']
if not isinstance(items, list):
items = [items]
cnt = len(items)
for idx in range(0,cnt):
amount = getKey(items[idx], ['Offers', 'Offer','OfferListing', 'SalePrice','Amount'])
if len(amount)<=0:
amount = getKey(items[idx], ['Offers', 'Offer', 'OfferListing', 'Price', 'Amount'])
if(len(amount)<=0):
amount = getKey(items[idx], ['OfferSummary', 'LowestNewPrice', 'Amount'])
amount = int(amount) / 100.0
availability = getKey(items[idx], ['Offers', 'Offer', 'OfferListing', 'AvailabilityAttributes', 'AvailabilityType'])
total = amount + shipping
total = total + total * (e_fp + p_fp) / 100.0
fee = total * (e_fp + p_fp) / 100.0
total = amount + shipping + fee
e_ff = total * e_fp / 100.0
p_ff = total * p_fp / 100.0
profit = total - amount - fee
sql="UPDATE ft_product SET ebayprice=" + str(total) + ",ebay_final_fee=" + str(e_ff) + ",paisapay_final_fee=" + str(p_ff) + ",profit=" + str(profit) + ",price=" + str(amount) + ",availability='" + str(availability) + "',date_last_scanned=NOW() WHERE id=" + str(id)
print sql
cursor.execute(sql)
db.commit()