manga-dl.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. #RECOURCES:
  3. #https://github.com/tsihoarana/manga-scraper/blob/master/scrap.py
  4. #https://www.howtolovecomics.com/2019/05/07/pokemon-manga-guide/
  5. # HOW TO:
  6. # I was kinda lazy, time limited, and probably won't use this script
  7. # many times in the future. Use the `-v` flag and input a manga4life URL
  8. import requests
  9. from bs4 import BeautifulSoup
  10. import json
  11. from PIL import Image
  12. import os
  13. import argparse
  14. def get_arguments():
  15. parser = argparse.ArgumentParser(description='Manga DL')
  16. parser.add_argument('-v', '--volume', type=str, metavar='VOLUME', help='select which volumes you want')
  17. #parser.add_argument('-g', '--general', action="store_true", default=False, help='Get general emotes')
  18. args = parser.parse_args()
  19. return args
  20. def get_volumes(url):
  21. title = url.split("/")[-2]
  22. data = requests.get(url).text
  23. soup = BeautifulSoup(data, "html.parser")
  24. chapters = json.loads(str(soup.find_all("script")[-1]).split("vm.Chapters = ")[1].split(";")[0])
  25. num = len(chapters)
  26. volumes = []
  27. for i in list(range(1, num+1)):
  28. volumes.append(f"https://manga4life.com/read-online/{title}-chapter-{i}-page-1.html")
  29. return volumes
  30. def get_pages(url):
  31. data = requests.get(url).text
  32. soup = BeautifulSoup(data, "html.parser")
  33. data = soup.find_all("script")[-1]
  34. title = str(data).split("vm.IndexName = \"")[1].split("\";")[0]
  35. chapters = json.loads(str(data).split("vm.CHAPTERS = ")[1].split(";")[0])
  36. pages = int(chapters[0]['Page'])
  37. chapter = url.split(".html")[0].split("-")[-1]
  38. urls = []
  39. for i in list(range(1, pages+1)):
  40. urls.append(f"https://official-ongoing-1.ivalice.us/manga/{title}/{chapter.zfill(4)}-{str(i).zfill(3)}.png")
  41. return urls
  42. def make_pdf(urls):
  43. images = []
  44. files = []
  45. total = len(urls)
  46. vol = urls[0].split("/")[-1].split("-")[0]
  47. for i, url in enumerate(urls):
  48. filename = url.split("/")[-1]
  49. title = url.split("/")[-2]
  50. data = requests.get(url)
  51. if not os.path.isfile(filename):
  52. with open(filename, "wb") as f:
  53. f.write(data.content)
  54. files.append(filename)
  55. images.append(Image.open(url.split("/")[-1]))
  56. print(f"{url} ({i}/{total})")
  57. images[0].save(title+"-Volume"+vol+".pdf", "PDF", resolution=100.0, save_all=True, append_images=images)
  58. for file in files:
  59. os.remove(file)
  60. def fzf(data):
  61. p = subprocess.run(COMMAND, input=data, capture_output=True, text=True)
  62. if p.stdout == "":
  63. sys.exit("Please select something.")
  64. else:
  65. return int(p.stdout.split("\n")[0].split(":")[0]) - 1
  66. def main():
  67. args = get_arguments()
  68. if args.volume:
  69. volumes = get_volumes(args.volume)
  70. title = args.volume.split("/")[-1]
  71. total = len(volumes)
  72. selection = input(f"Select which volumes to download: 1-{total}: ").split("-")
  73. start = int(selection[0])
  74. end = int(selection[1])
  75. for volume in list(range(start, end+1)):
  76. make_pdf(get_pages(f"https://manga4life.com/read-online/{title}-chapter-{volume}.html"))
  77. print(f"Finished downloading volume {volume}")
  78. #make_pdf(get_pages("https://manga4life.com/read-online/Pocket-Monster-Special-chapter-1.html"))
  79. if __name__ == '__main__':
  80. main()