-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
83 lines (71 loc) · 2.53 KB
/
main.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
import os
import threading
from const import logger, DOWNLOAD_DIR_PATH, GALLERIES_PATH
import grequests
from bs4 import BeautifulSoup
from src.entities.Book import Book
from src.parsers.BookParser import BookParser
from src.parsers.ResultPageParser import ResultPageParser, PAGE_PREFIX
from src.services.BookInfoService import BookInfoService
from src.headers.HeadersBuilder import HeadersBuilder
from src.services.ImageService import ImageService
from src.services.ResultService import ResultService
from src.services.ZipService import ZipService
import requests
headers = HeadersBuilder().build()
d = {
'a': "下載搜尋結果",
'b': "下載本子",
'c': "生產目錄清單",
'd': "生產ZIP清單",
'input': ''
}
inp = input("\n".join([key + "> " + d[key] for key in d]))
if inp == 'a':
inp = input("輸入搜尋結果的連結: ")
if inp == "#":
result_url = 'https://nhentai.net/language/chinese'
else:
result_url = inp.strip()
# download ResultPages
startPage = 1
endPage = 3
urls = {result_url + PAGE_PREFIX + str(page) for page in range(startPage, endPage)}
resultService = ResultService(headers, urls)
resultService.scrapy_items()
# storageService.save_items(resultService.get_items())
items = resultService.get_items()
# download BookInfos
bookInfoService = BookInfoService(headers, items)
bookInfoService.scrapy_book_infos()
# storageService.save_book_infos(bookInfoService.get_book_infos())
books = bookInfoService.get_book_infos()
zipping_tasks = []
for book in books:
# download Images
imageService = ImageService(headers, book)
downloaded = len(imageService.get_downloaded_images()) == len(book.image_names)
if not downloaded:
imageService.scrapy_images()
# zipping
def zipping():
zipService = ZipService(book, imageService.downloaded_path)
zipService.zip()
task = threading.Thread(target=zipping, args=())
zipping_tasks.append(task)
task.start()
[x.join() for x in zipping_tasks]
elif inp == 'b':
inp = input("輸入本子的連結: ")
url = None
if inp == "#":
url = 'https://nhentai.net/g/299246'
else:
url = inp.strip()
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.content, 'html.parser')
book = BookParser(soup, url).parse()
service = ImageService(headers, book)
service.scrapy_images()
zipService = ZipService(book, service.downloaded_path)
zipService.zip()