This repository has been archived by the owner on Sep 29, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
178 lines (158 loc) · 6.31 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import flask
from flask import request
from flask_socketio import SocketIO, emit, join_room, leave_room
import os
import urllib, json
import urllib.request
import io
import datetime
import base64
from libs.utils import daemonize, markdown, fetchGithubData, qrToB64, twitterAPI, newBuild, randomcode, buildCheck, webHook
#import libs.wraps.auth as auth
import libs.wraps.misc as misc
import configparser
import libs.db as database
app = flask.Flask(__name__)
socket = SocketIO(app)
config = configparser.ConfigParser()
config.read("auth/auth.cfg")
construction_mode = False
building = False
running = False
updateTime = 3600
PKSM_commits = 0
db = database.db(config['Database']['Address'])
@app.errorhandler(404)
def page_not_found(error):
nav_projects = database.get_all(db, "repos", "name")
return flask.render_template('404.html', nav_projects=nav_projects), 404
@app.route('/nav')
@app.route('/base')
@app.route('/project')
@app.route('/graph')
@app.route('/stats')
@app.route('/construction')
@app.route('/extra_saves.html')
@app.route('/downloads')
def template_error_catch():
return flask.abort(404)
@app.route("/tools/extra_save")
def extra_saves_tool():
nav_projects = database.get_all(db, "repos", "name")
return flask.render_template("extra_saves.html", nav_projects=nav_projects)
@app.route('/project/<project>')
def project(project):
data = database.get_one(db, "repos", project)
nav_projects = database.get_all(db, "repos", "name")
qr = ""
if data == None:
return flask.render_template('404.html', nav_projects=nav_projects), 404
if data['latest_release_cia'] != "":
qr = qrToB64(data['latest_release_cia'])
if data['readme'] == None:
data['readme'] = "<p>No ReadMe Available!</p>"
else:
html = markdown(data['readme'])
data['readme'] = html
return flask.render_template('project.html', project=data, nav_projects=nav_projects, qr=qr)
@app.route('/about')
def about():
data = database.get_all(db, "members", None)
nav_projects = database.get_all(db, "repos", "name")
return flask.render_template('about.html', members=data, nav_projects=nav_projects)
@app.route('/stats/downloads')
def downloadStats():
nav_projects = database.get_all(db, "repos", "name")
data = database.get_repo_downloads(db, "repos")
return flask.render_template('stats.html', data=data, nav_projects=nav_projects)
@app.route('/download/<code>')
def download(code):
project = database.get_download(db, "download_codes", code)
if project != None:
path = config['Files']['Folder']
with open(path+project['app']+"_commit.txt") as f:
commit = f.readline()
return flask.send_file(path+project['app']+"_Latest_Build.zip", as_attachment=True, attachment_filename="PKSM-"+commit+".zip")
else:
return flask.abort(404)
@app.route('/download/<code>/cia')
def download_cia(code):
project = database.get_download(db, "download_codes", code)
if project != None:
path = config['Files']['Folder']
with open(path+project['app']+"_commit.txt") as f:
commit = f.readline()
return flask.send_file(path+project['app']+".cia", as_attachment=True, attachment_filename="PKSM-"+commit+".cia")
else:
return flask.abort(404)
@app.route('/download/<code>/cia_qr.png')
def cia_qr(code):
project = database.get_download(db, "download_codes", code)
if project != None:
i = qrToB64("https://flagbrew.org/download/" + code + "/cia")
image = base64.b64decode(i)
return flask.send_file(io.BytesIO(image), mimetype="image/png")
else:
return flask.abort(404)
@daemonize(updateTime)
def updateData():
global running
global updateTime
global PKSM_commits
global building
if running:
print("damn looks like gunicorn is being a pain like always!")
else:
running = True
if construction_mode:
print("Data updater is disabled because construction mode is enabled!")
else:
repos, members = fetchGithubData(config['Github']['Token'])
database.updateData(db, "repos", repos, True, True, False)
database.updateData(db, "members", members, False, True, False)
print("Done updating github data!")
tweets = twitterAPI(config['Twitter']['Consumer_Key'], config['Twitter']['Consumer_Secret'], config['Twitter']['Access_Key'], config['Twitter']['Access_Secret'])
database.updateData(db, "tweets", tweets, False, False, True)
print("Done updating twitter data!")
if database.get_one(db, "repos", "PKSM")['commits'] > PKSM_commits:
PKSM_commits = database.get_one(db, "repos", "PKSM")['commits']
newBuild(config['Jenkins']['Address'], config['Jenkins']['Username'], config['Jenkins']['Key'])
building = True
print("Data will be updated once again in", updateTime/60 , "minutes!")
running = False
@daemonize(60)
def check_build():
global building
if building:
if not buildCheck(config['Jenkins']['Address'], config['Jenkins']['Username'], config['Jenkins']['Key']):
building = False
path = config['Files']['Folder']
with open(path+"PKSM"+"_commit.txt") as f:
commit = f.readline()
download_code = randomcode(10)
database.update_code(db, download_code, "PKSM")
d = config['Discord']['DownloadURL']
webHook(config['Discord']['Hook'], commit, "PKSM", d+download_code, download_code)
@app.context_processor
def get_time():
return {'now': datetime.datetime.now()}
@app.route('/')
@app.route('/<page>')
# @misc.construction(construction_mode)
def main(page="index"):
nav_projects = database.get_all(db, "repos", "name")
page += '.html'
if os.path.isfile('templates/' + page):
return flask.render_template(page, nav_projects=nav_projects)
return flask.abort(404)
@app.route('/api/tweets')
def tweetApi():
tweets = database.get_all(db, "tweets", None)
return database.jsonify(database.json(tweets))
if __name__ == "__main__":
if construction_mode:
app.debug = True
socket.run(app, host='127.0.0.1', port=4000, use_reloader=False)
else:
app.debug = False
socket.run(app, host='127.0.0.1', port=4000, use_reloader=False)