Explorar el Código

better code formatting + mention AUR package in README

Noah Vogt hace 3 años
padre
commit
862d136132
Se han modificado 2 ficheros con 55 adiciones y 34 borrados
  1. 5 1
      README.md
  2. 50 33
      issuu-dl

+ 5 - 1
README.md

@@ -4,4 +4,8 @@ This is a simple program to quickly install magazines etc. on [issuu.com](https:
 
 ## Dependencies
 
-This python script needs three non-standard python modules: termcolor, img2pdf and tqdm, typer.
+This python script needs four non-standard python modules: termcolor, img2pdf, tqdm and typer.
+
+## Packaging
+
+There is a package available for Arch Linux on the AUR: [issuu-dl](https://aur.archlinux.org/packages/issuu-dl).

+ 50 - 33
issuu-dl

@@ -22,8 +22,8 @@ from tqdm import tqdm
 
 
 class OutType(str, Enum):
-    PDF = 'pdf'
-    CBZ = 'cbz'
+    PDF = "pdf"
+    CBZ = "cbz"
 
 
 def error_msg(msg: str):
@@ -31,7 +31,7 @@ def error_msg(msg: str):
     sys.exit(1)
 
 
-class Downloader():
+class Downloader:
     def __init__(self):
         self.filenames = []
         self.temp_dir = ""
@@ -41,18 +41,16 @@ class Downloader():
         self.pages = ""
         self.request_session = requests.Session()
 
-
     def check_document_format(self, url, output_file):
-        self.target_doc = url.split('/')
+        self.target_doc = url.split("/")
         try:
-            self.target_doc = self.target_doc[3] + '/' + self.target_doc[5]
+            self.target_doc = self.target_doc[3] + "/" + self.target_doc[5]
         except IndexError:
             error_msg("wrong URL format, please recheck your link")
-        print("[*] target_doc is '{}'".format(colored(self.target_doc, 'yellow')))
-        self.outfile_base = self.target_doc.replace('/', '_')
+        print("[*] target_doc is '{}'".format(colored(self.target_doc, "yellow")))
+        self.outfile_base = self.target_doc.replace("/", "_")
         self.outfile = self.outfile_base + "." + output_file
 
-
     def download_metadata(self, url):
         print("[*] opening page...")
         request_session = requests.Session()
@@ -66,60 +64,76 @@ class Downloader():
         except AssertionError:
             error_msg("http status code received: {}".format(request.status_code))
         print(colored("[+] url confirmed", "green"))
-        assert 'issuu-reader3-embed-files' in request.content.decode()
+        assert "issuu-reader3-embed-files" in request.content.decode()
         print(colored("[+] reader3 support confirmed", "green"))
 
         print("[*] downloading reader3_4.json...")
 
-        request = request_session.get("https://reader3.isu.pub/{}/reader3_4.json".
-                                      format(self.target_doc))
+        request = request_session.get(
+            "https://reader3.isu.pub/{}/reader3_4.json".format(self.target_doc)
+        )
         json_data = json.loads(request.content.decode())
 
         publication_id = json_data["document"]["publicationId"]
         revision_id = json_data["document"]["revisionId"]
         self.pages = json_data["document"]["pages"]
 
-        print(colored("[+] fetched document data: ", "green") +
-              colored("publicationId is {}, revisionId is {}".format(
-              publication_id, revision_id), "yellow"))
+        print(
+            colored("[+] fetched document data: ", "green")
+            + colored(
+                "publicationId is {}, revisionId is {}".format(
+                    publication_id, revision_id
+                ),
+                "yellow",
+            )
+        )
         print(colored("[+] found {} pages".format(len(self.pages)), "green"))
 
-
     def download_pages(self):
         print("[*] downloading pages...")
 
         self.filenames = []
-        self.temp_dir = str(os.getpid())+"-"+self.outfile_base
+        self.temp_dir = str(os.getpid()) + "-" + self.outfile_base
 
         try:
             os.mkdir(self.temp_dir)
         except (FileExistsError, PermissionError):
             error_msg("could not create temporary directory '{}'".format(self.temp_dir))
         for page in tqdm(self.pages):
-            image_url = page["imageUri"].split('/')
-            image_path = os.path.join(self.temp_dir, image_url[1]+"-"+image_url[3])
-            request = self.request_session.get("https://"+page["imageUri"])
+            image_url = page["imageUri"].split("/")
+            image_path = os.path.join(self.temp_dir, image_url[1] + "-" + image_url[3])
+            request = self.request_session.get("https://" + page["imageUri"])
             with open(image_path, "wb") as write_image:
                 write_image.write(request.content)
             self.filenames.append(image_path)
 
         print(colored("[+] downloaded {} jpg files".format(len(self.pages)), "green"))
 
-
     def convert_files(self, output_file):
         if output_file == "pdf":
             print("[*] converting to single pdf...")
             with open(self.outfile, "wb") as out:
                 out.write(img2pdf.convert(self.filenames))
-            print(colored("[+] output pdf '{}' is ready".format(
-                  colored(self.outfile, "yellow")), "green"))
+            print(
+                colored(
+                    "[+] output pdf '{}' is ready".format(
+                        colored(self.outfile, "yellow")
+                    ),
+                    "green",
+                )
+            )
         else:
             print("[*] converting to single cbz...")
-            shutil.make_archive(self.outfile, 'zip', self.temp_dir)
-            os.rename(self.outfile+".zip", self.outfile)
-            print(colored("[+] output cbz '{}' is ready".format(
-                  colored(self.outfile, "yellow")), "green"))
-
+            shutil.make_archive(self.outfile, "zip", self.temp_dir)
+            os.rename(self.outfile + ".zip", self.outfile)
+            print(
+                colored(
+                    "[+] output cbz '{}' is ready".format(
+                        colored(self.outfile, "yellow")
+                    ),
+                    "green",
+                )
+            )
 
     def remove_tempfiles(self):
         print("[*] cleaning up temporary files...")
@@ -130,10 +144,14 @@ class Downloader():
 
 main = typer.Typer(add_completion=False)
 
+
 @main.command(no_args_is_help=True)
-def greeting(url: str=typer.Argument(..., help='Full issuu url'), output_file:
-             OutType = typer.Option('pdf', "--output-type", "-o",
-             help='Whether output is a pdf or cbz.')):
+def greeting(
+    url: str = typer.Argument(..., help="Full issuu url"),
+    output_file: OutType = typer.Option(
+        "pdf", "--output-type", "-o", help="Whether output is a pdf or cbz."
+    ),
+):
 
     download = Downloader()
     download.check_document_format(url, output_file)
@@ -143,6 +161,5 @@ def greeting(url: str=typer.Argument(..., help='Full issuu url'), output_file:
     download.remove_tempfiles()
 
 
-
-if __name__ == '__main__':
+if __name__ == "__main__":
     main()