-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_streaming.py
53 lines (45 loc) · 1.88 KB
/
twitter_streaming.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
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import sys
from wordnet.bin import paint
import config as cf
import re
TAG = paint('Twitter-Scrapper/','b')
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
j = json.loads(data)
if 'text' in j.keys():
sentence = ''
for word in j['text'].split():
# for every word in text,whose length is larger than or equals 5 and is made of alphabets only, enter that word to sentence.
if len(word) >=5 and re.search(r'^[a-zA-z]+$',word): sentence += ' '+word
# if final length of sentence is atleast greater than 10, print such sentences.
if len(sentence.split()) >5:
print(sentence)
return True
def on_error(self, status):
pass
def main(filter_list):
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(cf.consumer_key, cf.consumer_secret)
auth.set_access_token(cf.access_token, cf.access_token_secret)
stream = Stream(auth, l)
streamer(stream,filter_list)
#func to call streamer, and if any connction error occurs ignore it and call iself recursively.
def streamer(stream,filter_list):
#This line filter Twitter Streams to capture data by the keywords passed in sys.argv
try:
stream.filter(languages=['en'],track=filter_list,stall_warnings=True)
except :
#print(Fore.RED+TAG,'got an error',Style.RESET_ALL)
streamer(stream,filter_list)
if __name__=='__main__':
l = sys.argv
del l[0]
if not l: raise Exception(TAG+'please provide a list of filter words for streaming.')
#print(TAG,'### Tweets Filtered according to string: ',str(l))
main(l)