sdx23 / zotadd

A simple script to add pdf-files to Zotero via CLI
MIT License
12 stars 0 forks source link

Filenames with whitespace and reserved characters silently failed. #2

Open ijoseph opened 3 years ago

ijoseph commented 3 years ago

Here's a fix (along with some small style changes too).

diff --git a/zotadd b/zotadd
index 3f563cf..15d68c7 100755
--- a/zotadd
+++ b/zotadd
@@ -4,19 +4,24 @@
 # author: sdx23
 # github: https://github.com/sdx23/zotadd

-import os, sys, subprocess
-from tempfile import TemporaryDirectory
+import os
+from requests import post
+import subprocess
+import sys
 from shutil import copyfile
+from tempfile import TemporaryDirectory
 from time import sleep
-from requests import post
+from urllib.parse import quote
+
+

 zapi_url = "http://127.0.0.1:23119/connector/"
 zapi_timeout = 20
 zserv_port = 25852

-if __name__ == '__main__':
+if __name__ == "__main__":
     # check file
-    fname = sys.argv[1]
+    fname = " ".join(sys.argv[1:])
     if not os.path.isfile(fname):
         print("No such file '{}'. Aborting.".format(fname))
         sys.exit(1)
@@ -25,18 +30,23 @@ if __name__ == '__main__':
     # TODO: replace this hacky approach
     with TemporaryDirectory() as temp_wdir:
         # serve file
-        copyfile(fname, temp_wdir + '/' + os.path.basename(fname))
+        copyfile(fname, temp_wdir + "/" + os.path.basename(fname))
         os.chdir(temp_wdir)
-        zserv_process = subprocess.Popen(["python3", "-m", "http.server", "{}".format(zserv_port)])
+        zserv_process = subprocess.Popen(
+            ["python3", "-m", "http.server", "{}".format(zserv_port)]
+        )

-        zserv_url = "http://localhost:{}/{}".format(zserv_port, os.path.basename(fname))
+        zserv_url = "http://localhost:{}/{}".format(zserv_port, quote(os.path.basename(fname)))
         print(zserv_url)

         # import in zotero
         sleep(0.1)
         try:
-            response = post(zapi_url + "saveSnapshot",
-                        json={"url": zserv_url, "pdf": True}, timeout=zapi_timeout)
+            response = post(
+                zapi_url + "saveSnapshot",
+                json={"url": zserv_url, "pdf": True},
+                timeout=zapi_timeout,
+            )
         except:
             rv = 0
             print("Failed to contact Zotero API. Aborting.")