-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (62 loc) · 2.76 KB
/
main.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
from email import message
import keys # remove line,private smtp credentials are stored there
import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
url = requests.get('https://www.windfinder.com/weatherforecast/salzgittersee')
if url.status_code != 200:
print("error while getting weather data")
windArray = [] #stores all wind data in pairs of two, starting from min
rainArray = [] #stores all rain data
minWind = 14 #minimal amount of wind (kts)
maxWind = 30 #maximal amount of wind (kts)
maxRain = 0 #maximal amount of rain (mm/h)
minHours = 1 #minimal amount of hours in which the conditions are met
minTime = 6 #time from which one can sail
maxTime = 21 #time to which one can sail
def sendEmail():
emailSender = keys.emailSenderOwn #put your own credentials here
emailReceiver = keys.emailReceiverOwn #put your own credentials here
emailPassword = keys.emailPasswordOwn #put your own credentials here
smtp_server = smtplib.SMTP(keys.emailServerOwn, 587)
smtp_server.ehlo()
smtp_server.starttls()
smtp_server.ehlo()
smtp_server.login(emailSender, emailPassword)
body = f"The Wind is perfekt at\n\n{sailingPossibilitys}\nHave fun!"
msg_to_send = MIMEText(body)
msg_to_send['Subject'] = "You should go sailing today"
msg_to_send['From'] = f"Sailing Reminder <{emailSender}>"
msg_to_send['To'] = f"<{emailReceiver}>"
print(msg_to_send)
smtp_server.sendmail(emailSender, emailReceiver, msg_to_send.as_string())
smtp_server.quit()
print("email send")
pulled = url.content.decode("utf-8")
soup = BeautifulSoup(url.content, 'html.parser')
s = soup.select("div.weathertable__body > div:nth-of-type(8)")
for index, wind in zip(range(48), soup.select("span.units-ws")) :
windArray.append(float(wind.text))
for index, rain in zip(range(24), soup.select("div.data-rain")) :
if(rain.find("span")) != None:
rainArray.append(float(rain.select('span')[0].text))
else:
rainArray.append(0)
singleSteps = 0
countHour = 23
sail = False
sailingPossibilitys = ""
for x in range(0, 48, 2):
if windArray[x] >= minWind and windArray[x+1] <= maxWind and rainArray[singleSteps] <= maxRain and countHour+1 >= minTime and countHour+1 <= maxTime:
if countHour >= minHours:
dataString = """{hour} o'clock with {mWind} - {xWind}(kts) of wind and {rain} (mm/h) of rain\n""".format(hour = countHour+1, mWind = windArray[x], xWind = windArray[x+1], rain = rainArray[singleSteps])
sailingPossibilitys = sailingPossibilitys + dataString
sail = True
if singleSteps >= 0:
countHour = singleSteps
singleSteps = singleSteps+1
if sail == True:
sendEmail()
with open('scrapes.txt', 'w') as f:
f.write(str(windArray) + str(rainArray))