Advertisement
0rx

Easy_Nhentai_crawler_fix

0rx
Jul 15th, 2020 (edited)
2,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.86 KB | None | 0 0
  1. # Easy Manga Crawler for
  2. # NHENTAI.NET
  3. # Written by 0rX
  4. #
  5. # Usage:
  6. #    run it with python
  7. #   from terminal
  8. #   I haven't tried it on windows
  9. #    because I don't use windows
  10. #    it's useful to android users
  11. #    with terminal emulator such as
  12. #    termux or many other
  13. #   which included python3 already
  14. #
  15. #    if nhentai.net is blocked, please use vpn
  16.  
  17. '''
  18. change maxPageThread = 10
  19. if you have a fast internet connection
  20. don't exceed 10 to avoid your ip being
  21. banned by the server due to your ip
  22. requesting too many times simultaneously
  23.  
  24. '''
  25.  
  26.  
  27. from urllib import request as req
  28. import threading
  29. import time
  30. import json
  31. import os
  32. import struct
  33. import gzip
  34. import io
  35. import re
  36. import sys
  37.  
  38. #https://nhentai.net/g/92405/6/  <- this is a link to a random hentai manga gallery, you need to replace the value of gallery with "92405" to exactly download manga from this link
  39.  
  40. dl = True
  41. session_name = ""
  42. maxPageThread = 3
  43. total_page = 0
  44. cooldown = 0.5
  45. destination = ""
  46. series = ""
  47. gallery = ""
  48. dljob = []
  49. dldone = []
  50. dlfail = []
  51.  
  52. def galleryFind():
  53.  global destination
  54.  global total_page
  55.  global series
  56.  global gallery
  57.  gallery   =  str(input("Input manga gallery number :  ")) #<- you just need to change this value
  58.  
  59. #getting title and total page
  60.  if True:
  61.                         pagelink = "http://nhentai.net/g/%s"% (gallery)
  62.                         reque = req.Request(pagelink, headers={'Accept-encoding':'gzip','User-Agent': 'Mozilla/5.0'})
  63.                         rawl = req.urlopen(reque)
  64.                         #print(rawl.read())
  65.                         encoding = rawl.headers.get('Content-Encoding')
  66.                         print(encoding)
  67.                         if encoding == None:
  68.                             rawl = rawl.read()
  69.                         else:
  70.                             if encoding.upper() == "GZIP":
  71.                                 try:
  72.                                     compressedstream = io.BytesIO(rawl.read())
  73.                                     gzipper = gzip.GzipFile(fileobj=compressedstream)
  74.                                     rawl = str(gzipper.read().decode('utf-8'))
  75.                                 except Exception as exc:
  76.                                     print(exc)
  77. #                        rawl = str(req.urlopen(pagelink).read())
  78.                         #print(rawl)
  79.                         rawl3 = rawl.split("<div>")
  80.                         rawl2 = rawl.split("</h1>")
  81.                         #print(rawl2)
  82.                         print("total item: "+str(len(rawl2)))
  83.                         rawl_list = []
  84.                         rawl3_list = []
  85.                         for li in rawl2:
  86.                           x = li.split('<div class="caption">')
  87.                           for xx in x:
  88.                             rawl_list.append(xx)
  89.                         for pg in rawl3:
  90.                             pgs = pg.split("Pages:")
  91.                             for it in pgs:
  92.                                 rawl3_list.append(it)
  93.                         #print("total items in rawl_list :"+str(len(rawl3_list)))
  94.                         pfs = rawl3_list[1].split("<span class=\"name\">")
  95.                         for itemx in pfs:
  96.                                 rawl3_list.append(itemx)
  97.                         #print("total items in rawl_list :"+str(len(rawl3_list)))
  98.                         #print(rawl3_list)
  99.                         genx = rawl3_list[3].split("</span>")
  100.                         title_list = rawl_list[0]
  101.                         #[print(i+"\n\n") for i in rawl3_list]
  102.                         total_page =  int(genx[0])
  103.                         print("Total page :"+str(total_page))
  104.                         title1 = title_list[0:700]
  105.                         title = title1.split("content=\"")
  106.                         #print(title_list)
  107.                         title = title[2].split(" />")[0]
  108.                         #print(str(len(title)))
  109.                         #print(title1)
  110.                         artist = title_list.split("<span class=\"before\">")[1]
  111.                         #print(artist)
  112.                         #print(str(len(artist)))
  113.                         artist = artist.split("</span><span class=\"pretty\">")
  114.                         afterTitle = artist[1].split("</span><span class=\"after\">")
  115.                         titleDone = artist[0]+afterTitle[0]+afterTitle[1].split("<")[0]
  116.                         if "&#x27;" or "/" or "|" in titleDone:
  117.                             ti1 = titleDone.replace("&#x27;", "")
  118.                             ti2 = ti1.replace("/","-")
  119.                             ti2 = ti2.replace("|","+")
  120.                             ti2 = ti2.replace("\"","")
  121.                             print(ti2)
  122.                             titleDone = ti2
  123.                         series=titleDone
  124.                         print(series)
  125.  
  126.  destination = "pig/%s (%s)/"%(series,gallery)
  127.  createDir(destination)
  128.  
  129.  urls = {}
  130.  
  131. def createDir(destiny):
  132.     try:
  133.         os.mkdir(destiny)
  134.     except Exception as e:
  135.         print(e)
  136.  
  137.  
  138. def downloop(page):
  139.    global dlfail
  140.    try:
  141.     session_name = "%s (%s)"%(series,gallery)
  142.     if True:
  143.                 checklist = os.listdir(destination)
  144.                 if len(str(page)) == 1:
  145.                     pagenum = "00"+str(page)
  146.                 if len(str(page)) == 2:
  147.                     pagenum = "0"+str(page)
  148.                 if len(str(page)) > 2:
  149.                     pagenum = str(page)
  150.                 if pagenum+".jpg" in checklist:
  151.                         print(pagenum+".jpg Checked.")
  152.                        
  153.                         return
  154.                 if pagenum+".jpg" not in checklist:
  155.                         pagelink = "http://nhentai.net/g/%s/%s"% (gallery, page)
  156.                         reque = req.Request(pagelink, headers={'Accept-encoding':'gzip','User-Agent': 'Mozilla/5.0'})
  157.                         rawl = req.urlopen(reque)
  158.                         encoding = rawl.headers.get('Content-Encoding')
  159.                         #print(encoding)
  160.                         if encoding == None:
  161.                             rawl = str(rawl.read())
  162.                         else:
  163.                             if encoding.upper() == "GZIP":
  164.                                 try:
  165.                                     compressedstream = io.BytesIO(rawl.read())
  166.                                     gzipper = gzip.GzipFile(fileobj=compressedstream)
  167.                                     rawl = str(gzipper.read())
  168.                                 except Exception as exc:
  169.                                     print(exc)
  170.                         rawl2 = rawl.split("\\")
  171.                         rawl_list = []
  172.                         for li in rawl2:
  173.                           x = li.split("t<img ")
  174.                           for xx in x:
  175.                             rawl_list.append(xx)
  176.                         last_list = []
  177.                         #print(rawl_list)
  178.                        
  179.                         for i in rawl_list:
  180.                             if 'src="https://i.nhentai.net/galleries/' in i:
  181.                                 last_list.append(i)
  182.                         last_link = last_list[0].split('<img src=\"')
  183.                         last = last_link
  184.                         #print("\n\n".join(last))
  185.                         linkx = last[2].split("\" ")[0]
  186.                         #print(rawl_list)
  187.                         #print(str(len(rawl_list)))
  188.                         #linkx = rawl_list[2].split("\" width")[0]
  189.                         #print(linkx)
  190.                         req.urlretrieve(linkx, "%s%s.jpg"%(destination, pagenum))
  191.                         print(pagenum+".jpg is done")
  192.                         if page in dlfail:
  193.                             dlfail.remove(page)
  194.                             print("page "+str(page)+" removed from dlfail.")
  195.                         return
  196.    except Exception as ex:
  197.          print(ex)
  198.          print("Page no "+str(page)+" failed.")
  199.          if page not in dlfail:
  200.             dlfail.append(page)
  201.             if pagenum+'.jpg' in os.listdir(destination):
  202.                 os.remove("%s%s.jpg"%(destination,pagenum))
  203.                 print("failed page removed.")
  204. '''
  205. createDir()
  206. pagerange = total_page+1
  207. dljob = []
  208. dldone=[]
  209. '''
  210.  
  211. def progloop():
  212.     global dljob
  213.     global dldone
  214.     global dlfail
  215.     while True:
  216.         pagerange = total_page+1
  217.         print(dlfail)
  218.         for i in range(1, pagerange):
  219.             if i not in dldone or i in dlfail:
  220.                 if len(dljob) <= maxPageThread:
  221.                     dljob.append(i)
  222.         threads = [threading.Thread(target=downloop, args=(page,)) for page in dljob]
  223.         for thread in threads:
  224.             thread.start()
  225.             time.sleep(cooldown)
  226.         for thread in threads:
  227.             thread.join()
  228.             for jobs in dljob:
  229.                 dldone.append(jobs)
  230.             dljob.clear()
  231.         if len(os.listdir(destination)) >= (pagerange-1):
  232.             dldone.clear()
  233.             print("done.")
  234.             break
  235.  
  236. while True:
  237.     galleryFind()
  238.     progloop()
  239.  
  240. #downloop()
  241. print(session_name + " Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement