-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshub.py
71 lines (56 loc) · 1.38 KB
/
shub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
import re, yaml
from flask import Flask, request, redirect
class Engines:
def __init__(self):
self.load()
def load(self):
self.engines = yaml.load(open("engines.yaml"))
def get(self,key):
for item in self.engines:
if item["key"]==key:
return Engine(item)
raise KeyError
def get_default(self):
for item in self.engines:
if item.has_key("default"):
return Engine(item)
raise KeyError
class Engine:
def __init__(self,dic):
self.__dict__=dic
app = Flask(__name__)
app.debug = True
@app.route('/about')
def about():
engines = Engines()
rows = "".join(['<tr><td>%s</td><td>%s</td></tr>' % (i["key"], i["name"]) for i in engines.engines])
html = '''
<html>
<body>
<h3>Engines installed</h3>
<table border="1">''' + rows + '''
</table>
</body>
</html>'''
return html
@app.route('/search')
def search():
default = "g"
engines = Engines()
q = request.args["q"]
m = re.match(r'(\w+)\s+(.+)',q)
if m:
key, to = m.groups()
try:
url = engines.get(key).url
except KeyError:
to = q
url = engines.get_default().url
else:
url = engines.get_default().url
to = q
url = url % (to,)
return redirect(url)
if __name__ == '__main__':
app.run()