· 6 years ago · Jan 23, 2020, 11:36 AM
1from bs4 import BeautifulSoup
2import urllib.request
3import urllib.parse
4from datetime import datetime, timedelta
5import json
6
7'''
8Requires:
9- BeautifulSoup4
10- IFTTT Recipe for Webhook: Rich Notification with Message Content = value1 and URL = value2
11- OpenWeatherMap free account API key
12'''
13
14######################################
15
16# OpenWeather API Key (str)
17OW_API_KEY = ''
18
19# IFTTT KEY (str)
20IFTTT_KEY = ''
21
22# IFTTT_RECIPE_NAME (str)
23IFTTT_RECIPE_NAME = ''
24
25# Cloud threshold between 0 - 100 (int). Notification is triggered if value is below this.
26CLOUD_THRESHOLD = 25
27
28# Visibility Threshold (float). Objects are generally visible if this value is below 3. Notification is triggered if value is below this.
29VISIBILITY_THRESHOLD = 2.5
30
31# (str) Select your city from the top left at https://heavens-above.com/AllPassesFromLaunch.aspx and use that link (ends with ?lat=...&lng=...&loc=...&tz=...)
32HEAVENS_ABOVE_URL = "https://heavens-above.com/AllPassesFromLaunch.aspx?lat=47.3769&lng=8.5417&loc=Z%c3%bcrich&alt=0&tz=CET"
33
34# (str) City ID for OpenWeather from https://openweathermap.org/find?q=
35CITY_ID = ''
36
37#######################################
38
39ow_api_url = 'https://api.openweathermap.org/data/2.5/forecast?id='+CITY_ID+'&APPID='+OW_API_KEY
40
41ifttt_url = "https://maker.ifttt.com/trigger/"+IFTTT_RECIPE_NAME+"/with/key/"+IFTTT_KEY
42
43weather_data = urllib.request.urlopen(ow_api_url)
44
45weather_json = json.loads(weather_data.read())
46
47class happening:
48 def __init__(self, date, url, time, name, visibility, startDirection, startDegs, highDegs, endDegs):
49 self.date = date
50 self.time = time
51 self.url = url
52 self.name = name
53 self.visibility = visibility
54 self.startDirection = startDirection
55 self.startDegs = startDegs
56 self.highDegs = highDegs
57 self.endDegs = endDegs
58 self.datetime = datetime.strptime(str(datetime.now().year)+" "+date+" "+time, "%Y %d %B %H:%M:%S")
59
60 def __str__(self):
61 return(self.date+" "+self.time+": "+self.name+", Visibility: "+str(self.visibility))
62
63 def __repr__(self):
64 return(self.date+" "+self.time+": "+self.name+" Visibility: "+str(self.visibility))
65
66def checkViz(thing):
67 for item in weather_json['list']:
68 timey = datetime.strptime(item['dt_txt'], '%Y-%m-%d %H:%M:%S')
69 if timey<=thing.datetime<=timey+timedelta(hours=4):
70 if item['clouds']['all'] <= CLOUD_THRESHOLD:
71 info_str = "Good chances for {}, Clouds: {}%".format(thing, item['clouds']['all'])
72 _url = thing.url
73 return [info_str, _url]
74
75
76
77_page = urllib.request.urlopen(HEAVENS_ABOVE_URL)
78_html = _page.read()
79_soup = BeautifulSoup(_html,'html.parser')
80clickableRows = _soup.find_all('tr',class_='clickableRow')
81for row in clickableRows:
82 tds = row.find_all('td')
83 if float(tds[2].text) <= VISIBILITY_THRESHOLD:
84 thingie = happening(date=tds[0].text, url="https://heavens-above.com/"+tds[6].find_all("a")[0]['href'], time=tds[3].text, name=tds[1].text, visibility=float(tds[2].text), startDirection=tds[5].text, startDegs=tds[4].text, highDegs=tds[7].text, endDegs=tds[10].text)
85 feck = checkViz(thingie)
86 if feck and datetime.now().date() == thingie.datetime.date():
87 msg_content = feck[0]
88 msg_url = feck[1]
89 f = urllib.parse.quote_plus(msg_content)
90 u = urllib.parse.quote_plus(msg_url)
91 noti_url = ifttt_url+"?value1="+f+"&value2="+u
92 urllib.request.urlopen(noti_url)
93 break
94
95print("All done")
96quit()