From f02bcae3881178ff9e16afa57f0f5b82cf2b21f4 Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Fri, 17 Feb 2017 19:43:28 +0000 Subject: [PATCH] Use 'postalcode' parameter when a zipcode is used to get more accurate results. Added previously removed test. --- modules/test/test_weather.py | 1 + modules/weather.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/test/test_weather.py b/modules/test/test_weather.py index f49b3ab..79dd1e9 100644 --- a/modules/test/test_weather.py +++ b/modules/test/test_weather.py @@ -28,6 +28,7 @@ class TestWeather(unittest.TestCase): ('27959', check_places("Dare County", "North Carolina")), ('48067', check_places("Royal Oak", "Michigan")), ('23606', check_places("Newport News", "Virginia")), + ('23113', check_places("Midlothian", "Virginia")), ('27517', check_places("Chapel Hill", "North Carolina")), ('15213', check_places("Allegheny County", "Pennsylvania")), ('90210', check_places("Los Angeles County", "California")), diff --git a/modules/weather.py b/modules/weather.py index dfdbcfe..89580fd 100644 --- a/modules/weather.py +++ b/modules/weather.py @@ -17,8 +17,14 @@ r_from = re.compile(r'(?i)([+-]\d+):00 from') def location(q): - uri = 'https://nominatim.openstreetmap.org/search/?q={query}&format=json'.\ - format(query=web.quote(q)) + uri = 'https://nominatim.openstreetmap.org/search?%s={query}&format=json' + if q.isdigit(): + uri = uri % 'postalcode' + else: + uri = uri % 'q' + uri = uri . format(query = web.quote(q)) +# uri = 'https://nominatim.openstreetmap.org/search/?q={query}&format=json'.\ +# format(query=web.quote(q)) results = web.get(uri) data = json.loads(results)