-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_capture.py
164 lines (115 loc) · 4 KB
/
twitter_capture.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# module to connect to twitter stream
import json
import time
import sys
import localConfig
from localConfig import logging
from twarc import Twarc
from twisted.internet import protocol
# Apache Kafka
from kafka import KafkaClient, SimpleProducer
# init Kafka
kafka = KafkaClient("localhost:9092")
producer = SimpleProducer(kafka)
# #--- Python RQ ---*
# class TwitterStreamRQ(object):
# # WORKING TWITTER HOSE
# def __init__(self, search_terms):
# logging.info("initializing TwitterStream RQ")
# # globals to all instances
# self.t = Twarc(localConfig.client_key, localConfig.client_secret, localConfig.access_token, localConfig.access_token_secret)
# self.search_terms = search_terms
# # method to capture twitter stream
# def captureStream(self):
# fhand = open('/tmp/tweet_hose.csv','w')
# for tweet in self.t.stream(",".join(self.search_terms)):
# tweet_process.delay(tweet)
# fhand.write(json.dumps(tweet))
# fhand.write('\n')
# fhand.close()
class LocalStreamRQ(object):
# LOCAL TWITTER HOSE EMULATOR
def __init__(self, search_terms):
logging.info("initializing LocalStream RQ")
# globals to all instances
self.search_terms = search_terms
fhand = open('tests/real_tweets.csv','r')
self.lt = []
for line in fhand:
self.lt.append(json.loads(line))
# method to capture twitter stream
def captureStream(self):
while True:
logging.debug("sending stream at slight delay")
for tweet in self.lt:
tweet_process.delay(tweet)
time.sleep(.25)
# method to capture twitter stream
def burstStream(self):
logging.debug("sending all local tweets as burst")
for tweet in self.lt:
tweet_process.delay(tweet)
#--- Apache Kafka---*
class TwitterStreamKafka(object):
# WORKING TWITTER HOSE
def __init__(self, search_terms):
logging.info("initializing TwitterStream Kafka")
# globals to all instances
self.t = Twarc(localConfig.client_key, localConfig.client_secret, localConfig.access_token, localConfig.access_token_secret)
self.search_terms = search_terms
# method to capture twitter stream
def captureStream(self):
for tweet in self.t.stream(",".join(self.search_terms)):
result = producer.send_messages("betweezered", json.dumps(tweet))
class LocalStreamKafka(object):
# LOCAL TWITTER HOSE EMULATOR
def __init__(self, search_terms):
logging.info("initializing LocalStream Kafka")
# globals to all instances
self.search_terms = search_terms
fhand = open('tests/real_tweets.csv','r')
self.lt = []
for line in fhand:
self.lt.append(line)
# method to capture twitter stream
def captureStream(self):
while True:
logging.debug("sending stream at slight delay")
for tweet_json in self.lt:
result = producer.send_messages("betweezered", tweet_json)
time.sleep(.25)
# method to capture twitter stream
def burstStream(self):
logging.debug("sending all local tweets as burst")
for tweet_json in self.lt:
result = producer.send_messages("betweezered", tweet_json)
def main():
# grab stream type
if len(sys.argv) < 2:
logging.critical("Please enter either 'lt' for local tweets, or 'ts' for live tweets, followed with 'rq' or 'kafka' for capture type. e.g. 'ltrq' for local, rq capture, or 'tskafka' for live, kafka capture")
else:
stream_type = sys.argv[1]
# #--- RQ Capture ---#
# if stream_type == 'tsrq':
# ts = TwitterStreamRQ(localConfig.search_terms)
# ts.captureStream()
# if stream_type == 'ltrq':
# lt = LocalStreamRQ(localConfig.search_terms)
# if sys.argv[2] == "burst":
# lt.burstStream()
# if sys.argv[2] == "stream":
# lt.captureStream()
#--- Kafka Capture ---#
if stream_type == 'tskafka':
if len(sys.argv) > 2: # if search term override provided, used
localConfig.search_terms = [sys.argv[2]]
ts = TwitterStreamKafka(localConfig.search_terms)
ts.captureStream()
if stream_type == 'ltkafka':
lt = LocalStreamKafka(localConfig.search_terms)
if sys.argv[2] == "burst":
lt.burstStream()
if sys.argv[2] == "stream":
lt.captureStream()
if __name__ == '__main__':
main()