-
Notifications
You must be signed in to change notification settings - Fork 5
/
assignment1.py
66 lines (54 loc) · 2.47 KB
/
assignment1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python3
# Accessing the Twitter API
# This script describes the basic methodology for accessing a Twitter feed
# or something similar.
# Loading libraries needed for authentication and requests
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
import json
# In order to use this script, you must:
# - Have a Twitter account and create an app
# - Store in keys.json a property called "twitter" whose value is an
# object with two keys, "key" and "secret"
with open('keys.json', 'r') as f:
keys = json.loads(f.read())['twitter']
twitter_key = keys['key']
twitter_secret = keys['secret']
# We authenticate ourselves with the above credentials
# We will demystify this process later
#
# For documentation, see http://requests-oauthlib.readthedocs.io/en/latest/api.html
# and http://docs.python-requests.org/en/master/
client = BackendApplicationClient(client_id=twitter_key)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url='https://api.twitter.com/oauth2/token',
client_id=twitter_key,
client_secret=twitter_secret)
# Base url needed for all subsequent queries
base_url = 'https://api.twitter.com/1.1/'
# Particular page requested. The specific query string will be
# appended to that.
page = 'search/tweets.json'
# Depending on the query we are interested in, we append the necessary string
# As you read through the twitter API, you'll find more possibilities
req_url = base_url + page + '?q=Hanover+College&tweet_mode=extended&count=100'
# We perform a request. Contains standard HTTP information
response = oauth.get(req_url)
# Read the query results
results = json.loads(response.content.decode('utf-8'))
## Process the results
## CAUTION: The following code will attempt to read up to 10000 tweets that
## Mention Hanover College. You should NOT change this code.
tweets = results['statuses']
while True:
if not ('next_results' in results['search_metadata']):
break
if len(tweets) > 10000:
break
next_search = base_url + page + results['search_metadata']['next_results'] + '&tweet_mode=extended'
print(results['search_metadata']['next_results'])
response = oauth.get(next_search)
results = json.loads(response.content.decode('utf-8'))
tweets.extend(results['statuses'])
## CAUTION: For the rest of this assignment, the list "tweets" contains all the
## tweets you would want to work with. Do NOT change the list or the value of "tweets".