-
Notifications
You must be signed in to change notification settings - Fork 43
/
AmazonAlert.py
156 lines (130 loc) · 5.06 KB
/
AmazonAlert.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
# ----- Plotting Configuration -------------------------------------------------
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
from matplotlib.dates import DateFormatter
import numpy as np
import datetime as dt
def plotDatePrice(productID, productTitle, data):
# Data setup
x, y = [], []
for datapoint in data:
date = datapoint.split('|')[0]
price = float(datapoint.split('|')[1])
x.append(dt.datetime.strptime(date, '%Y-%m-%d'))
y.append(price)
x = matplotlib.dates.date2num(x)
x_np, y_np = np.array(x), np.array(y)
# Plot setup
ax = plt.figure(figsize=(6, 3)).add_subplot(111)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.plot(x_np, y_np, color='lightblue', lw=2)
ax.margins(0.05)
ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: ('$%i' % (x))))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
plt.yticks(fontsize=8)
plt.ylim(ymin=min(y)*0.7, ymax=max(y)*1.3)
plt.title('Recent Price History\n'+productTitle, weight ='light', fontsize=12, y=1.08)
plt.xticks(rotation=40, fontsize=7)
plt.tight_layout()
plt.savefig(productID+'.png')
return productID+'.png'
# ----- Email Configuration ----------------------------------------------------
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def sendEmail(product, graph, EMAIL_CREDENTIALS):
# Gmail credentials
self = EMAIL_CREDENTIALS[0]
password = EMAIL_CREDENTIALS[1]
fromAddr = EMAIL_CREDENTIALS[2]
toAddr = EMAIL_CREDENTIALS[3]
# Handle base
msg = MIMEMultipart()
msg['From'] = fromAddr
msg['To'] = toAddr
msg['Subject'] = "Price Alert: " + product
msgText = MIMEText('<center><br><img src="cid:image"><br></center>', 'html')
msg.attach(msgText)
# Embed image
image = open(graph, 'rb')
msgImage = MIMEImage(image.read())
msgImage.add_header('Content-ID', '<image>')
msg.attach(msgImage)
image.close()
# Send email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(self, password)
text = msg.as_string()
server.sendmail(fromAddr, toAddr, text)
server.quit()
# ----- Amazon API -------------------------------------------------------------
from amazon.api import AmazonAPI
import csv
import time
def readPrices(csvFile):
priceData = {}
targetData = {}
with open(csvFile, 'rb') as infile:
for line in infile:
row = line.strip('\n\r').split(',')
column = row[0].split('|')
priceData[column[0]] = row[1:]
targetData[column[0]] = column[1]
infile.close()
return priceData, targetData
def updatePrices(newPriceData, oldPriceData):
for pair in newPriceData:
product = pair[0]
price = pair[1]
try: oldPriceData[product].append(price)
except KeyError:
print "Product %s was skipped." % (product)
print "It has not been initalized with an alert price!"
return oldPriceData
def writePrices(newPriceData, targetData, csvFile):
with open(csvFile, 'wb') as outfile:
writer = csv.writer(outfile)
for product in newPriceData:
target = targetData[product]
writer.writerow([product+'|'+target]+newPriceData[product])
outfile.close()
def getPrice(productID, AWS_CREDENTIALS):
amazon = AmazonAPI(AWS_CREDENTIALS[0], AWS_CREDENTIALS[1], AWS_CREDENTIALS[2])
result = amazon.lookup(ItemId=productID)
return result.title, result.price_and_currency[0]
def addProduct(productID, csvFile, alertWhen, alertType, AWS_CREDENTIALS):
currentPrice = getPrice(productID, AWS_CREDENTIALS)[1]
if alertType == "percentChange":
delta = (float(alertWhen)/100)+1
alertPrice = currentPrice*delta
elif alertType == "desiredPrice":
alertPrice = float(alertWhen)
else: raise ValueError('Invalid alertType')
with open(csvFile, 'a') as outfile:
writer = csv.writer(outfile)
writer.writerow([productID+'|'+str(alertPrice)])
outfile.close()
def dailyScan(productIDs, csvFile, AWS_CREDENTIALS, EMAIL_CREDENTIALS):
prices, targets = readPrices(csvFile)
alerts = []
update = []
for productID in productIDs:
title, price = getPrice(productID, AWS_CREDENTIALS)
date = time.strftime("%Y-%m-%d")
update.append((productID,date+'|'+str(price)))
try:
if price <= float(targets[productID]):
alerts.append(productID)
except KeyError: pass
updatedPrices = updatePrices(update, prices)
writePrices(updatedPrices, targets, csvFile)
for alert in alerts:
graph = plotDatePrice(alert, title, updatedPrices[alert])
sendEmail(title, graph, EMAIL_CREDENTIALS)