From 42fa795fdbba60c568c92c6f25dc216a09d6d0df Mon Sep 17 00:00:00 2001 From: mrchucho Date: Fri, 7 Nov 2008 14:35:07 -0600 Subject: [PATCH] Split helper methods out into their own modules (and source files). --- main.py | 3 ++- models/alert.py | 34 +++------------------------------- models/place.py | 34 ++++------------------------------ utils/api/__init__.py | 0 utils/api/geo.py | 26 ++++++++++++++++++++++++++ utils/api/weather.py | 9 +++++++++ utils/encoder.py | 18 ++++++++++++++++++ 7 files changed, 62 insertions(+), 62 deletions(-) create mode 100644 utils/api/__init__.py create mode 100644 utils/api/geo.py create mode 100644 utils/api/weather.py create mode 100644 utils/encoder.py diff --git a/main.py b/main.py index 3481126..05f550f 100755 --- a/main.py +++ b/main.py @@ -15,8 +15,9 @@ # pytz imports itself, so this is necessary sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'utils', 'external'))) -from models.alert import Alert, AlertEncoder +from models.alert import Alert from models.place import Place +from utils.encoder import AlertEncoder class AlertHandler(webapp.RequestHandler): diff --git a/models/alert.py b/models/alert.py index 2acbd05..80017c5 100644 --- a/models/alert.py +++ b/models/alert.py @@ -1,13 +1,3 @@ -from google.appengine.api import urlfetch - -def cap_for_state(state): - result = urlfetch.fetch("http://www.weather.gov/alerts/%s.cap" % state.lower()) - if result.status_code != 200: - raise models.CAPError(result.status_code) - else: - return result.content - -# ------------------------------------------------------------------------------ import re import logging @@ -18,6 +8,7 @@ def cap_for_state(state): import utils.external.pytz from models import place import models +from utils.api import weather class Alert: NAME_RE = re.compile("[.,:]") @@ -76,7 +67,7 @@ def alerts_for(cls, place): raise models.InvalidPlace(place) logging.debug("Finding alerts for %s" % place) alerts = [] - doc = BeautifulStoneSoup(cap_for_state(place.state_abbreviation)) + doc = BeautifulStoneSoup(weather.cap_for_state(place.state_abbreviation)) for info in doc.findAll("cap:info"): area = info.find("cap:area") if area: @@ -104,26 +95,6 @@ def n(name): return re.match(r"%s\s*(County)?\s*\(%s\)" % (n(place.county), n(place.state)), area, re.IGNORECASE) -# ------------------------------------------------------------------------------ -from django.utils import simplejson as json - -class AlertEncoder(json.JSONEncoder): - - def default(self, o): - return { - 'place': { - 'name': o.place.named, - 'county': o.place.county, - 'state': o.place.state, - 'timezone': o.place.timezone - }, - 'event': o.event, - 'effective': o.effective, - 'expires': o.expires, - 'severity': o.severity - } - - if __name__ == '__main__': import sys p = place.Place(" ".join(sys.argv[1:])) @@ -131,3 +102,4 @@ def default(self, o): for alert in Alert.alerts_for(p): print("\tAlert %s Eff %s Exp %s Sev %d" % (alert.event,alert.effective,alert.expires,alert.severity)) + diff --git a/models/place.py b/models/place.py index 461114d..c9f7fed 100644 --- a/models/place.py +++ b/models/place.py @@ -1,35 +1,9 @@ -import urllib2 - -from google.appengine.api import urlfetch - -APP_ID = "OSkXnanV34GmWWqcfpA2CsbB18xDtJF6_mfp7Su.HpqXelHWX.ipRGVAe.dw1j8-" - -def fetch(url): - result = urlfetch.fetch(url, method=urlfetch.GET, headers={"Accept": "application/xml"}) - if result.status_code != 200: - raise models.GeoError(result.status_code) - else: - return result.content - -def place_for_search_term(search_term): - search_term = urllib2.quote("'%s'" % search_term) - return fetch("http://where.yahooapis.com/v1/places.q(%s)?appid=%s" % (search_term, APP_ID)) - -def parent_of_place(place): - return fetch("http://where.yahooapis.com/v1/place/%s/parent?appid=%s" % (place, APP_ID)) - -def children_of_place(place, **kwargs): - kwlist = {'type': -1} - kwlist.update(kwargs) - type = kwlist['type'] - return fetch("http://where.yahooapis.com/v1/place/%s/belongtos.type(%s)?appid=%s" % (place, type, APP_ID)) - -# ------------------------------------------------------------------------------ import re from utils.external.BeautifulSoup import BeautifulStoneSoup import models +from utils.api import geo class Place: TZ_CODE = "31" @@ -52,7 +26,7 @@ def state_abbreviation(self): return Place.STATE_ABBREV_RE.sub('',self.abbrev or '') def _find_place(self): - doc = BeautifulStoneSoup(place_for_search_term(self.named)) + doc = BeautifulStoneSoup(geo.place_for_search_term(self.named)) for place in doc.places: self.place = place for types in place.findAll(type=["County","State"]): @@ -77,7 +51,7 @@ def _find_timezone(self): def _find_timezone_for_woeid(self,woeid): try: - doc = BeautifulStoneSoup(children_of_place(woeid,type=Place.TZ_CODE)) + doc = BeautifulStoneSoup(geo.children_of_place(woeid,type=Place.TZ_CODE)) for place in doc.places: if len(place.findAll("placetypename",code=Place.TZ_CODE)) > 0: return place.findAll("name")[0].string @@ -87,7 +61,7 @@ def _find_timezone_for_woeid(self,woeid): def _find_parent_woeid(self,woeid): try: - doc = BeautifulStoneSoup(parent_of_place(woeid)) + doc = BeautifulStoneSoup(geo.parent_of_place(woeid)) for woeid in doc.woeid: return woeid return None diff --git a/utils/api/__init__.py b/utils/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/api/geo.py b/utils/api/geo.py new file mode 100644 index 0000000..5e12ccc --- /dev/null +++ b/utils/api/geo.py @@ -0,0 +1,26 @@ +import urllib2 + +from google.appengine.api import urlfetch + +APP_ID = "OSkXnanV34GmWWqcfpA2CsbB18xDtJF6_mfp7Su.HpqXelHWX.ipRGVAe.dw1j8-" + +def fetch(url): + result = urlfetch.fetch(url, method=urlfetch.GET, headers={"Accept": "application/xml"}) + if result.status_code != 200: + raise models.GeoError(result.status_code) + else: + return result.content + +def place_for_search_term(search_term): + search_term = urllib2.quote("'%s'" % search_term) + return fetch("http://where.yahooapis.com/v1/places.q(%s)?appid=%s" % (search_term, APP_ID)) + +def parent_of_place(place): + return fetch("http://where.yahooapis.com/v1/place/%s/parent?appid=%s" % (place, APP_ID)) + +def children_of_place(place, **kwargs): + kwlist = {'type': -1} + kwlist.update(kwargs) + type = kwlist['type'] + return fetch("http://where.yahooapis.com/v1/place/%s/belongtos.type(%s)?appid=%s" % (place, type, APP_ID)) + diff --git a/utils/api/weather.py b/utils/api/weather.py new file mode 100644 index 0000000..f79d69c --- /dev/null +++ b/utils/api/weather.py @@ -0,0 +1,9 @@ +from google.appengine.api import urlfetch + +def cap_for_state(state): + result = urlfetch.fetch("http://www.weather.gov/alerts/%s.cap" % state.lower()) + if result.status_code != 200: + raise models.CAPError(result.status_code) + else: + return result.content + diff --git a/utils/encoder.py b/utils/encoder.py new file mode 100644 index 0000000..f38810c --- /dev/null +++ b/utils/encoder.py @@ -0,0 +1,18 @@ +from django.utils import simplejson as json + +class AlertEncoder(json.JSONEncoder): + + def default(self, o): + return { + 'place': { + 'name': o.place.named, + 'county': o.place.county, + 'state': o.place.state, + 'timezone': o.place.timezone + }, + 'event': o.event, + 'effective': o.effective, + 'expires': o.expires, + 'severity': o.severity + } +