2021-03-03 15:21:13 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2021 Seonghyeon Cho
|
2022-05-01 15:39:43 +02:00
|
|
|
# Copyright 2022 Mike Fährmann
|
2021-03-03 15:21:13 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Extractors for https://comic.naver.com/"""
|
|
|
|
|
2021-04-17 02:15:59 +02:00
|
|
|
from .common import GalleryExtractor, Extractor, Message
|
|
|
|
from .. import text
|
2022-05-01 15:39:43 +02:00
|
|
|
import re
|
2021-03-03 15:21:13 +01:00
|
|
|
|
2022-05-01 15:39:43 +02:00
|
|
|
BASE_PATTERN = (r"(?:https?://)?comic\.naver\.com"
|
|
|
|
r"/(webtoon|challenge|bestChallenge)")
|
2021-03-03 15:21:13 +01:00
|
|
|
|
|
|
|
|
2021-04-17 02:15:59 +02:00
|
|
|
class NaverwebtoonBase():
|
|
|
|
"""Base class for naver webtoon extractors"""
|
2021-03-03 15:21:13 +01:00
|
|
|
category = "naverwebtoon"
|
|
|
|
root = "https://comic.naver.com"
|
|
|
|
|
|
|
|
|
2021-04-17 02:15:59 +02:00
|
|
|
class NaverwebtoonEpisodeExtractor(NaverwebtoonBase, GalleryExtractor):
|
2021-03-03 15:21:13 +01:00
|
|
|
subcategory = "episode"
|
|
|
|
directory_fmt = ("{category}", "{comic}")
|
|
|
|
filename_fmt = "{episode:>03}-{num:>02}.{extension}"
|
|
|
|
archive_fmt = "{title_id}_{episode}_{num}"
|
2022-05-01 15:39:43 +02:00
|
|
|
pattern = BASE_PATTERN + r"/detail(?:\.nhn)?\?([^#]+)"
|
2021-03-03 15:21:13 +01:00
|
|
|
test = (
|
2022-05-01 15:39:43 +02:00
|
|
|
(("https://comic.naver.com/webtoon/detail"
|
|
|
|
"?titleId=26458&no=1&weekday=tue"), {
|
2021-03-03 15:21:13 +01:00
|
|
|
"url": "47a956ba8c7a837213d5985f50c569fcff986f75",
|
|
|
|
"content": "3806b6e8befbb1920048de9888dfce6220f69a60",
|
|
|
|
"count": 14
|
|
|
|
}),
|
2022-05-01 15:39:43 +02:00
|
|
|
(("https://comic.naver.com/challenge/detail"
|
|
|
|
"?titleId=765124&no=1"), {
|
|
|
|
"pattern": r"https://image-comic\.pstatic\.net/nas"
|
|
|
|
r"/user_contents_data/challenge_comic/2021/01/19"
|
|
|
|
r"/342586/upload_7149856273586337846\.jpeg",
|
|
|
|
"count": 1,
|
|
|
|
}),
|
|
|
|
(("https://comic.naver.com/bestChallenge/detail.nhn"
|
|
|
|
"?titleId=771467&no=3"), {
|
|
|
|
"pattern": r"https://image-comic\.pstatic\.net/nas"
|
|
|
|
r"/user_contents_data/challenge_comic/2021/04/28"
|
|
|
|
r"/345534/upload_3617293622396203109\.jpeg",
|
|
|
|
"count": 1,
|
|
|
|
}),
|
2021-03-03 15:21:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, match):
|
2022-05-01 15:39:43 +02:00
|
|
|
path, query = match.groups()
|
|
|
|
url = "{}/{}/detail?{}".format(self.root, path, query)
|
2021-04-17 02:15:59 +02:00
|
|
|
GalleryExtractor.__init__(self, match, url)
|
|
|
|
|
|
|
|
query = text.parse_query(query)
|
2021-03-03 15:21:13 +01:00
|
|
|
self.title_id = query.get("titleId")
|
|
|
|
self.episode = query.get("no")
|
|
|
|
|
2021-04-17 02:15:59 +02:00
|
|
|
def metadata(self, page):
|
|
|
|
extr = text.extract_from(page)
|
2021-03-03 15:21:13 +01:00
|
|
|
return {
|
|
|
|
"title_id": self.title_id,
|
2021-04-17 02:15:59 +02:00
|
|
|
"episode" : self.episode,
|
|
|
|
"title" : extr('property="og:title" content="', '"'),
|
|
|
|
"comic" : extr('<h2>', '<span'),
|
|
|
|
"authors" : extr('class="wrt_nm">', '</span>').strip().split("/"),
|
|
|
|
"description": extr('<p class="txt">', '</p>'),
|
|
|
|
"genre" : extr('<span class="genre">', '</span>'),
|
|
|
|
"date" : extr('<dd class="date">', '</dd>'),
|
2021-03-03 15:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
2021-04-17 02:15:59 +02:00
|
|
|
def images(page):
|
2021-03-03 15:21:13 +01:00
|
|
|
view_area = text.extract(page, 'id="comic_view_area"', '</div>')[0]
|
2021-04-17 01:58:45 +02:00
|
|
|
return [
|
2021-04-17 02:15:59 +02:00
|
|
|
(url, None)
|
2021-04-17 01:58:45 +02:00
|
|
|
for url in text.extract_iter(view_area, '<img src="', '"')
|
|
|
|
if "/static/" not in url
|
|
|
|
]
|
2021-03-03 15:21:13 +01:00
|
|
|
|
|
|
|
|
2021-04-17 02:15:59 +02:00
|
|
|
class NaverwebtoonComicExtractor(NaverwebtoonBase, Extractor):
|
2021-03-03 15:21:13 +01:00
|
|
|
subcategory = "comic"
|
|
|
|
categorytransfer = True
|
2022-05-01 15:39:43 +02:00
|
|
|
pattern = (BASE_PATTERN + r"/list(?:\.nhn)?\?([^#]+)")
|
2021-03-03 15:21:13 +01:00
|
|
|
test = (
|
2022-05-01 15:39:43 +02:00
|
|
|
("https://comic.naver.com/webtoon/list?titleId=22073", {
|
2021-03-03 15:21:13 +01:00
|
|
|
"pattern": NaverwebtoonEpisodeExtractor.pattern,
|
|
|
|
"count": 32,
|
|
|
|
}),
|
2022-05-01 15:39:43 +02:00
|
|
|
("https://comic.naver.com/challenge/list?titleId=765124", {
|
|
|
|
"pattern": NaverwebtoonEpisodeExtractor.pattern,
|
|
|
|
"count": 25,
|
|
|
|
}),
|
|
|
|
("https://comic.naver.com/bestChallenge/list.nhn?titleId=789786", {
|
|
|
|
"pattern": NaverwebtoonEpisodeExtractor.pattern,
|
|
|
|
"count": ">= 12",
|
|
|
|
}),
|
2021-03-03 15:21:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, match):
|
2021-04-17 02:15:59 +02:00
|
|
|
Extractor.__init__(self, match)
|
2022-05-01 15:39:43 +02:00
|
|
|
self.path, query = match.groups()
|
|
|
|
query = text.parse_query(query)
|
2021-03-03 15:21:13 +01:00
|
|
|
self.title_id = query.get("titleId")
|
2021-04-17 02:15:59 +02:00
|
|
|
self.page_no = text.parse_int(query.get("page"), 1)
|
2021-03-03 15:21:13 +01:00
|
|
|
|
|
|
|
def items(self):
|
2022-05-01 15:39:43 +02:00
|
|
|
url = "{}/{}/list".format(self.root, self.path)
|
2021-03-03 15:21:13 +01:00
|
|
|
params = {"titleId": self.title_id, "page": self.page_no}
|
|
|
|
data = {"_extractor": NaverwebtoonEpisodeExtractor}
|
|
|
|
|
|
|
|
while True:
|
|
|
|
page = self.request(url, params=params).text
|
|
|
|
data["page"] = self.page_no
|
|
|
|
|
|
|
|
for episode_url in self.get_episode_urls(page):
|
|
|
|
yield Message.Queue, episode_url, data
|
|
|
|
|
|
|
|
if 'class="next"' not in page:
|
|
|
|
return
|
|
|
|
params["page"] += 1
|
|
|
|
|
|
|
|
def get_episode_urls(self, page):
|
|
|
|
"""Extract and return all episode urls in page"""
|
|
|
|
return [
|
2022-05-01 15:39:43 +02:00
|
|
|
self.root + path
|
|
|
|
for path in re.findall(
|
|
|
|
r'<a href="(/(?:webtoon|challenge|bestChallenge)'
|
|
|
|
r'/detail\?[^"]+)', page)
|
2021-03-03 15:21:13 +01:00
|
|
|
][::2]
|