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 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions paper2remarkable/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

from paper2remarkable.providers.epub import EPUBProvider
cvanelteren marked this conversation as resolved.
Show resolved Hide resolved
from .acl import ACL
from .acm import ACM
from .arxiv import Arxiv
Expand All @@ -19,6 +20,7 @@
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 @@ -49,4 +51,5 @@
LocalFile,
PdfUrl,
HTML,
EPUBProvider
]
65 changes: 65 additions & 0 deletions paper2remarkable/providers/epub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from ._base import Provider
from ..utils import chdir, upload_to_remarkable
import os, tempfile, shutil

class EPUBProvider(Provider):
"""Provider for direct EPUB uploads"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Override operations since we don't need PDF processing
self.operations = [] # No operations needed for direct EPUB upload

@staticmethod
def validate(src):
"""Validate if source is an EPUB file"""
# Convert to absolute path before validation
abs_path = os.path.abspath(os.path.expanduser(src))
return abs_path.lower().endswith('.epub') and os.path.exists(abs_path)


def get_abs_pdf_urls(self, src):
"""For EPUB files, just return the local path as absolute path"""
abs_path = os.path.abspath(os.path.expanduser(src))
return abs_path, abs_path


def run(self, src, filename=None):
cvanelteren marked this conversation as resolved.
Show resolved Hide resolved
"""Override run method to handle EPUB files directly"""
# Convert to absolute path
src = os.path.abspath(os.path.expanduser(src))

if not self.validate(src):
raise ValueError("Source must be a valid EPUB file")

# Generate filename if not provided
clean_filename = filename or os.path.basename(src)
if not clean_filename.endswith('.epub'):
clean_filename += '.epub'

self.initial_dir = os.getcwd()
with tempfile.TemporaryDirectory(prefix="p2r_") as working_dir:
with chdir(working_dir):
# Simply copy the EPUB file
shutil.copy(src, clean_filename)

if self.debug:
print("Paused in debug mode in dir: %s" % working_dir)
print("Press enter to exit.")
return input()

if self.upload:
return upload_to_remarkable(
clean_filename,
remarkable_dir=self.remarkable_dir,
rmapi_path=self.rmapi_path,
)

# If not uploading, copy to target directory
target_path = os.path.join(self.initial_dir, clean_filename)
while os.path.exists(target_path):
base = os.path.splitext(target_path)[0]
target_path = base + "_.epub"
shutil.move(clean_filename, target_path)

return target_path
24 changes: 17 additions & 7 deletions paper2remarkable/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
from . import __version__
from .exceptions import InvalidURLError
from .exceptions import UnidentifiedSourceError
from .providers import LocalFile
from .providers import LocalFile, EPUBProvider
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" % __version__
description="Paper2reMarkable version %s - Upload PDFs and EPUBs to reMarkable" % __version__
cvanelteren marked this conversation as resolved.
Show resolved Hide resolved
)
parser.add_argument(
cvanelteren marked this conversation as resolved.
Show resolved Hide resolved
"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 @@ -184,17 +189,22 @@ def choose_provider(cli_input):
Raised when the input *is* a valid url, but no provider can handle it.

"""

cvanelteren marked this conversation as resolved.
Show resolved Hide resolved
provider = cookiejar = None
if LocalFile.validate(cli_input):
# input is a local file

# Check if it's a local file first
if os.path.exists(cli_input):
new_input = cli_input
provider = LocalFile
# If it's an epub, use EPUBProvider
if cli_input.lower().endswith('.epub'):
provider = EPUBProvider
# Otherwise use LocalFile for PDFs
else:
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
Loading