-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (85 loc) · 3.81 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import configparser
import pyodbc
import requests
import json
config = configparser.ConfigParser()
config.read('config.ini')
API_KEY = config.get('credentials', 'api_key')
SERVER_NAME = config.get('credentials', 'server_name')
DATABASE_NAME = config.get('credentials', 'database_name')
USERNAME = config.get('credentials', 'username')
PASSWORD = config.get('credentials', 'password')
PORT = config.get('credentials', 'port')
def retrieve_cities_from_database():
# Establish the database connection
connection_string = 'DRIVER={SQL Server};SERVER=' + SERVER_NAME + ';PORT=' + PORT + ';DATABASE=' + DATABASE_NAME + ';UID=' + USERNAME + ';PWD=' + PASSWORD + ';'
conn = pyodbc.connect(connection_string)
# Create a cursor to execute SQL queries
cursor = conn.cursor()
# Execute the SQL query to retrieve data
sql_query = 'SELECT [Id], [CountryId], [Name], [Latitude], [Longitude] FROM [dbo].[Cities]'
cursor.execute(sql_query)
# Fetch all rows and retrieve the column names
rows = cursor.fetchall()
column_names = [column[0] for column in cursor.description]
# Convert the result set to a list of dictionaries
result_set = []
for row in rows:
result_set.append(dict(zip(column_names, row)))
# Close the database connection
conn.close()
return result_set
def get_weather_location(latitude, longitude, api_key):
url = f"http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey={api_key}&q={latitude},{longitude}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data['Key']
else:
print('API request failed with status code:', response.status_code)
return None
def get_current_weather(locationkey, api_key):
url = f"http://dataservice.accuweather.com/currentconditions/v1/{locationkey}?apikey={api_key}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data #data[0]
else:
print('API request failed with status code:', response.status_code)
return None
def store_cities_json_database(json_data):
# Establish the database connection
connection_string = 'DRIVER={SQL Server};SERVER=' + SERVER_NAME + ';PORT=' + PORT + ';DATABASE=' + DATABASE_NAME + ';UID=' + USERNAME + ';PWD=' + PASSWORD + ';'
conn = pyodbc.connect(connection_string)
# Create a cursor to execute SQL queries
cursor = conn.cursor()
try:
# Convert the JSON object to a string
json_string = json.dumps(json_data)
# Construct the SQL query
sql_query = 'INSERT INTO [dbo].[JsonData] ([JsonDocument]) VALUES (?)'
# Execute the SQL query with the JSON string as a parameter
cursor.execute(sql_query, json_string)
# Commit the changes
conn.commit()
print('JSON data stored successfully in the database.')
except Exception as e:
# Handle any exceptions that occur during the database operation
print('Error storing JSON data in the database:', str(e))
conn.rollback()
# Close the database connection
conn.close()
def main():
api_key = API_KEY
# Assign the function output to the dictionary
cities = retrieve_cities_from_database()
# Loop through the dictionary and pass multiple values to the API calling function
for row in cities:
latitude = row['Latitude']
longitude = row['Longitude']
locationkey = get_weather_location(latitude, longitude, api_key)
#print(locationkey)
json_data = get_current_weather(locationkey, api_key)
#print(json_data)
store_cities_json_database(json_data)
main()