Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kevin-De-Koninck/9e821979a8c2138ca387c6bb1583cde9 to your computer and use it in GitHub Desktop.
Save Kevin-De-Koninck/9e821979a8c2138ca387c6bb1583cde9 to your computer and use it in GitHub Desktop.
Send mail with info about the concentration of pollen (gras) in the air (Belgium).
# PYTHON 3
# Install modules using pip3 install
import sys
from urllib.request import urlopen
import bs4
# Defines meteovista
mol = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Mol/4053929"
brussel = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Brussel/4053951"
genk = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Genk/4055695"
antwerpen = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Antwerpen/4053795"
leuven = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Leuven/4054147"
drongen = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Drongen/4054729"
brugge = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Brugge/4054322"
mechelen = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Mechelen/4053875"
hasselt = "http://www.meteovista.be/Europa/Belgie/Hooikoorts-Hasselt/4055711"
# Change if needed
stad = antwerpen
stad2 = hasselt
# Defines meteo and airallergy
meteoURL = "http://www.meteo.be/meteo/view/nl/123501-Hooikoorts+allergie+risico.html"
airallergyURL = "https://airallergy.wiv-isp.be/nl"
#-------------------------------------------------------------------------------
#FUNCTION send mail
#-------------------------------------------------------------------------------
def send_the_mail(content, subject):
#Mail configuration
SMTPserver = 'smtp.zoho.com'
sender = 'YourMailHere@YourMailHere.com'
destination = ['YourMailHere@YourMailHere.com']
USERNAME = sender
PASSWORD = "YourPasswordHere"
# typical values for text_subtype are plain, html, xml
text_subtype = 'html'
from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
from email.mime.text import MIMEText
try:
# Create the message
msg = MIMEText(content, text_subtype)
msg['Subject'] = subject
msg['From'] = sender # some SMTP servers will do this automatically, not all
# Set up the connection
conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.quit()
except (Exception, exc) as err:
sys.exit( "mail failed; %s" % err ) # give a error message
#-------------------------------------------------------------------------------
#INFO FROM METEOVISTA.BE
#-------------------------------------------------------------------------------
#Open the pages as a 'soup'
page = urlopen(stad).read()
soup = bs4.BeautifulSoup(page, "html.parser")
page = urlopen(stad2).read()
soup2 = bs4.BeautifulSoup(page, "html.parser")
# Get all the info from the first page/city
pollen = soup.select(".wo.coldandflulayout.two-rows .col4")
titel = soup.select(".page-title")
gekozen_stad = titel[0].a.getText()
pollen_vandaag = pollen[0].div.get('title')
pollen_morgen = pollen[1].div.get('title')
temp = soup.select(".expandable-forecast-text")
pollen_praatje = temp[0].p.getText()
pollen_praatje = pollen_praatje.strip(' \t\n\r') #removes tabs and white spaces
# Get all the info from the second page/city
pollen2 = soup2.select(".wo.coldandflulayout.two-rows .col4")
titel2 = soup2.select(".page-title")
gekozen_stad2 = titel2[0].a.getText()
pollen_vandaag2 = pollen2[0].div.get('title')
pollen_morgen2 = pollen2[1].div.get('title')
temp = soup2.select(".expandable-forecast-text")
#-------------------------------------------------------------------------------
#INFO FROM METEO.BE
#-------------------------------------------------------------------------------
# Open the pages as a 'soup'
page = urlopen(meteoURL).read()
soup = bs4.BeautifulSoup(page, "html.parser")
meteo = soup.select(".table")
table = soup.find('table')
table_body = table.findAll('td')
meteo_pollen = table_body[1].getText()
#-------------------------------------------------------------------------------
# INFO FROM AIRALLERGY.BE
#-------------------------------------------------------------------------------
page = urlopen(airallergyURL).read()
soup = bs4.BeautifulSoup(page, "html.parser")
airallergy = soup.select(".content .node-stationinfo")
#-------------------------------------------------------------------------------
#INFO FROM
#-------------------------------------------------------------------------------
content = "<div style='max-width:65%'>"
if meteo_pollen != "":
content += "<h1>Volgens <a href='http://www.meteo.be/meteo/view/nl/123501-Hooikoorts+allergie+risico.html'>meteo.be</a></h1>" + "<br>"
content += "Het risico op allergie is vandaag <b>" + meteo_pollen + "</b><br><br><br>"
#
content += "<h1>Volgens <a href='http://www.meteovista.be/Europa/Hooikoorts-Belgie/131'>meteovista.be</a></h1>" #+ "<br>"
content += "<h3>Pollenallarm in " + gekozen_stad + "!</h3>" #+ "<br>"
content += "<b>Vandaag:</b> "+pollen_vandaag + "<br>"
content += "<b>Morgen:</b> "+pollen_morgen + "<br>"
content += "<h3>Pollenallarm in " + gekozen_stad2 + "!</h3>" #+ "<br>"
content += "<b>Vandaag:</b> "+pollen_vandaag2 + "<br>"
content += "<b>Morgen:</b> "+pollen_morgen2 + "<br>"
content += "<br><h3>Info:</h3> " + pollen_praatje + "<br>" + "<br>"
#
content += "<h1>Volgens <a href='https://airallergy.wiv-isp.be/nl'>airallergy.wiv-isp.be</a></h1>" + "<br>"
content += str(airallergy[0].div)
content += "</div>" #max-width
# Send the mail
if meteo_pollen != "":
send_the_mail(content,"Risico op allergie: '" + meteo_pollen + "'")
else:
send_the_mail(content,"Hoeveelheid: '" + pollen_vandaag + "'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment