cce-search-prototype/cce_search/search.py

181 lines
5.9 KiB
Python
Raw Permalink Normal View History

2019-07-13 21:21:31 +00:00
from cce_search.api import search, reg_search, ren_search, registration, renewal
from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for
)
import re
from urllib.parse import urlparse, parse_qs, parse_qsl, urlunparse, urlencode
from werkzeug.exceptions import abort
from requests import HTTPError
bp = Blueprint('search', __name__)
@bp.route('/')
def index():
results = None
title = None
2019-07-13 21:21:31 +00:00
paging=None
2020-02-24 23:55:45 +00:00
sentTitle = None
2019-07-13 21:21:31 +00:00
search_type = "ft"
2020-02-26 02:32:00 +00:00
arguments = request.args.get("title") or request.args.get("renewal") or request.args.get("registration") or request.args.get("author") or request.args.get("publisher")
2020-03-05 22:04:36 +00:00
#print("TEST HERE")
#print(request.args)
#print(request.args.get("title"))
print("----------------------------------------------------------")
2020-04-15 03:32:36 +00:00
unique = 0
2020-02-26 02:32:00 +00:00
if not arguments:
print("NO ARGUMENTS GIVEN. PLEASE GIVE ARGUMENTS")
else:
2020-02-24 23:55:45 +00:00
if request.args.get("renewal"):
2020-04-15 03:32:36 +00:00
results = ren_search(request.args['renewal'], request.args.get('page'),
2020-02-24 23:55:45 +00:00
request.args.get('per_page'))
2020-04-15 03:32:36 +00:00
paging = proc_pagination(results['data']['paging'], request.args.get('page'))
unique = 1
if request.args.get("registration") and unique == 0:
results = reg_search(request.args['registration'], request.args.get('page'),
2020-02-24 23:55:45 +00:00
request.args.get('per_page'))
2020-04-15 03:32:36 +00:00
paging = proc_pagination(results['data']['paging'], request.args.get('page'))
unique = 1 #technically not 100% unique but should be very simplified for now
if request.args.get("title") and unique == 0:
title = request.args['title']
results = search(title, request.args.get('page'),
request.args.get('per_page'))
paging = proc_pagination(results['data']['paging'],
request.args.get('page'))
2020-02-10 18:10:26 +00:00
2020-04-15 03:32:36 +00:00
if request.args.get("author") and unique == 0:
results = search(request.args['author'], request.args.get('page'),
2020-02-24 23:55:45 +00:00
request.args.get('per_page'))
2020-04-15 03:32:36 +00:00
paging = proc_pagination(results['data']['paging'],
request.args.get('page'))
if request.args.get("publisher") and unique == 0:
results = search(request.args['publisher'], request.args.get('page'),
2020-02-24 23:55:45 +00:00
request.args.get('per_page'))
2020-04-15 03:32:36 +00:00
paging = proc_pagination(results['data']['paging'],
request.args.get('page'))
#paging = proc_pagination(results['data']['paging'],
# request.args.get('page'))
2020-02-26 02:32:00 +00:00
print("PRINTING PAGING HERE")
2020-04-15 03:32:36 +00:00
print(paging)
results = proc_results(results)
print(results)
2020-04-15 05:15:02 +00:00
if results == []:
print("NO RESULTS")
noresults = 1
return render_template('search/index.html', noresults=noresults)
2020-03-05 22:04:36 +00:00
return render_template('search/index.html', results=results, term=title,
2020-02-26 02:32:00 +00:00
paging=paging, search_type=search_type)
2019-07-13 21:21:31 +00:00
def proc_results(r):
return [enhance_results(res) for res in r['data']['results']]
def enhance_results(r):
if r.get('type') == 'renewal':
2019-07-13 21:21:31 +00:00
return r
2020-03-05 21:24:10 +00:00
return {**r, **{'original': strip_tags(r.get('xml')),
2019-08-13 22:43:31 +00:00
'is_post_1963': is_post_1963(r.get('registrations')),
2019-08-14 03:06:29 +00:00
'is_foreign': is_foreign(r.get('registrations')),
'is_interim': is_interim(r.get('registrations')),
2020-03-05 21:24:10 +00:00
'source_url': ia_url(r.get('source', {}))}}
2019-07-13 21:21:31 +00:00
def strip_tags(xml):
if xml:
return re.sub(r"</?.+?>", "", xml).replace("\n", "")
return ""
2020-03-05 21:24:10 +00:00
def ia_url(src):
2019-07-13 21:21:31 +00:00
#return src
return "{}#page/{:d}/mode/1up".format(ia_stream(src.get('url', '')),
2019-07-13 21:21:31 +00:00
src.get('page', 0))
def ia_stream(url):
return url.replace('details', 'stream')
2019-08-13 22:43:31 +00:00
def is_post_1963(regs):
return any([r['date'] > '1963' for r in regs])
2019-08-14 03:06:29 +00:00
def is_foreign(regs):
return any([r['number'][:2] == 'AF' for r in regs])
def is_interim(regs):
return any([r['number'][:2] == 'AI' for r in regs])
2019-07-13 21:21:31 +00:00
def proc_pagination(pg, current):
if not pg['next'] and not pg['previous']:
return {**pg, **{'has_pages': False}}
per_page = extract_per_page(pg)
if current is None:
current = 1
else:
current = int(current) + 1
return {**pg, **{'has_pages': True,
'current_page': current,
'last_page': extract_last(pg),
'pages': dict([(p, extract_pg(pg.get(p), per_page))
for p in ['first', 'next', 'last',
'previous']])}}
def extract_pg(pg, per_page):
if pg:
oq = dict(parse_qsl(urlparse(pg).query))
t = urlparse(request.url)
return urlunparse(
t._replace(query=urlencode({**dict(parse_qsl(t.query)),
**{'page': oq['page'],
'per_page': oq['per_page']}})))
return None
def extract_per_page(pg):
return [parse_qs(urlparse(v).query)["per_page"][0]
for v in pg.values() if v][0]
def extract_last(pg):
last = pg.get('last')
if last is None:
return "1"
else:
return int(parse_qs(urlparse(last).query)["page"][0]) + 1
@bp.route('/cceid/<cceid>')
def cceid(cceid):
try:
results = registration(cceid)
2019-07-14 15:09:05 +00:00
return render_template('search/cceid.html', result=results["data"])
2019-07-13 21:21:31 +00:00
except HTTPError:
try:
results = renewal(cceid)
2019-07-14 15:09:05 +00:00
return render_template('search/cceid.html',
result=results["data"][0])
2019-07-13 21:21:31 +00:00
except HTTPError:
pass
2019-07-14 15:09:05 +00:00
return render_template('search/cceid.html', result=None, error=1)
2019-07-13 21:21:31 +00:00