Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.json.gz
*.txt
*.index
*.model
*.projection
*.mm
*.dict
84 changes: 80 additions & 4 deletions webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion webapp/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h1 class="heading">Card Codex</h1>
<div class="card-title">
<h2 class="card-name">{{target_card.name}}</h2>
<ul class="card-links list-inline">
<li><a href="https://scryfall.com/search?q=!{{target_card.name | urlencode}}">Scryfall</a></li>
<li><a href="https://scryfall.com/search?q={{target_card.name | urlencode}}">Scryfall</a></li>
</ul>
</div>
<p>{{target_card.manaCost | mana}} &middot; {{target_card.type}}</p>
Expand Down