forked from SubhrajitPrusty/codex-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (44 loc) · 1.7 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
import json
import os
from flask import Flask, url_for, render_template, request
from gevent.pywsgi import WSGIServer
from pymongo import MongoClient
from dotenv import load_dotenv
from fuzzywuzzy import fuzz
load_dotenv()
def getContent():
dburl = os.environ.get('MONGODB_URI')
client = MongoClient(dburl)
db = client.get_default_database()
members = db.members
data = []
for mem in members.find():
data.append(mem)
data = sorted(data, key=lambda k: k['totalCommits'])
return data[::-1]
app = Flask(__name__, static_url_path='/static')
content = getContent()
total = sum([x['totalCommits'] for x in content])
@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)
sanitize = lambda x: 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)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
http_server = WSGIServer(('', port), app)
http_server.serve_forever()