-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
57 lines (44 loc) · 1.51 KB
/
server.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
from flask import Flask, request, render_template
from sqlalchemy import create_engine, text
app = Flask(__name__)
engine = create_engine("postgresql://gaoyb7@localhost/dht_demo")
conn = engine.connect()
@app.route("/")
def index():
return render_template("base.html", total_torrents=torrents_count())
@app.route("/search")
def search():
kw = request.args.get("kw").strip()
kw = ":* & ".join(kw.split()) + ":*"
precheck_cmd = '''select to_tsquery('english', '%s')''' % kw
r = conn.execute(precheck_cmd)
print(precheck_cmd)
if r.fetchone()[0] == "":
return "None"
cmd = '''select * from hash_tab where to_tsvector('english', name) @@ \
to_tsquery('english', '%s')''' % kw
r = conn.execute(cmd)
counts = r.rowcount
result = []
cnt = 0
for item in r:
cnt += 1
if cnt > 100:
break
result.append((item[0], item[1]))
print("ok")
return render_template("search.html", match_torrents_count=counts,
total_torrents=torrents_count(), result=result)
def to_magnet(infohash, name):
magnet_base = "magnet:?xt=urn:btih:" + infohash
magnet = magnet_base + "&dn=" + name
return magnet
def torrents_count():
cmd = "select count(*) from hash_tab"
r = conn.execute(cmd)
for item in r:
return item[0]
if __name__ == "__main__":
app.template_folder = "simServer/templates"
app.debug = False
app.run(host="0.0.0.0", port=5000)