Use Velib GBFS API

This commit is contained in:
Lucas Verney 2022-03-14 21:30:31 +01:00
parent 6773227ae1
commit 79c0dc3b29
1 changed files with 44 additions and 35 deletions

View File

@ -2,7 +2,7 @@
import datetime import datetime
import json import json
import os import os
import pybikes import requests
import sqlite3 import sqlite3
import time import time
@ -67,34 +67,35 @@ def update_stations(conn):
for i in for i in
c.execute("SELECT id, name, address, latitude, longitude, banking, bonus, bike_stands FROM stations").fetchall()} c.execute("SELECT id, name, address, latitude, longitude, banking, bonus, bike_stands FROM stations").fetchall()}
velib = pybikes.get("velib") req_stations = requests.get('https://velib-metropole-opendata.smoove.pro/opendata/Velib_Metropole/station_information.json')
velib.update() stations = {
for station in velib.stations: station['stationCode']: station
for station in req_stations.json()['data']['stations']
}
req_status = requests.get('https://velib-metropole-opendata.smoove.pro/opendata/Velib_Metropole/station_status.json')
for station in req_status.json()['data']['stations']:
uid = station["stationCode"]
try: try:
# Get old station entry if it exists # Get old station entry if it exists
old_station = database_stations[station.extra["uid"]] old_station = database_stations[uid]
# Diff the two stations # Diff the two stations
event = [] event = []
if station.name != old_station[1]: if stations[uid]['name'] != old_station[1]:
event.append({"key": "name", event.append({"key": "name",
"old_value": old_station[1], "old_value": old_station[1],
"new_value": station.name}) "new_value": stations[uid]['name']})
if station.latitude != old_station[3]: if stations[uid]['latitude'] != old_station[3]:
event.append({"key": "latitude", event.append({"key": "latitude",
"old_value": old_station[3], "old_value": old_station[3],
"new_value": station.latitude}) "new_value": stations[uid]['lat']})
if station.longitude != old_station[4]: if stations[uid]['lon'] != old_station[4]:
event.append({"key": "longitude", event.append({"key": "longitude",
"old_value": old_station[4], "old_value": old_station[4],
"new_value": station.longitude}) "new_value": station[uid]['lon']})
if station.extra["banking"] != old_station[5]: if station["numDocksAvailable"] != old_station[7]:
event.append({"key": "banking",
"old_value": old_station[5],
"new_value": station.extra["banking"]})
if station.extra["slots"] != old_station[7]:
event.append({"key": "bike_stands", event.append({"key": "bike_stands",
"old_value": old_station[7], "old_value": old_station[7],
"new_value": station.extra["slots"]}) "new_value": stations[uid]["capacity"]})
# If diff was found # If diff was found
if len(event) > 0: if len(event) > 0:
# Update # Update
@ -102,43 +103,51 @@ def update_stations(conn):
"stations " + "stations " +
"SET name=?, latitude=?, longitude=?, " + "SET name=?, latitude=?, longitude=?, " +
"banking=?, bike_stands=? WHERE id=?", "banking=?, bike_stands=? WHERE id=?",
(station.name, (stations[uid]['name'],
station.latitude, stations[uid]['lat'],
station.longitude, stations[uid]['lon'],
station.extra["banking"], None,
station.extra["slots"], stations[uid]['capacity'],
station.extra["uid"])) uid))
# And insert event in the table # And insert event in the table
c.execute("INSERT INTO " + c.execute("INSERT INTO " +
"stationsevents(station_id, timestamp, event) " + "stationsevents(station_id, timestamp, event) " +
"VALUES(?, ?, ?)", "VALUES(?, ?, ?)",
(station.extra["uid"], (uid,
int(time.time()), int(time.time()),
json.dumps(event))) json.dumps(event)))
except KeyError: except KeyError:
c.execute("INSERT INTO " + c.execute("INSERT INTO " +
"stations(id, name, address, latitude, longitude, banking, bonus, bike_stands) " + "stations(id, name, address, latitude, longitude, banking, bonus, bike_stands) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?)", "VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
(station.extra["uid"], (uid,
station.name, stations[uid]['name'],
"", # Not available "", # Not available
station.latitude, stations[uid]['lat'],
station.longitude, stations[uid]['lon'],
station.extra["banking"], None, # Not available
False, # Not available False, # Not available
station.extra["slots"])) stations[uid]["capacity"]))
except TypeError: except TypeError:
conn.rollback() conn.rollback()
return return
numEBikesAvailable = (
station['numBikesAvailable']
- next(
x['ebike']
for x in station['num_bikes_available_types']
if 'ebike' in x
)
)
c.execute("INSERT INTO " + c.execute("INSERT INTO " +
"stationsstats(station_id, available_bikes, available_ebikes, free_stands, status, updated) " + "stationsstats(station_id, available_bikes, available_ebikes, free_stands, status, updated) " +
"VALUES(?, ?, ?, ?, ?, ?)", "VALUES(?, ?, ?, ?, ?, ?)",
(station.extra["uid"], (uid,
station.bikes - station.extra["ebikes"], station['numBikesAvailable'],
station.extra["ebikes"], numEBikesAvailable,
station.free, station['numDocksAvailable'],
station.extra["status"], None,
int(time.time()))) # Not available, using current timestamp int(time.time()))) # Not available, using current timestamp
conn.commit() conn.commit()