summaryrefslogtreecommitdiff
path: root/.config/conky/weather.py
blob: b699984601ec43b693f8e7950e4a2df14b2d9800 (plain)
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
#!/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  = "b91400ad90f74c8cc0bccf5e2978aaca"
CITY     = "Montevideo"
COUNTRY  = "uy"

########################################################
main_url = "http://api.openweathermap.org/data/2.5/weather?"
url = main_url+"APPID={}&q={},{}".format(API_KEY,CITY,COUNTRY)
print(url)
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()