{
"metadata": {
"name": "",
"signature": "sha256:486f22e94b3159d2473851c252cb7108ee4d8bf071f2944c7a29d0ac1105d4ad"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework 2 - Option 2... #\n",
"### Well Kinda....Requirements Modified ###\n",
"#### Melissa Serrano ###\n",
"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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import twitter\n",
"import json\n",
"import pygmaps\n",
"import webbrowser\n",
"import pprint as pp\n",
"\n",
"\n",
"CONSUMER_KEY = 'qAhZWyyxyi8MQZwxaCKojiC8O'\n",
"CONSUMER_SECRET = 'vCvQvS8icQpNucs5e42JZ0YqdmZkFg6QeGE3PH1sSUeZYyKzvC'\n",
"OAUTH_TOKEN = '2892604311-sLujj8H3JB5oHbIVPGZhC2vkZYNbW3CJsh2LICs'\n",
"OAUTH_TOKEN_SECRET = '9MFpHT1HKFmMyf3XD1UzH8APE0QwpkgbDEqt95GHmWQcw'\n",
"auth = twitter.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)\n",
"twitter_api = twitter.Twitter(auth=auth)\n",
"\n",
"\n",
"#Create a google map with the origin in the center of the laid out world\n",
"#zoom is 2 so that we can see all countries\n",
"mymap = pygmaps.maps(0, 0, 3)\n",
"\n",
"for i in range(1,6):\n",
" if i == 1:\n",
" #Israel\n",
" name = 'Israel'\n",
" trends = twitter_api.trends.place(_id=23424852)\n",
" lat = 31.00\n",
" lon = 35.00\n",
" if i == 2:\n",
" #Mexico\n",
" name = 'Mexico'\n",
" trends = twitter_api.trends.place(_id=23424900)\n",
" lat = 19.428\n",
" lon = -99.127\n",
" if i == 3:\n",
" #Brazil\n",
" name = 'Brazil'\n",
" trends = twitter_api.trends.place(_id=23424768)\n",
" lat = -15.7797\n",
" lon = -47.9297\n",
" if i == 4:\n",
" #France\n",
" name = 'France'\n",
" trends = twitter_api.trends.place(_id=23424819)\n",
" lat = 48.8534\n",
" lon = 2.3488\n",
" if i == 5:\n",
" #Russia\n",
" name = 'Russia'\n",
" trends = twitter_api.trends.place(_id=23424936)\n",
" lat = 60.00\n",
" lon = 90.00\n",
"\n",
" stop = len(trends[0]['trends'])\n",
" print stop\n",
" trendsLabel = [None]*5\n",
" for i in range(0,stop):\n",
" try:\n",
" trendsLabel[i] = trends[0]['trends'][i]['name']\n",
" print i\n",
" except KeyError, e:\n",
" print e\n",
" finally:\n",
" if i == 5:\n",
" break\n",
" pp.pprint(trendsLabel)\n",
" mymap.addpoint(lat, lon, \"#FF0000\", str(trendsLabel))\n",
" print\n",
"\n",
"\n",
"\n",
"#draw the google map with all our coordinates on it \n",
"#and save it in this directory \n",
"mymap.draw('./mymap.html')\n",
"#open this file in the browser automatically\n",
"webbrowser.open_new('mymap.html')\n",
"\n",
"#Display the map here in the ipython document\n",
"from IPython.display import HTML\n",
"HTML('')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"[u'#BREAKING',\n",
" u'#MTVHottest',\n",
" u'Dinah Jane',\n",
" u'#motavatormonday',\n",
" u'#\\u0627\\u062b\\u0646\\u0627\\u0646_\\u0644\\u0627\\u064a\\u062c\\u062a\\u0645\\u0639\\u0627\\u0646_\\u0627\\u0628\\u062f\\u0627']\n",
"\n",
"10"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"[u'#PorAmericaYo',\n",
" u'#FamiliaShore',\n",
" u'#RIPBuddy',\n",
" u'#OTRAKansasCity',\n",
" u'#CopaMX']\n",
"\n",
"10"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"[u'#MasterChefBR',\n",
" u'#RIPBuddy',\n",
" u'#PraNamorarComigo',\n",
" u'#OTRAKansasCity',\n",
" u'#CaliforniaMusicVideo']\n",
"\n",
"10"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"[u'#PourMoiBigAEst',\n",
" u'#CaliforniaMusicVideo',\n",
" u'#CHEBAR',\n",
" u'Hazard',\n",
" u'#growingupvampirediaries']\n",
"\n",
"10"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"[u'#OTRAKansasCity',\n",
" u'#\\u041f\\u041e\\u0420\\u041d\\u041e\\u041d\\u041e\\u0427\\u042c',\n",
" u'#CaliforniaMusicVideo',\n",
" u'#505\\u043d\\u0430\\u0433\\u043d\\u0443\\u0442228',\n",
" u'#MTVHottest']\n",
"\n"
]
},
{
"html": [
""
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
""
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}