forked from HexPandaa/RootMe-Badge-Generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
100 lines (77 loc) · 3.11 KB
/
main.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
import json
import os
from datetime import timedelta
from dotenv import load_dotenv
from flask import Flask, flash, render_template, request, send_from_directory
from flask_cors import CORS, cross_origin
from werkzeug.utils import secure_filename
from timeloop import Timeloop
from config import Config
from src.http_client import RMAPI
from src.parser import extract_data, extract_info_username_input
from src.storage import make_storage, make_storage_js
load_dotenv()
tl = Timeloop()
app = Flask(__name__)
app.config.from_object(Config)
api = RMAPI()
app.api = api
CORS(app)
URL = os.environ.get('URL')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html')
if 'username' not in request.form:
flash('A wrong form has been sent.', 'error')
return render_template('index.html')
username = request.form['username']
if not username:
flash('Username is empty', 'error')
return render_template('index.html')
username, id_auteur, flash_message, flash_type = extract_info_username_input(username, app.api)
if flash_message is not None and flash_type is not None: # username input is not related to a real RootMe account
flash(f'{flash_message}', flash_type)
return render_template('index.html')
url = f'{app.api.api_url}/auteurs/{id_auteur}'
content = app.api.http_get(url)
if content is None: # account exists but has a score equal to zero
data = {
'nom': username,
'position': app.api.number_users,
'score': 0,
'validations': []
}
else:
data = json.loads(content)
data = extract_data(data, id_auteur, app.api, URL)
# store static png badges
save_paths, folder_path, avatar_path = make_storage(app.api, data)
# update avatar_url with local url
data['avatar_url'] = f'{URL}/{avatar_path}'
# store dynamic js badge as js script
dynamic_js_badge = render_template('dynamic-js-badge.html', data=data)
js_file_path = make_storage_js(dynamic_js_badge, folder_path)
return render_template('badge.html', data=data, save_paths=save_paths, js_file_path=js_file_path)
@app.route('/storage_server/<string:filename>')
@cross_origin(origin='*')
def serve_files(filename):
filename = secure_filename(filename)
return send_from_directory(f'storage_server', filename)
@app.route('/storage_clients/<string:folder>/<string:filename>')
def serve_files_clients(folder, filename):
folder = secure_filename(folder)
filename = secure_filename(filename)
if filename == 'badge.js':
return send_from_directory(f'storage_clients/{folder}', filename, mimetype='text/javascript')
return send_from_directory(f'storage_clients/{folder}', filename)
@tl.job(interval=timedelta(days=1))
def update_number_rootme_challenges() -> None:
app.api.update_number_rootme_challenges()
@tl.job(interval=timedelta(days=1))
def update_number_rootme_users() -> None:
app.api.update_number_rootme_users()
def start_tl():
tl.start(block=True)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)