You should already have Twitter access setup for the lecture, if you do not, please revisit the lecture and make sure you have your Twitter credentials saved:
import pickle
import os
Make sure to select the relative path to the secret_twitter_credentials.pkl
file:
Twitter=pickle.load(open('../../secret_twitter_credentials.pkl','rb'))
import twitter
auth = twitter.oauth.OAuth(Twitter['Access Token'],
Twitter['Access Token Secret'],
Twitter['Consumer Key'],
Twitter['Consumer Secret'])
twitter_api = twitter.Twitter(auth=auth)
Find the Yahoo! Where On Earth ID for a place you are interested in at:
http://woeid.rosselliot.co.nz/
Set LOCAL_WOE_ID
to this integer number below:
LOCAL_WOE_ID = None
### BEGIN SOLUTION
### END SOLUTION
assert LOCAL_WOE_ID, "Remember to set LOCAL_WOE_ID to a location identifier"
Let's use the twitter API to retrieve trends
local_trends = twitter_api.trends.place(_id=LOCAL_WOE_ID)
local_trends
is a highly nested data structure made up of lists and dictionaries, explore it with type()
, len()
and indexing like [0]
and print out a list of all the trends:
list_of_trends = None
### BEGIN SOLUTION
### END SOLUTION
assert isinstance(list_of_trends, list), "list_of_trends should be a list"
Now let's execute a search on Twitter for the most popular trend and repeat the filtering step performed during lecture to remove duplicate results.
Set the q
variable to the most popular trend in the list we retrieved above:
q = None
### BEGIN SOLUTION
### END SOLUTION
Then let's execute the Twitter search:
# DO NOT MODIFY
count = 100
search_results = twitter_api.search.tweets(q=q, count=count)
statuses = search_results['statuses']
# DO NOT MODIFY
all_text = []
filtered_statuses = []
for s in statuses:
if not s["text"] in all_text:
filtered_statuses.append(s)
all_text.append(s["text"])
len(filtered_statuses)
81
We want to sort the tweets by the retweet count, therefore the first step is to create a list of tuples where the first element is the retweet count and then use the sorted
function to perform the sorting operation.
retweets = None
### BEGIN SOLUTION
### END SOLUTION
assert len(retweets) == len(filtered_statuses), "Make sure you are using filterest_statuses and not statuses"
assert len(retweets[0]) == 2, "Each tuple should only have 2 elements, retweet count and the tweet text"
Use the sorted
function to sort retweets and get the 10 more popular, we'd like to have the more popular tweet first.
popular_tweets = None
### BEGIN SOLUTION
### END SOLUTION
assert len(popular_tweets) == 10, "Find the 10 most popular"
assert popular_tweets[0][0] >= popular_tweets[-1][0], "Make sure you are sorting in descending order"