2019-12-17 18:00:35 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 shavitmichael, OzzieIsaacs
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-03-13 00:31:42 +01:00
|
|
|
import base64
|
2020-02-15 23:27:07 +01:00
|
|
|
import datetime
|
2020-02-18 20:36:32 +01:00
|
|
|
import os
|
2019-12-17 18:00:35 +01:00
|
|
|
import uuid
|
|
|
|
from time import gmtime, strftime
|
2021-12-01 20:29:05 +01:00
|
|
|
import json
|
2021-12-26 10:31:04 +01:00
|
|
|
from urllib.parse import unquote
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2019-12-30 15:16:09 +01:00
|
|
|
from flask import (
|
|
|
|
Blueprint,
|
|
|
|
request,
|
|
|
|
make_response,
|
|
|
|
jsonify,
|
2020-01-26 14:42:53 +01:00
|
|
|
current_app,
|
2019-12-30 15:16:09 +01:00
|
|
|
url_for,
|
|
|
|
redirect,
|
2020-01-26 16:52:40 +01:00
|
|
|
abort
|
2019-12-30 15:16:09 +01:00
|
|
|
)
|
2020-10-27 11:06:43 +01:00
|
|
|
from flask_login import current_user
|
2019-12-30 15:16:09 +01:00
|
|
|
from werkzeug.datastructures import Headers
|
2019-12-08 01:48:52 +01:00
|
|
|
from sqlalchemy import func
|
2020-03-13 00:59:05 +01:00
|
|
|
from sqlalchemy.sql.expression import and_, or_
|
2020-04-25 14:56:51 +02:00
|
|
|
from sqlalchemy.exc import StatementError
|
2021-07-11 08:28:45 +02:00
|
|
|
from sqlalchemy.sql import select
|
2019-12-30 15:16:09 +01:00
|
|
|
import requests
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
|
2021-10-09 14:57:23 +02:00
|
|
|
from . import config, logger, kobo_auth, db, calibre_db, helper, shelf as shelf_lib, ub, csrf, kobo_sync_status
|
2021-07-26 07:52:01 +02:00
|
|
|
from .constants import sqlalchemy_version2
|
2020-10-31 14:50:08 +01:00
|
|
|
from .helper import get_download_link
|
2020-01-20 06:14:53 +01:00
|
|
|
from .services import SyncToken as SyncToken
|
2019-12-17 18:00:35 +01:00
|
|
|
from .web import download_required
|
2020-10-31 14:50:08 +01:00
|
|
|
from .kobo_auth import requires_kobo_auth, get_auth_token
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2020-12-05 10:03:31 +01:00
|
|
|
KOBO_FORMATS = {"KEPUB": ["KEPUB"], "EPUB": ["EPUB3", "EPUB"]}
|
2019-12-30 15:16:09 +01:00
|
|
|
KOBO_STOREAPI_URL = "https://storeapi.kobo.com"
|
2020-04-04 06:57:54 +02:00
|
|
|
KOBO_IMAGEHOST_URL = "https://kbimages1-a.akamaihd.net"
|
2019-12-20 07:28:53 +01:00
|
|
|
|
2021-01-04 18:45:25 +01:00
|
|
|
SYNC_ITEM_LIMIT = 100
|
2020-12-06 13:21:25 +01:00
|
|
|
|
2019-12-20 07:04:12 +01:00
|
|
|
kobo = Blueprint("kobo", __name__, url_prefix="/kobo/<auth_token>")
|
2019-12-17 18:00:35 +01:00
|
|
|
kobo_auth.disable_failed_auth_redirect_for_blueprint(kobo)
|
2019-12-20 06:47:29 +01:00
|
|
|
kobo_auth.register_url_value_preprocessor(kobo)
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
|
2020-01-24 06:04:16 +01:00
|
|
|
|
2019-12-30 15:16:09 +01:00
|
|
|
def get_store_url_for_current_request():
|
|
|
|
# Programmatically modify the current url to point to the official Kobo store
|
2020-04-27 20:01:13 +02:00
|
|
|
__, __, request_path_with_auth_token = request.full_path.rpartition("/kobo/")
|
|
|
|
__, __, request_path = request_path_with_auth_token.rstrip("?").partition(
|
2019-12-30 15:16:09 +01:00
|
|
|
"/"
|
|
|
|
)
|
|
|
|
return KOBO_STOREAPI_URL + "/" + request_path
|
|
|
|
|
|
|
|
|
|
|
|
CONNECTION_SPECIFIC_HEADERS = [
|
|
|
|
"connection",
|
|
|
|
"content-encoding",
|
|
|
|
"content-length",
|
|
|
|
"transfer-encoding",
|
|
|
|
]
|
|
|
|
|
2021-02-06 20:29:43 +01:00
|
|
|
|
2020-01-27 20:32:37 +01:00
|
|
|
def get_kobo_activated():
|
|
|
|
return config.config_kobo_sync
|
2019-12-30 15:16:09 +01:00
|
|
|
|
2020-02-17 20:37:02 +01:00
|
|
|
|
|
|
|
def make_request_to_kobo_store(sync_token=None):
|
|
|
|
outgoing_headers = Headers(request.headers)
|
|
|
|
outgoing_headers.remove("Host")
|
|
|
|
if sync_token:
|
|
|
|
sync_token.set_kobo_store_header(outgoing_headers)
|
|
|
|
|
|
|
|
store_response = requests.request(
|
|
|
|
method=request.method,
|
|
|
|
url=get_store_url_for_current_request(),
|
|
|
|
headers=outgoing_headers,
|
|
|
|
data=request.get_data(),
|
|
|
|
allow_redirects=False,
|
|
|
|
timeout=(2, 10)
|
|
|
|
)
|
2021-12-01 20:29:05 +01:00
|
|
|
log.debug("Content: " + str(store_response.content))
|
|
|
|
log.debug("StatusCode: " + str(store_response.status_code))
|
2020-02-17 20:37:02 +01:00
|
|
|
return store_response
|
|
|
|
|
|
|
|
|
|
|
|
def redirect_or_proxy_request():
|
|
|
|
if config.config_kobo_proxy:
|
2020-01-26 16:52:40 +01:00
|
|
|
if request.method == "GET":
|
|
|
|
return redirect(get_store_url_for_current_request(), 307)
|
|
|
|
else:
|
2021-12-01 20:29:05 +01:00
|
|
|
# The Kobo device turns other request types into GET requests on redirects,
|
|
|
|
# so we instead proxy to the Kobo store ourselves.
|
2020-02-17 20:37:02 +01:00
|
|
|
store_response = make_request_to_kobo_store()
|
2019-12-30 15:16:09 +01:00
|
|
|
|
2020-01-26 16:52:40 +01:00
|
|
|
response_headers = store_response.headers
|
|
|
|
for header_key in CONNECTION_SPECIFIC_HEADERS:
|
|
|
|
response_headers.pop(header_key, default=None)
|
2019-12-30 15:16:09 +01:00
|
|
|
|
2020-01-26 16:52:40 +01:00
|
|
|
return make_response(
|
|
|
|
store_response.content, store_response.status_code, response_headers.items()
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return make_response(jsonify({}))
|
2019-12-30 15:16:09 +01:00
|
|
|
|
2020-02-17 20:37:02 +01:00
|
|
|
|
2020-02-15 23:27:07 +01:00
|
|
|
def convert_to_kobo_timestamp_string(timestamp):
|
2020-11-07 12:49:39 +01:00
|
|
|
try:
|
|
|
|
return timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
except AttributeError as exc:
|
|
|
|
log.debug("Timestamp not valid: {}".format(exc))
|
|
|
|
return datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
|
2020-02-15 23:27:07 +01:00
|
|
|
|
2020-03-13 00:31:42 +01:00
|
|
|
|
2019-12-17 18:00:35 +01:00
|
|
|
@kobo.route("/v1/library/sync")
|
2020-02-16 14:25:15 +01:00
|
|
|
@requires_kobo_auth
|
2019-12-17 18:00:35 +01:00
|
|
|
@download_required
|
|
|
|
def HandleSyncRequest():
|
2020-01-20 06:14:53 +01:00
|
|
|
sync_token = SyncToken.SyncToken.from_headers(request.headers)
|
2019-12-17 18:00:35 +01:00
|
|
|
log.info("Kobo library sync request received.")
|
2021-07-12 14:58:03 +02:00
|
|
|
log.debug("SyncToken: {}".format(sync_token))
|
2020-01-26 16:52:40 +01:00
|
|
|
if not current_app.wsgi_app.is_proxied:
|
2020-08-23 18:23:22 +02:00
|
|
|
log.debug('Kobo: Received unproxied request, changed request port to external server port')
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2021-12-12 20:31:45 +01:00
|
|
|
# if no books synced don't respect sync_token
|
|
|
|
if not ub.session.query(ub.KoboSyncedBooks).filter(ub.KoboSyncedBooks.user_id == current_user.id).count():
|
|
|
|
sync_token.books_last_modified = datetime.datetime.min
|
|
|
|
sync_token.books_last_created = datetime.datetime.min
|
|
|
|
sync_token.reading_state_last_modified = datetime.datetime.min
|
|
|
|
|
|
|
|
new_books_last_modified = sync_token.books_last_modified # needed for sync selected shelfs only
|
|
|
|
new_books_last_created = sync_token.books_last_created # needed to distinguish between new and changed entitlement
|
2020-02-15 23:27:07 +01:00
|
|
|
new_reading_state_last_modified = sync_token.reading_state_last_modified
|
2021-12-12 20:31:45 +01:00
|
|
|
|
2020-12-06 13:21:25 +01:00
|
|
|
new_archived_last_modified = datetime.datetime.min
|
2020-02-15 23:27:07 +01:00
|
|
|
sync_results = []
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2019-12-30 15:16:09 +01:00
|
|
|
# We reload the book database so that the user get's a fresh view of the library
|
|
|
|
# in case of external changes (e.g: adding a book through Calibre).
|
2020-05-21 22:26:06 +02:00
|
|
|
calibre_db.reconnect_db(config, ub.app_DB_path)
|
2019-12-30 15:16:09 +01:00
|
|
|
|
2021-05-15 10:45:51 +02:00
|
|
|
only_kobo_shelves = current_user.kobo_only_shelves_sync
|
2021-02-06 20:29:43 +01:00
|
|
|
|
2021-02-06 21:15:36 +01:00
|
|
|
if only_kobo_shelves:
|
2021-07-26 07:52:01 +02:00
|
|
|
if sqlalchemy_version2:
|
2021-07-11 08:28:45 +02:00
|
|
|
changed_entries = select(db.Books,
|
2021-02-08 20:10:12 +01:00
|
|
|
ub.ArchivedBook.last_modified,
|
|
|
|
ub.BookShelf.date_added,
|
|
|
|
ub.ArchivedBook.is_archived)
|
2021-07-11 08:28:45 +02:00
|
|
|
else:
|
|
|
|
changed_entries = calibre_db.session.query(db.Books,
|
|
|
|
ub.ArchivedBook.last_modified,
|
|
|
|
ub.BookShelf.date_added,
|
|
|
|
ub.ArchivedBook.is_archived)
|
|
|
|
changed_entries = (changed_entries
|
2021-02-08 20:10:12 +01:00
|
|
|
.join(db.Data).outerjoin(ub.ArchivedBook, db.Books.id == ub.ArchivedBook.book_id)
|
2021-10-03 09:53:46 +02:00
|
|
|
.join(ub.KoboSyncedBooks, ub.KoboSyncedBooks.book_id == db.Books.id, isouter=True)
|
|
|
|
.filter(or_(ub.KoboSyncedBooks.user_id != current_user.id,
|
|
|
|
ub.KoboSyncedBooks.book_id == None))
|
2021-10-10 10:23:58 +02:00
|
|
|
.filter(ub.BookShelf.date_added > sync_token.books_last_modified)
|
2021-10-03 09:53:46 +02:00
|
|
|
.filter(db.Data.format.in_(KOBO_FORMATS))
|
2021-10-10 10:23:58 +02:00
|
|
|
.filter(calibre_db.common_filters(allow_show_archived=True))
|
2021-02-08 20:10:12 +01:00
|
|
|
.order_by(db.Books.id)
|
2021-03-14 11:29:23 +01:00
|
|
|
.order_by(ub.ArchivedBook.last_modified)
|
2021-02-08 20:10:12 +01:00
|
|
|
.join(ub.BookShelf, db.Books.id == ub.BookShelf.book_id)
|
2021-02-06 20:29:43 +01:00
|
|
|
.join(ub.Shelf)
|
2021-07-10 17:09:04 +02:00
|
|
|
.filter(ub.Shelf.user_id == current_user.id)
|
2021-02-06 20:29:43 +01:00
|
|
|
.filter(ub.Shelf.kobo_sync)
|
|
|
|
.distinct()
|
2020-12-09 09:41:23 +01:00
|
|
|
)
|
|
|
|
else:
|
2021-07-26 07:52:01 +02:00
|
|
|
if sqlalchemy_version2:
|
2021-07-11 08:28:45 +02:00
|
|
|
changed_entries = select(db.Books, ub.ArchivedBook.last_modified, ub.ArchivedBook.is_archived)
|
|
|
|
else:
|
|
|
|
changed_entries = calibre_db.session.query(db.Books,
|
|
|
|
ub.ArchivedBook.last_modified,
|
|
|
|
ub.ArchivedBook.is_archived)
|
|
|
|
changed_entries = (changed_entries
|
2021-10-03 09:53:46 +02:00
|
|
|
.join(db.Data).outerjoin(ub.ArchivedBook, db.Books.id == ub.ArchivedBook.book_id)
|
|
|
|
.join(ub.KoboSyncedBooks, ub.KoboSyncedBooks.book_id == db.Books.id, isouter=True)
|
|
|
|
.filter(or_(ub.KoboSyncedBooks.user_id != current_user.id,
|
|
|
|
ub.KoboSyncedBooks.book_id == None))
|
|
|
|
.filter(calibre_db.common_filters())
|
|
|
|
.filter(db.Data.format.in_(KOBO_FORMATS))
|
|
|
|
.order_by(db.Books.last_modified)
|
|
|
|
.order_by(db.Books.id)
|
2020-12-09 09:41:23 +01:00
|
|
|
)
|
2020-01-26 14:42:53 +01:00
|
|
|
|
|
|
|
|
2020-02-15 23:27:07 +01:00
|
|
|
reading_states_in_new_entitlements = []
|
2021-07-26 07:52:01 +02:00
|
|
|
if sqlalchemy_version2:
|
2021-07-11 08:28:45 +02:00
|
|
|
books = calibre_db.session.execute(changed_entries.limit(SYNC_ITEM_LIMIT))
|
|
|
|
else:
|
|
|
|
books = changed_entries.limit(SYNC_ITEM_LIMIT)
|
2021-12-06 21:02:06 +01:00
|
|
|
log.debug("Books to Sync: {}".format(len(books.all())))
|
2021-07-11 08:28:45 +02:00
|
|
|
for book in books:
|
2021-01-30 14:47:18 +01:00
|
|
|
formats = [data.format for data in book.Books.data]
|
2020-09-01 18:22:49 +02:00
|
|
|
if not 'KEPUB' in formats and config.config_kepubifypath and 'EPUB' in formats:
|
2021-03-21 18:55:02 +01:00
|
|
|
helper.convert_book_format(book.Books.id, config.config_calibre_dir, 'EPUB', 'KEPUB', current_user.name)
|
2020-09-01 18:22:49 +02:00
|
|
|
|
2020-12-06 13:21:25 +01:00
|
|
|
kobo_reading_state = get_or_create_reading_state(book.Books.id)
|
2019-12-17 18:00:35 +01:00
|
|
|
entitlement = {
|
2020-12-06 13:21:25 +01:00
|
|
|
"BookEntitlement": create_book_entitlement(book.Books, archived=(book.is_archived == True)),
|
|
|
|
"BookMetadata": get_metadata(book.Books),
|
2019-12-17 18:00:35 +01:00
|
|
|
}
|
2020-02-15 23:27:07 +01:00
|
|
|
|
|
|
|
if kobo_reading_state.last_modified > sync_token.reading_state_last_modified:
|
2020-12-06 13:21:25 +01:00
|
|
|
entitlement["ReadingState"] = get_kobo_reading_state_response(book.Books, kobo_reading_state)
|
2020-02-15 23:27:07 +01:00
|
|
|
new_reading_state_last_modified = max(new_reading_state_last_modified, kobo_reading_state.last_modified)
|
2020-12-06 13:21:25 +01:00
|
|
|
reading_states_in_new_entitlements.append(book.Books.id)
|
2020-02-15 23:27:07 +01:00
|
|
|
|
2021-02-08 20:10:12 +01:00
|
|
|
ts_created = book.Books.timestamp
|
|
|
|
|
|
|
|
try:
|
|
|
|
ts_created = max(ts_created, book.date_added)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if ts_created > sync_token.books_last_created:
|
2020-02-15 23:27:07 +01:00
|
|
|
sync_results.append({"NewEntitlement": entitlement})
|
2019-12-17 18:00:35 +01:00
|
|
|
else:
|
2020-02-15 23:27:07 +01:00
|
|
|
sync_results.append({"ChangedEntitlement": entitlement})
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
new_books_last_modified = max(
|
2020-12-06 13:21:25 +01:00
|
|
|
book.Books.last_modified, new_books_last_modified
|
2019-12-17 18:00:35 +01:00
|
|
|
)
|
2021-02-08 20:10:12 +01:00
|
|
|
try:
|
|
|
|
new_books_last_modified = max(
|
|
|
|
new_books_last_modified, book.date_added
|
|
|
|
)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
new_books_last_created = max(ts_created, new_books_last_created)
|
2021-12-01 20:29:05 +01:00
|
|
|
kobo_sync_status.add_synced_books(book.Books.id)
|
2020-12-06 13:21:25 +01:00
|
|
|
|
2021-07-26 07:52:01 +02:00
|
|
|
if sqlalchemy_version2:
|
2021-07-11 08:28:45 +02:00
|
|
|
max_change = calibre_db.session.execute(changed_entries
|
|
|
|
.filter(ub.ArchivedBook.is_archived)
|
|
|
|
.order_by(func.datetime(ub.ArchivedBook.last_modified).desc()))\
|
|
|
|
.columns(db.Books).first()
|
|
|
|
else:
|
|
|
|
max_change = changed_entries.from_self().filter(ub.ArchivedBook.is_archived) \
|
|
|
|
.order_by(func.datetime(ub.ArchivedBook.last_modified).desc()).first()
|
2021-05-02 10:05:25 +02:00
|
|
|
|
|
|
|
max_change = max_change.last_modified if max_change else new_archived_last_modified
|
|
|
|
|
2020-12-06 13:21:25 +01:00
|
|
|
new_archived_last_modified = max(new_archived_last_modified, max_change)
|
|
|
|
|
|
|
|
# no. of books returned
|
2021-07-26 07:52:01 +02:00
|
|
|
if sqlalchemy_version2:
|
2021-07-11 08:28:45 +02:00
|
|
|
entries = calibre_db.session.execute(changed_entries).all()
|
|
|
|
book_count = len(entries)
|
|
|
|
else:
|
|
|
|
book_count = changed_entries.count()
|
2020-12-06 13:21:25 +01:00
|
|
|
# last entry:
|
2021-10-16 11:28:35 +02:00
|
|
|
cont_sync = bool(book_count)
|
2021-10-03 09:53:46 +02:00
|
|
|
log.debug("Remaining books to Sync: {}".format(book_count))
|
2020-12-06 13:21:25 +01:00
|
|
|
# generate reading state data
|
2021-03-14 11:29:23 +01:00
|
|
|
changed_reading_states = ub.session.query(ub.KoboReadingState)
|
|
|
|
|
|
|
|
if only_kobo_shelves:
|
2021-05-02 10:05:25 +02:00
|
|
|
changed_reading_states = changed_reading_states.join(ub.BookShelf,
|
|
|
|
ub.KoboReadingState.book_id == ub.BookShelf.book_id)\
|
|
|
|
.join(ub.Shelf)\
|
2021-07-10 17:09:04 +02:00
|
|
|
.filter(current_user.id == ub.Shelf.user_id)\
|
2021-05-02 10:05:25 +02:00
|
|
|
.filter(ub.Shelf.kobo_sync,
|
|
|
|
or_(
|
2021-10-16 11:28:35 +02:00
|
|
|
ub.KoboReadingState.last_modified > sync_token.reading_state_last_modified,
|
2021-07-10 17:09:04 +02:00
|
|
|
func.datetime(ub.BookShelf.date_added) > sync_token.books_last_modified
|
2021-05-02 10:05:25 +02:00
|
|
|
)).distinct()
|
2021-03-14 11:29:23 +01:00
|
|
|
else:
|
2021-05-02 10:05:25 +02:00
|
|
|
changed_reading_states = changed_reading_states.filter(
|
2021-10-16 11:28:35 +02:00
|
|
|
ub.KoboReadingState.last_modified > sync_token.reading_state_last_modified)
|
2021-05-02 10:05:25 +02:00
|
|
|
|
|
|
|
changed_reading_states = changed_reading_states.filter(
|
|
|
|
and_(ub.KoboReadingState.user_id == current_user.id,
|
|
|
|
ub.KoboReadingState.book_id.notin_(reading_states_in_new_entitlements)))
|
2021-10-16 11:28:35 +02:00
|
|
|
cont_sync |= bool(changed_reading_states.count() > SYNC_ITEM_LIMIT)
|
|
|
|
for kobo_reading_state in changed_reading_states.limit(SYNC_ITEM_LIMIT).all():
|
2020-05-21 18:16:11 +02:00
|
|
|
book = calibre_db.session.query(db.Books).filter(db.Books.id == kobo_reading_state.book_id).one_or_none()
|
2020-03-13 00:31:42 +01:00
|
|
|
if book:
|
|
|
|
sync_results.append({
|
|
|
|
"ChangedReadingState": {
|
|
|
|
"ReadingState": get_kobo_reading_state_response(book, kobo_reading_state)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
new_reading_state_last_modified = max(new_reading_state_last_modified, kobo_reading_state.last_modified)
|
|
|
|
|
2021-05-02 10:05:25 +02:00
|
|
|
sync_shelves(sync_token, sync_results, only_kobo_shelves)
|
2020-02-15 23:27:07 +01:00
|
|
|
|
2021-12-12 20:31:45 +01:00
|
|
|
# update last created timestamp to distinguish between new and changed entitlements
|
|
|
|
if not cont_sync:
|
|
|
|
sync_token.books_last_created = new_books_last_created
|
2019-12-17 18:00:35 +01:00
|
|
|
sync_token.books_last_modified = new_books_last_modified
|
2020-01-26 05:54:12 +01:00
|
|
|
sync_token.archive_last_modified = new_archived_last_modified
|
2020-03-13 00:59:05 +01:00
|
|
|
sync_token.reading_state_last_modified = new_reading_state_last_modified
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2021-10-16 11:28:35 +02:00
|
|
|
return generate_sync_response(sync_token, sync_results, cont_sync)
|
2019-12-30 15:16:09 +01:00
|
|
|
|
|
|
|
|
2020-12-06 13:21:25 +01:00
|
|
|
def generate_sync_response(sync_token, sync_results, set_cont=False):
|
2020-02-17 20:37:02 +01:00
|
|
|
extra_headers = {}
|
2021-12-27 09:31:32 +01:00
|
|
|
if config.config_kobo_proxy and not set_cont:
|
2020-02-17 20:37:02 +01:00
|
|
|
# Merge in sync results from the official Kobo store.
|
|
|
|
try:
|
|
|
|
store_response = make_request_to_kobo_store(sync_token)
|
2020-01-26 16:52:40 +01:00
|
|
|
|
2020-02-15 23:27:07 +01:00
|
|
|
store_sync_results = store_response.json()
|
|
|
|
sync_results += store_sync_results
|
2020-02-17 20:37:02 +01:00
|
|
|
sync_token.merge_from_store_response(store_response)
|
|
|
|
extra_headers["x-kobo-sync"] = store_response.headers.get("x-kobo-sync")
|
|
|
|
extra_headers["x-kobo-sync-mode"] = store_response.headers.get("x-kobo-sync-mode")
|
|
|
|
extra_headers["x-kobo-recent-reads"] = store_response.headers.get("x-kobo-recent-reads")
|
2020-01-26 16:52:40 +01:00
|
|
|
|
2021-04-04 19:40:34 +02:00
|
|
|
except Exception as ex:
|
|
|
|
log.error("Failed to receive or parse response from Kobo's sync endpoint: {}".format(ex))
|
2020-12-06 13:21:25 +01:00
|
|
|
if set_cont:
|
|
|
|
extra_headers["x-kobo-sync"] = "continue"
|
2020-02-17 20:37:02 +01:00
|
|
|
sync_token.to_headers(extra_headers)
|
|
|
|
|
2021-12-01 20:29:05 +01:00
|
|
|
log.debug("Kobo Sync Content: {}".format(sync_results))
|
|
|
|
# jsonify decodes the unicode string different to what kobo expects
|
|
|
|
response = make_response(json.dumps(sync_results), extra_headers)
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
2019-12-17 18:00:35 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@kobo.route("/v1/library/<book_uuid>/metadata")
|
2020-02-16 14:25:15 +01:00
|
|
|
@requires_kobo_auth
|
2019-12-17 18:00:35 +01:00
|
|
|
@download_required
|
|
|
|
def HandleMetadataRequest(book_uuid):
|
2020-01-26 16:52:40 +01:00
|
|
|
if not current_app.wsgi_app.is_proxied:
|
2020-08-23 18:23:22 +02:00
|
|
|
log.debug('Kobo: Received unproxied request, changed request port to external server port')
|
2019-12-17 18:00:35 +01:00
|
|
|
log.info("Kobo library metadata request received for book %s" % book_uuid)
|
2020-05-23 10:16:29 +02:00
|
|
|
book = calibre_db.get_book_by_uuid(book_uuid)
|
2019-12-17 18:00:35 +01:00
|
|
|
if not book or not book.data:
|
|
|
|
log.info(u"Book %s not found in database", book_uuid)
|
2019-12-30 15:16:09 +01:00
|
|
|
return redirect_or_proxy_request()
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
metadata = get_metadata(book)
|
2021-12-04 14:58:28 +01:00
|
|
|
response = make_response(json.dumps([metadata], ensure_ascii=False))
|
2021-12-04 11:16:33 +01:00
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_download_url_for_book(book, book_format):
|
2020-01-26 14:42:53 +01:00
|
|
|
if not current_app.wsgi_app.is_proxied:
|
2020-07-04 13:35:08 +02:00
|
|
|
if ':' in request.host and not request.host.endswith(']'):
|
2020-03-09 20:25:59 +01:00
|
|
|
host = "".join(request.host.split(':')[:-1])
|
|
|
|
else:
|
|
|
|
host = request.host
|
2020-07-03 18:19:11 +02:00
|
|
|
|
2020-10-31 14:50:08 +01:00
|
|
|
return "{url_scheme}://{url_base}:{url_port}/kobo/{auth_token}/download/{book_id}/{book_format}".format(
|
2020-03-07 11:07:35 +01:00
|
|
|
url_scheme=request.scheme,
|
2020-03-09 20:25:59 +01:00
|
|
|
url_base=host,
|
2020-07-03 18:19:11 +02:00
|
|
|
url_port=config.config_external_port,
|
2020-10-31 14:50:08 +01:00
|
|
|
auth_token=get_auth_token(),
|
2020-01-26 14:42:53 +01:00
|
|
|
book_id=book.id,
|
2020-03-07 11:07:35 +01:00
|
|
|
book_format=book_format.lower()
|
2020-01-26 14:42:53 +01:00
|
|
|
)
|
2020-03-07 11:07:35 +01:00
|
|
|
return url_for(
|
2021-03-23 02:08:31 +01:00
|
|
|
"kobo.download_book",
|
|
|
|
auth_token=kobo_auth.get_auth_token(),
|
2020-03-07 11:07:35 +01:00
|
|
|
book_id=book.id,
|
|
|
|
book_format=book_format.lower(),
|
|
|
|
_external=True,
|
|
|
|
)
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
2020-01-26 05:54:12 +01:00
|
|
|
def create_book_entitlement(book, archived):
|
2021-12-01 20:29:05 +01:00
|
|
|
book_uuid = str(book.uuid)
|
2019-12-17 18:00:35 +01:00
|
|
|
return {
|
|
|
|
"Accessibility": "Full",
|
2020-02-15 23:27:07 +01:00
|
|
|
"ActivePeriod": {"From": convert_to_kobo_timestamp_string(datetime.datetime.now())},
|
|
|
|
"Created": convert_to_kobo_timestamp_string(book.timestamp),
|
2019-12-17 18:00:35 +01:00
|
|
|
"CrossRevisionId": book_uuid,
|
|
|
|
"Id": book_uuid,
|
2020-01-26 05:54:12 +01:00
|
|
|
"IsRemoved": archived,
|
2019-12-17 18:00:35 +01:00
|
|
|
"IsHiddenFromArchive": False,
|
|
|
|
"IsLocked": False,
|
2020-03-13 00:59:05 +01:00
|
|
|
"LastModified": convert_to_kobo_timestamp_string(book.last_modified),
|
2019-12-17 18:00:35 +01:00
|
|
|
"OriginCategory": "Imported",
|
|
|
|
"RevisionId": book_uuid,
|
|
|
|
"Status": "Active",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def current_time():
|
|
|
|
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
|
|
|
|
|
|
|
|
|
|
|
|
def get_description(book):
|
|
|
|
if not book.comments:
|
|
|
|
return None
|
|
|
|
return book.comments[0].text
|
|
|
|
|
|
|
|
|
|
|
|
def get_author(book):
|
|
|
|
if not book.authors:
|
2020-07-05 20:54:36 +02:00
|
|
|
return {"Contributors": None}
|
2021-12-04 11:16:33 +01:00
|
|
|
author_list = []
|
|
|
|
autor_roles = []
|
|
|
|
for author in book.authors:
|
|
|
|
autor_roles.append({"Name":author.name}) #.encode('unicode-escape').decode('latin-1')
|
|
|
|
author_list.append(author.name)
|
|
|
|
return {"ContributorRoles": autor_roles, "Contributors":author_list}
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_publisher(book):
|
|
|
|
if not book.publishers:
|
|
|
|
return None
|
|
|
|
return book.publishers[0].name
|
|
|
|
|
|
|
|
|
|
|
|
def get_series(book):
|
|
|
|
if not book.series:
|
|
|
|
return None
|
|
|
|
return book.series[0].name
|
|
|
|
|
2020-05-17 10:46:48 +02:00
|
|
|
def get_seriesindex(book):
|
|
|
|
return book.series_index or 1
|
|
|
|
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
def get_metadata(book):
|
|
|
|
download_urls = []
|
2020-12-05 10:03:31 +01:00
|
|
|
kepub = [data for data in book.data if data.format == 'KEPUB']
|
|
|
|
|
|
|
|
for book_data in kepub if len(kepub) > 0 else book.data:
|
2019-12-30 15:16:09 +01:00
|
|
|
if book_data.format not in KOBO_FORMATS:
|
|
|
|
continue
|
|
|
|
for kobo_format in KOBO_FORMATS[book_data.format]:
|
2020-01-26 14:42:53 +01:00
|
|
|
# log.debug('Id: %s, Format: %s' % (book.id, kobo_format))
|
2019-12-17 18:00:35 +01:00
|
|
|
download_urls.append(
|
|
|
|
{
|
2019-12-30 15:16:09 +01:00
|
|
|
"Format": kobo_format,
|
2019-12-17 18:00:35 +01:00
|
|
|
"Size": book_data.uncompressed_size,
|
|
|
|
"Url": get_download_url_for_book(book, book_data.format),
|
2019-12-30 15:16:09 +01:00
|
|
|
# The Kobo forma accepts platforms: (Generic, Android)
|
|
|
|
"Platform": "Generic",
|
2019-12-17 18:00:35 +01:00
|
|
|
# "DrmType": "None", # Not required
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
book_uuid = book.uuid
|
|
|
|
metadata = {
|
2021-02-06 20:29:43 +01:00
|
|
|
"Categories": ["00000000-0000-0000-0000-000000000001", ],
|
2020-07-05 20:54:36 +02:00
|
|
|
# "Contributors": get_author(book),
|
2019-12-17 18:00:35 +01:00
|
|
|
"CoverImageId": book_uuid,
|
|
|
|
"CrossRevisionId": book_uuid,
|
|
|
|
"CurrentDisplayPrice": {"CurrencyCode": "USD", "TotalAmount": 0},
|
|
|
|
"CurrentLoveDisplayPrice": {"TotalAmount": 0},
|
|
|
|
"Description": get_description(book),
|
|
|
|
"DownloadUrls": download_urls,
|
|
|
|
"EntitlementId": book_uuid,
|
|
|
|
"ExternalIds": [],
|
|
|
|
"Genre": "00000000-0000-0000-0000-000000000001",
|
|
|
|
"IsEligibleForKoboLove": False,
|
|
|
|
"IsInternetArchive": False,
|
|
|
|
"IsPreOrder": False,
|
|
|
|
"IsSocialEnabled": True,
|
|
|
|
"Language": "en",
|
|
|
|
"PhoneticPronunciations": {},
|
2021-12-01 20:29:05 +01:00
|
|
|
"PublicationDate": convert_to_kobo_timestamp_string(book.pubdate),
|
2019-12-17 18:00:35 +01:00
|
|
|
"Publisher": {"Imprint": "", "Name": get_publisher(book),},
|
|
|
|
"RevisionId": book_uuid,
|
|
|
|
"Title": book.title,
|
|
|
|
"WorkId": book_uuid,
|
|
|
|
}
|
2020-07-05 20:54:36 +02:00
|
|
|
metadata.update(get_author(book))
|
2019-12-17 18:00:35 +01:00
|
|
|
|
|
|
|
if get_series(book):
|
2021-10-04 18:26:46 +02:00
|
|
|
name = get_series(book)
|
2019-12-17 18:00:35 +01:00
|
|
|
metadata["Series"] = {
|
|
|
|
"Name": get_series(book),
|
2020-05-17 10:46:48 +02:00
|
|
|
"Number": get_seriesindex(book), # ToDo Check int() ?
|
|
|
|
"NumberFloat": float(get_seriesindex(book)),
|
2019-12-17 18:00:35 +01:00
|
|
|
# Get a deterministic id based on the series name.
|
2021-12-01 20:29:05 +01:00
|
|
|
"Id": str(uuid.uuid3(uuid.NAMESPACE_DNS, name)),
|
2019-12-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
@csrf.exempt
|
2020-04-25 14:56:51 +02:00
|
|
|
@kobo.route("/v1/library/tags", methods=["POST", "DELETE"])
|
2020-07-05 20:54:36 +02:00
|
|
|
@requires_kobo_auth
|
2020-03-13 00:31:42 +01:00
|
|
|
# Creates a Shelf with the given items, and returns the shelf's uuid.
|
|
|
|
def HandleTagCreate():
|
2020-04-25 14:56:51 +02:00
|
|
|
# catch delete requests, otherwise the are handeld in the book delete handler
|
|
|
|
if request.method == "DELETE":
|
|
|
|
abort(405)
|
2020-03-13 00:31:42 +01:00
|
|
|
name, items = None, None
|
|
|
|
try:
|
2020-04-25 14:56:51 +02:00
|
|
|
shelf_request = request.json
|
2020-03-13 00:31:42 +01:00
|
|
|
name = shelf_request["Name"]
|
|
|
|
items = shelf_request["Items"]
|
2020-04-25 14:56:51 +02:00
|
|
|
if not name:
|
|
|
|
raise TypeError
|
|
|
|
except (KeyError, TypeError):
|
2020-03-13 00:31:42 +01:00
|
|
|
log.debug("Received malformed v1/library/tags request.")
|
2020-04-25 14:56:51 +02:00
|
|
|
abort(400, description="Malformed tags POST request. Data has empty 'Name', missing 'Name' or 'Items' field")
|
2020-03-13 00:31:42 +01:00
|
|
|
|
2020-04-22 05:36:47 +02:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.name == name, ub.Shelf.user_id ==
|
2020-03-13 00:31:42 +01:00
|
|
|
current_user.id).one_or_none()
|
|
|
|
if shelf and not shelf_lib.check_shelf_edit_permissions(shelf):
|
2020-04-25 14:56:51 +02:00
|
|
|
abort(401, description="User is unauthaurized to create shelf.")
|
2020-03-13 00:31:42 +01:00
|
|
|
|
|
|
|
if not shelf:
|
2020-04-25 14:56:51 +02:00
|
|
|
shelf = ub.Shelf(user_id=current_user.id, name=name, uuid=str(uuid.uuid4()))
|
2020-03-13 00:31:42 +01:00
|
|
|
ub.session.add(shelf)
|
|
|
|
|
|
|
|
items_unknown_to_calibre = add_items_to_shelf(items, shelf)
|
|
|
|
if items_unknown_to_calibre:
|
|
|
|
log.debug("Received request to add unknown books to a collection. Silently ignoring items.")
|
2021-01-03 09:53:34 +01:00
|
|
|
ub.session_commit()
|
2020-03-13 00:31:42 +01:00
|
|
|
return make_response(jsonify(str(shelf.uuid)), 201)
|
|
|
|
|
|
|
|
|
2021-10-06 18:41:01 +02:00
|
|
|
@csrf.exempt
|
2020-03-13 00:31:42 +01:00
|
|
|
@kobo.route("/v1/library/tags/<tag_id>", methods=["DELETE", "PUT"])
|
2020-07-05 20:54:36 +02:00
|
|
|
@requires_kobo_auth
|
2020-03-13 00:31:42 +01:00
|
|
|
def HandleTagUpdate(tag_id):
|
2020-04-22 05:36:47 +02:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.uuid == tag_id,
|
2020-03-13 00:31:42 +01:00
|
|
|
ub.Shelf.user_id == current_user.id).one_or_none()
|
|
|
|
if not shelf:
|
|
|
|
log.debug("Received Kobo tag update request on a collection unknown to CalibreWeb")
|
|
|
|
if config.config_kobo_proxy:
|
|
|
|
return redirect_or_proxy_request()
|
|
|
|
else:
|
|
|
|
abort(404, description="Collection isn't known to CalibreWeb")
|
|
|
|
|
|
|
|
if not shelf_lib.check_shelf_edit_permissions(shelf):
|
|
|
|
abort(401, description="User is unauthaurized to edit shelf.")
|
|
|
|
|
|
|
|
if request.method == "DELETE":
|
|
|
|
shelf_lib.delete_shelf_helper(shelf)
|
|
|
|
else:
|
|
|
|
name = None
|
|
|
|
try:
|
2020-04-25 14:56:51 +02:00
|
|
|
shelf_request = request.json
|
2020-03-13 00:31:42 +01:00
|
|
|
name = shelf_request["Name"]
|
2020-04-25 14:56:51 +02:00
|
|
|
except (KeyError, TypeError):
|
2020-03-13 00:31:42 +01:00
|
|
|
log.debug("Received malformed v1/library/tags rename request.")
|
|
|
|
abort(400, description="Malformed tags POST request. Data is missing 'Name' field")
|
|
|
|
|
|
|
|
shelf.name = name
|
|
|
|
ub.session.merge(shelf)
|
2021-01-03 09:53:34 +01:00
|
|
|
ub.session_commit()
|
2020-03-13 00:31:42 +01:00
|
|
|
return make_response(' ', 200)
|
|
|
|
|
|
|
|
|
|
|
|
# Adds items to the given shelf.
|
|
|
|
def add_items_to_shelf(items, shelf):
|
|
|
|
book_ids_already_in_shelf = set([book_shelf.book_id for book_shelf in shelf.books])
|
|
|
|
items_unknown_to_calibre = []
|
|
|
|
for item in items:
|
2020-04-25 14:56:51 +02:00
|
|
|
try:
|
|
|
|
if item["Type"] != "ProductRevisionTagItem":
|
|
|
|
items_unknown_to_calibre.append(item)
|
|
|
|
continue
|
|
|
|
|
2020-05-23 10:16:29 +02:00
|
|
|
book = calibre_db.get_book_by_uuid(item["RevisionId"])
|
2020-04-25 14:56:51 +02:00
|
|
|
if not book:
|
|
|
|
items_unknown_to_calibre.append(item)
|
|
|
|
continue
|
|
|
|
|
|
|
|
book_id = book.id
|
|
|
|
if book_id not in book_ids_already_in_shelf:
|
|
|
|
shelf.books.append(ub.BookShelf(book_id=book_id))
|
|
|
|
except KeyError:
|
2020-03-13 00:31:42 +01:00
|
|
|
items_unknown_to_calibre.append(item)
|
|
|
|
return items_unknown_to_calibre
|
|
|
|
|
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
@csrf.exempt
|
2020-03-13 00:31:42 +01:00
|
|
|
@kobo.route("/v1/library/tags/<tag_id>/items", methods=["POST"])
|
2020-07-05 20:54:36 +02:00
|
|
|
@requires_kobo_auth
|
2020-03-13 00:31:42 +01:00
|
|
|
def HandleTagAddItem(tag_id):
|
|
|
|
items = None
|
|
|
|
try:
|
2020-04-25 14:56:51 +02:00
|
|
|
tag_request = request.json
|
2020-03-13 00:31:42 +01:00
|
|
|
items = tag_request["Items"]
|
2020-04-25 14:56:51 +02:00
|
|
|
except (KeyError, TypeError):
|
2020-03-13 00:31:42 +01:00
|
|
|
log.debug("Received malformed v1/library/tags/<tag_id>/items/delete request.")
|
|
|
|
abort(400, description="Malformed tags POST request. Data is missing 'Items' field")
|
|
|
|
|
2020-04-22 05:36:47 +02:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.uuid == tag_id,
|
2020-03-13 00:31:42 +01:00
|
|
|
ub.Shelf.user_id == current_user.id).one_or_none()
|
|
|
|
if not shelf:
|
|
|
|
log.debug("Received Kobo request on a collection unknown to CalibreWeb")
|
|
|
|
abort(404, description="Collection isn't known to CalibreWeb")
|
|
|
|
|
|
|
|
if not shelf_lib.check_shelf_edit_permissions(shelf):
|
|
|
|
abort(401, description="User is unauthaurized to edit shelf.")
|
|
|
|
|
|
|
|
items_unknown_to_calibre = add_items_to_shelf(items, shelf)
|
|
|
|
if items_unknown_to_calibre:
|
2020-04-22 05:36:47 +02:00
|
|
|
log.debug("Received request to add an unknown book to a collection. Silently ignoring item.")
|
2020-03-13 00:31:42 +01:00
|
|
|
|
|
|
|
ub.session.merge(shelf)
|
2021-01-03 09:53:34 +01:00
|
|
|
ub.session_commit()
|
2020-03-13 00:31:42 +01:00
|
|
|
return make_response('', 201)
|
|
|
|
|
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
@csrf.exempt
|
2020-03-13 00:31:42 +01:00
|
|
|
@kobo.route("/v1/library/tags/<tag_id>/items/delete", methods=["POST"])
|
2020-07-05 20:54:36 +02:00
|
|
|
@requires_kobo_auth
|
2020-03-13 00:31:42 +01:00
|
|
|
def HandleTagRemoveItem(tag_id):
|
|
|
|
items = None
|
|
|
|
try:
|
2020-04-25 14:56:51 +02:00
|
|
|
tag_request = request.json
|
2020-03-13 00:31:42 +01:00
|
|
|
items = tag_request["Items"]
|
2020-04-25 14:56:51 +02:00
|
|
|
except (KeyError, TypeError):
|
2020-03-13 00:31:42 +01:00
|
|
|
log.debug("Received malformed v1/library/tags/<tag_id>/items/delete request.")
|
|
|
|
abort(400, description="Malformed tags POST request. Data is missing 'Items' field")
|
|
|
|
|
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.uuid == tag_id,
|
|
|
|
ub.Shelf.user_id == current_user.id).one_or_none()
|
|
|
|
if not shelf:
|
|
|
|
log.debug(
|
|
|
|
"Received a request to remove an item from a Collection unknown to CalibreWeb.")
|
|
|
|
abort(404, description="Collection isn't known to CalibreWeb")
|
|
|
|
|
|
|
|
if not shelf_lib.check_shelf_edit_permissions(shelf):
|
|
|
|
abort(401, description="User is unauthaurized to edit shelf.")
|
|
|
|
|
|
|
|
items_unknown_to_calibre = []
|
|
|
|
for item in items:
|
2020-04-25 14:56:51 +02:00
|
|
|
try:
|
|
|
|
if item["Type"] != "ProductRevisionTagItem":
|
|
|
|
items_unknown_to_calibre.append(item)
|
|
|
|
continue
|
2020-03-13 00:31:42 +01:00
|
|
|
|
2020-05-23 10:16:29 +02:00
|
|
|
book = calibre_db.get_book_by_uuid(item["RevisionId"])
|
2020-04-25 14:56:51 +02:00
|
|
|
if not book:
|
|
|
|
items_unknown_to_calibre.append(item)
|
|
|
|
continue
|
2020-03-13 00:31:42 +01:00
|
|
|
|
2020-04-25 14:56:51 +02:00
|
|
|
shelf.books.filter(ub.BookShelf.book_id == book.id).delete()
|
|
|
|
except KeyError:
|
|
|
|
items_unknown_to_calibre.append(item)
|
2021-01-03 09:53:34 +01:00
|
|
|
ub.session_commit()
|
2020-03-13 00:31:42 +01:00
|
|
|
|
|
|
|
if items_unknown_to_calibre:
|
|
|
|
log.debug("Received request to remove an unknown book to a collecition. Silently ignoring item.")
|
|
|
|
|
|
|
|
return make_response('', 200)
|
|
|
|
|
|
|
|
|
|
|
|
# Add new, changed, or deleted shelves to the sync_results.
|
|
|
|
# Note: Public shelves that aren't owned by the user aren't supported.
|
2021-02-06 21:15:36 +01:00
|
|
|
def sync_shelves(sync_token, sync_results, only_kobo_shelves=False):
|
2020-03-13 00:31:42 +01:00
|
|
|
new_tags_last_modified = sync_token.tags_last_modified
|
|
|
|
|
2021-02-08 20:10:12 +01:00
|
|
|
for shelf in ub.session.query(ub.ShelfArchive).filter(
|
|
|
|
func.datetime(ub.ShelfArchive.last_modified) > sync_token.tags_last_modified,
|
|
|
|
ub.ShelfArchive.user_id == current_user.id
|
|
|
|
):
|
2020-03-13 00:31:42 +01:00
|
|
|
new_tags_last_modified = max(shelf.last_modified, new_tags_last_modified)
|
|
|
|
sync_results.append({
|
|
|
|
"DeletedTag": {
|
|
|
|
"Tag": {
|
|
|
|
"Id": shelf.uuid,
|
|
|
|
"LastModified": convert_to_kobo_timestamp_string(shelf.last_modified)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-10-16 11:28:35 +02:00
|
|
|
ub.session.delete(shelf)
|
|
|
|
ub.session_commit()
|
|
|
|
|
2020-03-13 00:31:42 +01:00
|
|
|
|
2021-02-06 20:29:43 +01:00
|
|
|
extra_filters = []
|
2021-02-06 21:15:36 +01:00
|
|
|
if only_kobo_shelves:
|
2021-02-06 20:29:43 +01:00
|
|
|
for shelf in ub.session.query(ub.Shelf).filter(
|
|
|
|
func.datetime(ub.Shelf.last_modified) > sync_token.tags_last_modified,
|
|
|
|
ub.Shelf.user_id == current_user.id,
|
|
|
|
not ub.Shelf.kobo_sync
|
|
|
|
):
|
|
|
|
sync_results.append({
|
|
|
|
"DeletedTag": {
|
|
|
|
"Tag": {
|
|
|
|
"Id": shelf.uuid,
|
|
|
|
"LastModified": convert_to_kobo_timestamp_string(shelf.last_modified)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
extra_filters.append(ub.Shelf.kobo_sync)
|
|
|
|
|
2021-07-26 07:52:01 +02:00
|
|
|
if sqlalchemy_version2:
|
2021-07-11 08:28:45 +02:00
|
|
|
shelflist = ub.session.execute(select(ub.Shelf).outerjoin(ub.BookShelf).filter(
|
|
|
|
or_(func.datetime(ub.Shelf.last_modified) > sync_token.tags_last_modified,
|
|
|
|
func.datetime(ub.BookShelf.date_added) > sync_token.tags_last_modified),
|
|
|
|
ub.Shelf.user_id == current_user.id,
|
|
|
|
*extra_filters
|
|
|
|
).distinct().order_by(func.datetime(ub.Shelf.last_modified).asc())).columns(ub.Shelf)
|
|
|
|
else:
|
|
|
|
shelflist = ub.session.query(ub.Shelf).outerjoin(ub.BookShelf).filter(
|
|
|
|
or_(func.datetime(ub.Shelf.last_modified) > sync_token.tags_last_modified,
|
|
|
|
func.datetime(ub.BookShelf.date_added) > sync_token.tags_last_modified),
|
|
|
|
ub.Shelf.user_id == current_user.id,
|
|
|
|
*extra_filters
|
|
|
|
).distinct().order_by(func.datetime(ub.Shelf.last_modified).asc())
|
|
|
|
|
|
|
|
|
|
|
|
for shelf in shelflist:
|
2020-03-13 00:31:42 +01:00
|
|
|
if not shelf_lib.check_shelf_view_permissions(shelf):
|
|
|
|
continue
|
|
|
|
|
|
|
|
new_tags_last_modified = max(shelf.last_modified, new_tags_last_modified)
|
|
|
|
|
|
|
|
tag = create_kobo_tag(shelf)
|
|
|
|
if not tag:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if shelf.created > sync_token.tags_last_modified:
|
|
|
|
sync_results.append({
|
|
|
|
"NewTag": tag
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
sync_results.append({
|
|
|
|
"ChangedTag": tag
|
|
|
|
})
|
|
|
|
sync_token.tags_last_modified = new_tags_last_modified
|
2021-01-03 09:53:34 +01:00
|
|
|
ub.session_commit()
|
2020-03-13 00:31:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Creates a Kobo "Tag" object from a ub.Shelf object
|
|
|
|
def create_kobo_tag(shelf):
|
|
|
|
tag = {
|
|
|
|
"Created": convert_to_kobo_timestamp_string(shelf.created),
|
|
|
|
"Id": shelf.uuid,
|
|
|
|
"Items": [],
|
|
|
|
"LastModified": convert_to_kobo_timestamp_string(shelf.last_modified),
|
|
|
|
"Name": shelf.name,
|
|
|
|
"Type": "UserTag"
|
|
|
|
}
|
|
|
|
for book_shelf in shelf.books:
|
2020-05-23 14:40:50 +02:00
|
|
|
book = calibre_db.get_book(book_shelf.book_id)
|
2020-03-13 00:31:42 +01:00
|
|
|
if not book:
|
|
|
|
log.info(u"Book (id: %s) in BookShelf (id: %s) not found in book database", book_shelf.book_id, shelf.id)
|
2020-04-22 05:36:47 +02:00
|
|
|
continue
|
2020-03-13 00:31:42 +01:00
|
|
|
tag["Items"].append(
|
|
|
|
{
|
|
|
|
"RevisionId": book.uuid,
|
|
|
|
"Type": "ProductRevisionTagItem"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return {"Tag": tag}
|
|
|
|
|
2021-10-06 18:41:01 +02:00
|
|
|
@csrf.exempt
|
2020-02-15 23:27:07 +01:00
|
|
|
@kobo.route("/v1/library/<book_uuid>/state", methods=["GET", "PUT"])
|
2020-07-05 20:54:36 +02:00
|
|
|
@requires_kobo_auth
|
2020-02-15 23:27:07 +01:00
|
|
|
def HandleStateRequest(book_uuid):
|
2020-05-23 10:16:29 +02:00
|
|
|
book = calibre_db.get_book_by_uuid(book_uuid)
|
2020-02-15 23:27:07 +01:00
|
|
|
if not book or not book.data:
|
|
|
|
log.info(u"Book %s not found in database", book_uuid)
|
|
|
|
return redirect_or_proxy_request()
|
|
|
|
|
|
|
|
kobo_reading_state = get_or_create_reading_state(book.id)
|
|
|
|
|
|
|
|
if request.method == "GET":
|
|
|
|
return jsonify([get_kobo_reading_state_response(book, kobo_reading_state)])
|
|
|
|
else:
|
|
|
|
update_results_response = {"EntitlementId": book_uuid}
|
|
|
|
|
2020-04-25 14:56:51 +02:00
|
|
|
try:
|
|
|
|
request_data = request.json
|
|
|
|
request_reading_state = request_data["ReadingStates"][0]
|
|
|
|
|
|
|
|
request_bookmark = request_reading_state["CurrentBookmark"]
|
|
|
|
if request_bookmark:
|
|
|
|
current_bookmark = kobo_reading_state.current_bookmark
|
|
|
|
current_bookmark.progress_percent = request_bookmark["ProgressPercent"]
|
|
|
|
current_bookmark.content_source_progress_percent = request_bookmark["ContentSourceProgressPercent"]
|
|
|
|
location = request_bookmark["Location"]
|
|
|
|
if location:
|
|
|
|
current_bookmark.location_value = location["Value"]
|
|
|
|
current_bookmark.location_type = location["Type"]
|
|
|
|
current_bookmark.location_source = location["Source"]
|
|
|
|
update_results_response["CurrentBookmarkResult"] = {"Result": "Success"}
|
|
|
|
|
|
|
|
request_statistics = request_reading_state["Statistics"]
|
|
|
|
if request_statistics:
|
|
|
|
statistics = kobo_reading_state.statistics
|
|
|
|
statistics.spent_reading_minutes = int(request_statistics["SpentReadingMinutes"])
|
|
|
|
statistics.remaining_time_minutes = int(request_statistics["RemainingTimeMinutes"])
|
|
|
|
update_results_response["StatisticsResult"] = {"Result": "Success"}
|
|
|
|
|
|
|
|
request_status_info = request_reading_state["StatusInfo"]
|
|
|
|
if request_status_info:
|
|
|
|
book_read = kobo_reading_state.book_read_link
|
|
|
|
new_book_read_status = get_ub_read_status(request_status_info["Status"])
|
|
|
|
if new_book_read_status == ub.ReadBook.STATUS_IN_PROGRESS \
|
|
|
|
and new_book_read_status != book_read.read_status:
|
|
|
|
book_read.times_started_reading += 1
|
|
|
|
book_read.last_time_started_reading = datetime.datetime.utcnow()
|
|
|
|
book_read.read_status = new_book_read_status
|
|
|
|
update_results_response["StatusInfoResult"] = {"Result": "Success"}
|
|
|
|
except (KeyError, TypeError, ValueError, StatementError):
|
|
|
|
log.debug("Received malformed v1/library/<book_uuid>/state request.")
|
|
|
|
ub.session.rollback()
|
2020-02-15 23:27:07 +01:00
|
|
|
abort(400, description="Malformed request data is missing 'ReadingStates' key")
|
|
|
|
|
|
|
|
ub.session.merge(kobo_reading_state)
|
2021-01-03 09:53:34 +01:00
|
|
|
ub.session_commit()
|
2020-02-15 23:27:07 +01:00
|
|
|
return jsonify({
|
|
|
|
"RequestResult": "Success",
|
|
|
|
"UpdateResults": [update_results_response],
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def get_read_status_for_kobo(ub_book_read):
|
|
|
|
enum_to_string_map = {
|
|
|
|
None: "ReadyToRead",
|
|
|
|
ub.ReadBook.STATUS_UNREAD: "ReadyToRead",
|
|
|
|
ub.ReadBook.STATUS_FINISHED: "Finished",
|
|
|
|
ub.ReadBook.STATUS_IN_PROGRESS: "Reading",
|
|
|
|
}
|
|
|
|
return enum_to_string_map[ub_book_read.read_status]
|
|
|
|
|
|
|
|
|
|
|
|
def get_ub_read_status(kobo_read_status):
|
|
|
|
string_to_enum_map = {
|
|
|
|
None: None,
|
|
|
|
"ReadyToRead": ub.ReadBook.STATUS_UNREAD,
|
|
|
|
"Finished": ub.ReadBook.STATUS_FINISHED,
|
|
|
|
"Reading": ub.ReadBook.STATUS_IN_PROGRESS,
|
2019-12-17 18:00:35 +01:00
|
|
|
}
|
2020-02-15 23:27:07 +01:00
|
|
|
return string_to_enum_map[kobo_read_status]
|
|
|
|
|
|
|
|
|
|
|
|
def get_or_create_reading_state(book_id):
|
2020-04-25 14:56:51 +02:00
|
|
|
book_read = ub.session.query(ub.ReadBook).filter(ub.ReadBook.book_id == book_id,
|
|
|
|
ub.ReadBook.user_id == current_user.id).one_or_none()
|
2020-02-15 23:27:07 +01:00
|
|
|
if not book_read:
|
|
|
|
book_read = ub.ReadBook(user_id=current_user.id, book_id=book_id)
|
|
|
|
if not book_read.kobo_reading_state:
|
|
|
|
kobo_reading_state = ub.KoboReadingState(user_id=book_read.user_id, book_id=book_id)
|
|
|
|
kobo_reading_state.current_bookmark = ub.KoboBookmark()
|
|
|
|
kobo_reading_state.statistics = ub.KoboStatistics()
|
|
|
|
book_read.kobo_reading_state = kobo_reading_state
|
|
|
|
ub.session.add(book_read)
|
2021-01-03 15:40:17 +01:00
|
|
|
ub.session_commit()
|
2020-02-15 23:27:07 +01:00
|
|
|
return book_read.kobo_reading_state
|
|
|
|
|
|
|
|
|
|
|
|
def get_kobo_reading_state_response(book, kobo_reading_state):
|
|
|
|
return {
|
|
|
|
"EntitlementId": book.uuid,
|
|
|
|
"Created": convert_to_kobo_timestamp_string(book.timestamp),
|
|
|
|
"LastModified": convert_to_kobo_timestamp_string(kobo_reading_state.last_modified),
|
|
|
|
# AFAICT PriorityTimestamp is always equal to LastModified.
|
|
|
|
"PriorityTimestamp": convert_to_kobo_timestamp_string(kobo_reading_state.priority_timestamp),
|
|
|
|
"StatusInfo": get_status_info_response(kobo_reading_state.book_read_link),
|
|
|
|
"Statistics": get_statistics_response(kobo_reading_state.statistics),
|
|
|
|
"CurrentBookmark": get_current_bookmark_response(kobo_reading_state.current_bookmark),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_status_info_response(book_read):
|
|
|
|
resp = {
|
|
|
|
"LastModified": convert_to_kobo_timestamp_string(book_read.last_modified),
|
|
|
|
"Status": get_read_status_for_kobo(book_read),
|
|
|
|
"TimesStartedReading": book_read.times_started_reading,
|
|
|
|
}
|
|
|
|
if book_read.last_time_started_reading:
|
|
|
|
resp["LastTimeStartedReading"] = convert_to_kobo_timestamp_string(book_read.last_time_started_reading)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
def get_statistics_response(statistics):
|
|
|
|
resp = {
|
|
|
|
"LastModified": convert_to_kobo_timestamp_string(statistics.last_modified),
|
|
|
|
}
|
|
|
|
if statistics.spent_reading_minutes:
|
|
|
|
resp["SpentReadingMinutes"] = statistics.spent_reading_minutes
|
|
|
|
if statistics.remaining_time_minutes:
|
|
|
|
resp["RemainingTimeMinutes"] = statistics.remaining_time_minutes
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_bookmark_response(current_bookmark):
|
|
|
|
resp = {
|
|
|
|
"LastModified": convert_to_kobo_timestamp_string(current_bookmark.last_modified),
|
|
|
|
}
|
|
|
|
if current_bookmark.progress_percent:
|
|
|
|
resp["ProgressPercent"] = current_bookmark.progress_percent
|
|
|
|
if current_bookmark.content_source_progress_percent:
|
|
|
|
resp["ContentSourceProgressPercent"] = current_bookmark.content_source_progress_percent
|
|
|
|
if current_bookmark.location_value:
|
|
|
|
resp["Location"] = {
|
|
|
|
"Value": current_bookmark.location_value,
|
|
|
|
"Type": current_bookmark.location_type,
|
|
|
|
"Source": current_bookmark.location_source,
|
|
|
|
}
|
|
|
|
return resp
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2020-04-04 06:57:54 +02:00
|
|
|
@kobo.route("/<book_uuid>/<width>/<height>/<isGreyscale>/image.jpg", defaults={'Quality': ""})
|
|
|
|
@kobo.route("/<book_uuid>/<width>/<height>/<Quality>/<isGreyscale>/image.jpg")
|
2020-02-16 14:25:15 +01:00
|
|
|
@requires_kobo_auth
|
2020-04-04 06:57:54 +02:00
|
|
|
def HandleCoverImageRequest(book_uuid, width, height,Quality, isGreyscale):
|
2019-12-17 18:00:35 +01:00
|
|
|
book_cover = helper.get_book_cover_with_uuid(
|
|
|
|
book_uuid, use_generic_cover_on_failure=False
|
|
|
|
)
|
|
|
|
if not book_cover:
|
2020-01-26 16:52:40 +01:00
|
|
|
if config.config_kobo_proxy:
|
2020-03-09 20:25:59 +01:00
|
|
|
log.debug("Cover for unknown book: %s proxied to kobo" % book_uuid)
|
2020-04-04 06:57:54 +02:00
|
|
|
return redirect(KOBO_IMAGEHOST_URL +
|
|
|
|
"/{book_uuid}/{width}/{height}/false/image.jpg".format(book_uuid=book_uuid,
|
|
|
|
width=width,
|
|
|
|
height=height), 307)
|
2020-01-26 16:52:40 +01:00
|
|
|
else:
|
2020-03-09 20:25:59 +01:00
|
|
|
log.debug("Cover for unknown book: %s requested" % book_uuid)
|
2020-04-19 19:08:58 +02:00
|
|
|
# additional proxy request make no sense, -> direct return
|
|
|
|
return make_response(jsonify({}))
|
2020-03-09 20:25:59 +01:00
|
|
|
log.debug("Cover request received for book %s" % book_uuid)
|
2019-12-17 18:00:35 +01:00
|
|
|
return book_cover
|
|
|
|
|
|
|
|
|
2019-12-20 06:47:29 +01:00
|
|
|
@kobo.route("")
|
|
|
|
def TopLevelEndpoint():
|
|
|
|
return make_response(jsonify({}))
|
2019-11-06 05:18:52 +01:00
|
|
|
|
2019-12-20 07:04:12 +01:00
|
|
|
|
2021-10-06 18:41:01 +02:00
|
|
|
@csrf.exempt
|
2020-01-24 06:04:16 +01:00
|
|
|
@kobo.route("/v1/library/<book_uuid>", methods=["DELETE"])
|
2020-07-05 20:54:36 +02:00
|
|
|
@requires_kobo_auth
|
2020-01-24 06:04:16 +01:00
|
|
|
def HandleBookDeletionRequest(book_uuid):
|
|
|
|
log.info("Kobo book deletion request received for book %s" % book_uuid)
|
2020-05-23 10:16:29 +02:00
|
|
|
book = calibre_db.get_book_by_uuid(book_uuid)
|
2020-01-24 06:04:16 +01:00
|
|
|
if not book:
|
|
|
|
log.info(u"Book %s not found in database", book_uuid)
|
|
|
|
return redirect_or_proxy_request()
|
|
|
|
|
|
|
|
book_id = book.id
|
|
|
|
archived_book = (
|
|
|
|
ub.session.query(ub.ArchivedBook)
|
|
|
|
.filter(ub.ArchivedBook.book_id == book_id)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if not archived_book:
|
|
|
|
archived_book = ub.ArchivedBook(user_id=current_user.id, book_id=book_id)
|
|
|
|
archived_book.is_archived = True
|
2020-03-13 00:59:05 +01:00
|
|
|
archived_book.last_modified = datetime.datetime.utcnow()
|
2020-01-26 05:54:12 +01:00
|
|
|
|
2020-01-24 06:04:16 +01:00
|
|
|
ub.session.merge(archived_book)
|
2021-01-03 09:53:34 +01:00
|
|
|
ub.session_commit()
|
2021-12-01 20:29:05 +01:00
|
|
|
if archived_book.is_archived:
|
|
|
|
kobo_sync_status.remove_synced_book(book_id)
|
2021-10-16 20:50:04 +02:00
|
|
|
return "", 204
|
2020-01-24 06:04:16 +01:00
|
|
|
|
|
|
|
|
2019-12-30 15:16:09 +01:00
|
|
|
# TODO: Implement the following routes
|
2021-10-06 18:41:01 +02:00
|
|
|
@csrf.exempt
|
2020-03-13 00:59:05 +01:00
|
|
|
@kobo.route("/v1/library/<dummy>", methods=["DELETE", "GET"])
|
2020-03-13 00:31:42 +01:00
|
|
|
def HandleUnimplementedRequest(dummy=None):
|
2020-03-13 00:59:05 +01:00
|
|
|
log.debug("Unimplemented Library Request received: %s", request.base_url)
|
2019-12-30 15:16:09 +01:00
|
|
|
return redirect_or_proxy_request()
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2020-02-17 20:37:02 +01:00
|
|
|
|
2020-01-26 14:42:53 +01:00
|
|
|
# TODO: Implement the following routes
|
2021-10-04 17:50:32 +02:00
|
|
|
@csrf.exempt
|
2020-01-26 14:42:53 +01:00
|
|
|
@kobo.route("/v1/user/loyalty/<dummy>", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/user/profile", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/user/wishlist", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/user/recommendations", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/analytics/<dummy>", methods=["GET", "POST"])
|
|
|
|
def HandleUserRequest(dummy=None):
|
2020-02-17 20:37:02 +01:00
|
|
|
log.debug("Unimplemented User Request received: %s", request.base_url)
|
2020-01-26 16:52:40 +01:00
|
|
|
return redirect_or_proxy_request()
|
2019-12-17 18:00:35 +01:00
|
|
|
|
2020-02-17 20:37:02 +01:00
|
|
|
|
2021-12-04 15:44:41 +01:00
|
|
|
@csrf.exempt
|
|
|
|
@kobo.route("/v1/user/loyalty/benefits", methods=["GET"])
|
|
|
|
def handle_benefits():
|
|
|
|
if config.config_kobo_proxy:
|
|
|
|
return redirect_or_proxy_request()
|
|
|
|
else:
|
|
|
|
return make_response(jsonify({"Benefits": {}}))
|
|
|
|
|
|
|
|
|
|
|
|
@csrf.exempt
|
|
|
|
@kobo.route("/v1/analytics/gettests", methods=["GET", "POST"])
|
|
|
|
def handle_getests():
|
|
|
|
if config.config_kobo_proxy:
|
|
|
|
return redirect_or_proxy_request()
|
|
|
|
else:
|
|
|
|
testkey = request.headers.get("X-Kobo-userkey","")
|
|
|
|
return make_response(jsonify({"Result": "Success", "TestKey":testkey, "Tests": {}}))
|
|
|
|
|
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
@csrf.exempt
|
2020-02-17 20:37:02 +01:00
|
|
|
@kobo.route("/v1/products/<dummy>/prices", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/products/<dummy>/recommendations", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/products/<dummy>/nextread", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/products/<dummy>/reviews", methods=["GET", "POST"])
|
2021-12-01 20:29:05 +01:00
|
|
|
@kobo.route("/v1/products/featured/<dummy>", methods=["GET", "POST"])
|
|
|
|
@kobo.route("/v1/products/featured/", methods=["GET", "POST"])
|
2020-11-23 00:06:37 +01:00
|
|
|
@kobo.route("/v1/products/books/external/<dummy>", methods=["GET", "POST"])
|
2020-03-13 00:31:42 +01:00
|
|
|
@kobo.route("/v1/products/books/series/<dummy>", methods=["GET", "POST"])
|
2020-02-17 20:37:02 +01:00
|
|
|
@kobo.route("/v1/products/books/<dummy>", methods=["GET", "POST"])
|
2021-12-01 20:29:05 +01:00
|
|
|
@kobo.route("/v1/products/books/<dummy>/", methods=["GET", "POST"])
|
2020-02-17 20:37:02 +01:00
|
|
|
@kobo.route("/v1/products/dailydeal", methods=["GET", "POST"])
|
2021-12-01 20:29:05 +01:00
|
|
|
@kobo.route("/v1/products/deals", methods=["GET", "POST"])
|
2020-02-26 07:06:03 +01:00
|
|
|
@kobo.route("/v1/products", methods=["GET", "POST"])
|
2021-12-01 20:29:05 +01:00
|
|
|
@kobo.route("/v1/affiliate", methods=["GET", "POST"])
|
2021-12-04 15:44:41 +01:00
|
|
|
@kobo.route("/v1/deals", methods=["GET", "POST"])
|
2020-02-17 20:37:02 +01:00
|
|
|
def HandleProductsRequest(dummy=None):
|
|
|
|
log.debug("Unimplemented Products Request received: %s", request.base_url)
|
|
|
|
return redirect_or_proxy_request()
|
|
|
|
|
|
|
|
|
|
|
|
def make_calibre_web_auth_response():
|
|
|
|
# As described in kobo_auth.py, CalibreWeb doesn't make use practical use of this auth/device API call for
|
|
|
|
# authentation (nor for authorization). We return a dummy response just to keep the device happy.
|
2020-02-18 20:36:32 +01:00
|
|
|
content = request.get_json()
|
|
|
|
AccessToken = base64.b64encode(os.urandom(24)).decode('utf-8')
|
|
|
|
RefreshToken = base64.b64encode(os.urandom(24)).decode('utf-8')
|
2020-02-22 16:24:06 +01:00
|
|
|
return make_response(
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"AccessToken": AccessToken,
|
|
|
|
"RefreshToken": RefreshToken,
|
|
|
|
"TokenType": "Bearer",
|
|
|
|
"TrackingId": str(uuid.uuid4()),
|
|
|
|
"UserKey": content['UserKey'],
|
|
|
|
}
|
2020-02-18 20:36:32 +01:00
|
|
|
)
|
2020-02-22 16:24:06 +01:00
|
|
|
)
|
2020-02-17 20:37:02 +01:00
|
|
|
|
2020-02-19 18:20:03 +01:00
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
@csrf.exempt
|
2020-02-16 14:25:15 +01:00
|
|
|
@kobo.route("/v1/auth/device", methods=["POST"])
|
2020-02-19 18:20:03 +01:00
|
|
|
@requires_kobo_auth
|
2020-02-17 20:37:02 +01:00
|
|
|
def HandleAuthRequest():
|
2020-02-19 18:20:03 +01:00
|
|
|
log.debug('Kobo Auth request')
|
2020-02-17 20:37:02 +01:00
|
|
|
if config.config_kobo_proxy:
|
|
|
|
try:
|
|
|
|
return redirect_or_proxy_request()
|
2021-03-15 09:55:59 +01:00
|
|
|
except Exception:
|
2020-02-17 20:37:02 +01:00
|
|
|
log.error("Failed to receive or parse response from Kobo's auth endpoint. Falling back to un-proxied mode.")
|
|
|
|
return make_calibre_web_auth_response()
|
|
|
|
|
|
|
|
|
2020-01-26 16:52:40 +01:00
|
|
|
@kobo.route("/v1/initialization")
|
2020-02-16 14:25:15 +01:00
|
|
|
@requires_kobo_auth
|
2019-12-17 18:00:35 +01:00
|
|
|
def HandleInitRequest():
|
2020-02-17 20:37:02 +01:00
|
|
|
log.info('Init')
|
|
|
|
|
2020-03-30 08:17:08 +02:00
|
|
|
kobo_resources = None
|
|
|
|
if config.config_kobo_proxy:
|
|
|
|
try:
|
|
|
|
store_response = make_request_to_kobo_store()
|
|
|
|
store_response_json = store_response.json()
|
|
|
|
if "Resources" in store_response_json:
|
|
|
|
kobo_resources = store_response_json["Resources"]
|
2021-03-14 15:06:09 +01:00
|
|
|
except Exception:
|
2020-03-30 08:17:08 +02:00
|
|
|
log.error("Failed to receive or parse response from Kobo's init endpoint. Falling back to un-proxied mode.")
|
|
|
|
if not kobo_resources:
|
|
|
|
kobo_resources = NATIVE_KOBO_RESOURCES()
|
|
|
|
|
2020-01-26 16:52:40 +01:00
|
|
|
if not current_app.wsgi_app.is_proxied:
|
2020-08-23 18:23:22 +02:00
|
|
|
log.debug('Kobo: Received unproxied request, changed request port to external server port')
|
2020-03-09 20:25:59 +01:00
|
|
|
if ':' in request.host and not request.host.endswith(']'):
|
|
|
|
host = "".join(request.host.split(':')[:-1])
|
|
|
|
else:
|
|
|
|
host = request.host
|
2020-03-07 11:07:35 +01:00
|
|
|
calibre_web_url = "{url_scheme}://{url_base}:{url_port}".format(
|
|
|
|
url_scheme=request.scheme,
|
2020-03-09 20:25:59 +01:00
|
|
|
url_base=host,
|
2020-07-03 18:19:11 +02:00
|
|
|
url_port=config.config_external_port
|
2020-03-07 11:07:35 +01:00
|
|
|
)
|
2020-08-23 18:23:22 +02:00
|
|
|
log.debug('Kobo: Received unproxied request, changed request url to %s', calibre_web_url)
|
2020-03-30 08:17:08 +02:00
|
|
|
kobo_resources["image_host"] = calibre_web_url
|
2020-04-04 06:57:54 +02:00
|
|
|
kobo_resources["image_url_quality_template"] = unquote(calibre_web_url +
|
|
|
|
url_for("kobo.HandleCoverImageRequest",
|
|
|
|
auth_token=kobo_auth.get_auth_token(),
|
|
|
|
book_uuid="{ImageId}",
|
|
|
|
width="{width}",
|
|
|
|
height="{height}",
|
|
|
|
Quality='{Quality}',
|
2020-07-03 18:19:11 +02:00
|
|
|
isGreyscale='isGreyscale'))
|
2020-04-04 06:57:54 +02:00
|
|
|
kobo_resources["image_url_template"] = unquote(calibre_web_url +
|
|
|
|
url_for("kobo.HandleCoverImageRequest",
|
|
|
|
auth_token=kobo_auth.get_auth_token(),
|
|
|
|
book_uuid="{ImageId}",
|
|
|
|
width="{width}",
|
|
|
|
height="{height}",
|
2020-07-03 18:19:11 +02:00
|
|
|
isGreyscale='false'))
|
2020-01-26 16:52:40 +01:00
|
|
|
else:
|
2020-03-30 08:17:08 +02:00
|
|
|
kobo_resources["image_host"] = url_for("web.index", _external=True).strip("/")
|
|
|
|
kobo_resources["image_url_quality_template"] = unquote(url_for("kobo.HandleCoverImageRequest",
|
|
|
|
auth_token=kobo_auth.get_auth_token(),
|
|
|
|
book_uuid="{ImageId}",
|
|
|
|
width="{width}",
|
|
|
|
height="{height}",
|
2020-05-02 07:55:14 +02:00
|
|
|
Quality='{Quality}',
|
|
|
|
isGreyscale='isGreyscale',
|
2020-03-30 08:17:08 +02:00
|
|
|
_external=True))
|
|
|
|
kobo_resources["image_url_template"] = unquote(url_for("kobo.HandleCoverImageRequest",
|
|
|
|
auth_token=kobo_auth.get_auth_token(),
|
|
|
|
book_uuid="{ImageId}",
|
|
|
|
width="{width}",
|
|
|
|
height="{height}",
|
2020-05-02 07:55:14 +02:00
|
|
|
isGreyscale='false',
|
2020-03-30 08:17:08 +02:00
|
|
|
_external=True))
|
|
|
|
|
|
|
|
response = make_response(jsonify({"Resources": kobo_resources}))
|
|
|
|
response.headers["x-kobo-apitoken"] = "e30="
|
2020-02-17 20:37:02 +01:00
|
|
|
|
2020-03-30 08:17:08 +02:00
|
|
|
return response
|
2020-01-26 14:42:53 +01:00
|
|
|
|
|
|
|
|
2020-10-31 14:50:08 +01:00
|
|
|
@kobo.route("/download/<book_id>/<book_format>")
|
|
|
|
@requires_kobo_auth
|
|
|
|
@download_required
|
|
|
|
def download_book(book_id, book_format):
|
|
|
|
return get_download_link(book_id, book_format, "kobo")
|
|
|
|
|
|
|
|
|
2020-03-30 08:17:08 +02:00
|
|
|
def NATIVE_KOBO_RESOURCES():
|
2020-01-26 14:42:53 +01:00
|
|
|
return {
|
|
|
|
"account_page": "https://secure.kobobooks.com/profile",
|
|
|
|
"account_page_rakuten": "https://my.rakuten.co.jp/",
|
|
|
|
"add_entitlement": "https://storeapi.kobo.com/v1/library/{RevisionIds}",
|
|
|
|
"affiliaterequest": "https://storeapi.kobo.com/v1/affiliate",
|
|
|
|
"audiobook_subscription_orange_deal_inclusion_url": "https://authorize.kobo.com/inclusion",
|
|
|
|
"authorproduct_recommendations": "https://storeapi.kobo.com/v1/products/books/authors/recommendations",
|
|
|
|
"autocomplete": "https://storeapi.kobo.com/v1/products/autocomplete",
|
|
|
|
"blackstone_header": {"key": "x-amz-request-payer", "value": "requester"},
|
|
|
|
"book": "https://storeapi.kobo.com/v1/products/books/{ProductId}",
|
|
|
|
"book_detail_page": "https://store.kobobooks.com/{culture}/ebook/{slug}",
|
2020-05-09 17:11:56 +02:00
|
|
|
"book_detail_page_rakuten": "https://books.rakuten.co.jp/rk/{crossrevisionid}",
|
2020-01-26 14:42:53 +01:00
|
|
|
"book_landing_page": "https://store.kobobooks.com/ebooks",
|
|
|
|
"book_subscription": "https://storeapi.kobo.com/v1/products/books/subscriptions",
|
|
|
|
"categories": "https://storeapi.kobo.com/v1/categories",
|
|
|
|
"categories_page": "https://store.kobobooks.com/ebooks/categories",
|
|
|
|
"category": "https://storeapi.kobo.com/v1/categories/{CategoryId}",
|
|
|
|
"category_featured_lists": "https://storeapi.kobo.com/v1/categories/{CategoryId}/featured",
|
|
|
|
"category_products": "https://storeapi.kobo.com/v1/categories/{CategoryId}/products",
|
|
|
|
"checkout_borrowed_book": "https://storeapi.kobo.com/v1/library/borrow",
|
|
|
|
"configuration_data": "https://storeapi.kobo.com/v1/configuration",
|
|
|
|
"content_access_book": "https://storeapi.kobo.com/v1/products/books/{ProductId}/access",
|
|
|
|
"customer_care_live_chat": "https://v2.zopim.com/widget/livechat.html?key=Y6gwUmnu4OATxN3Tli4Av9bYN319BTdO",
|
|
|
|
"daily_deal": "https://storeapi.kobo.com/v1/products/dailydeal",
|
|
|
|
"deals": "https://storeapi.kobo.com/v1/deals",
|
|
|
|
"delete_entitlement": "https://storeapi.kobo.com/v1/library/{Ids}",
|
|
|
|
"delete_tag": "https://storeapi.kobo.com/v1/library/tags/{TagId}",
|
|
|
|
"delete_tag_items": "https://storeapi.kobo.com/v1/library/tags/{TagId}/items/delete",
|
|
|
|
"device_auth": "https://storeapi.kobo.com/v1/auth/device",
|
|
|
|
"device_refresh": "https://storeapi.kobo.com/v1/auth/refresh",
|
|
|
|
"dictionary_host": "https://kbdownload1-a.akamaihd.net",
|
|
|
|
"discovery_host": "https://discovery.kobobooks.com",
|
|
|
|
"eula_page": "https://www.kobo.com/termsofuse?style=onestore",
|
|
|
|
"exchange_auth": "https://storeapi.kobo.com/v1/auth/exchange",
|
|
|
|
"external_book": "https://storeapi.kobo.com/v1/products/books/external/{Ids}",
|
|
|
|
"facebook_sso_page": "https://authorize.kobo.com/signin/provider/Facebook/login?returnUrl=http://store.kobobooks.com/",
|
|
|
|
"featured_list": "https://storeapi.kobo.com/v1/products/featured/{FeaturedListId}",
|
|
|
|
"featured_lists": "https://storeapi.kobo.com/v1/products/featured",
|
|
|
|
"free_books_page": {
|
|
|
|
"EN": "https://www.kobo.com/{region}/{language}/p/free-ebooks",
|
|
|
|
"FR": "https://www.kobo.com/{region}/{language}/p/livres-gratuits",
|
|
|
|
"IT": "https://www.kobo.com/{region}/{language}/p/libri-gratuiti",
|
|
|
|
"NL": "https://www.kobo.com/{region}/{language}/List/bekijk-het-overzicht-van-gratis-ebooks/QpkkVWnUw8sxmgjSlCbJRg",
|
|
|
|
"PT": "https://www.kobo.com/{region}/{language}/p/livros-gratis",
|
|
|
|
},
|
|
|
|
"fte_feedback": "https://storeapi.kobo.com/v1/products/ftefeedback",
|
|
|
|
"get_tests_request": "https://storeapi.kobo.com/v1/analytics/gettests",
|
|
|
|
"giftcard_epd_redeem_url": "https://www.kobo.com/{storefront}/{language}/redeem-ereader",
|
|
|
|
"giftcard_redeem_url": "https://www.kobo.com/{storefront}/{language}/redeem",
|
2020-05-09 17:11:56 +02:00
|
|
|
"help_page": "https://www.kobo.com/help",
|
2020-01-26 14:42:53 +01:00
|
|
|
"kobo_audiobooks_enabled": "False",
|
|
|
|
"kobo_audiobooks_orange_deal_enabled": "False",
|
|
|
|
"kobo_audiobooks_subscriptions_enabled": "False",
|
|
|
|
"kobo_nativeborrow_enabled": "True",
|
|
|
|
"kobo_onestorelibrary_enabled": "False",
|
|
|
|
"kobo_redeem_enabled": "True",
|
|
|
|
"kobo_shelfie_enabled": "False",
|
|
|
|
"kobo_subscriptions_enabled": "False",
|
|
|
|
"kobo_superpoints_enabled": "False",
|
|
|
|
"kobo_wishlist_enabled": "True",
|
|
|
|
"library_book": "https://storeapi.kobo.com/v1/user/library/books/{LibraryItemId}",
|
|
|
|
"library_items": "https://storeapi.kobo.com/v1/user/library",
|
|
|
|
"library_metadata": "https://storeapi.kobo.com/v1/library/{Ids}/metadata",
|
|
|
|
"library_prices": "https://storeapi.kobo.com/v1/user/library/previews/prices",
|
|
|
|
"library_stack": "https://storeapi.kobo.com/v1/user/library/stacks/{LibraryItemId}",
|
|
|
|
"library_sync": "https://storeapi.kobo.com/v1/library/sync",
|
|
|
|
"love_dashboard_page": "https://store.kobobooks.com/{culture}/kobosuperpoints",
|
|
|
|
"love_points_redemption_page": "https://store.kobobooks.com/{culture}/KoboSuperPointsRedemption?productId={ProductId}",
|
|
|
|
"magazine_landing_page": "https://store.kobobooks.com/emagazines",
|
|
|
|
"notifications_registration_issue": "https://storeapi.kobo.com/v1/notifications/registration",
|
|
|
|
"oauth_host": "https://oauth.kobo.com",
|
|
|
|
"overdrive_account": "https://auth.overdrive.com/account",
|
|
|
|
"overdrive_library": "https://{libraryKey}.auth.overdrive.com/library",
|
|
|
|
"overdrive_library_finder_host": "https://libraryfinder.api.overdrive.com",
|
|
|
|
"overdrive_thunder_host": "https://thunder.api.overdrive.com",
|
|
|
|
"password_retrieval_page": "https://www.kobobooks.com/passwordretrieval.html",
|
|
|
|
"post_analytics_event": "https://storeapi.kobo.com/v1/analytics/event",
|
|
|
|
"privacy_page": "https://www.kobo.com/privacypolicy?style=onestore",
|
|
|
|
"product_nextread": "https://storeapi.kobo.com/v1/products/{ProductIds}/nextread",
|
|
|
|
"product_prices": "https://storeapi.kobo.com/v1/products/{ProductIds}/prices",
|
|
|
|
"product_recommendations": "https://storeapi.kobo.com/v1/products/{ProductId}/recommendations",
|
|
|
|
"product_reviews": "https://storeapi.kobo.com/v1/products/{ProductIds}/reviews",
|
|
|
|
"products": "https://storeapi.kobo.com/v1/products",
|
|
|
|
"provider_external_sign_in_page": "https://authorize.kobo.com/ExternalSignIn/{providerName}?returnUrl=http://store.kobobooks.com/",
|
|
|
|
"purchase_buy": "https://www.kobo.com/checkout/createpurchase/",
|
|
|
|
"purchase_buy_templated": "https://www.kobo.com/{culture}/checkout/createpurchase/{ProductId}",
|
|
|
|
"quickbuy_checkout": "https://storeapi.kobo.com/v1/store/quickbuy/{PurchaseId}/checkout",
|
|
|
|
"quickbuy_create": "https://storeapi.kobo.com/v1/store/quickbuy/purchase",
|
|
|
|
"rating": "https://storeapi.kobo.com/v1/products/{ProductId}/rating/{Rating}",
|
|
|
|
"reading_state": "https://storeapi.kobo.com/v1/library/{Ids}/state",
|
|
|
|
"redeem_interstitial_page": "https://store.kobobooks.com",
|
|
|
|
"registration_page": "https://authorize.kobo.com/signup?returnUrl=http://store.kobobooks.com/",
|
|
|
|
"related_items": "https://storeapi.kobo.com/v1/products/{Id}/related",
|
|
|
|
"remaining_book_series": "https://storeapi.kobo.com/v1/products/books/series/{SeriesId}",
|
|
|
|
"rename_tag": "https://storeapi.kobo.com/v1/library/tags/{TagId}",
|
|
|
|
"review": "https://storeapi.kobo.com/v1/products/reviews/{ReviewId}",
|
|
|
|
"review_sentiment": "https://storeapi.kobo.com/v1/products/reviews/{ReviewId}/sentiment/{Sentiment}",
|
|
|
|
"shelfie_recommendations": "https://storeapi.kobo.com/v1/user/recommendations/shelfie",
|
|
|
|
"sign_in_page": "https://authorize.kobo.com/signin?returnUrl=http://store.kobobooks.com/",
|
|
|
|
"social_authorization_host": "https://social.kobobooks.com:8443",
|
|
|
|
"social_host": "https://social.kobobooks.com",
|
|
|
|
"stacks_host_productId": "https://store.kobobooks.com/collections/byproductid/",
|
|
|
|
"store_home": "www.kobo.com/{region}/{language}",
|
|
|
|
"store_host": "store.kobobooks.com",
|
|
|
|
"store_newreleases": "https://store.kobobooks.com/{culture}/List/new-releases/961XUjtsU0qxkFItWOutGA",
|
|
|
|
"store_search": "https://store.kobobooks.com/{culture}/Search?Query={query}",
|
|
|
|
"store_top50": "https://store.kobobooks.com/{culture}/ebooks/Top",
|
|
|
|
"tag_items": "https://storeapi.kobo.com/v1/library/tags/{TagId}/Items",
|
|
|
|
"tags": "https://storeapi.kobo.com/v1/library/tags",
|
|
|
|
"taste_profile": "https://storeapi.kobo.com/v1/products/tasteprofile",
|
|
|
|
"update_accessibility_to_preview": "https://storeapi.kobo.com/v1/library/{EntitlementIds}/preview",
|
|
|
|
"use_one_store": "False",
|
|
|
|
"user_loyalty_benefits": "https://storeapi.kobo.com/v1/user/loyalty/benefits",
|
|
|
|
"user_platform": "https://storeapi.kobo.com/v1/user/platform",
|
|
|
|
"user_profile": "https://storeapi.kobo.com/v1/user/profile",
|
|
|
|
"user_ratings": "https://storeapi.kobo.com/v1/user/ratings",
|
|
|
|
"user_recommendations": "https://storeapi.kobo.com/v1/user/recommendations",
|
|
|
|
"user_reviews": "https://storeapi.kobo.com/v1/user/reviews",
|
|
|
|
"user_wishlist": "https://storeapi.kobo.com/v1/user/wishlist",
|
|
|
|
"userguide_host": "https://kbdownload1-a.akamaihd.net",
|
|
|
|
"wishlist_page": "https://store.kobobooks.com/{region}/{language}/account/wishlist",
|
|
|
|
}
|