Add Http.zipinfo

This commit is contained in:
James Vega 2004-04-28 21:13:33 +00:00
parent 7f87c1e40a
commit 20ac0993bc
3 changed files with 104 additions and 34 deletions

View File

@ -1,3 +1,6 @@
* Added Http.zipinfo, which returns a veritable plethora of
information about the supplied zip code.
* Added a configuration variable for every plugin, "public", that * Added a configuration variable for every plugin, "public", that
determines whether the plugin is considered public (i.e., whether determines whether the plugin is considered public (i.e., whether
it will show up in the list command when the list command isn't it will show up in the list command when the list command isn't

View File

@ -358,6 +358,64 @@ class Http(callbacks.Privmsg):
else: else:
irc.error('No matching file extenstions were found.') irc.error('No matching file extenstions were found.')
_zipinfore = re.compile(r'Latitude<BR>\(([^)]+)\)</th><th>Longitude<BR>'
r'\(([^)]+)\).*?<tr>(.*?)</tr>', re.I)
_zipstatre = re.compile(r'(Only about \d+,\d{3} of.*?in use.)')
def zipinfo(self, irc, msg, args):
"""<zip code>
Returns a plethora of information for the given <zip code>.
"""
zipcode = privmsgs.getArgs(args)
try:
int(zipcode)
except ValueError:
irc.error('Zip code must be a 5-digit integer.')
return
if len(zipcode) != 5:
irc.error('Zip code must be a 5-digit integer.')
return
url = 'http://zipinfo.com/cgi-local/zipsrch.exe?cnty=cnty&ac=ac&'\
'tz=tz&ll=ll&zip=%s&Go=Go' % zipcode
try:
text = webutils.getUrl(url)
except webutils.WebError, e:
irc.error(str(e))
if 'daily usage limit' in text:
irc.error('I have exceeded the site\'s daily usage limit.')
return
m = self._zipstatre.search(text)
if m:
irc.reply('%s %s is not one of them.' % (m.group(1), zipcode))
return
n = self._zipinfore.search(text)
if not n:
irc.error('Unable to retrieve information for that zip code.')
return
(latdir, longdir, rawinfo) = n.groups()
# Info consists of the following (whitespace separated):
# City, State Abbrev., Zip Code, County, FIPS Code, Area Code, Time
# Zone, Daylight Time(?), Latitude, Longitude
info = utils.htmlToText(rawinfo)
info = info.split()
resp = []
# County was more than one word. Hopfully it was only two words
if (len(info[-9]) > 2):
resp.append('City: %s' % ' '.join(info[:-10]))
resp.append('State: %s' % info[-10])
resp.append('County: %s' % ' '.join(info [-6:-8]))
else:
resp.append('City: %s' % ' '.join(info[:-9]))
resp.append('State: %s' % info[-9])
resp.append('County: %s' % info[-7])
resp.append('Area Code: %s' % info[-5])
resp.append('Time Zone: %s' % info[-4])
resp.append('Daylight Savings: %s' % info[-3])
resp.append('Latitude: %s (%s)' % (info[-2], latdir))
resp.append('Longitude: %s (%s)' % (info[-1], longdir))
irc.reply('; '.join(resp))
Class = Http Class = Http
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -109,5 +109,14 @@ if network:
def testPgpkey(self): def testPgpkey(self):
self.assertNotError('pgpkey jeremiah fincher') self.assertNotError('pgpkey jeremiah fincher')
def testZipinfo(self):
self.assertRegexp('zipinfo 02135',
r'City: Brighton; State: MA; County: Suffolk')
self.assertError('zipinfo 123456')
self.assertError('zipinfo O1233')
self.assertRegexp('zipinfo 00000',
r'Only about \d+,\d+ of the \d+,\d+ possible')
self.assertRegexp('zipinfo 78014', 'County: La Salle')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: