<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Exploring the Twitter trends API
In this notebook we'll take a quick look at the section of the Twitter Rest API that deals with trending terms
Step1:  Basic calls
The first call trends/available returns a set of locations, each with its own set of parameters.
Step2:  We see from this (and from their docs) that Twitter uses Yahoo WOEIDs to define place names.  How they arrived at this list of 467 places is not clear.  It's a mix of countries and cities, at least
Step3:  Hmm, let's see exactly what classification levels they have in this set
Step4:  'Unknown'??
Step5:  Ah, Soweto is a township, not a city, and Al Ahsa is a region.
Moving along, we can dig into specifc places one at a time
Step6:  There's also a way to fetch the list without hashtags
Step7:  Simplifying a bit
Let's write a few functions so we don't have to write all that out each time.
Step8:  Weird, there's an L-to-R issue rendering the output here
Step9:  Just for fun let's compare Algiers with Algeria.
Step10:  Very similar.  How about Chicago vs Miami?
Step11:  Looking nearby
There are specific javascript calls for fetching a user's geolocation through a browser (see Using Geolocation via MDN).  With a tool like that in place, you could fetch the user's lat and long and send it to the trends/closest Twitter API call
Step12:  Hmm, that parentid attribute might be useful.
<ASSISTANT_TASK:>
Python Code:
from collections import Counter
import os
from pprint import pprint
import tweepy
c_key = os.environ['CONSUMER_KEY']
c_secret = os.environ['CONSUMER_SECRET']
a_token = os.environ['ACCESS_TOKEN']
a_token_secret = os.environ['ACCESS_TOKEN_SECRET']
auth = tweepy.OAuthHandler(c_key, c_secret)
auth.set_access_token(a_token, a_token_secret)
api = tweepy.API(auth)
Explanation: Exploring the Twitter trends API
In this notebook we'll take a quick look at the section of the Twitter Rest API that deals with trending terms:
GET trends/available
GET trends/place
GET trends/closest
We'll work with the Tweepy library's support for these, explore the calls, and sketch out some possibilities.
Setup
First some library imports and user key setup.  This assumes you've arranged for the necessary app keys at apps.twitter.com.
End of explanation
trends_available = api.trends_available()
len(trends_available)
trends_available[0:3]
Explanation: Basic calls
The first call trends/available returns a set of locations, each with its own set of parameters.
End of explanation
sorted([t['name'] for t in trends_available])[:25]
Explanation: We see from this (and from their docs) that Twitter uses Yahoo WOEIDs to define place names.  How they arrived at this list of 467 places is not clear.  It's a mix of countries and cities, at least:
End of explanation
c = Counter([t['placeType']['name'] for t in trends_available])
c
Explanation: Hmm, let's see exactly what classification levels they have in this set:
End of explanation
unk = [t for t in trends_available if t['placeType']['name'] == 'Unknown']
unk
Explanation: 'Unknown'??
End of explanation
ams = [t for t in trends_available if t['name'] == 'Amsterdam'][0]
ams
trends_ams = api.trends_place(ams['woeid'])
trends_ams
trends_ams[0]['trends']
[(t['name'], t['tweet_volume']) for t in trends_ams[0]['trends']]
Explanation: Ah, Soweto is a township, not a city, and Al Ahsa is a region.
Moving along, we can dig into specifc places one at a time:
End of explanation
trends_ams = api.trends_place(ams['woeid'], exclude='hashtags')
[(t['name'], t['tweet_volume']) for t in trends_ams[0]['trends']]
Explanation: There's also a way to fetch the list without hashtags:
End of explanation
def getloc(name):
    try:
        return [t for t in trends_available if t['name'].lower() == name.lower()][0]
    except:
        return None
def trends(loc, exclude=False):
    return api.trends_place(loc['woeid'], 'hashtags' if exclude else None)
def top(trends):
    return sorted([(t['name'], t['tweet_volume']) for t in trends[0]['trends']], key=lambda a: a[1] or 0, reverse=True)
tok = getloc('tokyo')
trends_tok = trends(tok, True)
top(trends_tok)
alg = getloc('Algeria')
trends_alg = trends(alg)
top(trends_alg)
Explanation: Simplifying a bit
Let's write a few functions so we don't have to write all that out each time.
End of explanation
top(trends_alg)[1][0]
Explanation: Weird, there's an L-to-R issue rendering the output here:
End of explanation
algs = getloc('Algiers')
trends_algs = trends(algs)
top(trends_algs)
Explanation: Just for fun let's compare Algiers with Algeria.
End of explanation
chi = getloc('Chicago')
trends_chi = trends(chi)
chi
top(trends_chi)[:10]
mia = getloc('Miami')
mia
trends_mia = trends(mia)
top(trends_mia)[:10]
world = getloc('Worldwide')
trends_world = trends(world)
top(trends_world)[:25]
Explanation: Very similar.  How about Chicago vs Miami?
End of explanation
closeby = api.trends_closest(38.8860307, -76.9931073)
closeby[0]
top(trends(closeby[0]))
Explanation: Looking nearby
There are specific javascript calls for fetching a user's geolocation through a browser (see Using Geolocation via MDN).  With a tool like that in place, you could fetch the user's lat and long and send it to the trends/closest Twitter API call:
End of explanation
[t['name'] for t in trends_available if t['parentid'] == 23424977]
[t['name'] for t in trends_available if t['parentid'] == world['woeid']]
Explanation: Hmm, that parentid attribute might be useful.
End of explanation
<END_TASK>