Files
komgAPI/src/komgapi/endpoints/common_controller.py
2025-02-17 20:28:17 +01:00

92 lines
2.9 KiB
Python

from .baseapi import BaseAPI
import pathlib
import subprocess
import requests
import typing_extensions
from komgapi.schemas import *
class CommonBookController(BaseAPI):
def __init__(self, username, password, url, timeout=20) -> None:
super().__init__(username, password, url, timeout)
def getBookFile(
self, book_id: str, download_path: str = "~/Downloads"
) -> pathlib.Path:
"""Download the book file.
Args:
----
- book_id (str): the ID of the book to download.
- download_path (str, optional): The path to download the file to. Defaults to "~/Downloads".
Returns:
-------
- pathlib.Path: The path to the downloaded file.
"""
url = self.url + f"books/{book_id}/file"
download_path = pathlib.Path(download_path).expanduser()
subprocess.run(
[
"curl",
"-u",
f"{self._username}:{self._password}",
"-o",
f"{download_path}/{book_id}.zip",
url,
]
)
return pathlib.Path(f"{download_path}/{book_id}.zip")
def getRawPage(self, book_id: str, page: int) -> bytes:
"""Get the raw page of a book.
Args:
----
- book_id (str): the ID of the book to get the page from.
- page (int): the page to get.
Returns:
-------
- bytes: the page as a bytestring.
"""
url = self.url + f"books/{book_id}/pages/{page}/raw"
data = requests.get(url, auth=(self._username, self._password))
return data.content
def getPageThumbnail(self, book_id: str, page: int) -> bytes:
"""Get the thumbnail of a page.
Args:
----
- book_id (str): the ID of the book to get the page from.
- page (int): the page to get.
Returns:
-------
- bytes: the thumbnail as a bytestring.
"""
url = self.url + f"books/{book_id}/pages/{page}/thumbnail"
ic(url)
data = requests.get(url, auth=(self._username, self._password))
return data.content
def getProgress(self, book_id: str) -> Progress:
"""Get the progress of a book. The progress contains a timestamp of the latest modification, the device where the modification was made (if the data is present), and a Locator object.
Args:
----
- book_id (str): the ID of the book to get the progress from.
Returns:
-------
- Progress: the progress wrapped in a Progress class.
"""
url = self.url + f"books/{book_id}/progression"
data = self.getRequest(url)
return Progress(**data)
@typing_extensions.deprecated("This function is not implemented yet.")
def getbookRessource(self, book_id: str, ressource: str) -> bytes:
raise NotImplementedError