Skip to content

Instantly share code, notes, and snippets.

@ask-compu
Created March 22, 2018 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ask-compu/1a359579d99fc66c5e2db92ae25be10c to your computer and use it in GitHub Desktop.
Save ask-compu/1a359579d99fc66c5e2db92ae25be10c to your computer and use it in GitHub Desktop.
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