From d05ab59b47a25432672cfe5b7ba53f3cb1a6d6cb Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 3 Jan 2021 21:41:50 +0300 Subject: initial commit --- app/__init__.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 app/__init__.py (limited to 'app/__init__.py') diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..8ee21b9 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,91 @@ +import os +import time + +from . import acmespb +from flask import Flask, render_template +from flask_socketio import SocketIO, emit + +socketio = SocketIO() + + +def create_app(test_config=None): + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping( + SECRET_KEY='dev', + DATABASE=os.path.join(app.instance_path, 'app.sqlite'), + ) + + if test_config is None: + # load the instance config, if it exists, when not testing + app.config.from_pyfile('config.py', silent=True) + else: + # load the test config if passed in + app.config.from_mapping(test_config) + + # ensure the instance folder exists + try: + os.makedirs(app.instance_path) + except OSError: + pass + + socketio.init_app(app) + + @app.route('/') + def hello(): + return render_template('index.html') + + @socketio.on('get_hints') + def handle_get_hints_event(q): + print('[get_hints] id=%d, query=%s' % (q['id'], q['query'])) + if len(q['query']) < 3: + response = { + 'id': q['id'], + 'error': "query is too short" + } + emit('hints', response) + return + results = acmespb.search(q['query']) + response = { + 'id': q['id'], + 'response': results + } + emit('hints', response) + + @socketio.on('get_offers') + def handle_get_offers_event(q): + print('[get_offers] id=%d, query=%s' % (q['id'], q['query'])) + target_url, trade_names = acmespb.trade_names(q['query']) + if trade_names: + response = { + 'id': q['id'], + "response": trade_names + } + emit('hints', response) + return + + page = 1 + pages = 0 + target_url = None + while pages == 0 or page <= pages: + target_url, pages, offers = acmespb.offers(q['query'], page=page, target_url=target_url) + print("[%d] pages=%d, target_url=%s" % (page, pages, target_url)) + response = { + 'id': q['id'], + 'offers': [offer.as_dict() for offer in offers], + 'page': page, + 'pages': pages + } + emit('offers', response) + + time.sleep(0.5) + page += 1 + + response = { + 'id': q['id'], + 'end': True + } + emit('offers', response) + + # TODO empty response + + return app -- cgit v1.2.3