-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_producer.py
31 lines (21 loc) · 1020 Bytes
/
weather_producer.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
import requests
import logging
import time
from Kafka_producer import Kafka_producer
def get_weather():
# api_url = "https://api.open-meteo.com/v1/forecast?latitude=30.0626&longitude=31.2497¤t=temperature_2m,is_day,cloud_cover,wind_speed_10m&daily=sunrise,sunset&timezone=Africa%2FCairo"
api_url = "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m,is_day,cloud_cover,wind_speed_10m&daily=sunrise,sunset"
response = requests.get(api_url)
return response.json()
def main():
producer = Kafka_producer(topic_name = "weather_data", message_key = "Cairo")
producer.kafka_producer_conf(broker_address = "localhost:9092")
while True:
weather = get_weather()
logging.debug("Got Weather: %s", weather)
producer.kafka_produce(message_value = weather)
logging.debug("Produced a message in weather_data topic")
time.sleep(60)
if __name__ == "__main__":
logging.basicConfig(level = "DEBUG")
main()