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):
|
|
60 def __init__(self, url):
|
|
61 self.url = url
|
0
|
62
|
|
63
|
1
|
64 # TODO: need configuration
|
|
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:]
|
0
|
72
|
1
|
73 # def test(self, query):
|
|
74 # if query[0] != 'r':
|
|
75 # return False
|
|
76 # return query[1:].isdigit()
|
|
77
|
|
78 # class TracTicket(Location):
|
|
79 # def __init__(self):
|
|
80 # # url for # data
|
|
81 # number_url = 'http://trac.openplans.org/openplans/ticket/'
|
|
82 # Location.__init__(self, number_url)
|
0
|
83
|
1
|
84 # def process(self, query):
|
|
85 # if query.startswith('#'):
|
|
86 # return query[1:]
|
|
87 # return query
|
|
88
|
|
89 # def test(self, query):
|
|
90 # query = self.process(query)
|
|
91 # if len(query.split()) != 1:
|
|
92 # return False
|
|
93 # return query.isdigit()
|
0
|
94
|
|
95 class Wikipedia(Location):
|
|
96 """try to open the query in wikipedia"""
|
|
97 def __init__(self):
|
|
98 wikiurl = 'http://en.wikipedia.org/wiki/'
|
|
99 Location.__init__(self, wikiurl)
|
|
100
|
|
101 def process(self, query):
|
|
102 return urllib.quote_plus('_'.join(query.split()))
|
|
103
|
|
104 def test(self, query):
|
|
105 'test to see if the article exists'
|
|
106
|
|
107 # need a phony user agent so wikipedia won't know we're a bot
|
|
108 headers = {}
|
|
109 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'
|
|
110
|
|
111 request = urllib2.Request(self.url(query), None, headers)
|
|
112 try:
|
|
113 f = urllib2.urlopen(request).read()
|
|
114 except urllib2.HTTPError, e:
|
|
115 return False
|
|
116
|
|
117 if 'Wikipedia does not have an article with this exact name' in f:
|
|
118 return False
|
|
119 return True
|
|
120
|
|
121 class Google(Location):
|
|
122 def __init__(self):
|
|
123 googleurl = 'http://www.google.com/search?hl=en&q='
|
|
124 Location.__init__(self, googleurl)
|
|
125
|
|
126 def process(self, query):
|
|
127 return urllib.quote_plus(query)
|