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 def open(self, query):
|
|
23 if not self.test(query):
|
|
24 return False
|
|
25 url = self.url(query)
|
|
26 os.system("firefox '%s'" % url)
|
|
27 return True
|
|
28
|
|
29 class URL(Location):
|
|
30 """a straight URL"""
|
|
31
|
|
32 def process(self, query):
|
|
33 if '://' in query:
|
|
34 return query
|
|
35 return 'http://' + query
|
|
36
|
|
37 def test(self, query):
|
|
38 """try to open the url"""
|
|
39
|
|
40 if ' ' in query or '\n' in query:
|
|
41 return False
|
|
42
|
|
43 try:
|
|
44 site = urllib.urlopen(self.process(query))
|
|
45 except IOError:
|
|
46 return False
|
|
47 return True
|
|
48
|
|
49 class GoogleMap(Location):
|
|
50 """try to google-maps the address"""
|
|
51
|
|
52 def __init__(self):
|
|
53 gmapsurl='http://maps.google.com/maps?f=q&hl=en&q='
|
|
54 Location.__init__(self, gmapsurl)
|
|
55
|
|
56 def process(self, query):
|
|
57 theaddress = address.normalizeaddress(query)
|
|
58 if not theaddress:
|
|
59 return theaddress
|
|
60 return urllib.quote_plus(theaddress)
|
|
61
|
|
62 def test(self, query):
|
|
63 return bool(self.process(query))
|
|
64
|
|
65 class Revision(Location):
|
|
66 def __init__(self):
|
|
67 revision_url = 'http://trac.openplans.org/openplans/changeset/'
|
|
68 Location.__init__(self, revision_url)
|
|
69
|
|
70 def process(self, query):
|
|
71 return query[1:]
|
|
72
|
|
73 def test(self, query):
|
|
74 if query[0] != 'r':
|
|
75 return False
|
|
76 return query[1:].isdigit()
|
|
77
|
|
78
|
|
79
|
|
80 class TracTicket(Location):
|
|
81 def __init__(self):
|
|
82 # url for # data
|
|
83 number_url = 'http://trac.openplans.org/openplans/ticket/'
|
|
84 Location.__init__(self, number_url)
|
|
85
|
|
86 def process(self, query):
|
|
87 if query.startswith('#'):
|
|
88 return query[1:]
|
|
89 return query
|
|
90
|
|
91 def test(self, query):
|
|
92 query = self.process(query)
|
|
93 if len(query.split()) != 1:
|
|
94 return False
|
|
95 return query.isdigit()
|
|
96
|
|
97 class Wikipedia(Location):
|
|
98 """try to open the query in wikipedia"""
|
|
99 def __init__(self):
|
|
100 wikiurl = 'http://en.wikipedia.org/wiki/'
|
|
101 Location.__init__(self, wikiurl)
|
|
102
|
|
103 def process(self, query):
|
|
104 return urllib.quote_plus('_'.join(query.split()))
|
|
105
|
|
106 def test(self, query):
|
|
107 'test to see if the article exists'
|
|
108
|
|
109 # need a phony user agent so wikipedia won't know we're a bot
|
|
110 headers = {}
|
|
111 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'
|
|
112
|
|
113 request = urllib2.Request(self.url(query), None, headers)
|
|
114 try:
|
|
115 f = urllib2.urlopen(request).read()
|
|
116 except urllib2.HTTPError, e:
|
|
117 return False
|
|
118
|
|
119 if 'Wikipedia does not have an article with this exact name' in f:
|
|
120 return False
|
|
121 return True
|
|
122
|
|
123 class Google(Location):
|
|
124 def __init__(self):
|
|
125 googleurl = 'http://www.google.com/search?hl=en&q='
|
|
126 Location.__init__(self, googleurl)
|
|
127
|
|
128 def process(self, query):
|
|
129 return urllib.quote_plus(query)
|