Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Mangafox manga download (to PDF)
- Require:
- Python3
- reportlab (pdf creation)
- BeautifulSoup (html parsing)
- use: python mf_download.py [manga_web_page] [List_of_chapters_to_download]
- ex: python mf_download.py http://mangafox.me/manga/tokyo_ghoul/ [1,2,3]
- """
- from reportlab.pdfgen import canvas
- from urllib import request
- from bs4 import BeautifulSoup
- from os import mkdir
- from os.path import dirname, abspath
- import sys
- def get_pages(url, savedir):
- """
- Create a generator that save images to savedir.
- Return the absolute path of the image saved and the
- width/height of the image.
- url: Url of the manga first page
- savedir: Relative path where to save the images
- """
- current_page = 1
- base_url = dirname(url)
- current_url = url
- has_next_page = True
- while has_next_page:
- page = request.urlopen(current_url)
- page = (page.read()).decode("utf8", errors="ignore")
- page = BeautifulSoup(page).html
- viewer = page("div", id="viewer")[0]
- img = viewer.a.img
- if viewer.a["href"] == "javascript:void(0);":
- has_next_page = False
- else:
- current_url = base_url+"/"+viewer.a["href"]
- img_scr = request.urlopen(img["src"])
- img_name = savedir+"/"+str(current_page)+".jpg"
- with open(img_name, "wb") as i:
- i.write(img_scr.read())
- print("Got page "+str(current_page)+"!")
- current_page += 1
- yield abspath(img_name), 700, 1110
- yield None, 0, 0
- def save_chapter(name, url):
- """
- Fetch all images in a chapter and save them
- in a pdf.
- name: name of the chapter.
- url: Url of the manga first page
- """
- dirname = "chapter_"+name
- mkdir(dirname)
- chapter = canvas.Canvas(name+".pdf", pagesize=(700, 1110))
- for image, width, height in get_pages(url, dirname):
- chapter.drawImage(image, 0, 0, width, height)
- chapter.showPage()
- chapter.save()
- def mangafox_downloader(manga_page, chapters_list):
- """
- Fetch link to the manga chapter and create PDF from them.
- manga_page: Page of the mange
- chapters_list: List of chapters to save
- """
- current_chapter = 1
- mp = request.urlopen(manga_page)
- mp = (mp.read()).decode("utf8", errors="ignore")
- mp = BeautifulSoup(mp)
- chapters = (mp.html(id="chapters")[0])("ul")
- for sub in reversed(chapters):
- for li in reversed(sub("li")):
- h4 = li.div.h4
- if h4 and h4.span and (current_chapter in chapters_list):
- print("Saving chapter "+str(current_chapter)+"...")
- title = h4.span.text
- addr = h4.a["href"]
- save_chapter(title, addr)
- print("Finished saving chapter "+str(current_chapter)+"!")
- current_chapter += 1
- if __name__ == "__main__":
- if len(sys.argv) != 3:
- print("Script called with wrong number of argument.")
- else:
- url, chapters = sys.argv[1], eval(sys.argv[2])
- mangafox_downloader(url, chapters)
Add Comment
Please, Sign In to add comment