import twitter

CONSUMER_KEY = 
CONSUMER_SECRET = 
OAUTH_TOKEN = 
OAUTH_TOKEN_SECRET = 

# let's do the Oauth dance!
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)

# success if object created
print(twitter_api)

"""
Explanation: This example roughly follows one in 'Mining the Social Web,' by Russell: https://github.com/ptwobrussell/Mining-the-Social-Web-2nd-Edition. In order to follow along, you need to install the twitter package, have a twitter account, and get a consumer key, consumer secret, oauth token, and oauth token secret. You can get the keys at http://apps.twitter.com by building a new app. Follow this handy tutorial: http://iag.me/socialmedia/how-to-create-a-twitter-app-in-8-easy-steps/.
End of explanation
"""


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)

print(world_trends)
print(us_trends)

type(us_trends)

len(us_trends)

"""
Explanation: Twitter uses Where On Earth identifiers for places - see http://woeid.rosselliot.co.nz/ for quick lookups.
End of explanation
"""


import json

print(json.dumps(world_trends, indent=True))

len(world_trends)

type(world_trends[0])

world_trends[0].keys()  # last three are just a record of your request

world_trends[0]['trends'][5]

print(json.dumps(us_trends, indent=True))

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)

q = '#deeplearning'

count = 100

search_results = twitter_api.search.tweets(q=q, count=count)

type(search_results)  # surprise! It's a dict

search_results.keys()

statuses = search_results['statuses']
metadata = search_results['search_metadata']

type(statuses)

len(statuses)

type(statuses[0])

type(metadata)

metadata.keys()

for _ in range(5):
    print("Length of statuses", len(statuses))
    try:
        next_results = metadata['next_results']
    except KeyError as e:  # no more results when next_results doesn't exist
        break
        
    kwargs = dict([ kv.split('=') for kv in next_results[1:].split("&")])
    print(kwargs)  # to see what's happening
    search_results = twitter_api.search.tweets(**kwargs)
    statuses += search_results['statuses']

# show one sample result    
t = statuses[0]
print(json.dumps(t, indent=True))

t['entities']['urls']

status_texts = [status['text'] for status in statuses]

screen_names = [user_mention['screen_name'] for status in statuses 
                                            for user_mention in status['entities']['user_mentions']]

hashtags = [hashtag['text'] for status in statuses for hashtag in status['entities']['hashtags']]


print(json.dumps(status_texts[:5], indent=True))
print(json.dumps(screen_names[:5], indent=True))
print(json.dumps(hashtags[:5], indent=True))

from collections import Counter

for item in [screen_names, hashtags]:
    c = Counter(item)
    print(c.most_common()[:10])

# big list comprehension

retweets = [
            # Store out a tuple of these three values ...
            (status['retweet_count'], 
             status['retweeted_status']['user']['screen_name'],
             status['text']) 
    
            for status in statuses
            
            # ... so long as the status meets this condition.
                if 'retweeted_status' in status
           ]

sorted(retweets, reverse=True)

"""
Explanation: The format above is json (javascript object notation). You can read about json format at http://json.org and some examples of json are here: http://json.org/example.html. Quite a bit of what moves around on the web is in json format.
End of explanation
"""


