-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-Data-from-DB.py
58 lines (57 loc) · 2.07 KB
/
get-Data-from-DB.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
import requests
import mysql.connector
mydb = mysql.connector.connect(
user='USER',
password='PASSWORD',
host='127.0.0.1',
database='weather_forecast'
)
mycursor = mydb.cursor()
cities_name = ["cairo","alex","aswan","sharm","zagazig"]
# iterating through each city to get their data to append it in the dataset in google sheets using sheety
for city in cities_name:
mycursor.execute(f"SELECT * FROM {city}")
my_result = mycursor.fetchall()
dates = []
location_id = []
temperature_max = []
temperature_min = []
temperature_mean = []
rain_sum = []
wind_speed_max = []
for row in my_result:
datetime_value = row[1] # Assuming the date is in the second column (index 1)
formatted_date = datetime_value.strftime('%m/%d/%Y') # Format date as 'YYYY-MM-DD'
dates.append(formatted_date)
id_l = row[0]
location_id.append(id_l)
temp_max = row[2]
temperature_max.append(temp_max)
temp_min = row[3]
temperature_min.append(temp_min)
temp_mean = row[4]
temperature_mean.append(temp_mean)
rain = row[8]
rain_sum.append(rain)
wind = row[9]
wind_speed_max.append(wind)
for i in range(len(wind_speed_max)):
body = {
'weatherDatasetCsv': {
"locationId": location_id[i],
"date": dates[i],
"temperatureMax": temperature_max[i],
"temperatureMin": temperature_min[i],
"temperatureMean": temperature_mean[i],
"rainSum": rain_sum[i],
"windSpeedMax": wind_speed_max[i]
}
}
SHEETY_ENDPOINT = "https://api.sheety.co/url"
# here type your headers including the auth key that sheety provides for the project
SHEETY_HEADERS = {
'Content-Type': "application/json",
'Authorization': ''
}
sheety_post = requests.post(SHEETY_ENDPOINT, json=body, headers=SHEETY_HEADERS)
print(sheety_post.text)