-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_lendxs.py
67 lines (41 loc) · 1.66 KB
/
send_lendxs.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
from time import sleep
import requests
import json
from config import *
#city to query
target_city = 'Nairobi'
BASE_URL = "http://api.weatherstack.com/current"
headers = {
'user-agent':
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36",
}
def getCurrentWeather(url, city, api_key, my_headers):
'''Get the current weather from Weatherstack API'''
querystring = {"access_key": api_key, "query": city}
#query the current weather with the provided querystring
response = requests.request("GET",
url,
params=querystring,
headers=my_headers)
#convert response to json object from received string
current_weather = response.json()
print("current_weather data received")
#return the converted results
return current_weather
def sendToRabbitMq(channel, q_name, message_body):
'''Send message body to RabbitMq channel'''
channel.basic_publish(exchange='',
routing_key=q_name,
body=message_body)
print("Sent to rabbitmq")
#while loop to get the weather data.
while True:
try:
current_weather_data= getCurrentWeather(BASE_URL, target_city, ACCESS_KEY, headers)
#pause the loop for 10 minutes
#send our data to RabbitMQ qeuee
sendToRabbitMq(channel=nairobi_channel, q_name='lendxs', message_body=json.dumps(current_weather_data))
sleep(600)
except Exception as error:
#print out error is we get any
print(error)