Major overhaul

* Now using Blueprints instead of importing modules.
* Now using def create_app instead of just app
This commit is contained in:
odecif 2023-11-18 02:52:39 +01:00
parent 77bd652f94
commit 631dc761c6
27 changed files with 186 additions and 83 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
.venv/
instance/
__pycache__/
*.py[cod]
api/devapi.conf

12
bcnsGDSAPI/devapi.conf Normal file
View File

@ -0,0 +1,12 @@
[Database]
db_type = MySQL
host = localhost
user = odecif
pass = SDR:0508
db_name = gamedistrosys
contentpath = /home/odecif/projects/bcns-gameDistribution_testcontent/
nfo_suffix = _index.nfo
[Running]
host = localhost
port = 5501

86
bcnsGDSSite/__init__.py Normal file
View File

@ -0,0 +1,86 @@
from datetime import date
from flask import render_template, Flask, request
from json import JSONEncoder
from bcnsGDSSite.modules.gamelist import gamelist
from bcnsGDSSite.modules.game import game
import bcnsGDSSite.modules.init
import os
import inspect
import json
import glob
import tomllib
class MyJSONEncoder(JSONEncoder):
def default(self, o):
if isinstance(o, date):
return o.isoformat()
return super().default(o)
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
app.register_blueprint(gamelist)
app.register_blueprint(game)
print(app.url_map)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'bcns_gds.sqlite'),
)
app.config['JSON_AS_ASCII'] = False
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_file("config.toml", load=tomllib.load, text=False)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# import bcnsGDSSite.modules.gamelist # noqa: E402
bcnsGDSSite.modules.init.set_apihost("http://" +
app.config['API']['host'] +
':' + app.config['API']['port'])
bcnsGDSSite.modules.init.set_applanguage(
app.config['MAIN']['app_language'])
languages = bcnsGDSSite.modules.init.get_languages()
if not languages:
languages_path = os.path.dirname(
os.path.abspath(inspect.getfile(
inspect.currentframe())))
language_list = glob.glob(languages_path + "/language/*.json")
print("### Loaded languages")
for language in language_list:
filename = os.path.basename(language)
lang_code = filename.split('.')[0]
with open(language, 'r', encoding='utf-8') as file:
print("# ", lang_code)
languages[lang_code] = json.loads(file.read())
bcnsGDSSite.modules.init.set_languages(language)
# Check if valid language code is set. If not, return app default
def lang(lang_code):
if lang_code not in languages:
return "en_US"
return lang_code
@app.route("/", methods=["GET", "POST"])
@app.route("/<lang_code>/", methods=["GET", "POST"])
def home(lang_code=False):
lang_code = lang(lang_code)
if request.form:
lang_code = request.form["language_select"]
return render_template('home.html', **languages[lang_code],
lang_code=lang_code,
languages=languages.keys())
return app

11
bcnsGDSSite/devsite.conf Normal file
View File

@ -0,0 +1,11 @@
[Main]
hostip = 172.29.254.97
debug = True
host = localhost
port = 5500
contentpath = /home/odecif/project/bcns-gameDistribution_testcontent
[API]
#host = localhost
host = 172.29.29.51
port = 5501

View File

@ -1,13 +1,16 @@
from __main__ import app
import json
import requests
import base64
from flask import render_template, request, Response
import modules.init
from flask import render_template, request, Response, Blueprint
import bcnsGDSSite.modules.init
host_endpoint = modules.init.host_endpoint()
languages = modules.init.get_languages()
app_language = modules.init.app_language
host_endpoint = bcnsGDSSite.modules.init.get_apihost()
languages = bcnsGDSSite.modules.init.get_languages()
app_language = bcnsGDSSite.modules.init.app_language
game = Blueprint('game', __name__,
template_folder='templates')
# Check if valid language code is set. If not, return app default
@ -18,8 +21,8 @@ def lang(lang_code):
# Show game
@app.route("/<lang_code>/game")
def game(lang_code):
@game.route("/<lang_code>/game")
def showgame(lang_code):
lang_code = lang(lang_code)
gamepath = request.args.get("gamepath")
@ -38,13 +41,14 @@ def game(lang_code):
# Show game artwork
@app.route("/<lang_code>/game/artwork")
@game.route("/<lang_code>/game/artwork")
def artwork(lang_code):
pass
# Download a game
@app.route("/<lang_code>/game/download")
#@app.route("/<lang_code>/game/download")
@game.route("/<lang_code>/game/download")
def download(lang_code):
gamepath = request.args.get("gamepath")
gametitle = request.args.get("gametitle")
@ -66,7 +70,7 @@ def download(lang_code):
# Download file from backend. Could be documents, patches etc.
# gamepath full path to current games NFO-file
# filepath path from gamepath to the file in question
@app.route("/<lang_code>/game/getfile")
@game.route("/<lang_code>/game/getfile")
def getfile(lang_code):
gamepath = request.args.get("gamepath")
filepath = request.args.get("filepath")

View File

@ -1,14 +1,15 @@
from __main__ import app
import json
import requests
from flask import render_template, redirect, url_for, request
import modules.init
from flask import render_template, redirect, url_for, request, Blueprint
import bcnsGDSSite.modules.init
import base64
host_endpoint = modules.init.host_endpoint()
languages = modules.init.get_languages()
app_language = modules.init.app_language
host_endpoint = bcnsGDSSite.modules.init.get_apihost()
languages = bcnsGDSSite.modules.init.get_languages()
app_language = bcnsGDSSite.modules.init.app_language
gamelist = Blueprint('gamelist', __name__,
template_folder='templates')
# Check if valid language code is set. If not, return app default
def lang(lang_code):
@ -18,9 +19,8 @@ def lang(lang_code):
# Show gamelist
@app.route("/<lang_code>/gamelist", methods=["GET", "POST"])
# @app.route("/gamelist")
def gamelist(lang_code):
@gamelist.route("/<lang_code>/gamelist", methods=["GET", "POST"])
def showgamelist(lang_code):
lang_code = lang(lang_code)
thumbnails = request.args.get("thumbnails")
glist = None
@ -66,7 +66,7 @@ def gamelist(lang_code):
# Update/Refresh the gamelist by re-scanning the game archive (slow)
@app.route("/<lang_code>/gamelist/update")
@gamelist.route("/<lang_code>/gamelist/update")
def gamelist_update(lang_code):
lang_code = lang(lang_code)
response = requests.get(

View File

@ -0,0 +1,42 @@
import os
import inspect
global languages
languages = {}
global apihost
apihost = "http://media.odecif.net:5501"
global app_language
app_language = ""
def set_languages(languages_dict):
global languages
languages = languages_dict
return True
def get_languages():
global languages
return languages
def set_apihost(new_apihost):
global apihost
apihost = new_apihost
return apihost
def get_apihost():
global apihost
return apihost
def set_applanguage(new_applanguage):
global app_language
app_language = new_applanguage
return app_language
def init():
global apihost
apihost = ""

View File

@ -18,7 +18,7 @@
{% if game.game.documents %}
{% for document in game.game.documents %}
{% if document.type == 'manual' %}
<a href="{{ url_for('getfile', gamepath=game.path, filepath=document.path, lang_code=lang_code)}}">{{lang_game_get_manual}} {{document.path}}</a><br />
<a href="{{ url_for('game.getfile', gamepath=game.path, filepath=document.path, lang_code=lang_code)}}">{{lang_game_get_manual}} {{document.path}}</a><br />
{% endif %}
{% endfor %}
{% else %}
@ -26,15 +26,15 @@
{% endif %}
<h3>{{lang_game_download_header}}</h3>
<p>{{lang_game_download_ingress}}</p>
<a href="{{ url_for('download', targettype="torrent", gametitle=game.game.title, gamepath=game.path, lang_code=lang_code)}}"><button type="button">{{lang_game_download_torrent_button}}</button></a>
<a href="{{ url_for('download', targettype="zip", gamepath=game.path, gametitle=game.game.title, lang_code=lang_code)}}"><button type="button">{{lang_game_download_zip_button}}</button></a>
<a href="{{ url_for('game.download', targettype="torrent", gametitle=game.game.title, gamepath=game.path, lang_code=lang_code)}}"><button type="button">{{lang_game_download_torrent_button}}</button></a>
<a href="{{ url_for('game.download', targettype="zip", gamepath=game.path, gametitle=game.game.title, lang_code=lang_code)}}"><button type="button">{{lang_game_download_zip_button}}</button></a>
<p><a href="">{{lang_game_download_magnet_link}}</a></p>
<table class="game">
<tr>
<th>{{lang_game_artwork}}</th>
<td><div class="tooltip">
{% for part in game.game.artwork %}
<span class="tooltiptext">{{part.type}}</span><a href="{{ url_for('artwork', gamepath=game.path, art=part.filename, lang_code=lang_code)}}"><img src="data:image/jpeg;base64,{{part.data}}"></img></a>
<span class="tooltiptext">{{part.type}}</span><a href="{{ url_for('game.artwork', gamepath=game.path, art=part.filename, lang_code=lang_code)}}"><img src="data:image/jpeg;base64,{{part.data}}"></img></a>
{% endfor %}
</div></td>
</tr><tr>

View File

@ -9,7 +9,7 @@
{% block content %}
<h1>{{lang_gamelist_body_header}}</h1>
<p>{{lang_gamelist_body_ingress}}</p>
<a href="{{ url_for('gamelist', thumbnails="get", lang_code=lang_code)}}"><button type="button">{{lang_gamelist_get_thumbnails_button}}</button></a>
<a href="{{ url_for('gamelist.showgamelist', thumbnails="get", lang_code=lang_code)}}"><button type="button">{{lang_gamelist_get_thumbnails_button}}</button></a>
<table class="game">
<tr>
<th>{{lang_game_artwork}}</th>
@ -23,12 +23,12 @@
<tr>
{% for thumbnail in thumbnails %}
{% if thumbnail.nfo == item.nfo %}
<td><a href="{{ url_for('game', gamepath=item.path, lang_code=lang_code)}}"><img src="data:image/jpeg;base64,{{thumbnail.displayimage}}"></img></a></td>
<td><a href="{{ url_for('game.artwork', gamepath=item.path, lang_code=lang_code)}}"><img src="data:image/jpeg;base64,{{thumbnail.displayimage}}"></img></a></td>
{% endif %}
{% else %}
<td></td>
{% endfor %}
<td><a href="{{ url_for('game', gamepath=item.path, lang_code=lang_code)}}">{{item.game.title}}</a></td>
<td><a href="{{ url_for('game.showgame', gamepath=item.path, lang_code=lang_code)}}">{{item.game.title}}</a></td>
<td>{{item.game.year}}</td>
<td>{{item.game.genre}}</td>
<td>{{item.game.developer}}</td>

View File

@ -13,7 +13,7 @@
<nav>
<ul class="menu">
<li><a href="{{ url_for('home', lang_code=lang_code) }}">{{lang_navigation_home}}</a></li>
<li><a href="{{ url_for('gamelist', lang_code=lang_code) }}">{{lang_navigation_gamelist}}</a></li>
<li><a href="{{ url_for('gamelist.showgamelist', lang_code=lang_code) }}">{{lang_navigation_gamelist}}</a></li>
</ul>
</nav>
</strong>

View File

@ -1,54 +0,0 @@
import argparse
import configparser
import os
import inspect
global languages
languages = {}
def set_languages(languages_dict):
global languages
languages = languages_dict
return True
def get_languages():
global languages
return languages
def init():
global config
global app_language
app_language = 'en_US'
# Decide what config-file to use
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--environment', choices=['dev', 'prod', 'test'],
default='prod',
help='choose what environment type to run.'
' Defaults to prod')
args = parser.parse_args()
if args.environment == 'dev':
print("Using devsite.conf")
configfile = '/../devsite.conf'
elif args.environment == 'test':
print("Using testsite.conf")
configfile = '/../testsite.conf'
else:
print("Using site.conf")
configfile = '/../site.conf'
config = configparser.RawConfigParser()
config.read(os.path.dirname(
os.path.abspath(inspect.getfile(
inspect.currentframe()))) + configfile)
# Constructor for the API-endpoint
def host_endpoint():
return ('http://' + config.get('API', 'host') + ':' +
config.get('API', 'port'))