forked from eskerda/pybikes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiller.py
295 lines (249 loc) · 10.7 KB
/
filler.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2012, eskerda <[email protected]>
# Distributed under the AGPL license, see LICENSE.txt
""" This is a really ugly and nasty script to ease filling up instance files
without cities, latitudes and longitudes. Does more than it needs to """
import os
import sys, traceback
import time
import json
import argparse
from urlparse import urlparse
from collections import namedtuple
import traceback
from googlegeocoder import GoogleGeocoder
from slugify import slugify
import pybikes
geocoder = GoogleGeocoder()
CityRecord = namedtuple('CityRecord', 'city, country, lat, lng')
description = 'Given a PyBikes instance file, fills undeclared values'
parser = argparse.ArgumentParser(description = description)
parser.add_argument('input', metavar = "input",
type = argparse.FileType('r'), default = sys.stdin,
help="Input file")
parser.add_argument('-o', metavar = "output", dest = "output",
default = sys.stdout,
help="Output file")
parser.add_argument('-v', action="store_true", dest = 'verbose',
default = False, help="Verbose output for debugging (no progress)")
parser.add_argument('--proxy', metavar = "host:proxy", dest = 'proxy',
default = None, help="Use host:port as a proxy for site calls")
parser.add_argument('--httpsproxy', metavar = "host:proxy", dest = 'httpsproxy',
default = None, help="Use host:port as an HTTPS proxy for site calls")
parser.add_argument('--slugify', action="store_true", dest = 'slugify',
default = False, help="Correct slugs, using the name as input")
parser.add_argument('--geocode', action="store_true", dest = 'geocode',
default = False, help="Correct geodata using Google GeoCoder")
parser.add_argument('--correct_name', action="store_true", dest = "geoname",
default = False, help="Correct just the name using geodata")
parser.add_argument('-f', action="store_true", dest = 'overwrite',
default = False, help="Overwrite already set variables")
parser.add_argument('-i', action="store_true", dest = 'interactive',
default = False, help="Interactive prompt to select between results")
parser.add_argument('-c', action="store_true", dest = 'continuous',
default = False, help="Continuous write output file")
parser.add_argument('-s', action="store_true", dest = 'skip',
default = False, help="Skip complete instances")
args = parser.parse_args()
scraper = pybikes.utils.PyBikesScraper()
proxies = {}
prompts = {
'slug': '\n--------------\n' + \
'| Old: {old_tag}\n' + \
'|---------------\n' + \
'| New: {new_tag}\n' + \
'----------------\n' + \
'Overwrite? y/n/set: '
}
language = 'en'
metas = ['city', 'country']
data = {}
def clearline(length):
clearline = "\r" + "".join([" " for i in range(length)])
sys.stderr.flush()
sys.stderr.write(clearline)
sys.stderr.flush()
def print_status(i, total, status):
progress = "".join(["#" for step in range(i)]) + \
"".join([" " for step in range(total-i)])
status_pattern = "\r{0}/{1}: [{2}] {3}"
output = status_pattern.format(i, total, progress, status)
sys.stderr.flush()
sys.stderr.write(unicode(output))
sys.stderr.flush()
if (i == total):
sys.stderr.write('\n')
return len(output)
def geocode(instance, systemCls, language, address = None):
if address is not None:
query = address
else:
if args.verbose:
sys.stderr.write("--- Geocoding %s ---- \n" % instance['tag'])
bikesys = systemCls(** instance)
latitude, longitude = [0.0, 0.0]
if 'latitude' in instance['meta'] and 'longitude' in instance['meta']:
latitude = instance['meta']['latitude']
longitude = instance['meta']['longitude']
else:
if args.verbose:
sys.stderr.write("Updating system to get an initial lat/lng\n")
bikesys.update(scraper)
target = int(len(bikesys.stations) / 2)
latitude = bikesys.stations[target].latitude
longitude = bikesys.stations[target].longitude
if args.verbose:
sys.stderr.write(" >>> %s, %s <<< \n" % (str(latitude), str(longitude)))
sys.stderr.write("--- Javascript code for debugging output ---\n")
sys.stderr.write("var geocoder = new google.maps.Geocoder()\n")
sys.stderr.write("latlng = new google.maps.LatLng(%s,%s)\n" % (str(latitude), str(longitude)))
sys.stderr.write("geocoder.geocode({latLng:latlng}, function(res){console.log(res)})\n")
query = (latitude, longitude)
try:
info = geocoder.get(query, language = language)
except Exception as e:
print e
address = raw_input('Type an address: ')
return geocode(instance, systemCls, language, address)
if args.interactive:
for index, address in enumerate(info):
sys.stderr.write("%d: %s\n" % (index, address.formatted_address))
sys.stderr.write("%d: Change language\n" % len(info))
sys.stderr.write("%d: Manual address lookup\n" % int(len(info)+1))
sys.stderr.write('\n')
try:
res = int(raw_input('Select option (number): '))
if res == len(info):
language = raw_input('New language? ')
return geocode(instance, systemCls, language)
elif res == len(info)+1:
address = raw_input('Type an address: ')
return geocode(instance, systemCls, language, address)
elif res < len(info):
address = info[res]
metainfo = instance['meta']
lat = address.geometry.location.lat
lng = address.geometry.location.lng
for index, el in enumerate(address.address_components):
sys.stderr.write("%d: %s\n" % (index, el.short_name))
sys.stderr.write('Latitude: %s\n' % str(lat))
sys.stderr.write('Longitude: %s\n' % str(lng))
sys.stderr.write('\n')
for meta in metas:
res = raw_input('Select the %s: ' % meta)
if ',' in res:
res = res.split(',')
else:
res = [res]
metainfo[meta] = ''
for i, r in enumerate(res):
r = int(r)
metainfo[meta] += address.address_components[r].short_name
if (i < len(res)-1):
metainfo[meta] += ', '
if args.geoname:
lat = latitude
lng = longitude
metainfo['latitude'] = lat
metainfo['longitude'] = lng
instance['meta'] = metainfo
return True
except Exception as e:
print e
return geocode(instance, systemCls, language)
if args.verbose:
sys.stderr.write("\n")
def is_complete(instance):
fields = ['city','country','latitude','longitude','name']
complete = True
for field in fields:
complete = field in instance['meta']
if not complete:
return False
return complete
def write_output(data, way):
way = open(way, 'w')
corrected_data = json.dumps(data, sort_keys = False, indent = 4,
separators = (',',':') )
way.write(corrected_data)
way.write('\n')
way.close()
def handle_System(schema, cls, instances):
systemCls = pybikes.get_system_cls(schema, cls)
lastlen = 0
if args.interactive and args.geocode:
language = raw_input('Desired geocoding language? ')
for i, instance in enumerate(instances):
if not args.verbose:
clearline(lastlen)
lastlen = print_status(i+1, len(instances), \
"Testing %s" % repr(instance['meta']['name']))
if 'name' not in instance['meta'] or instance['meta']['name'] == "":
raise Exception("name not set in instance %s" % str(instance))
if args.skip and is_complete(instance):
if args.verbose:
sys.stderr.write("%s Looks complete, passing by\n" %
repr(instance['meta']['name'])
)
continue
if args.slugify:
tag = slugify(instance['meta']['name'])
r = None
if args.interactive:
r = raw_input(prompts['slug'].format(old_tag = instance['tag'],
new_tag = tag))
if r == 'set':
tag = raw_input("Set new tag: ")
elif r == 'n':
continue
if r != 'n' or args.overwrite or 'tag' not in instance or 'tag' == '':
instance['tag'] = tag
if args.geocode:
geocode(instance, systemCls, language)
time.sleep(1)
def main():
global data
if args.proxy is not None:
proxies['http'] = args.proxy
scraper.enableProxy()
if args.httpsproxy is not None:
proxies['https'] = args.httpsproxy
scraper.enableProxy()
scraper.setProxies(proxies)
if not args.slugify and not args.geocode:
sys.stderr.write("Nothing to do, stopping\n")
exit(0)
data = json.loads(args.input.read())
args.input.close()
if isinstance(data['class'], unicode):
#UniSystem
instances = data['instances']
system = data['system']
sys.stderr.write('Found %d instances for %s\n' % (len(instances), system))
handle_System(system, data['class'], instances)
elif isinstance(data['class'], dict):
#MuliSystem
for cls in data['class']:
instances = data['class'][cls]['instances']
system = data['system']
sys.stderr.write('Found %d instances for %s\n' % (len(instances), system))
handle_System(system, cls, instances)
else:
raise Exception('Malformed data file')
write_output(data, args.output)
if __name__ == "__main__":
try:
main()
except Exception as e:
traceback.print_exc()
if args.continuous:
if args.verbose:
sys.stderr.write("Writing file bc exception\n")
traceback.print_exc(file=sys.stderr)
write_output(data, args.output)
except KeyboardInterrupt as e:
print "KEYBOARD INTERRUPT"
if args.continuous:
if args.verbose:
sys.stderr.write("Writing file bc exception\n")
write_output(data, args.output)