-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathsync_nextbike.py
More file actions
92 lines (71 loc) · 2.3 KB
/
Copy pathsync_nextbike.py
File metadata and controls
92 lines (71 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Very dumb script that goes through the public list of nextbike feeds and
syncs it with pybikes. Might work for 80% of the cases.
"""
import json
import requests
from slugify import slugify
import pybikes
NEXTBIKE_LIVE_FEED = "https://api.nextbike.net/maps/nextbike-live.json"
# There are many tests systems. Put them here so they are ignored
ignored_domains = [
"wh",
"tb",
"bs",
"bw",
"lt",
]
def pybikes_net(country):
# Try to detect when a system has a distinct name other than
# nextbike-city. Or otherwise, detect when it is _not_ relevant
# that is easier I guess
city = " - ".join([c["name"] for c in country["cities"]])
tag = slugify(country["name"])
if "nextbike" not in tag:
tag = "nextbike-%s" % tag
return {
"domain": country["domain"],
"tag": tag,
"meta": {
"name": country["name"],
"city": city,
"country": country["country"],
"latitude": country["lat"],
"longitude": country["lng"],
},
}
data = requests.get(NEXTBIKE_LIVE_FEED).json()
py_data = pybikes.get_data("nextbike")
py_gbfs_data = pybikes.get_data("nextbike_gbfs")
domains = [c["domain"] for c in data["countries"]]
py_domains = [i["domain"] for i in py_data["instances"]]
py_domains += [i["domain"] for i in py_gbfs_data["instances"]]
instances = []
for i in py_data["instances"]:
if i["domain"] not in domains:
# domain not present, check if it works
instance = pybikes.get(i["tag"])
try:
instance.update()
except:
# its borked, remove it
continue
# keep it
instances.append(i)
# Now look for new ones
for country in data["countries"]:
places = sum([len(c["places"]) for c in country["cities"]])
# Ignore empty networks
if places == 0:
# Check if such system is in pybikes and remove it
n = next((i for i in instances if i["domain"] == country["domain"]), None)
if n:
instances.remove(n)
continue
# ignore already supported network
if country["domain"] in py_domains:
continue
if country["domain"] in ignored_domains:
continue
instances.append(pybikes_net(country))
py_data["instances"] = instances
print(json.dumps(py_data, indent=4))