-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter-api.rtf
58 lines (57 loc) · 2.13 KB
/
twitter-api.rtf
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
{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf600
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
{\*\expandedcolortbl;;\cssrgb\c0\c0\c0;}
\margl1440\margr1440\vieww37900\viewh20680\viewkind0
\deftab720
\pard\pardeftab720\sl280\partightenfactor0
\f0\fs24 \cf2 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 import tweepy\
from tweepy import OAuthHandler\
import json\
from timeit import default_timer as timer\
\
# Query Twitter API for each tweet in the Twitter archive and save JSON in a text file\
# These are hidden to comply with Twitter's API terms and conditions\
consumer_key = 'HIDDEN'\
consumer_secret = 'HIDDEN'\
access_token = 'HIDDEN'\
access_secret = 'HIDDEN'\
\
auth = OAuthHandler(consumer_key, consumer_secret)\
auth.set_access_token(access_token, access_secret)\
\
api = tweepy.API(auth, wait_on_rate_limit=True)\
\
# NOTE TO STUDENT WITH MOBILE VERIFICATION ISSUES:\
# df_1 is a DataFrame with the twitter_archive_enhanced.csv file. You may have to\
# change line 17 to match the name of your DataFrame with twitter_archive_enhanced.csv\
# NOTE TO REVIEWER: this student had mobile verification issues so the following\
# Twitter API code was sent to this student from a Udacity instructor\
# Tweet IDs for which to gather additional data via Twitter's API\
tweet_ids = df_1.tweet_id.values\
len(tweet_ids)\
\
# Query Twitter's API for JSON data for each tweet ID in the Twitter archive\
count = 0\
fails_dict = \{\}\
start = timer()\
# Save each tweet's returned JSON as a new line in a .txt file\
with open('tweet_json.txt', 'w') as outfile:\
# This loop will likely take 20-30 minutes to run because of Twitter's rate limit\
for tweet_id in tweet_ids:\
count += 1\
print(str(count) + ": " + str(tweet_id))\
try:\
tweet = api.get_status(tweet_id, tweet_mode='extended')\
print("Success")\
json.dump(tweet._json, outfile)\
outfile.write('\\n')\
except tweepy.TweepError as e:\
print("Fail")\
fails_dict[tweet_id] = e\
pass\
end = timer()\
print(end - start)\
print(fails_dict)\
}