This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qfxalert.py
136 lines (102 loc) · 3.4 KB
/
qfxalert.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
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from texttable import Texttable
from qfx import QFXScraper
from simple_config import Config
config = Config(
os.path.join(os.environ["HOME"], "qfxalert.json"),
defaults={
"movies": [],
"subscribed_keywords": ["avengers", "infinity", "3d"],
"subscribers": ["self@example.com"],
"from_mail": "hmm@hmm.com",
"mail_user": None,
"mail_password": None,
"mail_host": "localhost",
"mail_port": 1025,
"mail_ssl": False
}
)
def serialize_movie(movie):
fields = ['event_id', 'tickets', 'title']
return {k: getattr(movie, k) for k in fields}
def is_subscribed(title):
yes = False
title_list = title.lower().split(" ")
for keyword in config.subscribed_keywords:
if keyword.lower() in title_list:
yes = True
break
return yes
def alert_when_delta():
scraper = QFXScraper()
pulled = {}
# let's make a list of only relevant movies
for m in scraper.get_movies():
if is_subscribed(m.title):
movie = serialize_movie(m)
pulled[movie['event_id']] = movie
fresh = []
expired = []
old = {}
for movie in config.movies:
if movie['event_id'] in pulled.keys():
old[movie['event_id']] = movie
else:
if is_subscribed(movie['title']):
# sometimes expired movies may have been unsubbed
expired.append(movie)
movies = []
for event_id, new_version in pulled.items():
old_version = old.get(event_id)
movies.append(new_version)
if old_version:
if old_version['tickets'] != new_version['tickets']:
print("Tickets update for: {}".format(new_version['title']))
fresh.append(new_version)
else:
print("New one found: {}".format(new_version['title']))
fresh.append(new_version)
if not fresh:
return
config.update(movies=movies)
printables = []
for m in movies:
if m['tickets']:
m['tickets'] = "https://qfxcinemas.com{tickets}".format(**m)
printable = (
"------------------------------------------------\n"
"Title: {title}\n"
"Tickets: {tickets}\n"
"------------------------------------------------\n"
.format(**m)
)
printables.append(printable)
email_message = """
Yo!!
There's some update in the movie keywords you've subscribed:
{}
""".format("\n".join(printables))
message = MIMEMultipart()
message['Subject'] = 'Movie ticket updates'
message['From'] = config.from_mail
message['To'] = ', '.join(config.subscribers)
message.attach(MIMEText(email_message))
try:
if config.mail_ssl:
server = smtplib.SMTP_SSL(config.mail_host, config.mail_port)
else:
server = smtplib.SMTP(config.mail_host, config.mail_port)
server.ehlo()
if config.mail_password:
server.login(config.mail_user, config.mail_password)
server.sendmail(config.from_mail, config.subscribers, message.as_string())
server.close()
print('Email sent!')
except Exception as e:
print(e)
print('Something went wrong...')
if __name__ == "__main__":
alert_when_delta()