comparison smartopen/handlers.py @ 14:a62fbff067f8

start bringing this whole thing up to speed
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 01 May 2013 06:56:12 -0700
parents f11ce7b1a349
children 1281d999618c
comparison
equal deleted inserted replaced
13:f11ce7b1a349 14:a62fbff067f8
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 from subprocess import call
5 6
6 class Location(object): 7 class Location(object):
7 """ 8 """
8 generic class for locations 9 generic class for locations
9 """ 10 """
10 11
11 def __init__(self, baseurl=""): 12 def __init__(self, baseurl=""):
12 self.baseurl = baseurl 13 self.baseurl = baseurl
14 # should/could strip here?
13 15
14 def url(self, query): 16 def url(self, query):
15 return self.baseurl + self.process(query) 17 return self.baseurl + self.process(query)
16 18
17 def process(self, query): 19 def process(self, query):
78 if query[1:].isdigit(): 80 if query[1:].isdigit():
79 return 'ticket/' + str(query[1:]) 81 return 'ticket/' + str(query[1:])
80 82
81 def test(self, query): 83 def test(self, query):
82 return bool(self.process(query)) 84 return bool(self.process(query))
83 85
84 86
85 class Wikipedia(Location): 87 class Wikipedia(Location):
86 """try to open the query in wikipedia""" 88 """try to open the query in wikipedia"""
87 def __init__(self): 89 def __init__(self):
88 wikiurl = 'http://en.wikipedia.org/wiki/' 90 wikiurl = 'http://en.wikipedia.org/wiki/'
89 Location.__init__(self, wikiurl) 91 Location.__init__(self, wikiurl)
90 92
91 def process(self, query): 93 def process(self, query):
92 return urllib.quote_plus('_'.join(query.split())) 94 return urllib.quote_plus('_'.join(query.split()))
93 95
94 def test(self, query): 96 def test(self, query):
95 'test to see if the article exists' 97 'test to see if the article exists'
96 98
97 # need a phony user agent so wikipedia won't know we're a bot 99 # need a phony user agent so wikipedia won't know we're a bot
98 headers = {} 100 headers = {}
116 for c in (' ', '\n', '/'): 118 for c in (' ', '\n', '/'):
117 if c in query: 119 if c in query:
118 return False 120 return False
119 if self.exists(self.url(query)): 121 if self.exists(self.url(query)):
120 return True 122 return True
121 123
122 class Bugzilla(Location): 124 class Bugzilla(Location):
123 def __init__(self): 125 def __init__(self):
124 baseurl = 'https://bugzilla.mozilla.org/show_bug.cgi?id=' 126 baseurl = 'https://bugzilla.mozilla.org/show_bug.cgi?id='
125 Location.__init__(self, baseurl) 127 Location.__init__(self, baseurl)
126 128
127 def test(self, query): 129 def test(self, query):
128 try: 130 try:
129 int(query) 131 int(query)
130 return True 132 return True
131 except: 133 except:
132 return False 134 return False
133 135
134 class Google(Location): 136 class Google(Location):
135 def __init__(self): 137 def __init__(self):
136 googleurl = 'http://www.google.com/search?hl=en&q=' 138 googleurl = 'http://www.google.com/search?hl=en&q='
137 Location.__init__(self, googleurl) 139 Location.__init__(self, googleurl)
138 140
139 def process(self, query): 141 def process(self, query):
140 return urllib.quote_plus(query) 142 return urllib.quote_plus(query)
141 143
142 class FedEx(Location): 144 class FedEx(Location):
143 def __init__(self): 145 def __init__(self):
144 baseurl = 'http://www.fedex.com/Tracking?cntry_code=us&language=english&tracknumber_list=' 146 baseurl = 'http://www.fedex.com/Tracking?cntry_code=us&language=english&tracknumber_list='
145 Location.__init__(self, baseurl) 147 Location.__init__(self, baseurl)
146 148
147 def process(self, query): 149 def process(self, query):
148 if query.count(' ') == 2: 150 if query.count(' ') == 2:
149 query = ''.join(query.split(' ')) 151 query = ''.join(query.split(' '))
150 return query 152 return query
151 153
152 def test(self, query): 154 def test(self, query):
153 query = self.process(query) 155 query = self.process(query)
154 if len(query) != 12: 156 if len(query) != 12:
168 query = set(query) 170 query = set(query)
169 if query.issubset(string.digits + 'abcdef'): 171 if query.issubset(string.digits + 'abcdef'):
170 return True 172 return True
171 return False 173 return False
172 174
175 class UbuntuPackage(Location):
176 def __init__(self):
177 baseurl = 'http://packages.ubuntu.com/'
178 Location.__init__(self, baseurl)
179
180 def test(self, query):
181 if len(self.baseurl.strip().split()) > 1:
182 return False # no spaces in packages
183
184 # use `apt-cache show` for the package name
185 # the output could be captured:, e.g.
186 # ...
187 # Filename: pool/main/h/hello/hello_2.8-2_amd64.deb
188 # Size: 28124
189 # MD5sum: 73ad59a7920e2adcff9b84c00e38418a
190 # SHA1: 70ef927cfa40b8e367f9ca7821e0064a946726c5
191 # SHA256: f8341711f6b803e032be9aff2206dfdb4045a0c6c296b0ea0d310d415f10ff4d
192 # Description-en: The classic greeting, and a good example
193 # The GNU hello program produces a familiar, friendly greeting. It
194 # allows non-programmers to use a classic computer science tool which
195 # would otherwise be unavailable to them.
196 # .
197 # Seriously, though: this is an example of how to do a Debian package.
198 # It is the Debian version of the GNU Project's `hello world' program
199 # (which is itself an example for the GNU Project).
200 # Homepage: http://www.gnu.org/software/hello/
201 # Description-md5: b7df6fe7ffb325083a3a60819a7df548
202 # Bugs: https://bugs.launchpad.net/ubuntu/+filebug
203 # Origin: Ubuntu
204 # Supported: 18m
205
206 # in theory both the home page and the ubuntu page could be interesting
207 # (different handlers?)
208
209 returncode = call(['apt-cache', 'show', self.baseurl])
210 if returncode:
211 return False
212
173 # TODO: 213 # TODO:
214 # - https://mozillians.org/en-US/u/jhammel/ handler
174 # - https://mozillians.org/en-US/u/williamr/ handler 215 # - https://mozillians.org/en-US/u/williamr/ handler
216 # ... but no 404???