-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_app.py
More file actions
83 lines (69 loc) · 2.49 KB
/
Copy pathflask_app.py
File metadata and controls
83 lines (69 loc) · 2.49 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
from flask import Flask, render_template, request, redirect, send_from_directory, make_response
from pathlib import Path
import minify, requests
from data import data
debug = Path("debug").exists()
minify.minify_files(data)
app = Flask(__name__)
engines = data["engines"]
@app.route("/")
def index():
if debug: minify.minify_files(data)
return send_from_directory("static", "index.min.html")
@app.route("/search")
def search():
q = request.args.get("q")
b = request.cookies.get("b")
e = request.cookies.get("e")
if not b: b = "!"
if not q:
return redirect("/")
if b in q:
for bang in data["bangs"]:
if q == f"{b}{bang['t']}":
return redirect("https://" + bang["d"])
if q.endswith(f" {b}{bang['t']}"):
return redirect(bang["u"].replace("{{{s}}}", q.replace(f"{b}{bang['t']}", "").strip()))
for ai in data["ai"]:
if q.endswith(f" {ai}") or q.startswith(ai):
return redirect(data["ai"][ai][1].replace("{{{s}}}", q.replace(ai, "").strip()))
q = q.split(b)[0]
if e:
return redirect(e.replace("{{{s}}}", q.strip()))
else:
return redirect(data["default_engine"]["u"].replace("{{{s}}}", q.strip()))
@app.route("/api/setBangChar")
def set_bang():
b = request.args.get("q")
response = make_response(redirect("/"))
response.set_cookie("b", b, max_age=60*60*24*365*100, expires=None)
return response
@app.route("/api/unsetBangChar")
@app.route("/api/setDefaultEngine")
def set_default_engine():
e = request.args.get("q")
response = make_response(redirect("/"))
response.set_cookie("e", e, max_age=60*60*24*365*100, expires=None)
return response
@app.route("/api/unsetDefaultEngine")
def api_unsetDefaultEngine():
response = make_response(redirect("/"))
response.delete_cookie("e")
return response
@app.route("/api/complete/search")
def api_completeSearch():
q = request.args.get("q")
if not q:
return redirect("/")
suggestions = requests.get(f"https://suggestqueries.google.com/complete/search?output=firefox&q={q}", verify=False)
return suggestions.json()
@app.route("/complete/search")
def completeSearch():
return api_completeSearch()
@app.route("/opensearch.xml")
def opensearch(): return send_from_directory(".", "opensearch.xml")
@app.route("/privacy")
def privacy(): return render_template("privacy-policy.html")
if __name__ == "__main__":
port = 4664
app.run(port=port, host="0.0.0.0")