Created
March 22, 2018 02:10
-
-
Save ask-compu/1a359579d99fc66c5e2db92ae25be10c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import json | |
import urllib | |
import os | |
import time | |
hourAgo = time.time() - 3600 | |
def getWeather(): | |
if os.path.getmtime("weather.pkl") < hourAgo: | |
weatherUrl = "https://api.wunderground.com/api/[apikeygoeshere]/conditions/forecast/alerts/q/14892.json" | |
response= urllib.urlopen(weatherUrl) | |
rec_bytes=response.read() | |
jsonstring = json.loads(rec_bytes) | |
with open('weather.pkl', 'wb') as weatherPkl: | |
pickle.dump(jsonstring, weatherPkl) | |
return jsonstring | |
else: | |
with open('weather.pkl') as weatherPkl: | |
jsonstring = pickle.load(weatherPkl) | |
return jsonstring |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment