comparison 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
comparison
equal deleted inserted replaced
10:a963acb1713d 11:ba9058605c5a
1 import address 1 import address
2 import string 2 import string
3 import urllib 3 import urllib
4 import urllib2 4 import urllib2
5 5
6 class Location: 6 class Location(object):
7 """ 7 """
8 generic class for locations 8 generic class for locations
9 """ 9 """
10 10
11 def __init__(self, baseurl=""): 11 def __init__(self, baseurl=""):
18 return query 18 return query
19 19
20 def test(self, query): 20 def test(self, query):
21 return True 21 return True
22 22
23 def exists(self, URL):
24 """does a URL exist?"""
25 try:
26 urllib.urlopen(URL)
27 return True
28 except IOError:
29 return False
23 30
24 class URL(Location): 31 class URL(Location):
25 """a straight URL""" 32 """a straight URL"""
26 33
27 def process(self, query): 34 def process(self, query):
29 return query 36 return query
30 return 'http://' + query 37 return 'http://' + query
31 38
32 def test(self, query): 39 def test(self, query):
33 """try to open the url""" 40 """try to open the url"""
34
35 if ' ' in query or '\n' in query: 41 if ' ' in query or '\n' in query:
36 return False 42 return False
37 43 return self.exists(self.process(query))
38 try:
39 site = urllib.urlopen(self.process(query))
40 except IOError:
41 return False
42 return True
43 44
44 class GoogleMaps(Location): 45 class GoogleMaps(Location):
45 """try to google-maps the address""" 46 """try to google-maps the address"""
46 47
47 def __init__(self): 48 def __init__(self):
98 99
99 if 'Wikipedia does not have an article with this exact name' in f: 100 if 'Wikipedia does not have an article with this exact name' in f:
100 return False 101 return False
101 return True 102 return True
102 103
104 class Wiktionary(Location):
105 def __init__(self):
106 baseurl = 'http://en.wiktionary.org/wiki/'
107 Location.__init__(self, baseurl)
108 def test(self, query):
109 for c in (' ', '\n', '/'):
110 if c in query:
111 return False
112 if self.exists(self.url(query)):
113 return True
114
103 class Bugzilla(Location): 115 class Bugzilla(Location):
104 def __init__(self): 116 def __init__(self):
105 baseurl = 'https://bugzilla.mozilla.org/show_bug.cgi?id=' 117 baseurl = 'https://bugzilla.mozilla.org/show_bug.cgi?id='
106 Location.__init__(self, baseurl) 118 Location.__init__(self, baseurl)
107 119