forked from SubhrajitPrusty/codex-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
88 lines (65 loc) · 2.27 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
import os
import json
from gevent import monkey
monkey.patch_all()
from userdata import logger
from fuzzywuzzy import fuzz
from dotenv import load_dotenv
from pymongo import MongoClient
from gevent.pywsgi import WSGIServer
from flask import Flask, url_for, render_template, request
load_dotenv()
dburl = os.environ.get('MONGODB_URI')
client = MongoClient(dburl)
db = client.get_default_database()
members = db.members
app = Flask(__name__, static_url_path='/static')
@logger.catch
def getContent():
data = []
for mem in members.find():
data.append(mem)
data = sorted(data, key=lambda k: k['totalCommits'])
return data[::-1]
@logger.catch
@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)
@logger.catch
@app.route("/search")
def searchMember():
query = request.args.get("query")
if query == "":
return render_template('search.html', context=content, search=True, found=True)
# logger.debug(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)
@logger.catch
@app.route("/<username>")
def profile(username):
try:
user_details = [x for x in members.find({"username" : username})].pop()
# logger.debug(user_details)
return render_template("profile.html", user=user_details)
except IndexError:
logger.error(f'Username not found. username:{username}')
return "404"
except Exception as e:
logger.error(e)
raise e
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
http_server = WSGIServer(('', port), app.wsgi_app)
logger.debug("Server ready: ")
try:
http_server.serve_forever()
except KeyboardInterrupt:
logger.debug("Exiting")