-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
executable file
·50 lines (44 loc) · 1.76 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
import json
import datetime
from pathlib import Path
########################################################
# CONFIGURATION
########################################################
# UNITY = f for fahrenheit, c for celsius or k for kelvin
# API_KEY = free register in https://openweathermap.org
# and get your token
# CITY = your city name
# COUNTRY = your country abbreviation: us, ru, br, etc..
########################################################
UNITY = "c"
API_KEY = "your openweahter.org appid"
CITY = "city name"
COUNTRY = "country name"
########################################################
main_url = "http://api.openweathermap.org/data/2.5/weather?"
url = main_url+"APPID={}&q={},{}&cnt=3".format(API_KEY,CITY,COUNTRY)
metrics = {"k":"&units=default","c":"&units=metric","f":"&units=imperial"}
for key,value in metrics.items():
if UNITY == key:
url += value
request = urllib.request.urlopen(url)
if request.status != 200:
exit(0)
response = request.read()
j = json.loads(response.decode("utf-8"))
home = str(Path.home())
weather = open(home+"/.cache/weather.txt",'w+')
weather.write("city>"+j["name"]+"\n")
weather.write("country>"+j["sys"]["country"]+"\n")
weather.write("temp>"+str(j["main"]["temp"])+"\n")
weather.write("description>"+j["weather"][0]["description"].capitalize()+"\n")
weather.write("min>"+str(j["main"]["temp_min"])+"\n")
weather.write("max>"+str(j["main"]["temp_max"])+"\n")
weather.write("humidity>"+str(j["main"]["humidity"])+"\n")
weather.write("pressure>"+str(j["main"]["pressure"])+"\n")
weather.write("wind>"+str(j["wind"]["speed"])+"\n")
weather.write("update>"+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"\n")
weather.close()