Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup
- import urllib.request
- import urllib.parse
- from datetime import datetime, timedelta
- import json
- '''
- Requires:
- - BeautifulSoup4
- - IFTTT Recipe for Webhook: Rich Notification with Message Content = value1 and URL = value2
- - OpenWeatherMap free account API key
- '''
- ######################################
- # OpenWeather API Key (str)
- OW_API_KEY = ''
- # IFTTT KEY (str)
- IFTTT_KEY = ''
- # IFTTT_RECIPE_NAME (str)
- IFTTT_RECIPE_NAME = ''
- # Cloud threshold between 0 - 100 (int). Notification is triggered if value is below this.
- CLOUD_THRESHOLD = 25
- # Visibility Threshold (float). Objects are generally visible if this value is below 3. Notification is triggered if value is below this.
- VISIBILITY_THRESHOLD = 2.5
- # (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=...)
- HEAVENS_ABOVE_URL = "https://heavens-above.com/AllPassesFromLaunch.aspx?lat=47.3769&lng=8.5417&loc=Z%c3%bcrich&alt=0&tz=CET"
- # (str) City ID for OpenWeather from https://openweathermap.org/find?q=
- CITY_ID = ''
- #######################################
- ow_api_url = 'https://api.openweathermap.org/data/2.5/forecast?id='+CITY_ID+'&APPID='+OW_API_KEY
- ifttt_url = "https://maker.ifttt.com/trigger/"+IFTTT_RECIPE_NAME+"/with/key/"+IFTTT_KEY
- weather_data = urllib.request.urlopen(ow_api_url)
- weather_json = json.loads(weather_data.read())
- class happening:
- def __init__(self, date, url, time, name, visibility, startDirection, startDegs, highDegs, endDegs):
- self.date = date
- self.time = time
- self.url = url
- self.name = name
- self.visibility = visibility
- self.startDirection = startDirection
- self.startDegs = startDegs
- self.highDegs = highDegs
- self.endDegs = endDegs
- self.datetime = datetime.strptime(str(datetime.now().year)+" "+date+" "+time, "%Y %d %B %H:%M:%S")
- def __str__(self):
- return(self.date+" "+self.time+": "+self.name+", Visibility: "+str(self.visibility))
- def __repr__(self):
- return(self.date+" "+self.time+": "+self.name+" Visibility: "+str(self.visibility))
- def checkViz(thing):
- for item in weather_json['list']:
- timey = datetime.strptime(item['dt_txt'], '%Y-%m-%d %H:%M:%S')
- if timey<=thing.datetime<=timey+timedelta(hours=4):
- if item['clouds']['all'] <= CLOUD_THRESHOLD:
- info_str = "Good chances for {}, Clouds: {}%".format(thing, item['clouds']['all'])
- _url = thing.url
- return [info_str, _url]
- _page = urllib.request.urlopen(HEAVENS_ABOVE_URL)
- _html = _page.read()
- _soup = BeautifulSoup(_html,'html.parser')
- clickableRows = _soup.find_all('tr',class_='clickableRow')
- for row in clickableRows:
- tds = row.find_all('td')
- if float(tds[2].text) <= VISIBILITY_THRESHOLD:
- 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)
- feck = checkViz(thingie)
- if feck and datetime.now().date() == thingie.datetime.date():
- msg_content = feck[0]
- msg_url = feck[1]
- f = urllib.parse.quote_plus(msg_content)
- u = urllib.parse.quote_plus(msg_url)
- noti_url = ifttt_url+"?value1="+f+"&value2="+u
- urllib.request.urlopen(noti_url)
- break
- print("All done")
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement