Commit d0ae1b38c6e8d9617bdcec157f1195a7093e6bf5

Authored by
1 parent aa84636b

Code pour récupérer les informations et les convertir en nombre binaire

Showing 2 changed files with 122 additions and 0 deletions   Show diff stats
get_data.py 0 → 100644
@@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
  1 +import locale
  2 +import threading
  3 +import time
  4 +import requests
  5 +import json
  6 +import traceback
  7 +#import feedparser
  8 +
  9 +
  10 +LOCALE_LOCK = threading.Lock()
  11 +
  12 +time_format = 24 # 12 or 24
  13 +weather_api_token = '76e35ed2404406f58fe91265afacbb2b' # create account at https://darksky.net/dev/
  14 +weather_lang = 'fr' # see https://darksky.net/dev/docs/forecast for full list of language parameters values
  15 +weather_unit = 'auto' # see https://darksky.net/dev/docs/forecast for full list of unit parameters values
  16 +latitude = '39.917' # Set this if IP location lookup does not work for you (must be a string)
  17 +longitude = '116.433' # Set this if IP location lookup does not work for you (must be a string)
  18 +
  19 +
  20 +def tick():
  21 + if time_format == 12:
  22 + heure = int(time.strftime('%M %p')) #hour in 12h format
  23 + minute = int(time.strftime('%M %p'))
  24 + else:
  25 + heure = int(time.strftime('%H')) #hour in 24h format
  26 + minute =int(time.strftime('%M'))
  27 +
  28 + return(heure,minute)
  29 +
  30 +
  31 +def get_weather():
  32 +
  33 + weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit)
  34 +
  35 + r = requests.get(weather_req_url)
  36 + weather_obj = json.loads(r.text)
  37 +
  38 + temperature2 = int(weather_obj['currently']['temperature'])
  39 +
  40 + return(temperature2)
  41 +
  42 +def dec2bin(d,nb=6):
  43 +
  44 + if d == 0:
  45 + return "0".zfill(nb)
  46 + if d<0:
  47 + d += 1<<nb
  48 + b=""
  49 + while d != 0:
  50 + d, r = divmod(d, 2)
  51 + b = "01"[r] + b
  52 + return b.zfill(nb)
  53 +
  54 +def binary_expression(heure,minute,temperature):
  55 + t_low = dec2bin(temperature)
  56 +
  57 + if (temperature < 0):
  58 + t_high = '11'
  59 + else:
  60 + t_high = '10'
  61 +
  62 +
  63 + m_low = dec2bin(minute)
  64 + m_high = '01'
  65 +
  66 + h_low = dec2bin(heure)
  67 + h_high = '00'
  68 +
  69 + return(h_high+h_low,m_high+m_low,t_high+t_low)
  70 +
  71 +
  72 +heure,minute = tick()
  73 +temperature = get_weather()
  74 +
  75 +h,m,t = binary_expression(heure,minute, temperature)
  76 +
  77 +print(h,m,t)
  78 +
  79 +
  80 +
  81 +
  82 +
  83 +
test_appel_python.c 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <string.h>
  4 +
  5 +#define TAILLE_MAX_BUFFER 50
  6 +#define TAILLE_MOT 8
  7 +
  8 +int main()
  9 +{
  10 +
  11 + FILE *file = popen("python get_data.py","r");
  12 +
  13 + char a[TAILLE_MAX_BUFFER], b[TAILLE_MAX_BUFFER], c[TAILLE_MAX_BUFFER];
  14 +
  15 + fscanf(file,"%s",a);
  16 + fscanf(file,"%s",b);
  17 + fscanf(file,"%s",c);
  18 + pclose(file);
  19 +
  20 + int i;
  21 + char heure[TAILLE_MOT],minute[TAILLE_MOT],temperature[TAILLE_MOT];
  22 + for(i=0;i<8;i++){
  23 + heure[i]=a[i+2];
  24 + minute[i]=b[i+1];
  25 + temperature[i]=c[i+1];
  26 + }
  27 +
  28 + heure[8]='\0';
  29 + minute[8]='\0';
  30 + temperature[8]='\0';
  31 +
  32 +
  33 +
  34 + printf("%s\n",heure);
  35 + printf("%s\n",minute);
  36 + printf("%s\n",temperature);
  37 +
  38 + return(0);
  39 +}