* Separated /gamelist to 2 calls: /gamelist and /gamelist/thumbnails. * Refactored nested for-loop with a next() when looping artwork [fixes #20]
113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
from __main__ import app
|
|
from flask import jsonify, request, send_from_directory, send_file
|
|
import base64
|
|
import json
|
|
import modules.db_connect
|
|
from modules.functions import reduceartcv2, make_archive
|
|
import os
|
|
|
|
contentpath = modules.db_connect.contentpath()
|
|
|
|
|
|
# Fetch and present all information from one _index.nfo-file
|
|
@app.route('/game', methods=['POST'])
|
|
def game(
|
|
predefinednfo=False,
|
|
return_dict=False,
|
|
skip_artwork=False,
|
|
skip_displayimage=False):
|
|
nfopath = ""
|
|
if predefinednfo is not False:
|
|
nfopath = predefinednfo
|
|
else:
|
|
nfopath = base64.b64decode(request.json).decode()
|
|
nfo = json.load(open(nfopath, 'r'))
|
|
nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode()
|
|
nfo['nfo'] = os.path.basename(nfopath)
|
|
|
|
# Add front cover artwork in medium size
|
|
# TODO: Refactor to use next() instead of nested for()
|
|
if skip_displayimage is True:
|
|
nfo['game']['displayimage'] = ""
|
|
else:
|
|
artpath = os.path.dirname(nfopath)+'/art/'
|
|
i = 0
|
|
if 'artwork' in nfo['game']:
|
|
if nfo['game']['artwork']:
|
|
for art in nfo['game']['artwork']:
|
|
try:
|
|
if skip_artwork is False:
|
|
nfo['game']['artwork'][i]['data'] = reduceartcv2(
|
|
artpath+art['filename'], 'thumbnail')
|
|
if art['type'] == 'front':
|
|
nfo['game']['displayimage'] = reduceartcv2(
|
|
artpath+art['filename'], 'thumbnail')
|
|
except Exception as e:
|
|
print("Failing cover: " + art['filename'])
|
|
print(e)
|
|
i += 1
|
|
if 'displayimage' not in nfo['game']:
|
|
for art in nfo['game']['artwork']:
|
|
if art['type'] in ['cd', 'dvd']:
|
|
nfo['game']['displayimage'] = reduceartcv2(
|
|
artpath+art['filename'], 'thumbnail')
|
|
else:
|
|
nfo['game']['displayimage'] = ""
|
|
if return_dict is False:
|
|
return jsonify(nfo)
|
|
return nfo
|
|
|
|
|
|
# Fetch all artwork from a given _index.nfo-file
|
|
@app.route('/game/artwork/size/<size>', methods=['POST'])
|
|
@app.route('/game/artwork', methods=['POST'])
|
|
def artwork(size='max'):
|
|
f = open(request.json['nfo'], 'r')
|
|
nfo = json.load(f)
|
|
|
|
artpath = os.path.dirname(request.json['nfo'])+'/art/'
|
|
artlist = []
|
|
for art in nfo['game']['artwork']:
|
|
img = ""
|
|
|
|
# If max size, send image as-is
|
|
if size == 'max':
|
|
with open(artpath+art['filename'], mode='rb') as file:
|
|
img = file.read()
|
|
art['img'] = base64.b64encode(img).decode()
|
|
|
|
# Changes filetype to jpeg for size reduction. This also drops
|
|
# Alpha-channel
|
|
else:
|
|
art['img'] = reduceartcv2(artpath+art['filename'], size)
|
|
|
|
artlist.append(art)
|
|
|
|
return jsonify(artlist)
|
|
|
|
|
|
# Server game manual
|
|
@app.route('/getmanual', methods=["GET"])
|
|
def getmanual():
|
|
nfopath = base64.b64decode(request.json).decode()
|
|
nfo = json.load(open(nfopath, 'r'))
|
|
nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode()
|
|
manualname = nfo['game']['manual']
|
|
manualpath = os.path.dirname(nfopath)+'/'
|
|
return send_from_directory(manualpath, manualname, as_attachment=True)
|
|
|
|
|
|
# Game folder as ZIP-file (kinda slow)
|
|
@app.route('/getzipfile', methods=["GET"])
|
|
def getzipfile():
|
|
nfopath = base64.b64decode(request.json).decode()
|
|
nfo = json.load(open(nfopath, 'r'))
|
|
nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode()
|
|
path = os.path.dirname(nfopath)
|
|
dirname = os.path.basename(nfopath)
|
|
return send_file(
|
|
make_archive(dirname, path),
|
|
mimetype='application/zip',
|
|
as_attachment=True,
|
|
download_name=str(dirname) + '.zip')
|