-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support manually download as cbz
- Loading branch information
Showing
8 changed files
with
673 additions
and
636 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,37 @@ | ||
# coding: UTF-8 | ||
|
||
import sys | ||
import json | ||
import getopt | ||
import argparse | ||
import logging | ||
|
||
from crawler import Crawler | ||
|
||
import config | ||
from bot import ComicbookTelegramBot | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
version = "1.1.0" | ||
|
||
if __name__ == "__main__": | ||
help = """comicbook options: | ||
-h, --help Show help. | ||
-v, --version Show version and exit. | ||
-c, --comic a comic link on > nhentai.net | ||
> e-hentai.org | ||
> wnacg.com | ||
-o, --output Specify a output path.(temporarily disabled) | ||
-t, --telegram-bot Run telegram bot. | ||
""" | ||
|
||
link = "" | ||
output = "" | ||
|
||
if len(sys.argv) == 1: | ||
print(help) | ||
sys.exit() | ||
argv = sys.argv[1:] | ||
try: | ||
opts, args = getopt.getopt( | ||
argv, "hvc:o:ts", ["help", "version", "comic=", "output=", "telegram-bot"] | ||
) | ||
except getopt.GetoptError: | ||
print(help) | ||
sys.exit() | ||
for opt, arg in opts: | ||
if opt in ("-h", "--help"): | ||
print(help) | ||
if opt in ("-c", "--comic"): | ||
link = arg | ||
result = Crawler.download(link) | ||
result.pop("item") | ||
print(json.dumps(result, indent=2)) | ||
if opt in ("-o", "--output"): | ||
output = arg | ||
if opt in ("-v", "--version"): | ||
print(version) | ||
|
||
if ("-t", "") in opts or ("--telegram-bot", "") in opts: | ||
bot = ComicbookTelegramBot(config.TELEGRAM_BOT_TOKEN) | ||
bot.start() | ||
parser = argparse.ArgumentParser(description="comicbook") | ||
parser.add_argument( | ||
"-c", | ||
"--comic", | ||
required=True, | ||
help="a comic link on nhentai.net, e-hentai.org, wnacg.com", | ||
) | ||
parser.add_argument( | ||
"-f", | ||
"--format", | ||
default="epub", | ||
choices=["epub", "cbz"], | ||
help="Specify a format.", | ||
) | ||
parser.add_argument("-o", "--output", help="Specify a output path.") | ||
args = parser.parse_args() | ||
|
||
if args.output: | ||
result = Crawler.download_manually(args.comic, args.format, args.output) | ||
print(result) | ||
else: | ||
result = Crawler.download(args.comic) | ||
result.pop("item") | ||
print(json.dumps(result, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# coding: UTF-8 | ||
import os | ||
import logging | ||
from zipfile import ZipFile | ||
|
||
import requests | ||
|
||
from crawler.utils import ua | ||
import config | ||
|
||
logger = logging.getLogger("pipeline") | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
class ComicPipeline: | ||
def __init__(self, item): | ||
self.item = item | ||
self.cbz = None | ||
|
||
def generate(self, dir): | ||
self.cbz = ZipFile(dir, "w") | ||
slog = logger.getChild(f"{self.item.domain}-{self.item.id}") | ||
|
||
slog.info("start to download image resources") | ||
count = len(self.item.image_urls) | ||
|
||
session = requests.Session() | ||
session.headers.update({"User-Agent": ua.get_random_ua()}) | ||
session.proxies.update(config.PROXY) | ||
|
||
for (index, url) in enumerate(self.item.image_urls): | ||
r = session.get(url) | ||
if r.ok: | ||
slog.info("[%d/%d] %s [OK]", index + 1, count, url) | ||
image_name = url.split("/")[-1] | ||
self.cbz.writestr(image_name, r.content) | ||
else: | ||
slog.info("[%d/%d] %s [FAIL]", index + 1, count, url) | ||
return False | ||
slog.info("download completed") | ||
|
||
slog.info("cbzify...") | ||
self.cbz.close() | ||
slog.info("work done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# coding: UTF-8 | ||
|
||
import config | ||
from bot import ComicbookTelegramBot | ||
|
||
|
||
if __name__ == "__main__": | ||
bot = ComicbookTelegramBot(config.TELEGRAM_BOT_TOKEN) | ||
bot.start() |