|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +""" |
| 4 | +velodi.py |
| 5 | +
|
| 6 | +divia_api is a Python library that allows to retrieve the timetable |
| 7 | +of Divia’s bus and tramways straight from a Python script. |
| 8 | +Copyright (C) 2021 Firmin Launay |
| 9 | +
|
| 10 | +This program is free software: you can redistribute it and/or modify |
| 11 | +it under the terms of the GNU General Public License as published by |
| 12 | +the Free Software Foundation, either version 3 of the License, or |
| 13 | +(at your option) any later version. |
| 14 | +
|
| 15 | +This program is distributed in the hope that it will be useful, |
| 16 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +GNU General Public License for more details. |
| 19 | +
|
| 20 | +You should have received a copy of the GNU General Public License |
| 21 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | +""" |
| 23 | + |
| 24 | +from requests import get |
| 25 | +from json import loads |
| 26 | +from .normalize_characters import normalize |
| 27 | +# from normalize_characters import normalize |
| 28 | + |
| 29 | + |
| 30 | +def update_source() -> list: |
| 31 | + raw_response = get( |
| 32 | + "https://www.divia.fr/velo/diviavelodi/ou-trouver-les-stations-diviavelodi").content.decode("utf-8") |
| 33 | + data = raw_response.split("var datas = ")[1].split(";")[0] |
| 34 | + return loads(data)["diviavelodi"] |
| 35 | + |
| 36 | + |
| 37 | +class FreeVelodi: |
| 38 | + def __init__(self, bikes: int, docks: int): |
| 39 | + self.bikes = bikes |
| 40 | + self.docks = docks |
| 41 | + |
| 42 | + def __str__(self): |
| 43 | + return f"{self.bikes} free bikes & {self.docks} free docks." |
| 44 | + |
| 45 | + |
| 46 | +class VelodiStation: |
| 47 | + def __init__(self, code: str, raw_name: str, friendly_name: str): |
| 48 | + self.code = code |
| 49 | + self.raw_name = raw_name |
| 50 | + self.friendly_name = friendly_name |
| 51 | + |
| 52 | + def check(self): |
| 53 | + velodi_data = update_source() |
| 54 | + query_result = list(item for item in velodi_data if (item["infos"]["code_cykleo"] == self.code)) |
| 55 | + try: |
| 56 | + free = query_result[0]["infos"]["qucit"]["realtime"] |
| 57 | + return FreeVelodi(free["bikes"], free["docks"]) |
| 58 | + except IndexError: |
| 59 | + raise Exception("Data not found.") |
| 60 | + |
| 61 | + |
| 62 | +class Velodi: |
| 63 | + def __init__(self): |
| 64 | + self.data = update_source() |
| 65 | + self.stations = [] |
| 66 | + |
| 67 | + for station in self.data: |
| 68 | + station_name = station["infos"]["nom"] |
| 69 | + if " - n°" in station_name.replace(" ", ' ').lower(): |
| 70 | + friendly_name = " - ".join(station_name.replace(" ", ' ').split(" - ")[:-1]) |
| 71 | + else: |
| 72 | + friendly_name = station_name.split(" n°")[0].split(" N°")[0] |
| 73 | + self.stations.append(VelodiStation(station["infos"]["code_cykleo"], station_name, friendly_name)) |
| 74 | + |
| 75 | + def find_station(self, station_name: str) -> VelodiStation: |
| 76 | + corresponding_stations = list( |
| 77 | + item for item in self.stations if (item.friendly_name == station_name) or (item.raw_name == station_name)) |
| 78 | + if len(corresponding_stations) > 0: |
| 79 | + return corresponding_stations[0] |
| 80 | + corresponding_stations = list(item for item in self.stations |
| 81 | + if normalize( |
| 82 | + item.friendly_name.lower()) == normalize(station_name.lower()) or normalize( |
| 83 | + item.raw_name.lower()) == normalize(station_name.lower())) |
| 84 | + if len(corresponding_stations) > 0: |
| 85 | + return corresponding_stations[0] |
| 86 | + |
| 87 | + def get_station(self, station_code: str) -> VelodiStation: |
| 88 | + corresponding_stations = list(item for item in self.stations if item.code == station_code) |
| 89 | + if len(corresponding_stations > 0): |
| 90 | + return corresponding_stations[0] |
0 commit comments