Skip to content

Commit

Permalink
moved epub inside localfilehandler
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanelteren committed Nov 14, 2024
1 parent ca21571 commit d5c2e17
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 99 deletions.
3 changes: 0 additions & 3 deletions paper2remarkable/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from paper2remarkable.providers.epub import EPUBProvider
from .acl import ACL
from .acm import ACM
from .arxiv import Arxiv
Expand All @@ -20,7 +19,6 @@
from .pubmed import PubMed
from .semantic_scholar import SemanticScholar
from .springer import Springer
from .epub import EPUBProvider

# # The following providers are no longer functional due to Cloudflare blocking
# # automated access, and have therefore been removed from the list of providers
Expand Down Expand Up @@ -51,5 +49,4 @@
LocalFile,
PdfUrl,
HTML,
EPUBProvider
]
56 changes: 42 additions & 14 deletions paper2remarkable/providers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
class Provider(metaclass=abc.ABCMeta):
"""ABC for providers of pdf sources"""

SUPPORTED_FORMATS = ["pdf", "ps", "epub"]
def __init__(
self,
verbose=False,
Expand Down Expand Up @@ -77,20 +78,41 @@ def __init__(
logger.disable()

# Define the operations to run on the pdf. Providers can add others.
self.operations = [("rewrite", self.rewrite_pdf)]
if crop == "center":
self.operations.append(("center", self.center_pdf))
elif crop == "right":
self.operations.append(("right", self.right_pdf))
elif crop == "left":
self.operations.append(("crop", self.crop_pdf))
self.operations = {
format: [] for format in self.SUPPORTED_FORMATS
}
self._configure_operations(crop, blank)
logger.info("Starting %s provider" % type(self).__name__)

if blank:
self.operations.append(("blank", blank_pdf))

self.operations.append(("shrink", self.shrink_pdf))
def _configure_operations(self, crop, blank):
"""Configure operations for PDF and PS formats"""
# Formats that need PDF processing
pdf_formats = ['pdf', 'ps']
def add_operation(formats, operation_name, operation_func):
for fmt in formats:
self.operations[fmt].append((operation_name, operation_func))

logger.info("Starting %s provider" % type(self).__name__)
# Base operations
add_operation(pdf_formats, "rewrite", self.rewrite_pdf)

# Crop operations mapping
crop_operations = {
'center': ('center', self.center_pdf),
'right': ('right', self.right_pdf),
'left': ('crop', self.crop_pdf)
}

# Add crop operation if specified
if crop in crop_operations:
add_operation(pdf_formats, *crop_operations[crop])

# Add blank operation if specified
if blank:
add_operation(pdf_formats, "blank", blank_pdf)

# PDF-specific shrink operation
add_operation(['pdf'], "shrink", self.shrink_pdf)

@staticmethod
@abc.abstractmethod
Expand Down Expand Up @@ -210,17 +232,23 @@ def run(self, src, filename=None):

# generate nice filename if needed
clean_filename = filename or self.informer.get_filename(abs_url)
tmp_filename = "paper.pdf"
extension = clean_filename.split(".")[-1]
tmp_filename = f"paper.{extension}"

if extension not in self.SUPPORTED_FORMATS:
raise ValueError(f"Unsupported file format {extension}. Must be one of {self.SUPPORTED_FORMATS}")


self.initial_dir = os.getcwd()
with tempfile.TemporaryDirectory(prefix="p2r_") as working_dir:
with chdir(working_dir):
self.retrieve_pdf(pdf_url, tmp_filename)

assert_file_is_pdf(tmp_filename)
if extension in "pdf ps".split():
assert_file_is_pdf(tmp_filename)

intermediate_fname = tmp_filename
for opname, op in self.operations:
for opname, op in self.operations[extension]:
intermediate_fname = op(intermediate_fname)

shutil.copy(intermediate_fname, clean_filename)
Expand Down
65 changes: 0 additions & 65 deletions paper2remarkable/providers/epub.py

This file was deleted.

24 changes: 7 additions & 17 deletions paper2remarkable/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@
from . import __version__
from .exceptions import InvalidURLError
from .exceptions import UnidentifiedSourceError
from .providers import LocalFile, EPUBProvider
from .providers import LocalFile
from .providers import providers
from .utils import follow_redirects
from .utils import is_url


def build_argument_parser():
parser = argparse.ArgumentParser(
description="Paper2reMarkable version %s - Upload PDFs and EPUBs to reMarkable" % __version__
description="Paper2reMarkable version %s" % __version__
)
parser.add_argument(
"input",
help="One or more URLs to a paper or paths to local PDF/EPUB files",
nargs="?",
)
parser.add_argument(
"-b",
"--blank",
Expand Down Expand Up @@ -189,22 +184,17 @@ def choose_provider(cli_input):
Raised when the input *is* a valid url, but no provider can handle it.
"""

provider = cookiejar = None

# Check if it's a local file first
if os.path.exists(cli_input):
if LocalFile.validate(cli_input):
# input is a local file
new_input = cli_input
# If it's an epub, use EPUBProvider
if cli_input.lower().endswith('.epub'):
provider = EPUBProvider
# Otherwise use LocalFile for PDFs
else:
provider = LocalFile
provider = LocalFile
elif is_url(cli_input):
# input is a url
new_input, cookiejar = follow_redirects(cli_input)
provider = next((p for p in providers if p.validate(new_input)), None)
else:
# not a proper URL or non-existent file
raise UnidentifiedSourceError

if provider is None:
Expand Down

0 comments on commit d5c2e17

Please sign in to comment.