import json import threading import random import string import dataHandlerPTT as ptt import generalText as gen from queue import Queue from datetime import datetime from pprint import pprint from flask import Flask, render_template, request, jsonify, send_from_directory, Response, stream_with_context, redirect app = Flask(__name__) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 eventQueue = Queue() @app.route('/stream') def stream(): return Response(stream_with_context(eventStream(eventQueue)), mimetype='text/event-stream') def eventStream(eventQueue): while (True): eventNode = eventQueue.get(True) data = json.dumps(eventNode['data'], indent=4, ensure_ascii=False) data = data.splitlines() data = ['data:' + i for i in data] data = '\n'.join(data) yield "event:{event}\n{data}\n\n".format(event=eventNode['event'], data=data) @app.route('/data/') def send_data(path): return send_from_directory('data', path) @app.route('/generalTxt') def generalTxt(): return render_template('generalTxt.html', title="泛用文字視覺化工具") @app.route('/post/generalTxt/addText', methods=['POST']) def generalText_addText(): text = request.json['text'] stopwords = request.json['stopwords'] randId = ''.join(random.choices( string.ascii_uppercase + string.ascii_lowercase, k=15)) result = gen.processText(randId, text, stopwords) return jsonify(Result={ 'path': result }) @app.route('/') def index(): return redirect('/ptt') @app.route('/ptt') def pttSententree(): return render_template('ptt.html', title="PTT Sententree") @app.route('/ptt/keywordFrequency', methods=['POST']) def findKeywordFrequency(): print(request.json) content = request.json result = ptt.findKeywordFrequency(content) return jsonify(Result=result) @app.route('/dev/updateContent') def updateContent(): ptt.loadPostContents() return jsonify(Result={ 'done': True }) @app.route('/askProgressHandler', methods=['POST']) def askProgressHandler(): key = request.json['key'] randId = ''.join(random.choices( string.ascii_uppercase + string.ascii_lowercase, k=15)) threading.Thread(target=ptt.progressListener, args=(key, eventQueue, randId,)).start() return jsonify(Result={ 'id': randId }) @app.route('/addRequest', methods=['POST']) def addRequest(): content = request.json ready = False info = None files = ptt.findResult(content) info = files ready = True key = ptt.calcKey(content['startDate'], content["endDate"], content["keyword"], content["pos"]) return jsonify(Result={ 'ready': ready, 'info': info, 'key': key }) @app.route("/init", methods=['POST']) def initPage(): startDate = datetime.today().replace(day=1).strftime('%Y-%m-%d') endDate = datetime.today().strftime('%Y-%m-%d') files = ptt.getDefault(startDate, endDate) return jsonify(Result={ "startDate": startDate, 'endDate': endDate, 'keyword': '', 'info': files }) if __name__ == "__main__": app.run(debug=False, port=4980, host='0.0.0.0', threaded=True)