Skip to content

Commit 59661d4

Browse files
authored
Merge pull request #755 from eskerda/int-pedalada
int pedalada
2 parents bd62299 + ee6e4cd commit 59661d4

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

pybikes/data/pedalada.json

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"system": "pedalada",
3+
"class": "Pedalada",
4+
"instances": [
5+
{
6+
"endpoint": "https://vitelinhas.cm-fafe.pt",
7+
"meta": {
8+
"city": "Fafe",
9+
"country": "PT",
10+
"latitude": 41.4513,
11+
"longitude": -8.1728,
12+
"name": "Vitelinhas",
13+
"ebikes": true,
14+
"company": [
15+
"Município de Fafe"
16+
]
17+
},
18+
"tag": "vitelinhas"
19+
},
20+
{
21+
"endpoint": "https://trofinetes.mun-trofa.pt",
22+
"meta": {
23+
"city": "Trofa",
24+
"country": "PT",
25+
"latitude": 41.3436,
26+
"longitude": -8.5530,
27+
"name": "Trofinetes",
28+
"scooters": true,
29+
"company": [
30+
"Município de Trofa"
31+
]
32+
},
33+
"tag": "trofinetes"
34+
},
35+
{
36+
"endpoint": "https://biclis.cm-leiria.pt",
37+
"meta": {
38+
"city": "Leiria",
39+
"country": "PT",
40+
"latitude": 39.7495,
41+
"longitude": -8.8074,
42+
"name": "biclis",
43+
"ebikes": true,
44+
"company": [
45+
"Município de Leiria"
46+
]
47+
},
48+
"tag": "biclis"
49+
},
50+
{
51+
"endpoint": "https://bia.cm-ovar.pt",
52+
"meta": {
53+
"city": "Ovar",
54+
"country": "PT",
55+
"latitude": 40.8599,
56+
"longitude": -8.6246,
57+
"name": "BIA",
58+
"ebikes": true,
59+
"company": [
60+
"Município de Ovar"
61+
]
62+
},
63+
"tag": "bia"
64+
}
65+
]
66+
}

pybikes/pedalada.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import json
2+
3+
from pybikes import BikeShareSystem, BikeShareStation, PyBikesScraper
4+
5+
STATIONS_URL = '{endpoint}/php/get-stations-details-map.php'
6+
STATION_DETAILS_AND_RIDES_URL = '{endpoint}/php/get-station-details-and-rides.php?n={station_number}'
7+
STATION_IMAGE_URL = '{endpoint}/images/stations/station_{station_number}.jpg'
8+
9+
10+
def station_details_and_rides_url(endpoint, station_number):
11+
return STATION_DETAILS_AND_RIDES_URL.format(endpoint=endpoint, station_number=station_number)
12+
13+
def station_image_url(endpoint, station_number):
14+
return STATION_IMAGE_URL.format(endpoint=endpoint, station_number=station_number)
15+
16+
def format_status(data):
17+
if data['stationInMaintenance']:
18+
return 'maintenance'
19+
elif data['stationIsOpen']:
20+
return 'open'
21+
else:
22+
return 'closed'
23+
24+
25+
class Pedalada(BikeShareSystem):
26+
27+
sync = False
28+
29+
meta = {
30+
'company': [
31+
'Share2Go - Mobilidade Partilhada, Sociedade Unipessoal Lda'
32+
]
33+
}
34+
35+
def __init__(self, tag, meta, endpoint):
36+
meta['company'] += Pedalada.meta['company']
37+
super(Pedalada, self).__init__(tag, meta)
38+
self.endpoint = endpoint
39+
40+
@property
41+
def stations_url(self):
42+
return STATIONS_URL.format(endpoint=self.endpoint)
43+
44+
def update(self, scraper=None):
45+
scraper = scraper or PyBikesScraper()
46+
# they let ssl certificates to expire
47+
scraper.ssl_verification = False
48+
stations_data = json.loads(scraper.request(self.stations_url))
49+
50+
stations = []
51+
for station in stations_data:
52+
station = PedaladaStation(station, self.endpoint)
53+
stations.append(station)
54+
55+
self.stations = stations
56+
57+
class PedaladaStation(BikeShareStation):
58+
def __init__(self, data, endpoint):
59+
super(PedaladaStation, self).__init__()
60+
self.endpoint = endpoint
61+
62+
self.name = data['stationName']
63+
self.latitude = data['latitude']
64+
self.longitude = data['longitude']
65+
66+
self.extra = {
67+
'uid': data['stationNumber'],
68+
'slots': data['maximumNumberOfRides'],
69+
'online': data['state'] == 1,
70+
'photo': station_image_url(endpoint, data['stationNumber']),
71+
'status': format_status(data),
72+
}
73+
74+
def update(self, scraper=None):
75+
scraper = scraper or PyBikesScraper()
76+
# they let ssl certificates to expire
77+
scraper.ssl_verification = False
78+
station_details_and_rides = json.loads(scraper.request(station_details_and_rides_url(self.endpoint, self.extra['uid'])))
79+
80+
docked_bikes_count = len(station_details_and_rides['rides'])
81+
self.bikes = docked_bikes_count
82+
self.free = self.extra['slots'] - docked_bikes_count
83+
84+
station_details = station_details_and_rides['details'][0]
85+
self.extra['status'] = format_status(station_details)

0 commit comments

Comments
 (0)