From 6cc71d440c3f9e3953157a16b16e14ba3846d092 Mon Sep 17 00:00:00 2001 From: Christopher Chen Date: Fri, 21 Jul 2017 01:12:24 -0700 Subject: [PATCH 1/3] begin implementing api --- webapp/.gitignore | 7 ++++ webapp/app.py | 84 ++++++++++++++++++++++++++++++++++++-- webapp/templates/home.html | 2 +- 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 webapp/.gitignore diff --git a/webapp/.gitignore b/webapp/.gitignore new file mode 100644 index 0000000..21b150d --- /dev/null +++ b/webapp/.gitignore @@ -0,0 +1,7 @@ +*.json.gz +*.txt +*.index +*.model +*.projection +*.mm +*.dict diff --git a/webapp/app.py b/webapp/app.py index ee11e13..7723fa1 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -5,7 +5,7 @@ import re import os -from flask import Flask, render_template, redirect, request, Response +from flask import Flask, render_template, redirect, request, Response, abort app = Flask(__name__) from gensim import corpora, models, similarities @@ -32,17 +32,21 @@ def hashed_url_for_static_file(endpoint, values): param_name = 'h' while param_name in values: param_name = '_' + param_name - values[param_name] = static_file_hash(os.path.join(static_folder, filename)) + values[param_name] = static_file_hash( + os.path.join(static_folder, filename)) + def static_file_hash(filename): - return int(os.stat(filename).st_mtime) + return int(os.stat(filename).st_mtime) + +# Routes -## Routes @app.template_filter('mana') def manafy(s): return re.sub('[{}\s]+', '', s or '') + @app.template_filter('params') def params(d): def vals(): @@ -53,8 +57,70 @@ def vals(): return '&' + s if s else '' +def request_context(request): + """Extract context from request args.""" + context = {} + N = 10 + + try: + page = int(request.args.get('page')) + except (TypeError, ValueError): + page = 1 + context['page'] = page + offset = N * (page - 1) + + filters = {} + if request.args.get('ci'): + filters['ci'] = request.args.getlist('ci') + if filters: + context['filters'] = filters + + card_name = request.args.get('card') + card_text = request.args.get('text') + if card_name: + try: + context['search_type'] = 'card' + target_card = sim.get_card_by_name(card_name) + context['target_card'] = target_card + app.logger.debug('%s: %s' % (card_name, tokenize(target_card))) + context['similar_cards'] = sim.get_similar_cards( + card_name, N, offset, filters) + except Exception as e: + if app.debug: + raise e + msg = 'Card name not found. Please try again.' + return render_template('home.html', error=msg), 404 + + elif card_text: + context['search_type'] = 'text' + try: + context['target_card'] = {'name': card_text} + context['similar_cards'] = sim.text_search_similar_cards( + card_text, N, offset, filters) + except Exception as e: + if app.debug: + raise e + msg = ('Unable to search related cards.' + ' Try rephrasing or expanding your query.') + return render_template('home.html', error=msg), 404 + return context + + @app.route('/') def home(): + + try: + return render_template('home.html', **request_context(request)) + except KeyError: + msg = 'Card name not found. Please try again.' + return render_template('home.html', error=msg), 404 + except Exception as e: + if app.debug: + raise e + msg = ('Unable to search related cards.' + ' Try rephrasing or expanding your query.') + return render_template('home.html', error=msg), 404 + context = {} N = 10 @@ -102,6 +168,16 @@ def home(): return render_template('home.html', **context) + +@app.route("/api/") +def api_search(): + try: + + return json.dumps(request_context(request)) + except KeyError: + abort(404) + + @app.route('/random') def random_card(): card = random.choice(sim.cards) diff --git a/webapp/templates/home.html b/webapp/templates/home.html index 194db94..b24778b 100644 --- a/webapp/templates/home.html +++ b/webapp/templates/home.html @@ -54,7 +54,7 @@

Card Codex

{{target_card.name}}

{{target_card.manaCost | mana}} · {{target_card.type}}

From 612a8d208a10faea9a347937a077ec3cb3e8b65b Mon Sep 17 00:00:00 2001 From: Christopher Chen Date: Sun, 1 Mar 2020 14:54:22 -0800 Subject: [PATCH 2/3] Revert scryfall url change --- webapp/templates/home.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/templates/home.html b/webapp/templates/home.html index b24778b..194db94 100644 --- a/webapp/templates/home.html +++ b/webapp/templates/home.html @@ -54,7 +54,7 @@

Card Codex

{{target_card.name}}

{{target_card.manaCost | mana}} · {{target_card.type}}

From fb4745a648efbc80bd6487ddbbcfe1de4fbbf528 Mon Sep 17 00:00:00 2001 From: Christopher Chen Date: Sun, 1 Mar 2020 15:00:02 -0800 Subject: [PATCH 3/3] Unrevert template change to fix scryfall searches for names with spaces. For example, River Hoopoe wasn't working. --- webapp/templates/home.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/templates/home.html b/webapp/templates/home.html index 194db94..b24778b 100644 --- a/webapp/templates/home.html +++ b/webapp/templates/home.html @@ -54,7 +54,7 @@

Card Codex

{{target_card.name}}

{{target_card.manaCost | mana}} · {{target_card.type}}