First stab at server side generation

This commit is contained in:
Adam Goldsmith 2017-08-11 21:03:10 -04:00
parent ad22b9bb4d
commit a1e5b21189
2 changed files with 54 additions and 8 deletions

View File

@ -100,7 +100,7 @@ def makeDoubleSidedFace(outSVG, cardsPerRow, cardNum, deckType, cardFile,
makeFace(outSVG, cardsPerRow, cardNum, deckType, cardFile, cardFront) makeFace(outSVG, cardsPerRow, cardNum, deckType, cardFile, cardFront)
makeFace(outSVG, cardsPerRow, cardNum + 1, deckType, cardFile, cardBack) 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 # number of cards in x and y direction
cardsPerRow = math.ceil(math.sqrt(len(deckJson['deck']) + len(deckJson['character']) * 2)) cardsPerRow = math.ceil(math.sqrt(len(deckJson['deck']) + len(deckJson['character']) * 2))
outSVG = etree.ElementTree( outSVG = etree.ElementTree(
@ -116,8 +116,7 @@ def makeCards(deckJson, outfile):
outJson['ObjectStates'][0]['CustomDeck']['1'].update( outJson['ObjectStates'][0]['CustomDeck']['1'].update(
{"NumWidth": cardsPerRow, {"NumWidth": cardsPerRow,
"NumHeight": cardsPerRow, "NumHeight": cardsPerRow,
#"FaceURL": "http://adamgoldsmith.name/public/SotM/" + outfile + ".png", "FaceURL": host + outfile + ".png",
"FaceURL": "file://" + os.getcwd() + "/" + outfile + ".png",
"BackURL": "http://cloud-3.steamusercontent.com/ugc/156906385556221451/CE2C3AFE1759790CB0B532FFD636D05A99EC91F4/"}) "BackURL": "http://cloud-3.steamusercontent.com/ugc/156906385556221451/CE2C3AFE1759790CB0B532FFD636D05A99EC91F4/"})
cardNum = 0 cardNum = 0
@ -162,16 +161,16 @@ def makeCards(deckJson, outfile):
cardNum += 1 cardNum += 1
# write SVG and convert to PNG # write SVG and convert to PNG
outSVG.write(outfile + ".svg") outSVG.write(os.path.join(outdir, outfile) + ".svg")
command = ["inkscape", "-z", command = ["inkscape", "-z",
"-f", outfile + ".svg", "-f", os.path.join(outdir, outfile) + ".svg",
"-w", str(cardsPerRow * CARD_WIDTH * 5), "-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)) print("To regenerate PNG after editing SVG, run:\n " + " ".join(command))
subprocess.run(command) subprocess.run(command)
# Write TTS deck json # 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) json.dump(outJson, f)
if __name__ == '__main__': if __name__ == '__main__':
@ -186,4 +185,4 @@ if __name__ == '__main__':
with open(inputJson) as f: with open(inputJson) as f:
deckJson = json.load(f) deckJson = json.load(f)
makeCards(deckJson, outfile) makeCards(deckJson, outfile, os.getcwd(), "file://" + os.getcwd() + "/")

47
server.py Executable file
View File

@ -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 '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@app.route('/result/<filename>')
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')