-
Notifications
You must be signed in to change notification settings - Fork 5
/
Streaming_Tweets.py
113 lines (82 loc) · 3.18 KB
/
Streaming_Tweets.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
###################################
### ###
### Joshua G. Mausolf ###
### Department of Sociology ###
### Computation Institute ###
### University of Chicago ###
### ###
###################################
import csv, os, re
import argparse, textwrap
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
#Insert Your Twitter Credentials Here
from mycredentials import *
#Set Authorization
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
#Utility Functions
def remove_non_ascii_2(text):
return re.sub(r'[^\x00-\x7F]+', "'", text)
def start_csv():
with open('streaming_results.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(["DATE", "AUTHOR", "TWEET"])
#Tweepy Streaming Listener
class TwitterListener(StreamListener):
def on_status(self, status):
try:
with open('streaming_results.csv', 'a') as f:
writer = csv.writer(f)
#Tweet Content
author = str(status.author.screen_name.encode('utf-8'))
date = str(status.created_at)
tweet_raw = str(status.text.encode('utf-8'))
tweet = remove_non_ascii_2(tweet_raw)
print("{} Tweet: {} ...".format(date, tweet[0:50]))
writer.writerow([date, author, tweet])
except Exception as error:
print(error)
def on_error(self, status_code):
print(status_code)
#Wrapper Function
def streaming_tweets(keywords, language=["en"]):
"""
@keywords == search keywords, e.g. ["ImWithHer", "Trump"]
@languages == desired language, e.g.: ["en"]
"""
filename = keywords[0].replace(" ", "").replace("#", "")+".csv"
print(filename)
try:
start_csv()
while True:
try:
#Initialize Tweepy Streamer
twitterStream = Stream(auth, TwitterListener())
twitterStream.filter(track=keywords, languages=language)
except Exception as error:
print(error)
print ("[*] saving results to {}".format(filename))
os.rename("streaming_results.csv", filename)
except KeyboardInterrupt:
print ("[*] User KeyboardInterrupt: Tweepy Streamer Haulted")
print ("[*] saving results to {}".format(filename))
os.rename("streaming_results.csv", filename)
#INSERT YOUR DESIRED SEARCH TERMS
#streaming_tweets(["#ImWithHer", "#Trump"])
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Prepare input file',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('keywords', nargs='+', type=str,
help=textwrap.dedent("""\
Input one or more keywords to search
"#ImWithHer"
or
"#ImWithHer" "#Hillary"
The final command takes the following format:
python Streaming_Tweets.py "#ImWithHer" "#Hillary"
"""
))
args = parser.parse_args()
streaming_tweets(args.keywords)