Last active
June 14, 2016 19:54
-
-
Save miklb/8346411 to your computer and use it in GitHub Desktop.
Pythonista script to grab your current location, then retrieve current weather via forecast.io
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
| # -*- 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