get_data.py
1.96 KB
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
import locale
import threading
import time
import requests
import json
import traceback
#import feedparser
LOCALE_LOCK = threading.Lock()
time_format = 24 # 12 or 24
weather_api_token = '76e35ed2404406f58fe91265afacbb2b' # create account at https://darksky.net/dev/
weather_lang = 'fr' # see https://darksky.net/dev/docs/forecast for full list of language parameters values
weather_unit = 'auto' # see https://darksky.net/dev/docs/forecast for full list of unit parameters values
latitude = '39.917' # Set this if IP location lookup does not work for you (must be a string)
longitude = '116.433' # Set this if IP location lookup does not work for you (must be a string)
def tick():
if time_format == 12:
heure = int(time.strftime('%M %p')) #hour in 12h format
minute = int(time.strftime('%M %p'))
else:
heure = int(time.strftime('%H')) #hour in 24h format
minute =int(time.strftime('%M'))
return(heure,minute)
def get_weather():
weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit)
r = requests.get(weather_req_url)
weather_obj = json.loads(r.text)
temperature2 = int(weather_obj['currently']['temperature'])
return(temperature2)
def dec2bin(d,nb=6):
if d == 0:
return "0".zfill(nb)
if d<0:
d += 1<<nb
b=""
while d != 0:
d, r = divmod(d, 2)
b = "01"[r] + b
return b.zfill(nb)
def binary_expression(heure,minute,temperature):
t_low = dec2bin(temperature)
if (temperature < 0):
t_high = '11'
else:
t_high = '10'
m_low = dec2bin(minute)
m_high = '01'
h_low = dec2bin(heure)
h_high = '00'
return(h_high+h_low,m_high+m_low,t_high+t_low)
heure,minute = tick()
temperature = get_weather()
h,m,t = binary_expression(heure,minute, temperature)
print(h,m,t)