""" Social Flight Sim hack for Mashed 2008: When the plane enters a new country, post an automatic update to Twitter """ twitter_username = 'socialflightsim' twitter_password = '' from pprint import pprint import urllib, simplejson, urllib2, time from xml.etree import ElementTree as ET def get_lat_lon(): return tuple(ET.parse( urllib.urlopen("http://mashed.agm.me.uk/socialflightsim/tracklive.php") ).getroot().find( './/{http://earth.google.com/kml/2.0}coordinates' ).text.split(',')[:2]) def get_location(): return simplejson.load(urllib.urlopen( "http://ws.geonames.org/findNearbyPlaceNameJSON?lng=%s&lat=%s" % get_lat_lon() ))['geonames'] last_country = None def maybe_twitter(): locations = get_location() if locations: country = locations[0]['countryName'] if country != last_country: twitter('Navicomp: Now flying over %s' % country) globals()['last_country'] = country password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password( None, 'http://twitter.com/', twitter_username, twitter_password ) auth_handler = urllib2.HTTPBasicAuthHandler(password_manager) opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) def twitter(message): print urllib2.urlopen( "http://twitter.com/statuses/update.json", urllib.urlencode({ 'status': message, })).read() if __name__ == '__main__': while 1: time.sleep(5) maybe_twitter()