-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
97 lines (90 loc) · 3.1 KB
/
weather.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
# -*- coding: UTF-8 -*-
import requests
import sys
reload(sys)
import json
url = 'https://api.github.com'
payload = {'some': 'data'}
r = requests.post(url)
print r.status_code
# sys.setdefaultencoding('utf-8')
# s_city = "Minsk, BY"
# city_id=625144
# appid = "afdd37611c9659a2f5fc7992b5776290"
#
#
# def get_wind_direction(deg):
# l = ['С ','СВ',' В','ЮВ','Ю ','ЮЗ',' З','СЗ']
# for i in range(0,8):
# step = 45.
# min = i*step - 45/2.
# max = i*step + 45/2.
# if i == 0 and deg > 360-45/2.:
# deg = deg - 360
# if deg >= min and deg <= max:
# res = l[i]
# break
# return res
#
# # Проверка наличия в базе информации о нужном населенном пункте
# def get_city_id(s_city_name):
# try:
# res = requests.get("http://api.openweathermap.org/data/2.5/find",
# params={'q': s_city_name, 'type': 'like', 'units': 'metric', 'lang': 'en', 'APPID': appid})
# data = res.json()
# cities = ["{} ({})".format(d['name'], d['sys']['country'])
# for d in data['list']]
# print("city:", cities)
# city_id = data['list'][0]['id']
# print('city_id=', city_id)
# except Exception as e:
# print("Exception (find):", e)
# pass
# assert isinstance(city_id, int)
# print city_id
# return city_id
#
# # Запрос текущей погоды
# def request_current_weather(city_id):
# try:
# res = requests.get("http://api.openweathermap.org/data/2.5/weather",
# params={'id': city_id, 'units': 'metric', 'lang': 'en', 'APPID': appid})
# data = res.json()
# print("conditions:", data['weather'][0]['description'])
# print("temp:", data['main']['temp'])
# print("temp_min:", data['main']['temp_min'])
# print("temp_max:", data['main']['temp_max'])
# print("data:", data)
# except Exception as e:
# print("Exception (weather):", e)
# pass
#
# # Прогноз
# def request_forecast(city_id):
# try:
# res = requests.get("http://api.openweathermap.org/data/2.5/forecast",
# params={'id': city_id, 'units': 'metric', 'lang': 'en', 'APPID': appid})
# data = res.json()
# print('city:', data['city']['name'], data['city']['country'])
# for i in data['list']:
# print( (i['dt_txt'])[:16], '{0:+3.0f}'.format(i['main']['temp']),
# '{0:2.0f}'.format(i['wind']['speed']) + " м/с",
# get_wind_direction(i['wind']['deg']),
# i['weather'][0]['description'] )
# except Exception as e:
# print("Exception (forecast):", e)
# pass
#
# # #city_id for SPb
# # city_id = 519690
#
# import sys
# if len(sys.argv) == 2:
# s_city_name = sys.argv[1]
# print("city:", s_city_name)
# city_id = get_city_id(s_city_name)
# elif len(sys.argv) > 2:
# print('Enter name of city as one argument. For example: Petersburg,RU')
# sys.exit()
#
# request_forecast(city_id)