Skip to content

Commit 24634eb

Browse files
committed
Add Ganxeta system
1 parent 944f784 commit 24634eb

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

pybikes/data/ganxeta.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"instances": [
3+
{
4+
"tag": "ganxeta-reus",
5+
"meta": {
6+
"city": "Reus",
7+
"name": "Ganxeta",
8+
"country": "ES",
9+
"latitude": 41.1589,
10+
"longitude": 1.10623,
11+
"company": [
12+
"Ajuntament de Reus"
13+
]
14+
}
15+
}
16+
],
17+
"system": "ganxeta",
18+
"class": "Ganxeta"
19+
}

pybikes/ganxeta.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (C) 2023, Martín González Gómez <m@martingonzalez.net>
3+
4+
import json
5+
6+
from pybikes import BikeShareSystem, BikeShareStation, PyBikesScraper
7+
8+
ENDPOINT = "https://bks-api.ganxeta.cat/rms-bike-sharing-real-time/graphql"
9+
10+
11+
class Ganxeta(BikeShareSystem):
12+
headers = {
13+
"Content-Type": "application/json; charset=utf-8",
14+
}
15+
16+
payload = {
17+
"operationName": "StationsRealtime",
18+
"query": """query StationsRealtime {
19+
get_data_real_time {
20+
station_id
21+
station_state_id
22+
station_name
23+
station_address
24+
latitude
25+
longitude
26+
bikes_total
27+
bikes_available
28+
bikes_reserved
29+
bikes_maintenance
30+
bikes_occupancy_status
31+
bikes_occupancy_color
32+
racks_total
33+
racks_available
34+
racks_broken
35+
racks_occupancy_status
36+
racks_occupancy_color
37+
station_external_id
38+
}
39+
}""",
40+
}
41+
42+
def __init__(self, tag, meta):
43+
super(Ganxeta, self).__init__(tag, meta)
44+
45+
def update(self, scraper=None):
46+
scraper = scraper or PyBikesScraper()
47+
data = scraper.request(
48+
ENDPOINT,
49+
data=json.dumps(Ganxeta.payload),
50+
headers=Ganxeta.headers,
51+
method="POST",
52+
)
53+
data = json.loads(data)
54+
55+
stations = []
56+
57+
for station in data["data"]["get_data_real_time"]:
58+
stations.append(GanxetaStation(station))
59+
60+
self.stations = stations
61+
62+
63+
class GanxetaStation(BikeShareStation):
64+
def __init__(self, data):
65+
self.name = data["station_name"]
66+
self.latitude = float(data["latitude"])
67+
self.longitude = float(data["longitude"])
68+
69+
self.bikes = int(data["bikes_available"])
70+
self.free = int(data["racks_available"])
71+
72+
self.extra = {
73+
"uid": data["station_id"],
74+
"address": data["station_address"],
75+
"slots": int(data["racks_total"]),
76+
}

0 commit comments

Comments
 (0)