66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
from __main__ import app
|
|
from flask import jsonify, request
|
|
import base64
|
|
import json
|
|
import modules.db_connect
|
|
from modules.functions import reduceartcv2
|
|
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):
|
|
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()
|
|
|
|
# Add front cover artwork in medium size
|
|
artpath = os.path.dirname(nfopath)+'/art/'
|
|
i = 0
|
|
for art in nfo['game']['artwork']:
|
|
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')
|
|
i += 1
|
|
|
|
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)
|