Commits

Antoine Albertelli  committed 9329961

Added support for half chapter input.

  • Participants
  • Parent commits 04f33b5

Comments (0)

Files changed (1)

     page = requests.get(url)
     return BeautifulSoup(page.text)
 
+
+
+def clean_chapter_num(c):
+    """
+    Cleans the chapter number (converts it back to int if it is a round
+    number).
+    """
+    if int(c)==float(c): # maybe we can do better ?
+        return int(c)
+    else:
+        return c
+
 def get_page_count(soup):
     """
     Returns the number of page for a manga based on the soup from one page 
                             description="Image downloader for mangafox.com")
     parser.add_argument("manga_name", 
             help="Manga name on Mangafox.com. Ex : fairy_tail")
-    parser.add_argument("chapters", 
+    parser.add_argument("chapters", action='append', nargs="+", 
             help="Chapter number. Ex : 1 or 3-50")
     parser.add_argument("--no-threads", help="Use only a single thread.",
             action="store_true")
     args = parser.parse_args()
 
+
     range_exp = re.compile("[0123456789]+-[0123456789]+")
 
-    if range_exp.search(args.chapters):
-        start, end = map(int, args.chapters.split("-"))
-        chapters = range(start, end + 1)
-    else:
-        chapters = [int(args.chapters)]
-    
+    chapters = []
+
+    for expr in args.chapters[0]:
+        print(expr)
+        if range_exp.search(expr):
+            start, end = map(int, expr.split("-"))
+            chapters += range(start, end + 1)
+        else:
+            chapters += [float(expr)]
+        
     manga = args.manga_name
 
     threads = []
 
+
     for chapter in chapters:
-        os.mkdir("{0}_{1}".format(manga, chapter))
+        os.mkdir("{0}_{1}".format(manga, clean_chapter_num(chapter)))
         soup = get_soup(make_url(manga, chapter, 1))
         page_count = get_page_count(soup)
         for page in range(1, page_count+1):
             # The leading zeroes are needed so viewing apps dont get confused.
-            image_path = "{0}_{1}/{2:03}.jpg".format(manga, chapter, page)
-            page_url = make_url(manga, chapter, page)
+            image_path = "{0}_{1}/{2:03}.jpg".format(manga, clean_chapter_num(chapter), page)
+            page_url = make_url(manga, clean_chapter_num(chapter), page)
             if args.no_threads:
                 save_image(page_url, image_path)
             else:
         prev_finished = finished_images
         while finished_images != total_images:
             if prev_finished != finished_images:
-                # end="" means no line feed at the en
+                # end="" means no line feed at the end
                 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: