Twitter Updates with geolocation


Update your twitter with your current geolocation, from terminal
I was playing with a python wrapper around the Twitter API and made a simple class that would update twitter status with the geolocation derived from your IP address. The limitation begin proxies and a limited 70 chars status, yes there are few command like whereami but this is for the fun of it. When we travel here and there we can update our location with latitude and longitude, yes indeed even that can be made via a web browser, this for people who love there terminal more than anything.

The code is self explanatory with sensible comments where ever necessary, it uses urllib2 and python-twitter, which you must down from get python-twitter and install.

The below is the code that need to be edited, to indeed add the username and password of your twitter account.

#!/usr/bin/env python
# Twitt Geo-location
# -*- coding: utf-8 -*-
# Geolocate.py
# Author Hemanth.HM
# Site : www.h3manth.com
# Licensed under GNU GPL Version 3

import twitter
import urllib2

""" USER SETTINGS """
USERNAME="your_twitter_name"
PASSWORD="your_twitter_password"

class Geolocate:
    """Class Geolocate to get the current geo-location and also twitt the same"""
    def get_info(self):
        """Returns the current location"""
        """ Query URI's """
        myip="http://whatismyip.org/"
        url="http://api.hostip.info/get_html.php?ip=%s&position=true"
        pub_ip = urllib2.urlopen(myip).read()
        url= url%(pub_ip)
        location = urllib2.urlopen(url).read()
        location = location.splitlines()
        del location[-1] # Remove ip for security.
        return " ".join(location)

    def twit(self):
        """Updates twitter status with current location, limitation 70 chars"""
        try:
                status = raw_input("Type in your status [70 chars]: ")
                twit = twitter.Api(username=USERNAME, password=PASSWORD)
                twit.PostUpdate(status+" "+self.get_info())
        except Exception, why:
                print str(why)

def main():
        wai = Geolocate()
        wai.twit()

if __name__ == "__main__":
    main()


Example Location:
'Country: INDIA (IN) City: Bangalore Latitude: 12.9833 Longitude: 77.5833'
Get the code and do share your scripts in the comments below.

Share this