Skip to content

Made it Python3 ready and changed to new Google Geo server #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions bin/noaa
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Database (NDFD).
"""

import argparse
import ConfigParser
import configparser
import os
import sys

Expand Down Expand Up @@ -101,23 +101,23 @@ def format_conditions(conditions, padding=30, color=True):
def config_get_boolean(config, section, option, default=None):
try:
value = config.getboolean(section, option)
except ConfigParser.NoSectionError:
except configparser.NoSectionError:
value = default
except ConfigParser.NoOptionError:
except configparser.NoOptionError:
value = default

return value


def make_parser():
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.read(os.path.expanduser("~/.noaarc"))

try:
default_location = config.get('default', 'location').split()
except ConfigParser.NoSectionError:
except configparser.NoSectionError:
default_location = None
except ConfigParser.NoOptionError:
except configparser.NoOptionError:
default_location = None

default_metric = config_get_boolean(
Expand Down Expand Up @@ -220,14 +220,14 @@ def main():
pretty_location, fcast = forecast_func(args)

if args.heading:
print "Forecast for %s" % pretty_location
print("Forecast for %s" % pretty_location)

for datapoint in fcast:
print datapoint.date.strftime('%a'),
print format_conditions(datapoint.conditions, color=args.color),
print format_temp(datapoint.min_temp, color=args.color),
print format_temp(datapoint.max_temp, color=args.color),
print simple_temp_graph(datapoint.max_temp, color=args.color)
print(datapoint.date.strftime('%a'), end=' ')
print(format_conditions(datapoint.conditions, color=args.color), end=' ')
print(format_temp(datapoint.min_temp, color=args.color), end=' ')
print(format_temp(datapoint.max_temp, color=args.color), end=' ')
print(simple_temp_graph(datapoint.max_temp, color=args.color))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion noaa/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _parse_time_layouts(tree):
dt = parse_dt(tl_child.text)
end_times.append(dt)

time_layouts[key] = zip(start_times, end_times)
time_layouts[key] = list(zip(start_times, end_times))

return time_layouts

Expand Down
16 changes: 7 additions & 9 deletions noaa/geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ def geocode_location(location, api_key=None):

For high-volume traffic, you will need to specify an API-key.
"""
GEOCODE_URL = "http://maps.google.com/maps/geo"
params = [('q', location),
('sensor', 'false'),
('output', 'json')]
GEOCODE_URL = "http://maps.googleapis.com/maps/api/geocode/json"
params = [('address', location)]

if api_key:
params += [('key', api_key)]

resp = utils.open_url(GEOCODE_URL, params)
data = json.loads(resp.read())
data = json.loads(resp.read().decode())

if data['Status']['code'] != 200:
if data['status'] != 'OK':
raise exceptions.GeocodeException('Unable to geocode this location')

best_match = data['Placemark'][0]
address = best_match['address']
lon, lat, _ = best_match['Point']['coordinates']
address = data['results'][0]['formatted_address']
lon = data['results'][0]['geometry']['location']['lng']
lat = data['results'][0]['geometry']['location']['lat']

location = models.Location(lat, lon, address)
return location
4 changes: 2 additions & 2 deletions noaa/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def compiled_observation_for_lat_lon(lat, lon, stations, radius=10.0,
if not compiled_observation:
compiled_observation = copy.copy(station_observation.observation)

if not noaa.utils.any_none(compiled_observation.__dict__.values()):
if not noaa.utils.any_none(list(compiled_observation.__dict__.values())):
# If all the values are filled in, break out of loop
break

for attr, compiled_value in compiled_observation.__dict__.items():
for attr, compiled_value in list(compiled_observation.__dict__.items()):
if compiled_value is None:
station_value = getattr(station_observation.observation, attr)
setattr(compiled_observation, attr, station_value)
Expand Down
22 changes: 11 additions & 11 deletions noaa/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextlib
import math
import sys
import urllib
import urllib.request, urllib.parse, urllib.error
from xml.etree import ElementTree as ET

import dateutil.parser
Expand All @@ -25,12 +25,12 @@ def colorize(text, color):


def any_none(L):
return any(map(lambda x: x is None, L))
return any([x is None for x in L])


def all_numbers(L):
try:
map(float, L)
list(map(float, L))
return True
except ValueError:
return False
Expand All @@ -39,21 +39,21 @@ def all_numbers(L):
def print_tree(tree, indent=4):
"""Print an ElementTree for debugging purposes."""
def print_elem(elem, level):
print " " * (indent * level),
print 'tag="%s"' % elem.tag,
print 'text="%s"' % elem.text.strip() if elem.text is not None else "",
print 'attrib=%s' % elem.attrib
print(" " * (indent * level), end=' ')
print('tag="%s"' % elem.tag, end=' ')
print('text="%s"' % elem.text.strip() if elem.text is not None else "", end=' ')
print('attrib=%s' % elem.attrib)
for child in elem.getchildren():
print_elem(child, level + 1)
print_elem(tree.getroot(), 0)


def open_url(url, params=None):
if params:
query_string = urllib.urlencode(params)
query_string = urllib.parse.urlencode(params)
url = "?".join([url, query_string])

resp = urllib.urlopen(url)
resp = urllib.request.urlopen(url)
return resp


Expand All @@ -72,7 +72,7 @@ def die_on(*exception_classes, **kwargs):
try:
yield
except exception_classes as e:
print >> sys.stderr, msg_func(e)
print(msg_func(e), file=sys.stderr)
sys.exit(exit_code)


Expand All @@ -96,7 +96,7 @@ def hsin(theta):
return math.sin(float(theta) / 2) ** 2

if angle_units == "deg":
lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
lat1, lon1, lat2, lon2 = list(map(radians, [lat1, lon1, lat2, lon2]))
elif angle_units == "rad":
pass
else:
Expand Down