-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractTweet.py
More file actions
86 lines (56 loc) · 2.33 KB
/
ExtractTweet.py
File metadata and controls
86 lines (56 loc) · 2.33 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import csv
from collections import namedtuple
TWITTER_CONFIGS = 'config.json'
def get_twitter_configs():
#load configuration file
config = json.load(open(TWITTER_CONFIGS, 'r'))
twitter_configs = namedtuple('TwitterConfigs',
'consumer_key, consumer_secret, access_token, access_token_secret,file_name,count')
twitter_configs.consumer_key = config["consumer_key"]
twitter_configs.consumer_secret = config["consumer_secret"]
twitter_configs.access_token = config["access_token"]
twitter_configs.access_token_secret = config["access_token_secret"]
twitter_configs.file_name = config["file_name"]
twitter_configs.count = config["count"]
twitter_configs.filter = config["filter"]
return twitter_configs
class StdOutListener(StreamListener):
def __init__(self,count):
self.count=count
self.index = 1
def on_data(self, data):
a = json.loads(data,encoding='utf-8')
if a['lang'] == 'en' and len(a['text']) > 100:
special=[";",r"\r\n"]
current=a['text']
for curSpec in special:
current.replace(curSpec,"")
current=unicode(current.encode('utf-8'), 'ascii', 'ignore')
self.index=self.index+1
print current
writer.writerow([current])
if self.index >= self.count:
return False
else:
return True
def on_error(self, status):
print status
if __name__ == '__main__':
twitter_configs = get_twitter_configs()
count = twitter_configs.count
l = StdOutListener(count)
with open(twitter_configs.file_name, 'wb') as f:
writer = csv.writer(f,delimiter='\t')
auth = OAuthHandler(twitter_configs.consumer_key,
twitter_configs.consumer_secret)
auth.set_access_token(twitter_configs.access_token,
twitter_configs.access_token_secret)
stream = Stream(auth, l)
if len(twitter_configs.filter) > 0:
stream.filter(track=[twitter_configs.filter])
else:
stream.filter(track=["a"])