Skip to content

Instantly share code, notes, and snippets.

@miklb miklb/py_forecast.py
Last active Jun 14, 2016

Embed
What would you like to do?
Pythonista script to grab your current location, then retrieve current weather via forecast.io
# -*- coding:utf-8 -*
import location
import re
import sys
import json
import urllib
import webbrowser
from os import environ
from sys import exit, argv
location.start_updates()
loc = location.get_location()
location.stop_updates()
lat = loc['latitude']
lon = loc['longitude']
# Get my API key and construct the URL
APIkey = 'YOUR API KEY'
dsURL = 'https://api.forecast.io/forecast/%s/%s,%s?exclude=minutely,hourly,alerts,flag' \
% (APIkey, lat, lon)
# Get the data from Forecast.io.
try:
jsonString = urllib.urlopen(dsURL).read()
weather = json.loads(jsonString)
except (IOError, ValueError):
print "Connection failure to %s" % dsURL
exit()
data = 'NOW\n' + str(weather["currently"]["summary"]).capitalize() + ', '\
+ str(round(weather["currently"]["temperature"])) + u'°F'.encode('utf8') + '\n'
webbrowser.open("drafts://x-callback-url/create?text=" + urllib.quote(data.encode('utf-8')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.