-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_time_data_simulator.py
More file actions
44 lines (39 loc) · 1.72 KB
/
real_time_data_simulator.py
File metadata and controls
44 lines (39 loc) · 1.72 KB
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
import time
from kafka_producer import KafkaProducer
import logging
class RealTimeData:
def __init__(self, url: str, server: str, port: int, topic: str, key: str, chars: list, interval: int = 600):
self.url = url
self.server = server
self.port = port
self.topic = topic
self.key = key
self.chars = chars
self.interval = interval
self.producer = KafkaProducer(url=self.url,
server=self.server,
port=self.port,
topic=self.topic,
key=self.key)
self.data_collected = []
def simulate_real_time(self):
for char in list(self.chars):
new_url = f"{self.url}{char}"
new_key = f"{self.key}_{char}"
self.producer.update_url(new_url)
self.producer.update_key(new_key)
# Fetching the API data and store it in the data_collected list
data = self.producer.get_api_data()
if data:
self.data_collected.append(data)
self.producer.produce_to_kafka()
logging.info(f"Produced data for {char}")
else:
logging.error(f"Failed to get data for {char}")
logging.info(f"Produced data for {char}")
logging.info(f"Waiting {self.interval/60} minutes before fetching the next page...")
# time.sleep(self.interval)
return self.data_collected
def run(self):
logging.info("Starting real-time data simulation using Kafka...")
return self.simulate_real_time()