-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep01_basic.py
More file actions
62 lines (45 loc) · 1.69 KB
/
step01_basic.py
File metadata and controls
62 lines (45 loc) · 1.69 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
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 22 21:51:45 2017
#Reference/Source: Prof. Gene Lee's codes from Dropbox code files.
"""
##Reference/Source: Prof. Gene Lee's codes from Dropbox code files.
from twython import TwythonStreamer
import sys
import json
tweets = []
class MyStreamer(TwythonStreamer):
'''our own subclass of TwythonStremer'''
# overriding
def on_success(self, data):
if 'lang' in data and data['lang'] == 'en':
if "trump" in data['text'].lower(): #EDIT
tweets.append(data['text'])
print 'Received tweet #', len(tweets), data['text']
if len(tweets) >= 10000:
self.store_json()
self.disconnect()
# overriding
def on_error(self, status_code, data):
print status_code, data
self.disconnect()
def store_json(self):
with open('tweet_stream_{}_{}.json'.format(keyword, len(tweets)), 'w') as f:
json.dump(tweets, f, indent=4)
if __name__ == '__main__':
with open('caston_credentials_twitter.json', 'r') as f:
credentials = json.load(f)
# create your own app to get consumer key and secret
CONSUMER_KEY = credentials['CONSUMER_KEY']
CONSUMER_SECRET = credentials['CONSUMER_SECRET']
ACCESS_TOKEN = credentials['ACCESS_TOKEN']
ACCESS_TOKEN_SECRET = credentials['ACCESS_TOKEN_SECRET']
stream = MyStreamer(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
if len(sys.argv) > 1:
keyword = sys.argv[1]
else:
keyword = 'trump'
try:
stream.statuses.filter(track=keyword)
except:
stream.store_json()