Homework 2 - Option 2...

Well Kinda....Requirements Modified

Melissa Serrano

The code below gets the Twitter trends for 5 locations of interest today, by using the countries WOEID. I used a modified version of pygmaps, available under Issue 5 at https://code.google.com/p/pygmaps/. This modified version enables the use of labels for the maps pinpoints. When the user hovers over the pinpoint I display the top 5 trending topics on Twitter for the area indicated by th pin point on the map.

In [10]:
import twitter
import json
import pygmaps
import webbrowser
import pprint as pp


CONSUMER_KEY = 'qAhZWyyxyi8MQZwxaCKojiC8O'
CONSUMER_SECRET = 'vCvQvS8icQpNucs5e42JZ0YqdmZkFg6QeGE3PH1sSUeZYyKzvC'
OAUTH_TOKEN = '2892604311-sLujj8H3JB5oHbIVPGZhC2vkZYNbW3CJsh2LICs'
OAUTH_TOKEN_SECRET = '9MFpHT1HKFmMyf3XD1UzH8APE0QwpkgbDEqt95GHmWQcw'
auth = twitter.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)


#Create a google map with the origin in the center of the laid out world
#zoom is 2 so that we can see all countries
mymap = pygmaps.maps(0, 0, 3)

for i in range(1,6):
    if i == 1:
        #Israel
        name = 'Israel'
        trends = twitter_api.trends.place(_id=23424852)
        lat = 31.00
        lon = 35.00
    if i == 2:
        #Mexico
        name = 'Mexico'
        trends = twitter_api.trends.place(_id=23424900)
        lat = 19.428
        lon = -99.127
    if i == 3:
        #Brazil
        name = 'Brazil'
        trends = twitter_api.trends.place(_id=23424768)
        lat = -15.7797
        lon = -47.9297
    if i == 4:
        #France
        name = 'France'
        trends = twitter_api.trends.place(_id=23424819)
        lat = 48.8534
        lon = 2.3488
    if i == 5:
        #Russia
        name = 'Russia'
        trends = twitter_api.trends.place(_id=23424936)
        lat = 60.00
        lon = 90.00

    stop = len(trends[0]['trends'])
    print stop
    trendsLabel = [None]*5
    for i in range(0,stop):
        try:
            trendsLabel[i] = trends[0]['trends'][i]['name']
            print i
        except KeyError, e:
            print e
        finally:
            if i == 5:
                break
    pp.pprint(trendsLabel)
    mymap.addpoint(lat, lon, "#FF0000", str(trendsLabel))
    print



#draw the google map with all our coordinates on it 
#and save it in this directory    
mymap.draw('./mymap.html')
#open this file in the browser automatically
webbrowser.open_new('mymap.html')

#Display the map here in the ipython document
from IPython.display import HTML
HTML('<iframe src="mymap.html" height=850 width=1400></iframe>')
10
0
1
2
3
4
[u'#BREAKING',
 u'#MTVHottest',
 u'Dinah Jane',
 u'#motavatormonday',
 u'#\u0627\u062b\u0646\u0627\u0646_\u0644\u0627\u064a\u062c\u062a\u0645\u0639\u0627\u0646_\u0627\u0628\u062f\u0627']

10
0
1
2
3
4
[u'#PorAmericaYo',
 u'#FamiliaShore',
 u'#RIPBuddy',
 u'#OTRAKansasCity',
 u'#CopaMX']

10
0
1
2
3
4
[u'#MasterChefBR',
 u'#RIPBuddy',
 u'#PraNamorarComigo',
 u'#OTRAKansasCity',
 u'#CaliforniaMusicVideo']

10
0
1
2
3
4
[u'#PourMoiBigAEst',
 u'#CaliforniaMusicVideo',
 u'#CHEBAR',
 u'Hazard',
 u'#growingupvampirediaries']

10
0
1
2
3
4
[u'#OTRAKansasCity',
 u'#\u041f\u041e\u0420\u041d\u041e\u041d\u041e\u0427\u042c',
 u'#CaliforniaMusicVideo',
 u'#505\u043d\u0430\u0433\u043d\u0443\u0442228',
 u'#MTVHottest']

Out[10]: