From a1e5b21189c09ea29eb3170584fab7e6dae14197 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 11 Aug 2017 21:03:10 -0400 Subject: [PATCH] First stab at server side generation --- SotMDeckBuilder.py | 15 +++++++-------- server.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 8 deletions(-) create mode 100755 server.py diff --git a/SotMDeckBuilder.py b/SotMDeckBuilder.py index f8e535f..364f689 100755 --- a/SotMDeckBuilder.py +++ b/SotMDeckBuilder.py @@ -100,7 +100,7 @@ def makeDoubleSidedFace(outSVG, cardsPerRow, cardNum, deckType, cardFile, makeFace(outSVG, cardsPerRow, cardNum, deckType, cardFile, cardFront) makeFace(outSVG, cardsPerRow, cardNum + 1, deckType, cardFile, cardBack) -def makeCards(deckJson, outfile): +def makeCards(deckJson, outfile, outdir, host): # number of cards in x and y direction cardsPerRow = math.ceil(math.sqrt(len(deckJson['deck']) + len(deckJson['character']) * 2)) outSVG = etree.ElementTree( @@ -116,8 +116,7 @@ def makeCards(deckJson, outfile): outJson['ObjectStates'][0]['CustomDeck']['1'].update( {"NumWidth": cardsPerRow, "NumHeight": cardsPerRow, - #"FaceURL": "http://adamgoldsmith.name/public/SotM/" + outfile + ".png", - "FaceURL": "file://" + os.getcwd() + "/" + outfile + ".png", + "FaceURL": host + outfile + ".png", "BackURL": "http://cloud-3.steamusercontent.com/ugc/156906385556221451/CE2C3AFE1759790CB0B532FFD636D05A99EC91F4/"}) cardNum = 0 @@ -162,16 +161,16 @@ def makeCards(deckJson, outfile): cardNum += 1 # write SVG and convert to PNG - outSVG.write(outfile + ".svg") + outSVG.write(os.path.join(outdir, outfile) + ".svg") command = ["inkscape", "-z", - "-f", outfile + ".svg", + "-f", os.path.join(outdir, outfile) + ".svg", "-w", str(cardsPerRow * CARD_WIDTH * 5), - "-e", outfile + ".png"] + "-e", os.path.join(outdir, outfile) + ".png"] print("To regenerate PNG after editing SVG, run:\n " + " ".join(command)) subprocess.run(command) # Write TTS deck json - with open(outfile + ".json", "w") as f: + with open(os.path.join(outdir, outfile) + ".json", "w") as f: json.dump(outJson, f) if __name__ == '__main__': @@ -186,4 +185,4 @@ if __name__ == '__main__': with open(inputJson) as f: deckJson = json.load(f) - makeCards(deckJson, outfile) + makeCards(deckJson, outfile, os.getcwd(), "file://" + os.getcwd() + "/") diff --git a/server.py b/server.py new file mode 100755 index 0000000..e4c7a58 --- /dev/null +++ b/server.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +import os +import json + +from flask import Flask +from flask import Flask, request, redirect, url_for, send_from_directory +from werkzeug.utils import secure_filename + +import SotMDeckBuilder + +app = Flask(__name__) + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() == "json" + +@app.route('/', methods=['GET', 'POST']) +def upload_file(): + if request.method == 'POST': + # check if the post request has the file part + if 'file' not in request.files: + return redirect(request.url) + file = request.files['file'] + # if user does not select file, browser also + # submit a empty part without filename + if file.filename == '': + return redirect(request.url) + if file and allowed_file(file.filename): + filename = secure_filename(file.filename).replace(".json", "") + SotMDeckBuilder.makeCards(json.load(file.stream), + filename, "result", request.url_root + "result/") + return redirect(url_for('get_result', filename=filename + ".json")) + return ''' + + Upload new File +

Upload new File

+
+

+ +

+ ''' + +@app.route('/result/') +def get_result(filename): + return send_from_directory("result", filename, as_attachment=True) + +if __name__ == "__main__": + app.run(debug=True, host='0.0.0.0')