-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
98 lines (73 loc) · 2.63 KB
/
main.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
import sys
import os.path
import config
import value
from amazon.api import AmazonAPI
from twilio.rest import TwilioRestClient
amazon = AmazonAPI(config.amazon_key, config.amazon_secret, config.amazon_tag)
twilio = TwilioRestClient(config.twilio_sid, config.twilio_token)
def getProduct(productName):
try:
product = amazon.search_n(1, Keywords=productName, SearchIndex='All')
return product
except:
print ("Product not found")
sys.exit()
def getPriceStatus(price1, price2, product):
if (price1 > product):
return 1 # When current deal is cheaper than last purchase
elif (price2 > product):
return 2 # When current deal is cheaper than yesterday deal
else:
return 0
def isValidPrice(num):
try:
float(num)
return True
except ValueError:
return False
def getLastPrice(product):
if (os.path.exists('price_values.txt')):
file = open('price_values.txt')
lines = file.readlines()
last_price = float(lines[-1])
else:
last_price = product
return last_price
def sendMessage(status, product, lastPrice):
product_name = product[0].title
product_price = (product[0].price_and_currency)[0]
if (status == 1):
message = twilio.messages.create(body="The current price on Amazon for "
+ product_name + " $" + str(product_price) + " is lower than your last purchase: $"
+ value.price, from_=config.from_number, to=config.to_number)
elif (status == 2):
message = twilio.messages.create(body="The current price on Amazon for "
+ product_name + " $" + str(product_price) + " is lower than yesterday price: $"
+ str(lastPrice), from_=config.from_number, to=config.to_number)
# --------------------------------------------------------
name = value.name
price = value.price
product = getProduct(value.name)
product_price = (product[0].price_and_currency)[0]
print ("Best match: " + product[0].title)
if isValidPrice(price):
price = float(price)
else:
price = product_price
lastPrice = getLastPrice(product_price)
status = getPriceStatus(price, lastPrice, product_price)
if (status != 0):
if (status == 1):
print ("The current price on Amazon: " + "$"
+ str(product_price) + " is lower than your last purchase!")
sendMessage(1, product, lastPrice)
elif (status == 2):
print ("The current price on Amazon: " + "$"
+ str(product_price) + " is lower than yesterday price $" + str(lastPrice))
sendMessage(2, product, lastPrice)
outputFile = open('price_values.txt', 'a')
outputFile.write(str(product_price) + '\n')
outputFile.close()
else:
print ("Nothing to report")