-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathweather.py
More file actions
106 lines (88 loc) · 3.54 KB
/
weather.py
File metadata and controls
106 lines (88 loc) · 3.54 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
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
# -*- coding: utf-8 -*-
'''
Created on Thu Jan 4 20:41:45 2018
Modified Jan 29 2019
Simplified June 16 2022
'''
# weather.py
import datetime
import json
import urllib.request
BASE_URL = '/data/2.5/{}?q={},{}&appid={}'
def get_current_weather(city, country, apikey,**kwargs):
'''get current conditions in specified location
appid='b1b15e88fa797225412429c1c50c122a1'
get_current_weather('London','uk',appid,api='samples')'''
# select between samples or prod api
if 'api' in kwargs:
if kwargs['api'] == 'samples':
DOMAIN = "http://samples.openweathermap.org"
else:
print('not the right api arg')
DOMAIN = "http://api.openweathermap.org"
else:
DOMAIN = "http://api.openweathermap.org"
# Read current conditions
try:
# url = 'http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1'
url = DOMAIN + BASE_URL.format('weather',city,country,apikey)
json_data = json.loads(urllib.request.urlopen(url).read())
return json_data
except urllib.error.URLError:
# If weather API doesnt work
print("API not available")
def parse_current_json(json_data):
'''parse and extract json data from the current weather data'''
try:
# select data of interest from dictionary
weather_info = json_data['main']
weather_info.update(json_data['wind'])
weather_info.update(json_data['coord'])
weather_info['city'] = json_data['name']
# add current date and time
weather_info['current_time'] = str(datetime.datetime.now())
except KeyError as e:
# use current dictionary (because it probably came from backup file)
try:
# If this fails then the json_data didn't come from backup file
json_data.pop('City')
weather_info = json_data
except:
# print('Something else went wrong while parsing current json')
raise e
return weather_info
def get_forecast(city, country, apikey, **kwargs):
'''get forecast conditions in specified location
appid='b1b15e88fa797225412429c1c50c122a1'
get_forecast('Muenchen','DE',appid,api='samples')'''
# select between samples or prod api
if 'api' in kwargs:
if kwargs['api'] == 'samples':
DOMAIN = "http://samples.openweathermap.org"
else:
print('not the right api arg')
DOMAIN = "http://api.openweathermap.org"
else:
DOMAIN = "http://api.openweathermap.org"
# get forecast
try:
url = DOMAIN + BASE_URL.format('forecast',city,country,apikey)
json_data = json.loads(urllib.request.urlopen(url).read())
return json_data
except:
print("API not available")
def parse_forecast_json(json_data):
'''parse and extract json data from the weather forecast data'''
try:
# parse forecast json data
data = json_data['list']
wind_keys = ['deg','speed']
weather_info = {'current_time':[], 'temp':[], 'deg':[],
'speed':[], 'humidity':[], 'pressure':[]}
for data_point in data[0:40]:
for k in list(weather_info.keys())[1:]: #Taking a slice so we don't add the time
weather_info[k].append(float(data_point['wind' if k in wind_keys else 'main'][k]))
weather_info['current_time'].append(data_point['dt_txt'])
return weather_info
except:
print('Something went wrong while parsing forecast json')