-
Notifications
You must be signed in to change notification settings - Fork 1
/
tweepy_listener.py
67 lines (55 loc) · 1.77 KB
/
tweepy_listener.py
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
#!/usr/bin/env python
# encoding: utf-8
#download all the tweets of one usr
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import argparse
import string
import config
import json
argsfuc = argparser.ArgumentParser(description="Twitter Downloader")
argsfuc.add_argument('-kwf', dest='kwf', default='./keyword_list.txt', help='the keywords file')
argsv = argsfuc.parse_args()
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class MyListener(StreamListener):
"""Custom StreamListener for streaming data."""
def __init__(self, data_dir, query):
query_fname = query.replace(' ', '_')
self.outfile = "%s/stream_%s.txt" % (data_dir, query_fname)
def on_data(self, data):
try:
with open(self.outfile, 'a') as f:
f.write(data)
print(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
time.sleep(5)
return True
def on_error(self, status):
print(status)
return True
@classmethod
def parse(cls, api, raw):
status = cls.first_parse(api, raw)
setattr(status, 'json', json.dumps(raw))
return status
def cracker(keywordls):
lista = keywordls
for idx,itm in enumerate(lista):
locals()['listen'+str(idx)] = Stream(auth, MyListener(args.data_dir, itm))
locals()['listen'+str(idx)].filter(track=[itm])
return True
if __name__ == '__main__':
tkeys = [itm.strip() for itm in open(argsv.kwf, 'r').readlines()]
cracker(tkeys)