|
| 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