commit
b2ccf22191
|
@ -2,9 +2,9 @@
|
||||||
import re
|
import re
|
||||||
import csv
|
import csv
|
||||||
import itertools
|
import itertools
|
||||||
from requests import HTTPError
|
|
||||||
|
|
||||||
from fedireads import books_manager
|
from fedireads import books_manager
|
||||||
|
from fedireads.models import Edition
|
||||||
|
|
||||||
|
|
||||||
# Mapping goodreads -> fedireads shelf titles.
|
# Mapping goodreads -> fedireads shelf titles.
|
||||||
|
@ -42,13 +42,7 @@ class GoodreadsCsv:
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for line in itertools.islice(self.reader, MAX_ENTRIES):
|
for line in itertools.islice(self.reader, MAX_ENTRIES):
|
||||||
entry = GoodreadsItem(line)
|
yield GoodreadsItem(line)
|
||||||
try:
|
|
||||||
entry.resolve()
|
|
||||||
except HTTPError:
|
|
||||||
pass
|
|
||||||
yield entry
|
|
||||||
|
|
||||||
|
|
||||||
class GoodreadsItem:
|
class GoodreadsItem:
|
||||||
''' a processed line in a goodreads csv '''
|
''' a processed line in a goodreads csv '''
|
||||||
|
@ -56,22 +50,27 @@ class GoodreadsItem:
|
||||||
self.line = line
|
self.line = line
|
||||||
self.book = None
|
self.book = None
|
||||||
|
|
||||||
|
|
||||||
def resolve(self):
|
def resolve(self):
|
||||||
''' try various ways to lookup a book '''
|
''' try various ways to lookup a book '''
|
||||||
self.book = self.get_book_from_isbn()
|
self.book = (
|
||||||
if not self.book:
|
self.get_book_from_db_isbn() or
|
||||||
self.book = self.get_book_from_title_author()
|
self.get_book_from_isbn() or
|
||||||
|
self.get_book_from_title_author()
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_book_from_db_isbn(self):
|
||||||
|
''' see if we already know about the book '''
|
||||||
|
try:
|
||||||
|
return Edition.objects.get(isbn=self.isbn)
|
||||||
|
except Edition.DoesNotExist:
|
||||||
|
return None
|
||||||
|
|
||||||
def get_book_from_isbn(self):
|
def get_book_from_isbn(self):
|
||||||
''' search by isbn '''
|
''' search by isbn '''
|
||||||
isbn = unquote_string(self.line['ISBN13'])
|
search_results = books_manager.search(self.isbn)
|
||||||
search_results = books_manager.search(isbn)
|
|
||||||
if search_results:
|
if search_results:
|
||||||
return books_manager.get_or_create_book(search_results[0].key)
|
return books_manager.get_or_create_book(search_results[0].key)
|
||||||
|
|
||||||
|
|
||||||
def get_book_from_title_author(self):
|
def get_book_from_title_author(self):
|
||||||
''' search by title and author '''
|
''' search by title and author '''
|
||||||
search_term = construct_search_term(
|
search_term = construct_search_term(
|
||||||
|
@ -82,6 +81,10 @@ class GoodreadsItem:
|
||||||
if search_results:
|
if search_results:
|
||||||
return books_manager.get_or_create_book(search_results[0].key)
|
return books_manager.get_or_create_book(search_results[0].key)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def isbn(self):
|
||||||
|
return unquote_string(self.line['ISBN13'])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def shelf(self):
|
def shelf(self):
|
||||||
''' the goodreads shelf field '''
|
''' the goodreads shelf field '''
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
from io import BytesIO, TextIOWrapper
|
from io import BytesIO, TextIOWrapper
|
||||||
import re
|
import re
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from requests import HTTPError
|
||||||
|
|
||||||
from django.contrib.auth import authenticate, login, logout
|
from django.contrib.auth import authenticate, login, logout
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
@ -424,6 +425,10 @@ def import_data(request):
|
||||||
for item in GoodreadsCsv(TextIOWrapper(
|
for item in GoodreadsCsv(TextIOWrapper(
|
||||||
request.FILES['csv_file'],
|
request.FILES['csv_file'],
|
||||||
encoding=request.encoding)):
|
encoding=request.encoding)):
|
||||||
|
try:
|
||||||
|
item.resolve()
|
||||||
|
except HttpError:
|
||||||
|
pass
|
||||||
if item.book:
|
if item.book:
|
||||||
results.append(item)
|
results.append(item)
|
||||||
if item.rating or item.review:
|
if item.rating or item.review:
|
||||||
|
|
Loading…
Reference in New Issue