1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 11:12:40 +01:00
gallery-dl/gallery_dl/extractor/naverwebtoon.py

106 lines
3.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Copyright 2021 Seonghyeon Cho
2023-03-18 23:31:40 +01:00
# Copyright 2022-2033 Mike Fährmann
#
# 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
BASE_PATTERN = (r"(?:https?://)?comic\.naver\.com"
r"/(webtoon|challenge|bestChallenge)")
2021-04-17 02:15:59 +02:00
class NaverwebtoonBase():
"""Base class for naver webtoon extractors"""
category = "naverwebtoon"
root = "https://comic.naver.com"
2021-04-17 02:15:59 +02:00
class NaverwebtoonEpisodeExtractor(NaverwebtoonBase, GalleryExtractor):
subcategory = "episode"
directory_fmt = ("{category}", "{comic}")
filename_fmt = "{episode:>03}-{num:>02}.{extension}"
archive_fmt = "{title_id}_{episode}_{num}"
pattern = BASE_PATTERN + r"/detail(?:\.nhn)?\?([^#]+)"
example = "https://comic.naver.com/webtoon/detail?titleId=12345&no=1"
def __init__(self, match):
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)
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)
return {
"title_id": self.title_id,
2021-04-17 02:15:59 +02:00
"episode" : self.episode,
"comic" : extr('titleName: "', '"'),
2023-03-18 23:31:40 +01:00
"tags" : [t.strip() for t in text.extract_iter(
extr("tagList: [", "],"), '"tagName":"', '"')],
2023-03-18 23:31:40 +01:00
"title" : extr('"subtitle":"', '"'),
"author" : [a.strip() for a in text.extract_iter(
extr('"writers":[', ']'), '"name":"', '"')],
"artist" : [a.strip() for a in text.extract_iter(
extr('"painters":[', ']'), '"name":"', '"')]
}
@staticmethod
2021-04-17 02:15:59 +02:00
def images(page):
view_area = text.extr(page, 'id="comic_view_area"', '</div>')
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-04-17 02:15:59 +02:00
class NaverwebtoonComicExtractor(NaverwebtoonBase, Extractor):
subcategory = "comic"
categorytransfer = True
2023-03-18 23:31:40 +01:00
pattern = BASE_PATTERN + r"/list(?:\.nhn)?\?([^#]+)"
example = "https://comic.naver.com/webtoon/list?titleId=12345"
def __init__(self, match):
2021-04-17 02:15:59 +02:00
Extractor.__init__(self, match)
self.path, query = match.groups()
query = text.parse_query(query)
self.title_id = query.get("titleId")
2021-04-17 02:15:59 +02:00
self.page_no = text.parse_int(query.get("page"), 1)
2023-03-18 23:31:40 +01:00
self.sort = query.get("sort", "ASC")
def items(self):
2023-03-18 23:31:40 +01:00
url = self.root + "/api/article/list"
headers = {
"Accept": "application/json, text/plain, */*",
}
params = {
"titleId": self.title_id,
"page" : self.page_no,
"sort" : self.sort,
}
while True:
2023-03-18 23:31:40 +01:00
data = self.request(url, headers=headers, params=params).json()
path = data["webtoonLevelCode"].lower().replace("_c", "C", 1)
base = "{}/{}/detail?titleId={}&no=".format(
self.root, path, data["titleId"])
2023-03-18 23:31:40 +01:00
for article in data["articleList"]:
article["_extractor"] = NaverwebtoonEpisodeExtractor
yield Message.Queue, base + str(article["no"]), article
2023-03-18 23:31:40 +01:00
params["page"] = data["pageInfo"]["nextPage"]
if not params["page"]:
return