Skip to content
Browse files

Remove geocodes of JSON-fetching responsibilities, this is now done b…

…y WMB
  • Loading branch information...
1 parent 6aff6a8 commit 6eb5186bd970bd608a0faec2cd1ea90beb4cedee @qwghlm committed
Showing with 38 additions and 24 deletions.
  1. +27 −14 geotools.py
  2. +11 −10 whensmybus.py
View
41 geotools.py
@@ -7,7 +7,6 @@
import math
import urllib
from pprint import pprint
-from utils import fetch_json
# Geocoders Define the URL and how to parse the resulting JSON object
@@ -23,12 +22,12 @@ def __init__(self):
self.params = {}
return
- def fetch_result(self):
+ def get_query_url(self):
"""
- Fetch a JSON result using URL and params
+ Fetch a URL to fetch geodata
"""
query_url = self.url % urllib.urlencode(self.params)
- return fetch_json(query_url, 'stop_not_found')
+ return query_url
class BingGeocoder(BaseGeocoder):
"""
@@ -43,13 +42,17 @@ def __init__(self, api_key):
'key' : api_key
}
- def geocode(self, placename):
+ def get_geocode_url(self, placename):
"""
- Geocode a placename, return list of matching place(s), each represented by a (latitude, longitude) tuple
+ Get URL to access API, given a search query
"""
self.params['query'] = placename + ', London'
- obj = self.fetch_result()
-
+ return self.get_query_url()
+
+ def parse_geodata(self, obj):
+ """
+ Given a JSON object of geodata for a query, return list of matching place(s), each represented by a (latitude, longitude) tuple
+ """
if obj['resourceSets'][0]['estimatedTotal'] == 0:
return []
@@ -72,13 +75,17 @@ def __init__(self, appid):
'locale' : 'en_GB'
}
- def geocode(self, placename):
+ def get_geocode_url(self, placename):
"""
- Geocode a placename, return list of matching place(s), each represented by a (latitude, longitude) tuple
+ Get URL to access API, given a search query
"""
self.params['q'] = placename + ', London, UK'
- obj = self.fetch_result()
+ return self.get_query_url()
+ def parse_geodata(self, obj):
+ """
+ Given a JSON object of geodata for a query, return list of matching place(s), each represented by a (latitude, longitude) tuple
+ """
if obj['ResultSet']['Error'] or obj['ResultSet']['Found'] == 0:
return []
@@ -94,20 +101,26 @@ class GoogleGeocoder(BaseGeocoder):
Geocoder for Google Maps
"""
def __init__(self):
-
+ """
+ Constructor
+ """
self.url = 'http://maps.googleapis.com/maps/api/geocode/json?%s'
self.params = {
'region' : 'uk',
'sensor' : 'false'
}
- def geocode(self, placename):
+ def get_geocode_url(self, placename):
"""
Get URL to access API, given a search query
"""
self.params['address'] = placename + ', London'
- obj = self.fetch_result()
+ return self.get_query_url()
+ def parse_geodata(self, obj):
+ """
+ Given a JSON object of geodata for a query, return list of matching place(s), each represented by a (latitude, longitude) tuple
+ """
if not obj['results'] or obj['status'] == 'ZERO_RESULTS':
return []
View
21 whensmybus.py
@@ -12,10 +12,6 @@
TODO
- Tube, Train, Tram, DLR & Boat equivalents
- Maybe deprecate use of "From", or a better error message for it (use proper natural language parsing?)
-
-Before next release:
- - Better unit testing for multiple routes in same geolocation
- - Caching URL lookups (split this out as a separate WMBBrowser object?)
"""
# Standard libraries of Python 2.6
import ConfigParser
@@ -35,7 +31,7 @@
# From other modules in this package
from geotools import convertWGS84toOSGrid, YahooGeocoder, heading_to_direction
from exception_handling import WhensMyTransportException
-from utils import fetch_json, load_database, capwords
+from utils import WMBBrowser, load_database, capwords
from fuzzy_matching import get_best_fuzzy_match, get_bus_stop_name_similarity
# Some constants we use
@@ -103,7 +99,10 @@ def __init__(self, instance_name, testing=None, silent=False):
(self.settingsdb, self.settings) = load_database('%s.settings.db' % self.instance_name)
self.settings.execute("create table if not exists %s_settings (setting_name unique, setting_value)" % self.instance_name)
self.settingsdb.commit()
-
+
+ # JSON Browser
+ self.browser = WMBBrowser()
+
# API keys
yahoo_app_id = config.get(self.instance_name, 'yahoo_app_id')
self.geocoder = yahoo_app_id and YahooGeocoder(yahoo_app_id)
@@ -614,7 +613,9 @@ def get_stops_by_stop_name(self, route_number, origin):
for run in (1, 2):
if run not in relevant_stops and self.geocoder:
logging.debug("No match found for run %s, attempting to get geocode placename %s", run, origin)
- points = self.geocoder.geocode(origin)
+ geocode_url = self.geocoder.get_geocode_url(origin)
+ geodata = self.browser.fetch_json(geocode_url)
+ points = self.geocoder.parse_geodata(geodata)
if not points:
logging.debug("Could not find any matching location for %s", origin)
continue
@@ -653,7 +654,7 @@ def get_departure_data(self, relevant_stops, route_number):
stop_name = capwords(stop_name.strip())
tfl_url = TFL_API_URL % stop_number
- bus_data = fetch_json(tfl_url)
+ bus_data = self.browser.fetch_json(tfl_url)
arrivals = bus_data.get('arrivals', [])
# Handle TfL's JSON-encoded error message
@@ -736,12 +737,12 @@ def parse_message(self, message):
raise WhensMyTransportException('nonexistent_line', parsed_line)
line_code = line[0]
- print (line_code, origin, destination)
+ #print (line_code, origin, destination)
if __name__ == "__main__":
WMB = WhensMyBus()
WMB.check_tweets()
- #WMB.check_followers()
+ WMB.check_followers()
#WMT = WhensMyTube(testing=True)
#WMT.parse_message("Norvern Line")

0 comments on commit 6eb5186

Please sign in to comment.
Something went wrong with that request. Please try again.