Skip to content

Commit 20d15ca

Browse files
committed
Implement OTP feeds and move helsinki to it
1 parent cee1958 commit 20d15ca

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed

pybikes/data/otp.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"system":"otp",
3+
"class": "OTP",
4+
"instances":[
5+
{
6+
"tag":"citybikes-helsinki",
7+
"meta":{
8+
"city":"Helsinki",
9+
"name":"City bikes",
10+
"country":"FI",
11+
"company":[
12+
"Helsinki City Transport",
13+
"Helsinki Regional Transport",
14+
"CityBikeFinland",
15+
"Smoove SAS",
16+
"Moventia",
17+
"Alepa"
18+
],
19+
"longitude":24.938379,
20+
"latitude":60.16985569999999
21+
},
22+
"feed_url":"https://dev.hsl.fi/matka.hsl.fi/otp/routers/hsl/bike_rental"
23+
}
24+
]
25+
}

pybikes/data/smoove.json

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,7 @@
22
"system":"smoove",
33
"class":{
44
"SmooveAPI":{
5-
"instances":[
6-
{
7-
"tag":"citybikes-helsinki",
8-
"meta":{
9-
"city":"Helsinki",
10-
"name":"City bikes",
11-
"country":"FI",
12-
"company":[
13-
"Helsinki City Transport",
14-
"Helsinki Regional Transport",
15-
"CityBikeFinland",
16-
"Smoove SAS",
17-
"Moventia",
18-
"Alepa"
19-
],
20-
"longitude":24.938379,
21-
"latitude":60.16985569999999
22-
},
23-
"feed_url":"https://helsinki-fi-smoove.klervi.net/api-public/stations"
24-
}
25-
]
5+
"instances":[]
266
},
277
"Smoove":{
288
"instances":[

pybikes/otp.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
import json
3+
4+
from .base import BikeShareSystem, BikeShareStation
5+
from .utils import PyBikesScraper
6+
7+
# Documented at
8+
# http://dev.opentripplanner.org/apidoc/0.15.0/ns0_bikeRentalStationList.html
9+
10+
11+
class OTP(BikeShareSystem):
12+
# Please note company is not provided by this class and should be added on
13+
# the metadata JSON, as OTP implementation is generic for different systems
14+
meta = {
15+
'system': 'OTP',
16+
}
17+
18+
def __init__(self, tag, feed_url, meta):
19+
super(OTP, self).__init__(tag, meta)
20+
self.feed_url = feed_url
21+
22+
def update(self, scraper=None):
23+
scraper = scraper or PyBikesScraper()
24+
scraper.headers['Accept'] = 'application/json'
25+
26+
data = json.loads(scraper.request(self.feed_url))
27+
stations = []
28+
for st in data['stations']:
29+
name = st['name']
30+
bikes = st['bikesAvailable']
31+
free = st['spacesAvailable']
32+
lat = st['x']
33+
lng = st['y']
34+
extra = {
35+
'uid': st['id']
36+
}
37+
station = BikeShareStation(name, lat, lng, bikes, free, extra)
38+
stations.append(station)
39+
40+
self.stations = stations

pybikes/smoove.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ def update(self, scraper=None):
5959

6060

6161
class SmooveAPI(Smoove):
62-
62+
# Example feed:
63+
# https://helsinki-fi-smoove.klervi.net/api-public/stations
6364
def update(self, scraper=None):
6465
scraper = scraper or utils.PyBikesScraper()
6566

6667
data = json.loads(scraper.request(self.feed_url))
67-
68+
stations = []
6869
for s in data['result']:
6970
lat, lng = map(float, s['coordinates'].split(','))
7071
name = s['name']
@@ -82,4 +83,6 @@ def update(self, scraper=None):
8283
extra['uid'] = idx
8384

8485
station = BikeShareStation(name, lat, lng, bikes, free, extra)
85-
self.stations.append(station)
86+
stations.append(station)
87+
88+
self.stations = stations

0 commit comments

Comments
 (0)