-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (52 loc) · 2.17 KB
/
Copy pathmain.py
File metadata and controls
62 lines (52 loc) · 2.17 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
import json
from Widget import Widget
import os.path
# Get IP address:
ip = requests.get('https://checkip.amazonaws.com').text
# Get latitude and longitude from IP address:
request_url = 'https://geolocation-db.com/jsonp/' + ip
response = requests.get(request_url)
result = response.content.decode()
result = result.split("(")[1].strip(")")
result = json.loads(result)
latitude = result['latitude']
longitude = result['longitude']
city = result['city']
# Get weather data using latitude and longitude :
try:
forecast_url = "https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&hourly=temperature_2m," \
"relativehumidity_2m,precipitation,weathercode,windspeed_10m&daily=temperature_2m_max," \
"temperature_2m_min,sunrise,sunset&timezone=auto".format(latitude, longitude)
forecast = requests.get(forecast_url)
forecast_data = forecast.content.decode()
file = open("json_weather.txt", "w")
file.write(forecast_data)
file.close()
forecast_data = json.loads(forecast_data)
# print(json.dumps(forecast_data, indent=4, sort_keys=True))
Widget(forecast_data, city, False)
except Exception as e:
filename = "json_weather.txt"
if os.path.isfile(filename):
file = open("json_weather.txt", "r")
lines = file.readlines()
forecast_data = lines[0]
forecast_data = json.loads(forecast_data)
Widget(forecast_data, city, True)
'''
# API key:
API key: l1xeuPlkJkMUnQOTQWkUn4W3eJ8arGWG
# Get location key using latitude and longitude:
location_url = "http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=l1xeuPlkJkMUnQOTQWkUn4W3eJ8arGWG&q={}%2C{}".format(latitude, longitude)
location = requests.get(location_url)
location_data = location.json()
print(location_data)
print(latitude, longitude)
location_key = location_data['Key']
# Get current weather data using location key:
forecast_url = "http://dataservice.accuweather.com/currentconditions/v1/{}?apikey=l1xeuPlkJkMUnQOTQWkUn4W3eJ8arGWG".format(location_key, latitude, longitude)
forecast = requests.get(forecast_url)
forecast_data = forecast.json()
# Print weather data:
print(forecast_data) '''