agunq

nhentai downloader

May 12th, 2019 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #coded by Agunq
  2. #email agunq.e@gmail.com
  3. #last test 25 Feb 2021
  4.  
  5. import re
  6. import sys
  7. import os
  8. import threading
  9.  
  10. if sys.version_info[0] < 3:
  11.     class urllib:
  12.         request = __import__("urllib2")
  13.     input = raw_input
  14. else:
  15.     import urllib.request
  16.  
  17. def nhentai_get_page(gid):
  18.     host = "nhentai"
  19.     url = "%s://%s.net/g/%s/1/" % ("ht" + "tps", host, gid)
  20.     hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
  21.     req = urllib.request.Request(url, headers = hdr)
  22.     resp = urllib.request.urlopen(req).read().decode("utf-8")
  23.     pages = re.search("<span class=\"num-pages\">(.*?)<\/span><\/button>", resp)
  24.     if pages:
  25.         pages = int(pages.group(1))
  26.  
  27.     img = re.findall("<img src=\"(.*?)\"(.*?|)>", resp)
  28.     if len(img) > 0:
  29.         img = img[1][0]
  30.         imgid = img.split("/")[4]
  31.         ext = img.split(".")[-1]
  32.     newset = []
  33.     for i in range(1, pages + 1):
  34.         url_image = "%s://i.%s.net/galleries/%s/%s.%s" % ("ht" + "tps", host, imgid, i, ext)
  35.         newset.append(url_image)
  36.     return newset
  37.  
  38.  
  39.    
  40. def nhentai_get_image(gid, url_images):
  41.     host = "nhentai"
  42.     for url in url_images:
  43.         try:
  44.             if not os.path.exists('%s-%s' % (host, gid)):
  45.                 os.makedirs('%s-%s' % (host, gid))
  46.             hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
  47.             req = urllib.request.Request(url, headers = hdr)
  48.             resp = urllib.request.urlopen(req).read()
  49.             print("[now] [download] %s-%s/%s" % (host, gid, url.split("/")[-1]))
  50.             f = open("%s-%s/%s" % (host, gid, url.split("/")[-1]), "wb+")
  51.             f.write(resp)
  52.             f.close()
  53.         except:
  54.             print("[error] [failed] [download] %s-%s/%s" % (host, gid, url.split("/")[-1]))
  55.             next
  56.    
  57. def chunk_list(lis, chunk_size):
  58.     for i in range(0, len(lis), chunk_size):
  59.         yield lis[i:i + chunk_size]
  60.    
  61. def nhentai(gid):
  62.     print("[start] [download] %s" % gid)
  63.     url_images = nhentai_get_page(gid)
  64.     url_images = chunk_list(url_images, int(int(len(url_images))/5)) # 5 = total threads, you can change it to 10 or more
  65.     thread_list = []
  66.     num = 0
  67.     for url in url_images:
  68.         th = threading.Thread(target=nhentai_get_image, args=(gid, url, ))
  69.         th.start()
  70.         thread_list.append(th)
  71.         num += 1
  72.     for thread in thread_list:
  73.         thread.join()  
  74.     print("[finish] [done] %s" % gid)
  75.  
  76. #example
  77. nhentai("161745")
Add Comment
Please, Sign In to add comment