-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
243 lines (188 loc) · 8.07 KB
/
Copy pathserver.py
File metadata and controls
243 lines (188 loc) · 8.07 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import flask
import unicodedata
import gzip
import urllib.request
from bs4 import BeautifulSoup
from io import StringIO
import psycopg2
import geojson
import configparser
app = flask.Flask(__name__)
def getdata(code, limit):
data_url = "http://www.kvb-koeln.de/generated/?aktion=show&code=" + str(code) + "&title=graph"
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 '
'Safari/537.36 '
}
req = urllib.request.Request(data_url, None, headers)
page = urllib.request.urlopen(req)
gzipped = page.info().get('Content-Encoding') == 'gzip'
if gzipped:
buf = StringIO(page.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()
else:
data = page
"""
headers = requests.utils.default_headers()
headers.update(
{
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 '
'Safari/537.36',
}
)
request = requests.get(data_url, headers=headers)
c = request.text
print(request.headers)
with open("htmldata.html", 'w') as f:
f.write(request.text)
with open("htmldata.html", 'rb') as f:
c = f.read().decode("UTF-8")
"""
soup = BeautifulSoup(data, "html.parser")
table = soup.findAll("table")[1]
# print(table)
data = []
for row in table.findAll("tr"):
# print(row)
item = {}
cols = row.findAll("td")
if len(cols) >= 3:
item['id'] = str(unicodedata.normalize('NFKD', cols[0].getText())).strip()
item['haltestelle'] = str(unicodedata.normalize('NFKD', cols[1].getText())).replace(" ", "")
item['abfahrt'] = str(unicodedata.normalize('NFKD', cols[2].getText())).strip().replace(" Min", "")
data.append(item)
if limit is not None:
return data[0:int(limit)]
else:
return data
def shutdown_server():
func = flask.request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.before_request
def option_autoreply():
if flask.request.method == 'OPTIONS':
resp = app.make_default_options_response()
headers = None
if 'ACCESS_CONTROL_REQUEST_HEADERS' in flask.request.headers:
headers = flask.request.headers['ACCESS_CONTROL_REQUEST_HEADERS']
h = resp.headers
# Allow the origin which made the XHR
h['Access-Control-Allow-Origin'] = flask.request.headers['Origin']
# Allow the actual method
h['Access-Control-Allow-Methods'] = flask.request.headers['Access-Control-Request-Method']
# Allow for 10 seconds
h['Access-Control-Max-Age'] = "10"
# We also keep current headers
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
@app.after_request
def set_allow_origin(resp):
h = resp.headers
# Allow crossdomain for other HTTP Verbs
if flask.request.method != 'OPTIONS' and 'Origin' in flask.request.headers:
h['Access-Control-Allow-Origin'] = flask.request.headers['Origin']
return resp
@app.route("/")
def home():
req = flask.request
return flask.render_template("index.html", path=flask.request.path, base=flask.request.base_url,
title="PublicDepartCologne")
@app.route('/database', methods=['GET'])
def database():
lat = flask.request.args.get('lat', default=None)
long = flask.request.args.get('long', default=None)
# radius = flask.request.args.get('radius', default=None)
if lat is None or long is None:
return flask.render_template("error.html", error_title="No Lat or long",
error="Bitte gib die Parameter ?lat und &long an")
else:
# print("Test")
try:
config = configparser.ConfigParser()
config.read("config.ini")
host = config.get("postgres", "host")
db = config.get("postgres", "db")
user = config.get("postgres", "user")
password = config.get("postgres", "password")
connect_str = "dbname=" + db + " user=" + user + " host=" + host + " " + \
"password=" + password + ""
# use our connection values to establish a connection
conn = psycopg2.connect(connect_str)
# create a psycopg2 cursor that can execute queries
cursor = conn.cursor()
# ST_Distance(haltestellen.geom, ST_MakePoint(""" + lat + """, """ + long + """)::geography) as distance
cursor.execute((
"""SELECT *, ST_Distance(haltestellen.geom, ST_MakePoint(<long>, <lat>)::geography) as distance, st_asgeojson(geom) FROM public.haltestellen ORDER BY distance LIMIT 5; """).replace(
"<lat>", lat).replace("<long>", long))
# cursor.execute("""SELECT * FROM public.haltestellen WHERE ST_DWithin(haltestellen.geom, ST_MakePoint(6.9881160312996728, 50.96659064159747)::geography, 10000);""")
# create a new table with a single column called "name"
# cursor.execute("""CREATE TABLE tutorials (name char(40));""")
# run a SELECT statement - no data in there, but we can try it
# cursor.execute("""SELECT * from tutorials""")
rows = cursor.fetchall()
# print(rows)
# print(sum(rows, ()))
features = []
# print(rows)
# print(rows[0][0])
for col in rows:
# print(col)
properties = {}
# print(col)
# print("Sum " + sum(the_tuple, ()))
# print(the_tuple.split(",")[1:len(the_tuple)-1])
"""
print(type(the_tuple))
print(the_tuple)
print(tuple(the_tuple))
"""
distance = col[1]
properties['gid'] = col[0]
properties['name'] = col[1]
properties['knotennummer'] = str(col[2])
properties['typ'] = col[3]
properties['nr_stadtteil'] = str(col[4])
properties['stadtteil'] = col[5]
properties['nr_stadtbez'] = col[6]
properties['stadtbez'] = col[7]
properties['hyperlink'] = col[8]
properties['distance'] = col[11]
geo = col[12]
# properties['geo'] = the_tuple[8]
feature = geojson.Feature(geometry=geojson.loads(geo), properties=properties)
# print(properties)
features.append(feature)
collection = geojson.FeatureCollection(features)
# return flask.jsonify(data)
resp = flask.Response(geojson.dumps(collection), status=200, mimetype='application/json')
return resp
# return flask.jsonify(data)
except psycopg2.DatabaseError as e:
print("Uh oh, can't connect. Invalid dbname, user or password?")
print(e)
return ""
@app.route("/abfahrt/<code>", methods=['GET', 'POST'])
@app.route("/abfahrt/")
def api(code=None):
limit = flask.request.args.get('limit', default=None)
if code is None:
return flask.render_template("error.html", base=flask.request.base_url, path=flask.request.path,
error="Bitte nutze <code>{{ base }}<code>/</code> um die Abfahrtszeiten zu bekommen",
error_title="Error: Kein Code")
else:
req = flask.request
data = getdata(code, limit)
resp = flask.Response(data, status=200, mimetype='application/json')
return flask.jsonify(data)
@app.route("/shutdown", methods=['GET'])
def shutdown():
shutdown_server()
return 'Server shutting down'
if __name__ == "__main__":
# For Public use
# app.run(host='192.168.199.142')
app.run()