import twitter
%load_ext retina # Display images with retina
Setup authentication...
consumer_key = "CMHKXlMkCfpDieJBdFNQe0SDg"
consumer_secret = "4r0BN4iMaiqkywcJf518FchlT9IEggnQY8jNJ1pr6jqx5lIXu5"
access_token = "1358375179-yozRj6VttftJtBkYCds2Mt2F0ITlQfKt7svHVLt"
access_token_secret = "dhwXZd9D3xbEKi3rMyqBQOL8JLVl4AehLI8cy21grZO6m"
auth = twitter.OAuth(access_token, access_token_secret,
consumer_key, consumer_secret)
twitter_api = twitter.Twitter(auth=auth)
# Check to see if it worked
if twitter_api:
print ('Success')
Success
We can use Yahoo!GeoPlanet's Where On Earth (WOE) ID system, which is an API designed to provide an unique identifier to any named place on earth. The WOE ID for earth is 1.
#Figure how to use this API later
ap_id = '7BjgHs42'
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)
import json
print json.dumps(world_trends, indent = 1)
print
print json.dumps(us_trends, indent = 1)
[ { "created_at": "2014-10-03T11:15:10Z", "trends": [ { "url": "http://twitter.com/search?q=%23w%C9%AFwBayipr%D1%94ssC%C3%B8m", "query": "%23w%C9%AFwBayipr%D1%94ssC%C3%B8m", "name": "#w\u026fwBayipr\u0454ssC\u00f8m", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23BaskentAmaGeceUlas", "query": "%23BaskentAmaGeceUlas", "name": "#BaskentAmaGeceUlas", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23ElyarFoxAnnouncement", "query": "%23ElyarFoxAnnouncement", "name": "#ElyarFoxAnnouncement", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23WorstReplyToILoveYou", "query": "%23WorstReplyToILoveYou", "name": "#WorstReplyToILoveYou", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23Holler3rdWin", "query": "%23Holler3rdWin", "name": "#Holler3rdWin", "promoted_content": null }, { "url": "http://twitter.com/search?q=%22Chelsea+Hadi%22", "query": "%22Chelsea+Hadi%22", "name": "Chelsea Hadi", "promoted_content": null }, { "url": "http://twitter.com/search?q=%22Bulan+Spesialmu%22", "query": "%22Bulan+Spesialmu%22", "name": "Bulan Spesialmu", "promoted_content": null }, { "url": "http://twitter.com/search?q=%22Udah+Sampe+Rumah+Kak%22", "query": "%22Udah+Sampe+Rumah+Kak%22", "name": "Udah Sampe Rumah Kak", "promoted_content": null }, { "url": "http://twitter.com/search?q=%22M%C3%B3nica+Oriol%22", "query": "%22M%C3%B3nica+Oriol%22", "name": "M\u00f3nica Oriol", "promoted_content": null }, { "url": "http://twitter.com/search?q=%22Iklan+Via+TTI+082288113332+Lebih%22", "query": "%22Iklan+Via+TTI+082288113332+Lebih%22", "name": "Iklan Via TTI 082288113332 Lebih", "promoted_content": null } ], "as_of": "2014-10-03T11:17:56Z", "locations": [ { "woeid": 1, "name": "Worldwide" } ] } ] [ { "created_at": "2014-10-03T11:10:29Z", "trends": [ { "url": "http://twitter.com/search?q=%23MeanGirlsAppreciationDay", "query": "%23MeanGirlsAppreciationDay", "name": "#MeanGirlsAppreciationDay", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23VoteVamps", "query": "%23VoteVamps", "name": "#VoteVamps", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23BadThemeParks", "query": "%23BadThemeParks", "name": "#BadThemeParks", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23ItsOctober3rd", "query": "%23ItsOctober3rd", "name": "#ItsOctober3rd", "promoted_content": null }, { "url": "http://twitter.com/search?q=%23WorstReplyToILoveYou", "query": "%23WorstReplyToILoveYou", "name": "#WorstReplyToILoveYou", "promoted_content": null }, { "url": "http://twitter.com/search?q=%22Rich+Rod%22", "query": "%22Rich+Rod%22", "name": "Rich Rod", "promoted_content": null }, { "url": "http://twitter.com/search?q=Oregon", "query": "Oregon", "name": "Oregon", "promoted_content": null }, { "url": "http://twitter.com/search?q=Ebola", "query": "Ebola", "name": "Ebola", "promoted_content": null }, { "url": "http://twitter.com/search?q=Annabelle", "query": "Annabelle", "name": "Annabelle", "promoted_content": null }, { "url": "http://twitter.com/search?q=Ducks", "query": "Ducks", "name": "Ducks", "promoted_content": null } ], "as_of": "2014-10-03T11:17:57Z", "locations": [ { "woeid": 23424977, "name": "United States" } ] } ]
To find out which trends are the same between the two regions, we use Python's set
data structure. A set refers to the mathematical notion of a data structure that stores an unordered collection of unique items. These sets can be computed on with other sets of items using setwise operations
. For example, a setwise intersection computes common items between sets, and a setwise union combines all the items from the sets (addition), and setwise difference acts as a subtraction operation.
world_trends_set = set([trend['name']
for trend in world_trends[0]['trends']])
us_trends_set = set([trend['name']
for trend in us_trends[0]['trends']])
common_trends = world_trends_set.intersection(us_trends_set)
print common_trends
set([u'#WorstReplyToILoveYou'])
q = '#WorstReplyToILoveYou'
count = 100
results = twitter_api.search.tweets(q=q, count = count)
statuses = results['statuses']
for _ in range(5):
print 'Length of statuses', len(statuses)
try:
next_results = results['search_metadata']['next_results']
except KeyError, e: # No more results when next_results is null
break
# Create a dictionary from next_results, of the form:
kwargs = dict([kv.split('=')for kv in next_results[1:].split('&')])
results = twitter_api.search.tweets(**kwargs)
statuses += results['statuses']
print json.dumps(statuses[0], indent=1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-69-32ebda659ba2> in <module>() 2 count = 100 3 ----> 4 results = twitter_api.search.tweets(q=q, count = count) 5 statuses = results['statuses'] 6 for _ in range(5): /Users/ryankelly/anaconda/lib/python2.7/site-packages/twitter/api.pyc in __call__(self, **kwargs) 264 265 req = urllib_request.Request(uriBase, body, headers) --> 266 return self._handle_response(req, uri, arg_data, _timeout) 267 268 def _handle_response(self, req, uri, arg_data, _timeout=None): /Users/ryankelly/anaconda/lib/python2.7/site-packages/twitter/api.pyc in _handle_response(self, req, uri, arg_data, _timeout) 288 return wrap_response({}, handle.headers) 289 elif "json" == self.format: --> 290 res = json.loads(data.decode('utf8')) 291 return wrap_response(res, handle.headers) 292 else: /Users/ryankelly/anaconda/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 336 parse_int is None and parse_float is None and 337 parse_constant is None and object_pairs_hook is None and not kw): --> 338 return _default_decoder.decode(s) 339 if cls is None: 340 cls = JSONDecoder /Users/ryankelly/anaconda/lib/python2.7/json/decoder.pyc in decode(self, s, _w) 364 365 """ --> 366 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 367 end = _w(s, end).end() 368 if end != len(s): /Users/ryankelly/anaconda/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx) 380 """ 381 try: --> 382 obj, end = self.scan_once(s, idx) 383 except StopIteration: 384 raise ValueError("No JSON object could be decoded") ValueError: Expecting object: line 1 column 353224 (char 353223)
print kwargs
{u'q': u'%2523WorstReplyToILoveYou', u'count': u'100', u'include_entities': u'1', u'max_id': u'518000264926601215'}
from IPython.core.display import HTML
def css_styling():
styles = open("/users/ryankelly/desktop/custom_notebook2.css", "r").read()
return HTML(styles)
css_styling()