Skip to content

Instantly share code, notes, and snippets.

@petterw
Created January 12, 2011 23:30
Show Gist options
  • Save petterw/777110 to your computer and use it in GitHub Desktop.
Save petterw/777110 to your computer and use it in GitHub Desktop.
geoip+temperature
import pycurl
from cStringIO import StringIO
geoip_api_key="" # http://geoio.com/signup.php
weather_api_key="" # http://www.worldweatheronline.com/register.aspx
############################
def get(url):
c = pycurl.Curl()
buffer = StringIO()
c.setopt(pycurl.WRITEFUNCTION, buffer.write)
c.setopt(pycurl.URL, url)
c.perform()
return buffer.getvalue()
external_ip=get("http://plutoid.org/services/ip/?r=ip2temp")
location=get("http://api.geoio.com/q.php?key="+geoip_api_key+"&qt=geoip&d=pipe&q="+external_ip).split("|")
latitude=location[4]
longitude=location[5]
weather=get("http://www.worldweatheronline.com/feed/weather.ashx?q="+latitude+","+longitude+"&format=csv&num_of_days=2&key="+weather_api_key)
temp=0
try:
temp = int(weather.split("\n")[8].split(",")[1])
except:
print "ERR"
exit
if temp > 20:
print "<fc=#ee4444>"+str(temp)+"</fc>C"
elif temp<0:
print "<fc=#3bb9ff>"+str(temp)+"</fc>C"
else:
print str(temp)+"C"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment