· 6 years ago · Nov 04, 2019, 04:58 AM
1#97519eaef3dacac79a66972f4ac13e9b
2# https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=97519eaef3dacac79a66972f4ac13e9b
3import requests
4from pprint import pprint
5
6# API KEY
7API_key = "97519eaef3dacac79a66972f4ac13e9b"
8
9# Device ID
10d_id = 1
11
12# Auth Token
13token = "sd"
14
15# This stores the url
16base_url = "http://api.openweathermap.org/data/2.5/weather?"
17
18# This will ask the user to enter city ID
19city_id = "2172797"
20
21# This is final url. This is concatenation of base_url, API_key and city_id
22Final_url = base_url + "appid=" + API_key + "&id=" + city_id
23
24# this variable contain the JSON data which the API returns
25weather_data = requests.get(Final_url).json()
26temp = weather_data['main']['temp']
27temp = int(temp) - 273.15
28
29# Accessing wind speed, it resides in wind and its key is speed
30wind_speed = weather_data['wind']['speed']
31
32# Accessing Description, it resides in weather and its key is description
33description = weather_data['weather'][0]['description']
34
35# Accessing Latitude, it resides in coord and its key is lat
36latitude = weather_data['coord']['lat']
37
38# Accessing Longitude, it resides in coord and its key is lon
39longitude = weather_data['coord']['lon']
40
41hu = weather_data['main']['humidity']
42print(hu)
43pr = weather_data['main']['pressure']
44print(pr)
45
46# Printing Data
47print('\nTemperature : ',temp)
48print('\nWind Speed : ',wind_speed)
49print('\nDescription : ',description)
50print('\nLatitude : ',latitude)
51print('\nLongitude : ',longitude)
52
53r = requests.post("http://shlok-weather2.herokuapp.com/data", data={"device_id": d_id, "temp": temp, "humidity": hu, "pressure": pr})
54
55print(r)