2019-07-13 18:45:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs, cervinko, jkrehm, bodybybuddha, ok11,
|
|
|
|
# andy29485, idalin, Kyosfonica, wuqi, Kennyl, lemmsh,
|
|
|
|
# falgh1, grunjol, csitko, ytils, xybydy, trasba, vrabe,
|
|
|
|
# ruben-herold, marblepebble, JackED42, SiphonSquirrel,
|
|
|
|
# apetresc, nanu-c, mutschler
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import datetime
|
2023-08-24 11:49:22 +00:00
|
|
|
import json
|
2021-11-13 13:57:01 +00:00
|
|
|
from urllib.parse import unquote_plus
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2024-05-07 06:27:55 +00:00
|
|
|
from flask import Blueprint, request, render_template, make_response, abort, Response, g
|
2022-04-26 09:28:20 +00:00
|
|
|
from flask_babel import get_locale
|
2023-02-04 13:51:41 +00:00
|
|
|
from flask_babel import gettext as _
|
2024-07-14 14:24:07 +00:00
|
|
|
|
2024-07-13 10:17:48 +00:00
|
|
|
|
2022-03-26 18:35:56 +00:00
|
|
|
from sqlalchemy.sql.expression import func, text, or_, and_, true
|
|
|
|
from sqlalchemy.exc import InvalidRequestError, OperationalError
|
2022-04-26 09:28:20 +00:00
|
|
|
|
2024-05-07 05:30:57 +00:00
|
|
|
from . import logger, config, db, calibre_db, ub, isoLanguages, constants
|
2024-07-14 14:24:07 +00:00
|
|
|
from .usermanagement import requires_basic_auth_if_no_ano, auth
|
2020-05-23 08:16:29 +00:00
|
|
|
from .helper import get_download_link, get_book_cover
|
2019-07-13 18:45:48 +00:00
|
|
|
from .pagination import Pagination
|
2020-12-12 10:23:17 +00:00
|
|
|
from .web import render_read_books
|
2024-07-14 14:24:07 +00:00
|
|
|
|
2022-04-26 09:28:20 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
opds = Blueprint('opds', __name__)
|
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/")
|
2019-08-03 10:56:32 +00:00
|
|
|
@opds.route("/opds")
|
2019-07-13 18:45:48 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_index():
|
|
|
|
return render_xml_template('index.xml')
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/osd")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_osd():
|
|
|
|
return render_xml_template('osd.xml', lang='en-EN')
|
|
|
|
|
|
|
|
|
2023-05-27 14:25:06 +00:00
|
|
|
# @opds.route("/opds/search", defaults={'query': ""})
|
2021-11-13 13:57:01 +00:00
|
|
|
@opds.route("/opds/search/<path:query>")
|
2019-07-13 18:45:48 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_cc_search(query):
|
2021-11-13 13:57:01 +00:00
|
|
|
# Handle strange query from Libera Reader with + instead of spaces
|
2022-03-28 11:58:41 +00:00
|
|
|
plus_query = unquote_plus(request.environ['RAW_URI'].split('/opds/search/')[1]).strip()
|
2021-11-13 13:57:01 +00:00
|
|
|
return feed_search(plus_query)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/search", methods=["GET"])
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_normal_search():
|
2021-03-21 10:54:39 +00:00
|
|
|
return feed_search(request.args.get("query", "").strip())
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-03-21 19:14:17 +00:00
|
|
|
@opds.route("/opds/books")
|
|
|
|
@requires_basic_auth_if_no_ano
|
2021-03-22 16:46:15 +00:00
|
|
|
def feed_booksindex():
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_element_index(db.Books.sort, None, 'opds.feed_letter_books')
|
2021-03-22 16:46:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/books/letter/<book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_letter_books(book_id):
|
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
letter = true() if book_id == "00" else func.upper(db.Books.sort).startswith(book_id)
|
2021-03-21 19:14:17 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), 0,
|
2021-03-22 16:46:15 +00:00
|
|
|
db.Books,
|
|
|
|
letter,
|
2022-03-26 18:35:56 +00:00
|
|
|
[db.Books.sort],
|
|
|
|
True, config.config_read_column)
|
2021-03-22 16:46:15 +00:00
|
|
|
|
2021-03-21 19:14:17 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@opds.route("/opds/new")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_new():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_RECENT):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2019-07-13 18:45:48 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), 0,
|
2022-03-26 18:35:56 +00:00
|
|
|
db.Books, True, [db.Books.timestamp.desc()],
|
|
|
|
True, config.config_read_column)
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/discover")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_discover():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_RANDOM):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2022-03-26 18:35:56 +00:00
|
|
|
query = calibre_db.generate_linked_query(config.config_read_column, db.Books)
|
|
|
|
entries = query.filter(calibre_db.common_filters()).order_by(func.random()).limit(config.config_books_per_page)
|
2019-07-13 18:45:48 +00:00
|
|
|
pagination = Pagination(1, config.config_books_per_page, int(config.config_books_per_page))
|
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/rated")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_best_rated():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_BEST_RATED):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2019-07-13 18:45:48 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books, db.Books.ratings.any(db.Ratings.rating > 9),
|
2022-03-26 18:35:56 +00:00
|
|
|
[db.Books.timestamp.desc()],
|
|
|
|
True, config.config_read_column)
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/hot")
|
2024-07-15 16:38:56 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
def feed_hot():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_HOT):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2019-07-13 18:45:48 +00:00
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
all_books = ub.session.query(ub.Downloads, func.count(ub.Downloads.book_id)).order_by(
|
|
|
|
func.count(ub.Downloads.book_id).desc()).group_by(ub.Downloads.book_id)
|
|
|
|
hot_books = all_books.offset(off).limit(config.config_books_per_page)
|
|
|
|
entries = list()
|
|
|
|
for book in hot_books:
|
2022-03-26 18:35:56 +00:00
|
|
|
query = calibre_db.generate_linked_query(config.config_read_column, db.Books)
|
|
|
|
download_book = query.filter(calibre_db.common_filters()).filter(
|
|
|
|
book.Downloads.book_id == db.Books.id).first()
|
2022-03-13 11:34:21 +00:00
|
|
|
if download_book:
|
2022-03-26 18:35:56 +00:00
|
|
|
entries.append(download_book)
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
ub.delete_download(book.Downloads.book_id)
|
2022-03-13 11:34:21 +00:00
|
|
|
num_books = entries.__len__()
|
2019-07-13 18:45:48 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1),
|
2022-03-13 11:34:21 +00:00
|
|
|
config.config_books_per_page, num_books)
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/author")
|
2024-07-15 16:38:56 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2019-07-13 18:45:48 +00:00
|
|
|
def feed_authorindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_AUTHOR):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_element_index(db.Authors.sort, db.books_authors_link, 'opds.feed_letter_author')
|
2021-03-21 19:14:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/author/letter/<book_id>")
|
2024-07-15 16:38:56 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2021-03-21 19:14:17 +00:00
|
|
|
def feed_letter_author(book_id):
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_AUTHOR):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2021-03-21 19:14:17 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2021-03-22 16:46:15 +00:00
|
|
|
letter = true() if book_id == "00" else func.upper(db.Authors.sort).startswith(book_id)
|
2020-05-23 08:16:29 +00:00
|
|
|
entries = calibre_db.session.query(db.Authors).join(db.books_authors_link).join(db.Books)\
|
2021-03-21 19:14:17 +00:00
|
|
|
.filter(calibre_db.common_filters()).filter(letter)\
|
2020-05-23 08:16:29 +00:00
|
|
|
.group_by(text('books_authors_link.author'))\
|
2021-03-22 18:01:18 +00:00
|
|
|
.order_by(db.Authors.sort)
|
2019-07-13 18:45:48 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
2021-03-22 18:01:18 +00:00
|
|
|
entries.count())
|
|
|
|
entries = entries.limit(config.config_books_per_page).offset(off).all()
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='opds.feed_author', pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/author/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_author(book_id):
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_xml_dataset(db.Authors, book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/publisher")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_publisherindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_PUBLISHER):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2019-07-13 18:45:48 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2020-05-23 08:16:29 +00:00
|
|
|
entries = calibre_db.session.query(db.Publishers)\
|
|
|
|
.join(db.books_publishers_link)\
|
|
|
|
.join(db.Books).filter(calibre_db.common_filters())\
|
|
|
|
.group_by(text('books_publishers_link.publisher'))\
|
|
|
|
.order_by(db.Publishers.sort)\
|
2020-04-19 17:08:58 +00:00
|
|
|
.limit(config.config_books_per_page).offset(off)
|
2019-07-13 18:45:48 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
2020-05-21 16:16:11 +00:00
|
|
|
len(calibre_db.session.query(db.Publishers).all()))
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='opds.feed_publisher', pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/publisher/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_publisher(book_id):
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_xml_dataset(db.Publishers, book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/category")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_categoryindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_CATEGORY):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_element_index(db.Tags.name, db.books_tags_link, 'opds.feed_letter_category')
|
2021-03-22 16:46:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/category/letter/<book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_letter_category(book_id):
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_CATEGORY):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2021-03-22 16:46:15 +00:00
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
letter = true() if book_id == "00" else func.upper(db.Tags.name).startswith(book_id)
|
2020-05-23 08:16:29 +00:00
|
|
|
entries = calibre_db.session.query(db.Tags)\
|
|
|
|
.join(db.books_tags_link)\
|
|
|
|
.join(db.Books)\
|
2021-03-22 16:46:15 +00:00
|
|
|
.filter(calibre_db.common_filters()).filter(letter)\
|
2020-05-23 08:16:29 +00:00
|
|
|
.group_by(text('books_tags_link.tag'))\
|
2021-03-22 18:01:18 +00:00
|
|
|
.order_by(db.Tags.name)
|
2019-07-13 18:45:48 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
2021-03-22 18:01:18 +00:00
|
|
|
entries.count())
|
|
|
|
entries = entries.offset(off).limit(config.config_books_per_page).all()
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='opds.feed_category', pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/category/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_category(book_id):
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_xml_dataset(db.Tags, book_id)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/series")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_seriesindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_SERIES):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_element_index(db.Series.sort, db.books_series_link, 'opds.feed_letter_series')
|
|
|
|
|
2021-03-22 16:46:15 +00:00
|
|
|
|
|
|
|
@opds.route("/opds/series/letter/<book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_letter_series(book_id):
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_SERIES):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2021-03-22 16:46:15 +00:00
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
letter = true() if book_id == "00" else func.upper(db.Series.sort).startswith(book_id)
|
2020-05-23 08:16:29 +00:00
|
|
|
entries = calibre_db.session.query(db.Series)\
|
|
|
|
.join(db.books_series_link)\
|
|
|
|
.join(db.Books)\
|
2021-03-22 16:46:15 +00:00
|
|
|
.filter(calibre_db.common_filters()).filter(letter)\
|
2020-05-23 08:16:29 +00:00
|
|
|
.group_by(text('books_series_link.series'))\
|
2021-03-22 18:01:18 +00:00
|
|
|
.order_by(db.Series.sort)
|
2019-07-13 18:45:48 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
2021-03-22 18:01:18 +00:00
|
|
|
entries.count())
|
|
|
|
entries = entries.offset(off).limit(config.config_books_per_page).all()
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=entries, folder='opds.feed_series', pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/series/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_series(book_id):
|
|
|
|
off = request.args.get("offset") or 0
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.series.any(db.Series.id == book_id),
|
2022-03-26 18:35:56 +00:00
|
|
|
[db.Books.series_index],
|
|
|
|
True, config.config_read_column)
|
2020-04-02 17:07:41 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2020-03-07 10:07:35 +00:00
|
|
|
@opds.route("/opds/ratings")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_ratingindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_RATING):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2020-03-07 10:07:35 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2020-05-21 16:16:11 +00:00
|
|
|
entries = calibre_db.session.query(db.Ratings, func.count('books_ratings_link.book').label('count'),
|
2022-03-13 11:34:21 +00:00
|
|
|
(db.Ratings.rating / 2).label('name')) \
|
2020-05-23 08:16:29 +00:00
|
|
|
.join(db.books_ratings_link)\
|
|
|
|
.join(db.Books)\
|
|
|
|
.filter(calibre_db.common_filters()) \
|
|
|
|
.group_by(text('books_ratings_link.rating'))\
|
|
|
|
.order_by(db.Ratings.rating).all()
|
2020-03-07 10:07:35 +00:00
|
|
|
|
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
len(entries))
|
|
|
|
element = list()
|
|
|
|
for entry in entries:
|
2021-03-22 16:46:15 +00:00
|
|
|
element.append(FeedObject(entry[0].id, _("{} Stars").format(entry.name)))
|
2020-03-07 10:07:35 +00:00
|
|
|
return render_xml_template('feed.xml', listelements=element, folder='opds.feed_ratings', pagination=pagination)
|
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2020-03-07 10:07:35 +00:00
|
|
|
@opds.route("/opds/ratings/<book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_ratings(book_id):
|
2022-03-14 16:12:35 +00:00
|
|
|
return render_xml_dataset(db.Ratings, book_id)
|
2020-04-02 16:23:24 +00:00
|
|
|
|
2020-03-07 10:07:35 +00:00
|
|
|
|
2019-12-15 12:32:34 +00:00
|
|
|
@opds.route("/opds/formats")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_formatindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_FORMAT):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2019-12-15 12:32:34 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2020-05-23 08:16:29 +00:00
|
|
|
entries = calibre_db.session.query(db.Data).join(db.Books)\
|
|
|
|
.filter(calibre_db.common_filters()) \
|
|
|
|
.group_by(db.Data.format)\
|
|
|
|
.order_by(db.Data.format).all()
|
2019-12-15 12:32:34 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
len(entries))
|
2020-03-07 10:07:35 +00:00
|
|
|
element = list()
|
2019-12-15 12:32:34 +00:00
|
|
|
for entry in entries:
|
2020-03-07 10:07:35 +00:00
|
|
|
element.append(FeedObject(entry.format, entry.format))
|
|
|
|
return render_xml_template('feed.xml', listelements=element, folder='opds.feed_format', pagination=pagination)
|
2019-12-15 12:32:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/formats/<book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_format(book_id):
|
|
|
|
off = request.args.get("offset") or 0
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.data.any(db.Data.format == book_id.upper()),
|
2022-03-26 18:35:56 +00:00
|
|
|
[db.Books.timestamp.desc()],
|
|
|
|
True, config.config_read_column)
|
2020-04-02 17:07:41 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2020-04-02 16:23:24 +00:00
|
|
|
|
2019-12-15 12:32:34 +00:00
|
|
|
|
|
|
|
@opds.route("/opds/language")
|
|
|
|
@opds.route("/opds/language/")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_languagesindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().check_visibility(constants.SIDEBAR_LANGUAGE):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2019-12-15 12:32:34 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2024-07-13 10:17:48 +00:00
|
|
|
if auth.current_user().filter_language() == "all":
|
2020-05-23 08:16:29 +00:00
|
|
|
languages = calibre_db.speaking_language()
|
2019-12-15 12:32:34 +00:00
|
|
|
else:
|
2020-05-21 16:16:11 +00:00
|
|
|
languages = calibre_db.session.query(db.Languages).filter(
|
2024-07-13 10:17:48 +00:00
|
|
|
db.Languages.lang_code == auth.current_user().filter_language()).all()
|
2021-09-29 17:00:02 +00:00
|
|
|
languages[0].name = isoLanguages.get_language_name(get_locale(), languages[0].lang_code)
|
2019-12-15 12:32:34 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
len(languages))
|
|
|
|
return render_xml_template('feed.xml', listelements=languages, folder='opds.feed_languages', pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/language/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_languages(book_id):
|
|
|
|
off = request.args.get("offset") or 0
|
2020-06-06 19:21:10 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), 0,
|
2020-05-23 08:16:29 +00:00
|
|
|
db.Books,
|
|
|
|
db.Books.languages.any(db.Languages.id == book_id),
|
2022-03-26 18:35:56 +00:00
|
|
|
[db.Books.timestamp.desc()],
|
|
|
|
True, config.config_read_column)
|
2020-04-02 17:07:41 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-03-07 10:07:35 +00:00
|
|
|
|
2020-04-02 18:18:24 +00:00
|
|
|
@opds.route("/opds/shelfindex")
|
2019-07-13 18:45:48 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2020-04-02 16:23:24 +00:00
|
|
|
def feed_shelfindex():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not (auth.current_user().is_authenticated or g.allow_anonymous):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2019-07-13 18:45:48 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2023-02-04 13:51:41 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(
|
2024-07-13 10:17:48 +00:00
|
|
|
or_(ub.Shelf.is_public == 1, ub.Shelf.user_id == auth.current_user().id)).order_by(ub.Shelf.name).all()
|
2020-04-02 16:23:24 +00:00
|
|
|
number = len(shelf)
|
2019-07-13 18:45:48 +00:00
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
number)
|
|
|
|
return render_xml_template('feed.xml', listelements=shelf, folder='opds.feed_shelf', pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/shelf/<int:book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_shelf(book_id):
|
2024-07-13 10:17:48 +00:00
|
|
|
if not (auth.current_user().is_authenticated or g.allow_anonymous):
|
2024-05-07 05:30:57 +00:00
|
|
|
abort(404)
|
2024-05-07 06:27:55 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2024-07-13 10:17:48 +00:00
|
|
|
if auth.current_user().is_anonymous:
|
2020-04-19 17:08:58 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.is_public == 1,
|
2020-04-26 06:40:53 +00:00
|
|
|
ub.Shelf.id == book_id).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2024-07-13 10:17:48 +00:00
|
|
|
shelf = ub.session.query(ub.Shelf).filter(or_(and_(ub.Shelf.user_id == int(auth.current_user().id),
|
2019-07-13 18:45:48 +00:00
|
|
|
ub.Shelf.id == book_id),
|
|
|
|
and_(ub.Shelf.is_public == 1,
|
2020-04-26 06:40:53 +00:00
|
|
|
ub.Shelf.id == book_id))).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
result = list()
|
2024-06-18 18:13:26 +00:00
|
|
|
pagination = list()
|
2019-07-13 18:45:48 +00:00
|
|
|
# user is allowed to access shelf
|
|
|
|
if shelf:
|
2022-03-26 18:35:56 +00:00
|
|
|
result, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
|
|
|
config.config_books_per_page,
|
|
|
|
db.Books,
|
|
|
|
ub.BookShelf.shelf == shelf.id,
|
|
|
|
[ub.BookShelf.order.asc()],
|
|
|
|
True, config.config_read_column,
|
|
|
|
ub.BookShelf, ub.BookShelf.book_id == db.Books.id)
|
|
|
|
# delete shelf entries where book is not existent anymore, can happen if book is deleted outside calibre-web
|
|
|
|
wrong_entries = calibre_db.session.query(ub.BookShelf) \
|
|
|
|
.join(db.Books, ub.BookShelf.book_id == db.Books.id, isouter=True) \
|
|
|
|
.filter(db.Books.id == None).all()
|
|
|
|
for entry in wrong_entries:
|
|
|
|
log.info('Not existing book {} in {} deleted'.format(entry.book_id, shelf))
|
|
|
|
try:
|
|
|
|
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == entry.book_id).delete()
|
|
|
|
ub.session.commit()
|
|
|
|
except (OperationalError, InvalidRequestError) as e:
|
|
|
|
ub.session.rollback()
|
|
|
|
log.error_or_exception("Settings Database error: {}".format(e))
|
2020-04-02 17:07:41 +00:00
|
|
|
return render_xml_template('feed.xml', entries=result, pagination=pagination)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/download/<book_id>/<book_format>/")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def opds_download_link(book_id, book_format):
|
2024-07-13 10:17:48 +00:00
|
|
|
if not auth.current_user().role_download():
|
2024-10-19 04:36:03 +00:00
|
|
|
return abort(401)
|
|
|
|
client = "kobo" if "Kobo" in request.headers.get('User-Agent') else ""
|
2020-05-21 11:54:28 +00:00
|
|
|
return get_download_link(book_id, book_format.lower(), client)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2019-11-07 20:04:03 +00:00
|
|
|
@opds.route("/ajax/book/<string:uuid>/<library>")
|
2020-04-19 17:08:58 +00:00
|
|
|
@opds.route("/ajax/book/<string:uuid>", defaults={'library': ""})
|
2019-07-13 18:45:48 +00:00
|
|
|
@requires_basic_auth_if_no_ano
|
2019-11-07 20:04:03 +00:00
|
|
|
def get_metadata_calibre_companion(uuid, library):
|
2020-05-21 16:16:11 +00:00
|
|
|
entry = calibre_db.session.query(db.Books).filter(db.Books.uuid.like("%" + uuid + "%")).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
if entry is not None:
|
|
|
|
js = render_template('json.txt', entry=entry)
|
|
|
|
response = make_response(js)
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2023-08-24 11:49:22 +00:00
|
|
|
@opds.route("/opds/stats")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def get_database_stats():
|
|
|
|
stat = dict()
|
|
|
|
stat['books'] = calibre_db.session.query(db.Books).count()
|
|
|
|
stat['authors'] = calibre_db.session.query(db.Authors).count()
|
|
|
|
stat['categories'] = calibre_db.session.query(db.Tags).count()
|
|
|
|
stat['series'] = calibre_db.session.query(db.Series).count()
|
|
|
|
return Response(json.dumps(stat), mimetype="application/json")
|
|
|
|
|
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
@opds.route("/opds/thumb_240_240/<book_id>")
|
|
|
|
@opds.route("/opds/cover_240_240/<book_id>")
|
|
|
|
@opds.route("/opds/cover_90_90/<book_id>")
|
|
|
|
@opds.route("/opds/cover/<book_id>")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_get_cover(book_id):
|
|
|
|
return get_book_cover(book_id)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/readbooks")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_read_books():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not (auth.current_user().check_visibility(constants.SIDEBAR_READ_AND_UNREAD) and not auth.current_user().is_anonymous):
|
2024-05-07 05:30:57 +00:00
|
|
|
return abort(403)
|
2022-03-13 11:34:21 +00:00
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
result, pagination = render_read_books(int(off) / (int(config.config_books_per_page)) + 1, True, True)
|
|
|
|
return render_xml_template('feed.xml', entries=result, pagination=pagination)
|
|
|
|
|
|
|
|
|
|
|
|
@opds.route("/opds/unreadbooks")
|
|
|
|
@requires_basic_auth_if_no_ano
|
|
|
|
def feed_unread_books():
|
2024-07-13 10:17:48 +00:00
|
|
|
if not (auth.current_user().check_visibility(constants.SIDEBAR_READ_AND_UNREAD) and not auth.current_user().is_anonymous):
|
2024-05-07 05:30:57 +00:00
|
|
|
return abort(403)
|
2022-03-13 11:34:21 +00:00
|
|
|
off = request.args.get("offset") or 0
|
|
|
|
result, pagination = render_read_books(int(off) / (int(config.config_books_per_page)) + 1, False, True)
|
|
|
|
return render_xml_template('feed.xml', entries=result, pagination=pagination)
|
|
|
|
|
|
|
|
|
2022-04-26 12:44:55 +00:00
|
|
|
class FeedObject:
|
|
|
|
def __init__(self, rating_id, rating_name):
|
|
|
|
self.rating_id = rating_id
|
|
|
|
self.rating_name = rating_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.rating_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.rating_name
|
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def feed_search(term):
|
|
|
|
if term:
|
2022-03-27 12:07:58 +00:00
|
|
|
entries, __, ___ = calibre_db.get_search_results(term, config=config)
|
2021-12-05 12:04:13 +00:00
|
|
|
entries_count = len(entries) if len(entries) > 0 else 1
|
|
|
|
pagination = Pagination(1, entries_count, entries_count)
|
2022-03-26 18:35:56 +00:00
|
|
|
return render_xml_template('feed.xml', searchterm=term, entries=entries, pagination=pagination)
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
return render_xml_template('feed.xml', searchterm="")
|
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def render_xml_template(*args, **kwargs):
|
2020-04-19 17:08:58 +00:00
|
|
|
# ToDo: return time in current timezone similar to %z
|
2019-07-13 18:45:48 +00:00
|
|
|
currtime = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S+00:00")
|
2024-05-07 05:30:57 +00:00
|
|
|
xml = render_template(current_time=currtime, instance=config.config_calibre_web_title, constants=constants.sidebar_settings, *args, **kwargs)
|
2019-07-13 18:45:48 +00:00
|
|
|
response = make_response(xml)
|
|
|
|
response.headers["Content-Type"] = "application/atom+xml; charset=utf-8"
|
|
|
|
return response
|
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
def render_xml_dataset(data_table, book_id):
|
2019-07-13 18:45:48 +00:00
|
|
|
off = request.args.get("offset") or 0
|
2022-03-13 11:34:21 +00:00
|
|
|
entries, __, pagination = calibre_db.fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), 0,
|
|
|
|
db.Books,
|
2022-03-14 16:12:35 +00:00
|
|
|
getattr(db.Books, data_table.__tablename__).any(data_table.id == book_id),
|
2022-03-26 18:35:56 +00:00
|
|
|
[db.Books.timestamp.desc()],
|
|
|
|
True, config.config_read_column)
|
2022-03-13 11:34:21 +00:00
|
|
|
return render_xml_template('feed.xml', entries=entries, pagination=pagination)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-04-19 17:08:58 +00:00
|
|
|
|
2022-03-13 11:34:21 +00:00
|
|
|
def render_element_index(database_column, linked_table, folder):
|
|
|
|
shift = 0
|
|
|
|
off = int(request.args.get("offset") or 0)
|
2022-03-26 18:35:56 +00:00
|
|
|
entries = calibre_db.session.query(func.upper(func.substr(database_column, 1, 1)).label('id'), None, None)
|
|
|
|
# query = calibre_db.generate_linked_query(config.config_read_column, db.Books)
|
2022-03-14 16:12:35 +00:00
|
|
|
if linked_table is not None:
|
2022-03-13 11:34:21 +00:00
|
|
|
entries = entries.join(linked_table).join(db.Books)
|
|
|
|
entries = entries.filter(calibre_db.common_filters()).group_by(func.upper(func.substr(database_column, 1, 1))).all()
|
|
|
|
elements = []
|
2023-11-06 15:35:39 +00:00
|
|
|
if off == 0 and entries:
|
2022-03-13 11:34:21 +00:00
|
|
|
elements.append({'id': "00", 'name': _("All")})
|
|
|
|
shift = 1
|
|
|
|
for entry in entries[
|
|
|
|
off + shift - 1:
|
|
|
|
int(off + int(config.config_books_per_page) - shift)]:
|
|
|
|
elements.append({'id': entry.id, 'name': entry.id})
|
|
|
|
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
|
|
|
len(entries) + 1)
|
|
|
|
return render_xml_template('feed.xml',
|
|
|
|
letterelements=elements,
|
|
|
|
folder=folder,
|
|
|
|
pagination=pagination)
|