-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterScraper.py
More file actions
50 lines (42 loc) · 1.24 KB
/
twitterScraper.py
File metadata and controls
50 lines (42 loc) · 1.24 KB
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
#!/usr/bin/env python
from tweepy import OAuthHandler
import tweepy
import json
from time import sleep
import sys
# Keys provided from the Twitter custom application
consumer_key=""
consumer_secret=""
access_token=""
access_token_secret=""
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#---------------------
# Where On Earth ID - WOEID
# (http://www.woeidlookup.com/)
# 1 - Worldwide
# 2459115 - NYC
# 2367105 - Suffolk County (Boston)
WOEID = 2459115
# Get trending topics for a specific WOEID
# TODO: Get trending topics every 5 min
trends1 = api.trends_place(WOEID)
trends1
trends = set([trend['name'] for trend in trends1[0]['trends']])
trendsList = list(trends)
with open("favorites.txt", 'a') as f:
if (WOEID == 2459115):
f.write('Trending topics in NYC: \n')
elif (WOEID == 2367105):
f.write('Trending topics in BOS: \n')
else:
print 'WOEID not supported!'
# From trending topics, get Tweets
for t in trendsList:
print 'Retrieving tweets for ' + str(t)
sys.stdout.flush()
for tweet in tweepy.Cursor(api.search, q=t, rpp=100, since="2018-03-14", lang="en").items(1000):
f.write(json.dumps([tweet._json], indent=1))
f.write('\n')
sleep(0.5)