get_data.py 1.96 KB
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)