Created
November 29, 2014 22:18
-
-
Save marklee77/f48c5ad39020049596e9 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
#!/usr/bin/env python | |
# Copyright 2014 Mark Stillwell <[email protected]> | |
# development platform is Ubuntu 14.04 (Trusty Tahir) | |
# requries python, python-networkmanager, python-pycurl, python-simplejson | |
# also requires a font with icons at the appropriate values | |
# The author uses one generated at icomoon.io by including all of the glyphs | |
# from the Meteocons, Ionicons, and Font Awesome icon libraries. | |
from NetworkManager import NetworkManager as nm | |
import pycurl | |
import simplejson as json | |
from io import BytesIO | |
if not nm.ActiveConnections: | |
exit(0) | |
# keys and suggested values at http://openweathermap.org/weather-conditions | |
# unicode values are based on icomoon.io icon font including Meteocons | |
icons = {'01d': u'\ue601', '01n': u'\ue602', | |
'02d': u'\ue607', '02n': u'\ue608', | |
'03d': u'\ue60d', '03n': u'\ue60d', | |
'04d': u'\ue618', '04n': u'\ue618', | |
'09d': u'\ue611', '09n': u'\ue611', | |
'10d': u'\ue622', '10n': u'\ue622', | |
'11d': u'\ue60e', '11n': u'\ue60e', | |
'13d': u'\ue616', '13n': u'\ue616', | |
'50d': u'\ue609', '50n': u'\ue60a'} | |
buf = BytesIO() | |
c = pycurl.Curl() | |
c.setopt(c.URL, "freegeoip.net/json/") | |
c.setopt(c.WRITEDATA, buf) | |
try: | |
c.perform() | |
geoinfo = json.loads(buf.getvalue()) | |
except Exception: | |
exit(0) | |
buf = BytesIO() | |
c = pycurl.Curl() | |
c.setopt(c.URL, 'api.openweathermap.org/data/2.5/weather?lat={latitude}' | |
'&lon={longitude}&units=metric'.format(**geoinfo)) | |
c.setopt(c.WRITEDATA, buf) | |
try: | |
c.perform() | |
weatherinfo = json.loads(buf.getvalue()) | |
except Exception: | |
exit(0) | |
short_text = (u' '.join(set(icons[w['icon']] for w in weatherinfo['weather'])) | |
+ u' {:d}\u00b0'.format(int(weatherinfo['main']['temp'])) | |
).encode('utf-8') | |
print short_text | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment