-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkafkaproducer.py
32 lines (27 loc) · 1 KB
/
kafkaproducer.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
from kafka import KafkaProducer
import datetime, time as t, random, threading
import urllib.request
def send_at():
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])
topic = 'bus'
while True:
contents = urllib.request.urlopen("http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?DirectionID=1&returnlist=DirectionID,StopPointName,EstimatedTime,LineName").read()
station = []
bus = []
time = []
data1 = contents.decode('utf-8')
data1 = data1.split("\n")
# print(data1)
bad_chars = ['"', '[', ']', '\r']
for d in data1[1:]:
for i in bad_chars:
d = d.replace(i, '')
d = d.split(',')
#print(d)
msg = '%s--%s--%s' % (d[1], d[2], d[4])
print(msg)
producer.send(topic, msg.encode('ascii'))
print("#####################################################################")
t.sleep(240)
if __name__ == "__main__":
send_at()