aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGertjan van den Burg <gertjanvandenburg@gmail.com>2019-10-24 14:56:53 +0100
committerGertjan van den Burg <gertjanvandenburg@gmail.com>2019-10-24 14:56:53 +0100
commit2b8289495ff5910d75013b903d82085bcd7742a1 (patch)
treef1df433202d8f7ad1c36cdb5af8bb77a70e7e0e2
parentSwitch to logging module throughout (diff)
downloadpaper2remarkable-2b8289495ff5910d75013b903d82085bcd7742a1.tar.gz
paper2remarkable-2b8289495ff5910d75013b903d82085bcd7742a1.zip
Move upload functionality to utils
-rw-r--r--paper2remarkable/providers/_base.py30
-rw-r--r--paper2remarkable/utils.py27
2 files changed, 33 insertions, 24 deletions
diff --git a/paper2remarkable/providers/_base.py b/paper2remarkable/providers/_base.py
index 3692924..85415a9 100644
--- a/paper2remarkable/providers/_base.py
+++ b/paper2remarkable/providers/_base.py
@@ -15,14 +15,13 @@ import os
import requests
import shutil
import string
-import subprocess
import tempfile
import time
import titlecase
import unidecode
from ..pdf_ops import crop_pdf, center_pdf, blank_pdf, shrink_pdf
-from ..utils import exception
+from ..utils import upload_to_remarkable, check_file_is_pdf
HEADERS = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) "
@@ -191,27 +190,6 @@ class Provider(metaclass=abc.ABCMeta):
logging.info("Downloading url: %s" % url)
return res.content
- def upload_to_rm(self, filepath):
- remarkable_dir = self.remarkable_dir.rstrip("/")
- logging.info("Starting upload to reMarkable")
- if remarkable_dir:
- status = subprocess.call(
- [self.rmapi_path, "mkdir", remarkable_dir + "/"],
- stdout=subprocess.DEVNULL,
- )
- if not status == 0:
- exception(
- "Creating directory %s on reMarkable failed"
- % remarkable_dir
- )
- status = subprocess.call(
- [self.rmapi_path, "put", filepath, remarkable_dir + "/"],
- stdout=subprocess.DEVNULL,
- )
- if not status == 0:
- exception("Uploading file %s to reMarkable failed" % filepath)
- logging.info("Upload successful.")
-
def run(self, src, filename=None):
info = self.get_paper_info(src)
clean_filename = self.create_filename(info, filename)
@@ -234,7 +212,11 @@ class Provider(metaclass=abc.ABCMeta):
return input()
if self.upload:
- return self.upload_to_rm(clean_filename)
+ return upload_to_remarkable(
+ clean_filename,
+ remarkable_dir=self.remarkable_dir,
+ rmapi_path=self.rmapi_path,
+ )
target_path = os.path.join(self.initial_dir, clean_filename)
while os.path.exists(target_path):
diff --git a/paper2remarkable/utils.py b/paper2remarkable/utils.py
index 5188afb..26b024e 100644
--- a/paper2remarkable/utils.py
+++ b/paper2remarkable/utils.py
@@ -10,6 +10,8 @@ Copyright: 2019, G.J.J. van den Burg
import PyPDF2
+import logging
+import subprocess
import sys
GITHUB_URL = "https://github.com/GjjvdBurg/arxiv2remarkable"
@@ -35,3 +37,28 @@ def check_file_is_pdf(filename):
return True
except PyPDF2.utils.PdfReadError:
exception("Downloaded file isn't a valid pdf file.")
+
+
+def upload_to_remarkable(filepath, remarkable_dir="/", rmapi_path="rmapi"):
+ logging.info("Starting upload to reMarkable")
+
+ # Create the reMarkable dir if it doesn't exist
+ remarkable_dir = remarkable_dir.rstrip("/")
+ if remarkable_dir:
+ status = subprocess.call(
+ [rmapi_path, "mkdir", remarkable_dir + "/"],
+ stdout=subprocess.DEVNULL,
+ )
+ if not status == 0:
+ exception(
+ "Creating directory %s on reMarkable failed" % remarkable_dir
+ )
+
+ # Upload the file
+ status = subprocess.call(
+ [rmapi_path, "put", filepath, remarkable_dir + "/"],
+ stdout=subprocess.DEVNULL,
+ )
+ if not status == 0:
+ exception("Uploading file %s to reMarkable failed" % filepath)
+ logging.info("Upload successful.")