This repository has been archived by the owner on Feb 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathapp.py
105 lines (78 loc) · 2.4 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
from loguru import logger
from gevent import monkey
from fuzzywuzzy import fuzz
from dotenv import load_dotenv
from gevent.pywsgi import WSGIServer
from flask import Flask, render_template, request, send_from_directory
monkey.patch_all()
from pymongo import MongoClient # noqa: E402
load_dotenv()
dburl = os.environ.get('DB_URI')
client = MongoClient(dburl, retryWrites=False)
db = client.get_default_database()
members = db.members
app = Flask(__name__, static_url_path='', static_folder="static")
def getContent():
data = []
for mem in members.find():
data.append(mem)
data = sorted(data, key=lambda k: k['totalCommits'])
return data[::-1]
@app.route("/")
def index():
global content
global total
content = getContent()
total = sum([x['totalCommits'] for x in content])
return render_template(
'index.html',
context=content,
totalC=total,
search=False)
@app.route("/search")
def searchMember():
query = request.args.get("query")
if query == "":
return render_template(
'search.html',
context=content,
search=True,
found=True)
# print(query)
def sanitize(x): return x.lower() if x else " "
ratios = [{"ratio": max(
[
fuzz.partial_ratio(sanitize(x['name']), query.lower()),
fuzz.partial_ratio(sanitize(x['username']), query.lower())
]
), "data": x} for x in content]
ratios = sorted(ratios, key=lambda k: k['ratio'])
result = [x['data'] for x in ratios if x['ratio'] > 60][::-1]
found = len(result) != 0
return render_template(
'search.html',
context=result,
search=True,
found=found)
@app.route("/<username>")
def profile(username):
try:
user_details = [x for x in members.find({"username": username})].pop()
return render_template("profile.html", user=user_details)
except IndexError:
return "404", 404
except Exception as e:
logger.error(e)
raise e
@app.route("/robots.txt")
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
http_server = WSGIServer(('', port), app.wsgi_app)
print("Server ready:")
try:
http_server.serve_forever()
except KeyboardInterrupt:
print("Exiting")