Created
March 9, 2019 15:01
-
-
Save datacorner/e8d533c577e62e712f453a355e671ee8 to your computer and use it in GitHub Desktop.
Python Timer JSON API call each x seconds ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Timer | |
import requests | |
import pandas as pd | |
from time import localtime, strftime | |
def update(): | |
getData() | |
set_timer() | |
def set_timer(): | |
Timer(durationinsec, update).start() | |
def main(): | |
update() | |
def getData(): | |
global iteration | |
url = "htt Get URL call here" | |
mytime = strftime("%Y-%m-%d %H:%M:%S", localtime()) | |
resp = requests.get(url) | |
if resp.status_code != 200: | |
print(iteration, " - Erreur dans la récupération des données") | |
else: | |
data = resp.json() | |
dff = pd.DataFrame(columns =['Timer', 'ID', 'Station', 'Code Station', 'Type de stations', 'Etat de la station', | |
'Nb bornes disponibles', 'Nombres de bornes en station', 'Nombre vélo en PARK+', | |
'Dispo vélo mécanique', 'Dispo vélo électrique', | |
'geo']) | |
for rec in data['records']: | |
dff.loc[len(dff)] = [mytime, | |
rec['recordid'], | |
rec['fields']['station_name'], | |
rec['fields']['station_code'], | |
rec['fields']['station_type'], | |
rec['fields']['station_state'], | |
rec['fields']['nbfreeedock'], | |
rec['fields']['nbedock'], | |
rec['fields']['nbbikeoverflow'], | |
rec['fields']['nbbike'], | |
rec['fields']['nbebike'], | |
rec['fields']['geo']] | |
if int(data['nhits']) > 0: | |
with open("vélib_batch_parheure.csv", 'a') as f: | |
dff.to_csv(f, header=True, index=False) | |
print(mytime, " - ", iteration, " - Fin de la récupération, Nb de lignes récupérées: ", data['nhits']) | |
iteration = iteration + 1 | |
#---------------------------------------- | |
durationinsec = 1*60*60 | |
iteration = 1 | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment