-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.py
106 lines (90 loc) · 3.85 KB
/
sender.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
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from datetime import date
import os
class Sender:
def __init__(self, properties, emailTo):
self.emailTo = emailTo
self.msg = MIMEMultipart()
self.font = "Calibri"
self.fontSize = 14
self.writeEmail(properties, emailTo)
self.sendEmail(emailTo)
def attachImage(self, property):
path = property.getScreenshotPath()
imgData = open(path, "rb").read()
image = MIMEImage(imgData, name=os.path.basename(path))
self.msg.attach(image)
os.remove(path)
def attachLink(self, property):
url = property.getUrl()
link = f"""
View <a href="https://{url}">listing</a></p>
"""
self.msg.attach(MIMEText(link, 'html'))
def attachPrices(self, property):
price = property.getPricePer()
total = property.getTotal()
prices = f"<p>Price per night: £{price}, Total: £{total}</p>"
self.msg.attach(MIMEText(prices, 'html'))
def attachLocation(self, property, distance, name):
if distance:
self.msg.attach(MIMEText(f"<p>The property is roughly {distance}km from Oktoberfest.</p>", 'html'))
else:
self.msg.attach(MIMEText("<p>Could not locate this property.</p>", 'html'))
if name:
distanceName = property.getDistanceName()
distanceHtml = f"<p>, {distanceName}km from Oktoberfest.</p>" if distanceName else "<br>"
location = f"<p>Based near {name}{distanceHtml}</p>"
self.msg.attach(MIMEText(location, 'html'))
else:
self.msg.attach(MIMEText("<p>There were no local points of interest.</p>", 'html'))
def attachCommute(self, property, start):
if start:
commutes = []
start = "rough location"
commutes = property.getCommute()
if commutes:
if commutes[0] != "":
self.msg.attach(MIMEText(f"<p>How to get to Oktoberfest from {start}:</p>", 'html'))
commuteStr = ""
for idx in range(len(commutes)):
self.msg.attach(MIMEText(f"<h4>Option {idx+1}</h4>", 'html'))
commuteStr += f"<p>{commutes[idx]}</p>"
self.msg.attach(MIMEText(commuteStr, 'html'))
else:
self.msg.attach(MIMEText("<p>There were no calculated commutes.</p>", 'html'))
def writeEmail(self, properties, emailTo):
self.msg["From"] = "[email protected]"
self.msg["To"] = emailTo
self.msg["Subject"] = f"Airbnb search {self.getDate()}"
heading = f"<h4>Sir, we found {len(properties)} listing(s) for you.</h4>"
self.msg.attach(MIMEText(heading, 'html'))
if properties:
idx = 1
for property in properties:
self.msg.attach(MIMEText(f"<p><b>{idx}.</b>", 'html'))
self.attachLink(property)
self.attachImage(property)
self.attachPrices(property)
distance = property.getDistance()
name = property.getName()
self.attachLocation(property, distance, name)
self.attachCommute(property, distance)
idx += 1
def sendEmail(self, emailTo):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("[email protected]", "my token")
server.sendmail("[email protected]", emailTo, self.msg.as_string())
server.quit()
@staticmethod
def getDate():
today = date.today()
day = today.day
month = today.strftime('%B')
yearShort = today.strftime('%y')
return f"{day}-{month}-{yearShort}"