Antoine Albertelli avatar Antoine Albertelli committed 239cc5f

Added a progress bar to the download.

Comments (0)

Files changed (1)

 import re
 import threading
 
-# We don't need volume number, mangafox can do that for us \o/
+total_images = 0
+finished_images = 0
+
+def make_progress_bar(current, total, length=200):
+    """Returns a string representing a progress bar, wget style."""
+    if current > total:
+        raise ValueError("Current > total")
+
+    length = length - 2
+    sharp_count = int(length * current / total)
+    progress_bar = "["
+    progress_bar += "#" * sharp_count
+    progress_bar += ">" 
+    progress_bar += " " * (length - sharp_count - 1)
+    progress_bar += "]"
+    progress_bar += " {0} / {1}".format(current, total)
+    return progress_bar
 
 def get_soup(url):
     """
 
 def save_image(url, path):
     """url is the url of the main page."""
+    global total_images, finished_images
+    total_images += 1
     soup = get_soup(url)
-    print(url)
     image_data = requests.get(soup.find(id="image")["src"])
     if image_data.status_code == 200:
         with open(path, "wb") as outfile:
     else:
         print("Cannot open image, dafuq ?")
 
+    finished_images += 1
+
 def main():
     """Entry point of the program."""
+    global finished_images, total_images
 
     parser = argparse.ArgumentParser(
                             description="Image downloader for mangafox.com")
                 thread.start()
                 threads.append(thread)
 
+    if not args.no_threads:
+        prev_finished = finished_images
+        while finished_images != total_images:
+            if prev_finished != finished_images:
+                # end="" means no line feed at the en
+                print("\r"+make_progress_bar(finished_images, total_images), end="")
+                prev_finished = finished_images
+
+
+
+        print("\r"+make_progress_bar(finished_images, total_images)) 
 
-    for thread in threads:
-        thread.join()
+        for thread in threads:
+            thread.join()
 
 if __name__ == "__main__":
     main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.