Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
47 lines (38 sloc) 1.33 KB
import sys
import csv
import time
from twitter import Api, TwitterError
CONSUMER_KEY = "YOUR_KEY"
CONSUMER_SECRET = "YOUR_KEY"
ACCESS_TOKEN = "YOUR_KEY"
ACCESS_TOKEN_SECRET = "YOUR_KEY"
def main(in_path):
api = Api(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN,
access_token_secret=ACCESS_TOKEN_SECRET,
)
in_path_tokens = in_path.split('.')
out_path_tokens = in_path_tokens[:-1] + ['full'] + in_path_tokens[-1:]
out_path = '.'.join(out_path_tokens)
with open(in_path, 'r') as fin, open(out_path, 'w') as fout:
reader = csv.DictReader(fin)
writer = csv.DictWriter(fout, fieldnames=reader.fieldnames + ['text'])
writer.writeheader()
for i, row in enumerate(reader):
time.sleep(1.1) # rate limit for twitter api
print(f"fetching tweet: {i+1}")
try:
status = api.GetStatus(row['tweet_id'])
row['text'] = status.full_text or status.text
writer.writerow(row)
except TwitterError as e:
print(f"failed to retrieve tweet with id: {row['tweet_id']}")
print(e)
if __name__ == '__main__':
try:
path = sys.argv[1]
except IndexError:
path = './data/train_tweets.csv'
main(path)
You can’t perform that action at this time.