From 46ef9c8f47ebe265ff1ab0624ba80b8c073bdbbf Mon Sep 17 00:00:00 2001 From: toyg Date: Tue, 27 May 2014 14:25:56 +0100 Subject: [PATCH] extracted template, implemented GeoLocationService with OpenStreetMap --- Twistmapper.py | 12 ++-- map.html | 4 +- twistscraper.py | 159 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 129 insertions(+), 46 deletions(-) diff --git a/Twistmapper.py b/Twistmapper.py index bc04086..008f6be 100644 --- a/Twistmapper.py +++ b/Twistmapper.py @@ -11,12 +11,12 @@ from twistscraper import TwisterScraper TEMPLATE = None with open("map.html", "rb") as mapTemplate: - TEMPLATE = Template(mapTemplate.read()) + TEMPLATE = Template(mapTemplate.read().decode('utf-8')) def generate_map(userdb): ts = TwisterScraper(userdb) - loc_users = [u for u in ts.db.users.values() if u.location != ''] + loc_users = [u for u in ts.db.users.values() if hasattr(u, 'location') and u.location != ''] noLoc_user_num = len(ts.db.users) - len(loc_users) loc_users_fake_num = 0 locDb = {} @@ -32,7 +32,7 @@ def generate_map(userdb): locDb[u.location]['users'] = [u.username] else: loc_users_fake_num += 1 - # second pass to aggregate misspellings + # second pass to aggregate misspellings done = [] newLocDb = {} for loc, locDict in locDb.items(): @@ -48,7 +48,7 @@ def generate_map(userdb): # find the most popular name locMax = max(sameCoord, key=lambda x: len(x[1])) location = locMax[0] - coordHash = '/'.join([str(locDict['coordinates']['lat']), str(locDict['coordinates']['lng'])]) + coordHash = '/'.join([str(locDict['coordinates'][0]), str(locDict['coordinates'][1])]) # if we haven't seen this set of coordinates yet... if coordHash not in done: @@ -70,8 +70,8 @@ def generate_map(userdb): for k in newLocDb.keys(): locStrings.append("['

{name} - {numusers}

{users}', {lat}, {lng}]".format( name=k.replace("'", "'"), - lat=newLocDb[k]['coordinates']['lat'], - lng=newLocDb[k]['coordinates']['lng'], + lat=newLocDb[k]['coordinates'][0], + lng=newLocDb[k]['coordinates'][1], users=',
'.join(newLocDb[k]['users']), numusers=len(newLocDb[k]['users']))) locStrings.sort() diff --git a/map.html b/map.html index f711ecc..390e61b 100644 --- a/map.html +++ b/map.html @@ -30,7 +30,9 @@
  • Users with unrealistic location: $users_fake_loc
  • Users without location: $users_no_loc
  • -
    For any feedback, ping @toyg on Twister.
    +
    For any feedback, ping @toyg on Twister.
    Location coordinates provided by OpenStreetMap Nominatim. +