Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added epub support #154

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions paper2remarkable/providers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
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 +79,40 @@ 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))
def _configure_operations(self, crop, blank):
"""Configure operations for PDF and PS formats"""
# Formats that need PDF processing
# No processing for epubs is assumed
pdf_formats = ["pdf", "ps"]

self.operations.append(("shrink", self.shrink_pdf))
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,24 @@ 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
4 changes: 3 additions & 1 deletion paper2remarkable/providers/arxiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def __init__(self, *args, **kwargs):
self.informer = ArxivInformer()

# register the dearxiv operation
self.operations.insert(0, ("dearxiv", self.dearxiv))
for format in self.operations:
if format in "pdf ps".split():
self.operations[format].insert(0, ("dearxiv", self.dearxiv))

def get_abs_pdf_urls(self, url):
"""Get the pdf and abs url from any given arXiv url"""
Expand Down
5 changes: 3 additions & 2 deletions paper2remarkable/providers/pdf_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ def get_filename(self, abs_url):
)

filename = path_parts[-1]
if not filename.endswith(".pdf"):
ext = filename.split(".")[-1]
if ext not in [".pdf", "epub"]:
raise FilenameMissingError(
provider="PdfUrl",
url=abs_url,
reason="URL path didn't end in .pdf",
reason="URL path didn't end in .pdf or .epub",
)
logger.warning(
"Using filename {filename} extracted from url. "
Expand Down
7 changes: 7 additions & 0 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ def test_pdfurl_2(self):
filename = prov.run(url)
self.assertEqual("NoREC.pdf", os.path.basename(filename))

def test_epub(self):
prov = PdfUrl(upload=False, verbose=VERBOSE)
url = "https://www.gutenberg.org/ebooks/2701.epub.images"
filename = prov.run(url)
exp = "pg2701-images.epub"
self.assertEqual(exp, os.path.basename(filename))

def test_jmlr_1(self):
prov = JMLR(upload=False, verbose=VERBOSE)
url = "http://www.jmlr.org/papers/volume17/14-526/14-526.pdf"
Expand Down
Loading