diff --git a/fedireads/templates/import_results.html b/fedireads/templates/import_results.html
new file mode 100644
index 00000000..73996cfa
--- /dev/null
+++ b/fedireads/templates/import_results.html
@@ -0,0 +1,28 @@
+{% extends 'layout.html' %}
+{% block content %}
+
+
+
Search results
+ {% for book in results %}
+
+ {% endfor %}
+
+
Failures
+
+ {% for book in failures %}
+ -
+ {{ book.Title }}
+ {{ book.Author }}
+
+ {% endfor %}
+
+
+
+{% endblock %}
diff --git a/fedireads/view_actions.py b/fedireads/view_actions.py
index ab63f923..a3e2c2e5 100644
--- a/fedireads/view_actions.py
+++ b/fedireads/view_actions.py
@@ -297,20 +297,49 @@ def unquote_string(text):
else:
return text
+def construct_search_term(title, author):
+ # Strip brackets (usually series title from search term)
+ title = re.sub(r'\s*\([^)]*\)\s*', '', title)
+ # Open library doesn't like including author initials in search term.
+ author = re.sub(r'(\w\.)+\s*', '', author)
+
+ return ' '.join([title, author])
+
import itertools
from io import TextIOWrapper
+from requests import HTTPError
@login_required
def import_data(request):
form = forms.ImportForm(request.POST, request.FILES)
if form.is_valid():
reader = csv.DictReader(TextIOWrapper(request.FILES['csv_file'], encoding=request.encoding))
- for line in itertools.islice(reader, 10):
+ results = []
+ failures = []
+ for line in itertools.islice(reader, 20):
isbn = unquote_string(line['ISBN13'])
print(line['Title'], isbn, line['Exclusive Shelf'])
search_results = books_manager.search(isbn)
if search_results:
book = books_manager.get_or_create_book(search_results[0].key)
- return HttpResponse('thanks')
+ print(book)
+ results.append(book)
+ else:
+ try:
+ search_term = construct_search_term(line['Title'], line['Author'])
+ print("Search term: ", search_term)
+ search_results = books_manager.search(search_term)
+ if search_results:
+ book = books_manager.get_or_create_book(search_results[0].key)
+ print(book)
+ results.append(book)
+ else:
+ failures.append(line)
+ except HTTPError:
+ failures.append(line) #
+ return TemplateResponse(request, 'import_results.html', {
+ 'results': results,
+ 'failures': failures
+ })
else:
return HttpResponseBadRequest()