-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputs.py
69 lines (55 loc) · 2.43 KB
/
outputs.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
import smtplib
from datetime import datetime
from email.mime.text import MIMEText
from jinja2 import Environment, FileSystemLoader, select_autoescape
jenv = Environment(
loader=FileSystemLoader("templates"),
autoescape=select_autoescape()
)
# Sends an email using Gmail SMTP (make sure to use an App Password)
def send_email(subject, body, sender, recipients, password, html=False):
msg = MIMEText(body, 'html' if html else 'plain')
msg['Subject'] = subject
msg['From'] = f"AMC Showtime Notifier <{sender}>"
msg['To'] = ', '.join(recipients)
# Adding this header prevents emails being grouped into threads
msg.add_header('X-Entity-Ref-ID', 'null')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server:
smtp_server.login(sender, password)
smtp_server.sendmail(sender, recipients, msg.as_string())
def gen_formated_showtimes(showtimes, theatres):
by_films = {}
theatre_keys = [t.split('/')[-1] for t in theatres]
for fk in set([x.film.key for x in showtimes]):
ts = [(t, [s for s in showtimes if s.film.key == fk and s.theatre == t]) for t in theatre_keys]
by_films[fk] = [t for t in ts if len(t[1]) > 0]
body = ""
for (k, ts) in by_films.items():
body += k + "\n"
for t in ts:
body += f" {t[0]}\n"
for s in t[1]:
ds = s.date.strftime("%Y-%m-%d %I:%M %p")
body += f" [{ds}] - {s.link}\n"
body += "\n"
return body
def gen_new_showtimes_html(showtimes, theatres):
template = jenv.get_template("email.html.jinja")
by_films = {}
theatre_keys = [t.split('/')[-1] for t in theatres]
for fk in set([x.film.key for x in showtimes]):
ts = [(t, [s for s in showtimes if s.film.key == fk and s.theatre == t]) for t in theatre_keys]
by_films[fk] = [t for t in ts if len(t[1]) > 0]
return template.render(by_films=by_films,
now=datetime.now().strftime('%Y-%d-%m %H:%M'))
def gen_formated_film_results(film_results):
body = ""
for film in film_results:
body += film.title + "\n"
for t in set([s.theatre for s in film.showtimes]):
body += f" {t}\n"
for s in sorted([s for s in film.showtimes if s.theatre == t], key=lambda x: x.datetime):
ds = s.datetime.strftime("%Y-%m-%d %I:%M %p")
body += f" [{ds}] - {s.link}\n"
body += "\n"
return body