0
|
1 import address
|
|
2 import urllib
|
|
3 import urllib2
|
|
4
|
|
5 class Location:
|
|
6 """
|
|
7 generic class for locations
|
|
8 """
|
|
9
|
|
10 def __init__(self, baseurl=""):
|
|
11 self.baseurl = baseurl
|
|
12
|
|
13 def url(self, query):
|
|
14 return self.baseurl + self.process(query)
|
|
15
|
|
16 def process(self, query):
|
|
17 return query
|
|
18
|
|
19 def test(self, query):
|
|
20 return True
|
|
21
|
|
22
|
|
23 class URL(Location):
|
|
24 """a straight URL"""
|
|
25
|
|
26 def process(self, query):
|
|
27 if '://' in query:
|
|
28 return query
|
|
29 return 'http://' + query
|
|
30
|
|
31 def test(self, query):
|
|
32 """try to open the url"""
|
|
33
|
|
34 if ' ' in query or '\n' in query:
|
|
35 return False
|
|
36
|
|
37 try:
|
|
38 site = urllib.urlopen(self.process(query))
|
|
39 except IOError:
|
|
40 return False
|
|
41 return True
|
|
42
|
1
|
43 class GoogleMaps(Location):
|
0
|
44 """try to google-maps the address"""
|
|
45
|
|
46 def __init__(self):
|
|
47 gmapsurl='http://maps.google.com/maps?f=q&hl=en&q='
|
|
48 Location.__init__(self, gmapsurl)
|
|
49
|
|
50 def process(self, query):
|
|
51 theaddress = address.normalizeaddress(query)
|
|
52 if not theaddress:
|
|
53 return theaddress
|
|
54 return urllib.quote_plus(theaddress)
|
|
55
|
|
56 def test(self, query):
|
|
57 return bool(self.process(query))
|
|
58
|
1
|
59 class Trac(Location):
|
2
|
60 def __init__(self, baseurl):
|
|
61 baseurl = baseurl.strip('/') + '/'
|
|
62 Location.__init__(self, baseurl)
|
0
|
63
|
2
|
64 def process(self, query):
|
|
65 if query[0] == 'r':
|
|
66 if query[1:].isdigit():
|
|
67 return 'changeset/' + str(query[1:])
|
|
68 if query[0] == '#':
|
|
69 if query[1:].isdigit():
|
|
70 return 'ticket/' + str(query[1:])
|
0
|
71
|
2
|
72 def test(self, query):
|
|
73 return bool(self.process(query))
|
|
74
|
0
|
75
|
|
76 class Wikipedia(Location):
|
|
77 """try to open the query in wikipedia"""
|
|
78 def __init__(self):
|
|
79 wikiurl = 'http://en.wikipedia.org/wiki/'
|
|
80 Location.__init__(self, wikiurl)
|
|
81
|
|
82 def process(self, query):
|
|
83 return urllib.quote_plus('_'.join(query.split()))
|
|
84
|
|
85 def test(self, query):
|
|
86 'test to see if the article exists'
|
|
87
|
|
88 # need a phony user agent so wikipedia won't know we're a bot
|
|
89 headers = {}
|
|
90 headers['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4'
|
|
91
|
|
92 request = urllib2.Request(self.url(query), None, headers)
|
|
93 try:
|
|
94 f = urllib2.urlopen(request).read()
|
|
95 except urllib2.HTTPError, e:
|
|
96 return False
|
|
97
|
|
98 if 'Wikipedia does not have an article with this exact name' in f:
|
|
99 return False
|
|
100 return True
|
|
101
|
|
102 class Google(Location):
|
|
103 def __init__(self):
|
|
104 googleurl = 'http://www.google.com/search?hl=en&q='
|
|
105 Location.__init__(self, googleurl)
|
|
106
|
|
107 def process(self, query):
|
|
108 return urllib.quote_plus(query)
|