Archived
1
0
Fork 0
This repository has been archived on 2024-06-28. You can view files and clone it, but cannot push or open issues or pull requests.
citybike/import.py

49 lines
1.2 KiB
Python
Raw Permalink Normal View History

2016-05-31 14:06:15 +02:00
#!/usr/bin/python3
import json
import sys
from pprint import pprint
2016-07-18 09:37:38 +02:00
import requests
2016-06-02 06:56:18 +02:00
from config import database
2016-05-31 14:06:15 +02:00
import MySQLdb
try:
2016-06-02 06:56:18 +02:00
db = MySQLdb.connect(database["host"],
database["user"],
database["passwd"],
database["db"])
2016-05-31 14:06:15 +02:00
cur = db.cursor()
2016-07-18 09:37:38 +02:00
payload = {
"data": (
'[out:json][timeout:25];'
'area(3600109166)->.searchArea;'
'node["amenity"="bicycle_rental"]["network"="Citybike Wien"](area.searchArea);'
'out body;>;out skel qt;'
)
}
print("Overpass Abfrage")
r = requests.get('https://overpass-api.de/api/interpreter', params=payload)
data = r.json()
print("erfolgreich")
i = 0
2016-05-31 14:06:15 +02:00
for station in data["elements"]:
if station["type"] == "node":
2016-07-18 09:37:38 +02:00
tags = station["tags"]
cur.execute("REPLACE INTO stationen (ref, lon, lat, name) VALUES (%s,%s,%s,%s)",
(tags["ref"], station["lon"], station["lat"], tags["name"]))
i += 1
2016-05-31 14:06:15 +02:00
db.commit()
2016-07-18 09:37:38 +02:00
print("%s Stationen importiert" % i)
2016-05-31 14:06:15 +02:00
db.close()
except MySQLdb.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)