· 6 years ago · Nov 04, 2019, 03:26 PM
1#97519eaef3dacac79a66972f4ac13e9b
2# https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=97519eaef3dacac79a66972f4ac13e9b
3import requests
4from pprint import pprint
5import time
6import random
7
8# API KEY
9API_key = "97519eaef3dacac79a66972f4ac13e9b"
10
11# Auth Token
12token = '616d1144ce6ec5460c28d7b161280ab2'
13
14# This stores the url
15base_url = "http://api.openweathermap.org/data/2.5/weather?"
16
17# This will ask the user to enter city ID
18city_id = "2172797"
19
20# This is final url. This is concatenation of base_url, API_key and city_id
21Final_url = base_url + "appid=" + API_key + "&id=" + city_id
22
23chance = 100
24while(chance):
25 # this variable contain the JSON data which the API returns
26 weather_data = requests.get(Final_url).json()
27 temp = weather_data['main']['temp']
28 temp = int(temp) - 273.15
29 num = random.random()
30 temp += num
31 print(temp)
32 hu = weather_data['main']['humidity']
33 num = random.random()
34 hu += num
35 print(hu)
36 pr = weather_data['main']['pressure']
37 num = random.random()
38 pr += num
39 print(pr)
40
41 datumd = {
42 "temp": temp, "humidity": hu, "pressure": pr
43 }
44
45 r = requests.post("http://localhost:3000/data/upload", headers={'Authorization': token}, data=datumd)
46
47 print(r)
48 time.sleep(1)
49 chance -= 1