2020-03-29 07:05:09 +00:00
|
|
|
''' handle reading a csv from goodreads '''
|
2020-03-25 12:29:21 +00:00
|
|
|
import re
|
|
|
|
import csv
|
|
|
|
import itertools
|
2020-04-15 11:13:38 +00:00
|
|
|
import dateutil.parser
|
2020-03-25 12:29:21 +00:00
|
|
|
|
|
|
|
from fedireads import books_manager
|
2020-04-15 11:13:38 +00:00
|
|
|
from fedireads.models import Edition, ReadThrough
|
2020-03-25 12:29:21 +00:00
|
|
|
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-03-25 12:58:27 +00:00
|
|
|
# Mapping goodreads -> fedireads shelf titles.
|
|
|
|
GOODREADS_SHELVES = {
|
2020-03-29 07:05:09 +00:00
|
|
|
'read': 'read',
|
|
|
|
'currently-reading': 'reading',
|
|
|
|
'to-read': 'to-read',
|
2020-03-25 12:58:27 +00:00
|
|
|
}
|
2020-03-29 07:05:09 +00:00
|
|
|
# TODO: remove or notify about this in the UI
|
2020-03-25 14:14:38 +00:00
|
|
|
MAX_ENTRIES = 20
|
2020-03-25 12:58:27 +00:00
|
|
|
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-03-25 12:29:21 +00:00
|
|
|
def unquote_string(text):
|
2020-03-29 07:05:09 +00:00
|
|
|
''' resolve csv quote weirdness '''
|
2020-03-25 12:29:21 +00:00
|
|
|
match = re.match(r'="([^"]*)"', text)
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
2020-04-01 13:58:30 +00:00
|
|
|
return text
|
2020-03-25 12:29:21 +00:00
|
|
|
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-03-25 12:29:21 +00:00
|
|
|
def construct_search_term(title, author):
|
2020-03-29 07:05:09 +00:00
|
|
|
''' formulate a query for the data connector '''
|
2020-03-25 12:29:21 +00:00
|
|
|
# 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])
|
|
|
|
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-04-01 13:58:30 +00:00
|
|
|
class GoodreadsCsv:
|
2020-03-29 07:05:09 +00:00
|
|
|
''' define a goodreads csv '''
|
2020-03-25 12:29:21 +00:00
|
|
|
def __init__(self, csv_file):
|
|
|
|
self.reader = csv.DictReader(csv_file)
|
|
|
|
|
|
|
|
def __iter__(self):
|
2020-03-25 14:14:38 +00:00
|
|
|
for line in itertools.islice(self.reader, MAX_ENTRIES):
|
2020-04-12 12:54:10 +00:00
|
|
|
yield GoodreadsItem(line)
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-04-01 13:58:30 +00:00
|
|
|
class GoodreadsItem:
|
2020-03-29 07:05:09 +00:00
|
|
|
''' a processed line in a goodreads csv '''
|
2020-03-25 12:29:21 +00:00
|
|
|
def __init__(self, line):
|
|
|
|
self.line = line
|
|
|
|
self.book = None
|
|
|
|
|
|
|
|
def resolve(self):
|
2020-03-29 07:05:09 +00:00
|
|
|
''' try various ways to lookup a book '''
|
2020-04-12 12:54:59 +00:00
|
|
|
self.book = (
|
|
|
|
self.get_book_from_db_isbn() or
|
|
|
|
self.get_book_from_isbn() or
|
|
|
|
self.get_book_from_title_author()
|
|
|
|
)
|
2020-03-25 12:29:21 +00:00
|
|
|
|
2020-04-12 12:54:59 +00:00
|
|
|
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
|
2020-03-29 07:05:09 +00:00
|
|
|
|
2020-03-25 12:29:21 +00:00
|
|
|
def get_book_from_isbn(self):
|
2020-03-29 07:05:09 +00:00
|
|
|
''' search by isbn '''
|
2020-04-12 12:54:59 +00:00
|
|
|
search_results = books_manager.search(self.isbn)
|
2020-03-25 12:29:21 +00:00
|
|
|
if search_results:
|
|
|
|
return books_manager.get_or_create_book(search_results[0].key)
|
|
|
|
|
|
|
|
def get_book_from_title_author(self):
|
2020-03-29 07:05:09 +00:00
|
|
|
''' search by title and author '''
|
|
|
|
search_term = construct_search_term(
|
|
|
|
self.line['Title'],
|
|
|
|
self.line['Author']
|
|
|
|
)
|
2020-03-25 12:29:21 +00:00
|
|
|
search_results = books_manager.search(search_term)
|
|
|
|
if search_results:
|
|
|
|
return books_manager.get_or_create_book(search_results[0].key)
|
|
|
|
|
2020-04-12 12:54:59 +00:00
|
|
|
@property
|
|
|
|
def isbn(self):
|
|
|
|
return unquote_string(self.line['ISBN13'])
|
|
|
|
|
2020-03-25 12:58:27 +00:00
|
|
|
@property
|
|
|
|
def shelf(self):
|
2020-03-29 07:05:09 +00:00
|
|
|
''' the goodreads shelf field '''
|
2020-03-25 12:58:27 +00:00
|
|
|
if self.line['Exclusive Shelf']:
|
|
|
|
return GOODREADS_SHELVES[self.line['Exclusive Shelf']]
|
|
|
|
|
2020-04-01 14:41:19 +00:00
|
|
|
@property
|
|
|
|
def review(self):
|
|
|
|
return self.line['My Review']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def rating(self):
|
|
|
|
return int(self.line['My Rating'])
|
|
|
|
|
2020-04-15 11:13:38 +00:00
|
|
|
@property
|
|
|
|
def date_added(self):
|
|
|
|
if self.line['Date Added']:
|
|
|
|
return dateutil.parser.parse(self.line['Date Added'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def date_read(self):
|
|
|
|
if self.line['Date Read']:
|
|
|
|
return dateutil.parser.parse(self.line['Date Read'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def reads(self):
|
|
|
|
return [ReadThrough(
|
|
|
|
# Date added isn't the start date, but it's (perhaps) better than nothing.
|
|
|
|
start_date=self.date_added,
|
|
|
|
finish_date=self.date_read,
|
|
|
|
pages_read=None,
|
|
|
|
)]
|
|
|
|
|
2020-03-25 12:29:21 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "<GoodreadsItem {!r}>".format(self.line['Title'])
|
2020-03-25 14:14:38 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{} by {}".format(self.line['Title'], self.line['Author'])
|