Mercurial > hg > smartopen
diff smartopen/handlers.py @ 11:ba9058605c5a
add a wiktionary handler
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 22 Dec 2010 17:22:04 -0800 |
parents | a963acb1713d |
children | bb995bdf82e2 |
line wrap: on
line diff
--- a/smartopen/handlers.py Tue Sep 21 12:01:34 2010 -0700 +++ b/smartopen/handlers.py Wed Dec 22 17:22:04 2010 -0800 @@ -3,7 +3,7 @@ import urllib import urllib2 -class Location: +class Location(object): """ generic class for locations """ @@ -20,6 +20,13 @@ def test(self, query): return True + def exists(self, URL): + """does a URL exist?""" + try: + urllib.urlopen(URL) + return True + except IOError: + return False class URL(Location): """a straight URL""" @@ -31,15 +38,9 @@ def test(self, query): """try to open the url""" - if ' ' in query or '\n' in query: return False - - try: - site = urllib.urlopen(self.process(query)) - except IOError: - return False - return True + return self.exists(self.process(query)) class GoogleMaps(Location): """try to google-maps the address""" @@ -100,6 +101,17 @@ return False return True +class Wiktionary(Location): + def __init__(self): + baseurl = 'http://en.wiktionary.org/wiki/' + Location.__init__(self, baseurl) + def test(self, query): + for c in (' ', '\n', '/'): + if c in query: + return False + if self.exists(self.url(query)): + return True + class Bugzilla(Location): def __init__(self): baseurl = 'https://bugzilla.mozilla.org/show_bug.cgi?id='