Guest User

Mangafox downloader v-sleep_depravation

a guest
Jul 26th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. """
  2. Mangafox manga download (to PDF)
  3.  
  4. Require:
  5.    Python3
  6.    reportlab      (pdf creation)
  7.    BeautifulSoup  (html parsing)
  8.    
  9. use: python mf_download.py [manga_web_page] [List_of_chapters_to_download]
  10. ex:  python mf_download.py http://mangafox.me/manga/tokyo_ghoul/ [1,2,3]
  11. """
  12.  
  13. from reportlab.pdfgen import canvas
  14. from urllib import request
  15. from bs4 import BeautifulSoup
  16.  
  17. from os import mkdir
  18. from os.path import dirname, abspath
  19.  
  20. import sys
  21.  
  22. def get_pages(url, savedir):
  23.     """
  24.        Create a generator that save images to savedir.
  25.        Return the absolute path of the image saved and the
  26.        width/height of the image.
  27.        
  28.        url: Url of the manga first page
  29.        savedir: Relative path where to save the images
  30.    """
  31.     current_page = 1
  32.     base_url = dirname(url)
  33.     current_url = url
  34.     has_next_page = True
  35.    
  36.     while has_next_page:
  37.        
  38.         page = request.urlopen(current_url)
  39.         page = (page.read()).decode("utf8", errors="ignore")
  40.         page = BeautifulSoup(page).html
  41.        
  42.         viewer = page("div", id="viewer")[0]
  43.         img = viewer.a.img
  44.        
  45.         if viewer.a["href"] == "javascript:void(0);":
  46.             has_next_page = False
  47.         else:
  48.             current_url = base_url+"/"+viewer.a["href"]
  49.        
  50.         img_scr = request.urlopen(img["src"])
  51.         img_name = savedir+"/"+str(current_page)+".jpg"
  52.         with open(img_name, "wb") as i:
  53.             i.write(img_scr.read())
  54.        
  55.         print("Got page "+str(current_page)+"!")
  56.         current_page += 1
  57.        
  58.         yield abspath(img_name), 700, 1110
  59.        
  60.     yield None, 0, 0
  61.    
  62.  
  63. def save_chapter(name, url):
  64.     """
  65.        Fetch all images in a chapter and save them
  66.        in a pdf.
  67.        
  68.        name: name of the chapter.
  69.        url: Url of the manga first page
  70.    """
  71.     dirname = "chapter_"+name
  72.     mkdir(dirname)
  73.     chapter = canvas.Canvas(name+".pdf", pagesize=(700, 1110))
  74.    
  75.     for image, width, height in get_pages(url, dirname):
  76.         chapter.drawImage(image, 0, 0, width, height)
  77.         chapter.showPage()
  78.    
  79.     chapter.save()
  80.  
  81. def mangafox_downloader(manga_page, chapters_list):
  82.     """
  83.        Fetch link to the manga chapter and create PDF from them.
  84.        
  85.        manga_page: Page of the mange
  86.        chapters_list: List of chapters to save
  87.    """
  88.     current_chapter = 1
  89.    
  90.     mp = request.urlopen(manga_page)
  91.     mp = (mp.read()).decode("utf8", errors="ignore")
  92.     mp = BeautifulSoup(mp)
  93.     chapters = (mp.html(id="chapters")[0])("ul")
  94.    
  95.     for sub in reversed(chapters):
  96.         for li in reversed(sub("li")):
  97.             h4 = li.div.h4
  98.             if h4 and h4.span and (current_chapter in chapters_list):
  99.                 print("Saving chapter "+str(current_chapter)+"...")
  100.                 title = h4.span.text
  101.                 addr = h4.a["href"]
  102.                 save_chapter(title, addr)
  103.                 print("Finished saving chapter "+str(current_chapter)+"!")
  104.             current_chapter += 1
  105.  
  106.    
  107. if __name__ == "__main__":
  108.     if len(sys.argv) != 3:
  109.         print("Script called with wrong number of argument.")
  110.     else:
  111.         url, chapters = sys.argv[1], eval(sys.argv[2])
  112.         mangafox_downloader(url, chapters)
Add Comment
Please, Sign In to add comment