forked from snipem/gameprices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psnmailalert.py
executable file
·124 lines (90 loc) · 2.87 KB
/
psnmailalert.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
#!/usr/bin/env python
from psnpricealert.psn import psn
from psnpricealert.utils import utils
import csv
import sys
if (sys.version_info[0] == 2):
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
else:
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
import smtplib
def getMailConfig():
mailConfig = {}
mailConfig = utils.getJsonFile("mailconfig.json")
return mailConfig
def getAlerts(alertsFilename):
alerts = []
with open(alertsFilename) as csvfile:
alertsreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in alertsreader:
alert = {}
alert['cid'] = row[0]
alert['price'] = row[1]
alert['store'] = row[2]
alerts.append(alert)
return alerts
def setAlerts(filename, alerts):
c = csv.writer(open(filename, "w"))
for alert in alerts:
c.writerow([alert['cid'],alert['price'],alert['store']])
def alertIsMatched(alert, item):
return float(psn.getPrice(item)) <= float(alert['price'])
def checkAlertsAndGenerateMailBody(alerts):
bodyElements = []
unmatchedAlerts = list(alerts)
for alert in alerts:
cid = alert['cid']
store = alert['store']
item = psn.getItemForCid(cid,store)
if (item == None):
utils.print_enc("No item found for cid '"+cid+"' in store "+store)
else:
if (alertIsMatched(alert, item)):
bodyElements.append(generateBodyElement(alert, item))
unmatchedAlerts.remove(alert)
body = "\n".join(bodyElements)
return unmatchedAlerts, body
def sendMail(body):
mailConfig = getMailConfig()
msg = MIMEMultipart('alternative')
msg['From'] = mailConfig["from"]
msg['To'] = mailConfig["to"]
msg['Subject'] = "Playstation Network Price Drop"
if (sys.version_info[0] == 2):
sendBody = body.encode('utf-8')
else:
sendBody = body
htmlMail = MIMEText(sendBody, 'html')
msg.attach(htmlMail)
mailServer = smtplib.SMTP(mailConfig["server"])
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(mailConfig["username"],mailConfig["password"])
mailServer.sendmail(mailConfig["from"], msg['To'], msg.as_string())
mailServer.quit()
def generateBodyElement(alert, item):
returnBody = []
returnBody.append("<p><img src='"+psn.getImage(item)+"'/></p>")
returnBody.append("<p>"+psn.getName(item)+"</p>")
returnBody.append("<p>Wished: "+str(alert['price'])+"</p>")
returnBody.append("<p>Is now: "+str(psn.getPrice(item))+"</p>")
return "\n".join(returnBody)
def main():
alertsFilename = "alerts.csv"
alerts = getAlerts(alertsFilename)
alertsRemaining, body = checkAlertsAndGenerateMailBody(alerts)
utils.print_enc("Finished processing")
if (len(body) > 0):
sendMail(body)
utils.print_enc("Mail was sent")
setAlerts(alertsFilename,alertsRemaining)
else:
utils.print_enc("No mail was sent")
exit(0)
if __name__ == "__main__":
main()