-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_nhentai.net_.py
124 lines (108 loc) · 4.9 KB
/
_nhentai.net_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from bs4 import BeautifulSoup
from urllib.request import urlopen
import requests
import shutil
import time
import re
import os
def pageParser(Link):
grabbedPage = requests.get(Link)
return (BeautifulSoup(grabbedPage.content, 'html.parser'))
def infoScraper(itemCode):
grabbedPage = requests.get("https://nhentai.net/g/{}".format(itemCode))
print(grabbedPage.status_code)
if grabbedPage.status_code == 429:
print('Retrying info page grab (429)')
return('e429')
parsedPage = BeautifulSoup(grabbedPage.content, 'html.parser')
textPage = str(parsedPage.get_text())
if not (('Languages:\n\t\t\t\t\t\t\t\ttranslated' in textPage) or ('Languages:\n\t\t\t\t\t\t\t\tenglish' in textPage)):
return ('lang')
if 'chinese' in textPage:
return ('lang')
if 'yaoi' in textPage:
return ('seku')
mangaTitle = re.search(r'">(.*)<', str(parsedPage.find_all('span', 'before')[0])).group(1) + re.search(r'">(.*)<', str(parsedPage.find_all('span', 'pretty')[0])).group(1) + re.search(r'">(.*)<', str(parsedPage.find_all('span', 'after')[0])).group(1)
mangaPages = re.search(r'Pages:\n\t\t\t\t\t\t\t\t(.*)\n\t\t\t\t\t\t\t\tUploaded:', parsedPage.get_text()).group(1)
print('{} - {}'.format(itemCode, mangaTitle))
print(mangaPages + ' pages')
return (mangaTitle, mangaPages)
def idScraper(itemCode):
grabbedPage = requests.get("https://nhentai.net/g/{}/1/".format(itemCode))
print(grabbedPage.status_code)
if grabbedPage.status_code == 429:
print('Retrying id page grab (429)')
return('e429')
parsedPage = BeautifulSoup(grabbedPage.content, 'html.parser')
try:
imageId = re.search(r'https://i.nhentai.net/galleries/(.*)/1.jpg', str(parsedPage)).group(1)
print(imageId)
except:
try:
imageId = re.search(r'https://i.nhentai.net/galleries/(.*)/1.png', str(parsedPage)).group(1)
print(imageId)
except:
imageId = re.search(r'https://i.nhentai.net/galleries/(.*)/1.gif', str(parsedPage)).group(1)
print(imageId)
return (imageId)
itemCode = 300000
while True:
itemCode -= 1
check = requests.get('https://nhentai.net/g/{}/'.format(itemCode))
if check.status_code == 404:
print(str(itemCode) + ' not found.\n')
if check.status_code == 429:
itemCode += 1
print('Retrying main page grab (429)')
time.sleep(1)
continue
else:
info = infoScraper(itemCode)
if info == 'lang':
print(str(itemCode) + " isn't English.\n")
continue
if info == 'seku':
print(str(itemCode) + " is Yaoi.\n")
continue
if info == 'e429':
itemCode += 1
time.sleep(1)
continue
image = idScraper(itemCode)
if image == 'e429':
itemCode += 1
time.sleep(1)
continue
if not os.path.exists(info[0]):
os.makedirs(info[0])
for i in range(1, int(info[1])+1):
try:
if os.path.exists(info[0]+("/{}.jpg".format(i))):
print("Exists - " + "https://i.nhentai.net/galleries/{}/{}.jpg".format(image, i))
continue
imgFile = urlopen("https://i.nhentai.net/galleries/{}/{}.jpg".format(image, i))
with open(info[0]+("/{}.jpg".format(i)), 'wb') as saveFile:
saveFile.write(imgFile.read())
print("https://i.nhentai.net/galleries/{}/{}.jpg".format(image, i))
except:
try:
if os.path.exists(info[0]+("/{}.png".format(i))):
print("Exists - " + "https://i.nhentai.net/galleries/{}/{}.png".format(image, i))
continue
imgFile = urlopen("https://i.nhentai.net/galleries/{}/{}.png".format(image, i))
with open(info[0]+("/{}.png".format(i)), 'wb') as saveFile:
saveFile.write(imgFile.read())
print("https://i.nhentai.net/galleries/{}/{}.png".format(image, i))
except:
if os.path.exists(info[0]+("/{}.gif".format(i))):
print("Exists - " + "https://i.nhentai.net/galleries/{}/{}.gif".format(image, i))
continue
imgFile = urlopen("https://i.nhentai.net/galleries/{}/{}.gif".format(image, i))
with open(info[0]+("/{}.gif".format(i)), 'wb') as saveFile:
saveFile.write(imgFile.read())
print("https://i.nhentai.net/galleries/{}/{}.gif".format(image, i))
if not os.path.exists('drive/Shareddrives/Scrapes/NHentaiArchives/' + info[0] + '/' + str(itemCode)+'.zip'):
shutil.make_archive('drive/Shareddrives/Scrapes/NHentaiArchives/' + info[0] + '/' + str(itemCode), 'zip', info[0])
else:
print('Exists - ' + info[0] + '.zip')
print()