| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
n |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
"""Extractors for https://www.webtoons.com/""" |
| 11 |
|
| 12 |
from .common import GalleryExtractor, Extractor, Message |
| 13 |
from .. import text, util, dt |
| 14 |
|
| 15 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?webtoons\.com" |
| 16 |
LANG_PATTERN = BASE_PATTERN + r"/(([^/?#]+)" |
| 17 |
|
| 18 |
|
| 19 |
class WebtoonsBase(): |
| 20 |
category = "webtoons" |
| 21 |
root = "https://www.webtoons.com" |
| 22 |
directory_fmt = ("{category}", "{comic}") |
| 23 |
filename_fmt = ("{episode_no}-{num:>02}{type:?-//}{sortOrder:?//}." |
| 24 |
"{extension}") |
| 25 |
archive_fmt = "{title_no}_{episode_no}_{num}" |
| 26 |
cookies_domain = ".webtoons.com" |
| 27 |
request_interval = (0.5, 1.5) |
| 28 |
|
| 29 |
def setup_agegate_cookies(self): |
| 30 |
self.cookies_update({ |
| 31 |
"atGDPR" : "AD_CONSENT", |
| 32 |
"needCCPA" : "false", |
| 33 |
"needCOPPA" : "false", |
| 34 |
"needGDPR" : "false", |
| 35 |
"pagGDPR" : "true", |
| 36 |
"ageGatePass": "true", |
| 37 |
}) |
| 38 |
|
| 39 |
_init = setup_agegate_cookies |
| 40 |
|
| 41 |
def request(self, url, **kwargs): |
| 42 |
response = Extractor.request(self, url, **kwargs) |
| 43 |
if response.history and "/ageGate" in response.url: |
| 44 |
raise self.exc.AbortExtraction( |
| 45 |
f"HTTP redirect to age gate check ('{response.url}')") |
| 46 |
return response |
| 47 |
|
| 48 |
|
| 49 |
class WebtoonsEpisodeExtractor(WebtoonsBase, GalleryExtractor): |
| 50 |
"""Extractor for an episode on webtoons.com""" |
| 51 |
subcategory = "episode" |
| 52 |
pattern = (LANG_PATTERN + r"/([^/?#]+)/([^/?#]+)/[^/?#]+)" |
| 53 |
r"/viewer\?([^#'\"]+)") |
| 54 |
example = ("https://www.webtoons.com/en/GENRE/TITLE/NAME/viewer" |
| 55 |
"?title_no=123&episode_no=12345") |
| 56 |
images_urls = [] |
| 57 |
|
| 58 |
def _init(self): |
| 59 |
self.setup_agegate_cookies() |
| 60 |
|
| 61 |
base, self.lang, self.genre, self.comic, query = self.groups |
| 62 |
params = text.parse_query(query) |
| 63 |
self.title_no = params.get("title_no") |
| 64 |
self.episode_no = params.get("episode_no") |
| 65 |
self.page_url = f"{self.root}/{base}/viewer?{query}" |
| 66 |
self.bgm = self.config("bgm", True) |
| 67 |
|
| 68 |
def metadata(self, page): |
| 69 |
extr = text.extract_from(page) |
| 70 |
title = extr('<meta property="og:title" content="', '"') |
| 71 |
descr = extr('<meta property="og:description" content="', '"') |
| 72 |
|
| 73 |
if extr('<div class="subj_info"', '\n'): |
| 74 |
comic_name = extr(">", "<") |
| 75 |
episode_name = extr('<h1 class="subj_episode" title="', '"') |
| 76 |
else: |
| 77 |
comic_name = episode_name = "" |
| 78 |
|
| 79 |
if extr('<span class="tx _btnOpenEpisodeLis', '"'): |
| 80 |
episode = extr(">#", "<") |
| 81 |
else: |
| 82 |
episode = "" |
| 83 |
|
| 84 |
if extr('<span class="author"', "\n"): |
| 85 |
username = extr("/u/", '"') |
| 86 |
author_name = extr("<span>", "</span>") |
| 87 |
else: |
| 88 |
username = author_name = "" |
| 89 |
|
| 90 |
return { |
| 91 |
"genre" : self.genre, |
| 92 |
"comic" : self.comic, |
| 93 |
"title_no" : self.title_no, |
| 94 |
"episode_no" : self.episode_no, |
| 95 |
"title" : text.unescape(title), |
| 96 |
"episode" : episode, |
| 97 |
"comic_name" : text.unescape(comic_name), |
| 98 |
"episode_name": text.unescape(episode_name), |
| 99 |
"username" : username, |
| 100 |
"author_name" : text.unescape(author_name), |
| 101 |
"description" : text.unescape(descr), |
| 102 |
"lang" : self.lang, |
| 103 |
"language" : util.code_to_language(self.lang), |
| 104 |
} |
| 105 |
|
| 106 |
def images(self, page): |
| 107 |
quality = self.config("quality") |
| 108 |
if quality is None or quality == "original": |
| 109 |
quality = {"jpg": False, "jpeg": False, "webp": False} |
| 110 |
elif not quality: |
| 111 |
quality = None |
| 112 |
elif isinstance(quality, str): |
| 113 |
quality = {"jpg": quality, "jpeg": quality} |
| 114 |
elif isinstance(quality, int): |
| 115 |
quality = "q" + str(quality) |
| 116 |
quality = {"jpg": quality, "jpeg": quality} |
| 117 |
elif not isinstance(quality, dict): |
| 118 |
quality = None |
| 119 |
|
| 120 |
if self.bgm: |
| 121 |
num = 0 |
| 122 |
self.paths = paths = {} |
| 123 |
else: |
| 124 |
num = None |
| 125 |
|
| 126 |
results = [] |
| 127 |
for url in text.extract_iter( |
| 128 |
page, 'class="_images" data-url="', '"'): |
| 129 |
|
| 130 |
path, _, query = url.rpartition("?") |
| 131 |
if num is not None: |
| 132 |
num += 1 |
| 133 |
paths[path[path.find("/", 8):]] = num |
| 134 |
if quality is not None: |
| 135 |
type = quality.get(path.rpartition(".")[2].lower()) |
| 136 |
if type is False: |
| 137 |
url = path |
| 138 |
elif type: |
| 139 |
url = f"{path}?type={type}" |
| 140 |
|
| 141 |
results.append((_url(url), None)) |
| 142 |
return results |
| 143 |
|
| 144 |
def assets(self, page): |
| 145 |
assets = [] |
| 146 |
|
| 147 |
if self.config("thumbnails", False): |
| 148 |
active = text.extr(page, 'class="on', '</a>') |
| 149 |
url = _url(text.extr(active, 'data-url="', '"')) |
| 150 |
assets.append({"url": url, "type": "thumbnail"}) |
| 151 |
|
| 152 |
if self.bgm: |
| 153 |
if bgm := text.extr(page, "episodeBgmList:", ",\n"): |
| 154 |
self._asset_bgm(assets, util.json_loads(bgm)) |
| 155 |
|
| 156 |
return assets |
| 157 |
|
| 158 |
def _asset_bgm(self, assets, bgm_list): |
| 159 |
params = { |
| 160 |
|
| 161 |
"quality" : "HIGH", |
| 162 |
"acceptCodecs": "AAC,MP3", |
| 163 |
} |
| 164 |
headers = { |
| 165 |
"Accept" : "application/json", |
| 166 |
"Content-Type": "application/json", |
| 167 |
} |
| 168 |
paths = self.paths |
| 169 |
|
| 170 |
if isinstance(self.bgm, str): |
| 171 |
remux = ext = self.bgm.lower() |
| 172 |
else: |
| 173 |
ext = "mp4" |
| 174 |
remux = False |
| 175 |
|
| 176 |
for bgm in bgm_list: |
| 177 |
url = (f"https://apis.naver.com/audiocweb/audiocplayogwweb/play" |
| 178 |
f"/audio/{bgm['audioId']}/hls/token") |
| 179 |
data = self.request_json( |
| 180 |
url, params=params, headers=headers, interval=False) |
| 181 |
token = data["result"]["playToken"] |
| 182 |
data = util.json_loads(util.b64decode(token)) |
| 183 |
audio = data["audioInfo"] |
| 184 |
play = bgm.get("playImageUrl", "") |
| 185 |
stop = bgm.get("stopImageUrl", "") |
| 186 |
|
| 187 |
assets.append({ |
| 188 |
**bgm, |
| 189 |
**audio, |
| 190 |
"num_play": paths.get(play) or 0, |
| 191 |
"num_stop": paths.get(stop) or 0, |
| 192 |
"filename_play": play[play.rfind("/")+1:play.rfind(".")], |
| 193 |
"filename_stop": stop[stop.rfind("/")+1:stop.rfind(".")], |
| 194 |
"extension": ext, |
| 195 |
"type": "bgm", |
| 196 |
"url" : "ytdl:" + audio["url"], |
| 197 |
"_ytdl_manifest": audio["type"].lower(), |
| 198 |
"_ytdl_manifest_remux": remux, |
| 199 |
}) |
| 200 |
|
| 201 |
|
| 202 |
class WebtoonsComicExtractor(WebtoonsBase, Extractor): |
| 203 |
"""Extractor for an entire comic on webtoons.com""" |
| 204 |
subcategory = "comic" |
| 205 |
categorytransfer = True |
| 206 |
filename_fmt = "{type}.{extension}" |
| 207 |
archive_fmt = "{title_no}_{type}" |
| 208 |
pattern = LANG_PATTERN + r"/([^/?#]+)/([^/?#]+))/list\?([^#]+)" |
| 209 |
example = "https://www.webtoons.com/en/GENRE/TITLE/list?title_no=123" |
| 210 |
|
| 211 |
def items(self): |
| 212 |
kw = self.kwdict |
| 213 |
base, lang, kw["genre"], kw["comic"], query = self.groups |
| 214 |
params = text.parse_query(query) |
| 215 |
kw["title_no"] = title_no = text.parse_int(params.get("title_no")) |
| 216 |
kw["page"] = page_no = text.parse_int(params.get("page"), 1) |
| 217 |
kw["lang"] = lang |
| 218 |
|
| 219 |
path = f"/{base}/list?title_no={title_no}&page={page_no}" |
| 220 |
response = self.request(self.root + path) |
| 221 |
if response.history: |
| 222 |
parts = response.url.split("/") |
| 223 |
base = "/".join(parts[3:-1]) |
| 224 |
page = response.text |
| 225 |
|
| 226 |
if self.config("banners") and (asset := self._asset_banner(page)): |
| 227 |
yield Message.Directory, "", asset |
| 228 |
yield Message.Url, asset["url"], asset |
| 229 |
|
| 230 |
data = {"_extractor": WebtoonsEpisodeExtractor} |
| 231 |
while True: |
| 232 |
for url, dt_string in self._extract_episodes(page): |
| 233 |
params = text.parse_query(url.rpartition("?")[2]) |
| 234 |
data["episode_no"] = text.parse_int(params.get("episode_no")) |
| 235 |
data["date"] = _parse_date(dt_string, lang) |
| 236 |
yield Message.Queue, url, data |
| 237 |
|
| 238 |
kw["page"] = page_no = page_no + 1 |
| 239 |
path = f"/{base}/list?title_no={title_no}&page={page_no}" |
| 240 |
if path not in page: |
| 241 |
return |
| 242 |
page = self.request(self.root + path).text |
| 243 |
|
| 244 |
def _extract_episodes(self, page): |
| 245 |
"""Extract and return all episode urls and dates in 'page'""" |
| 246 |
page = text.extr(page, 'id="_listUl"', "</ul>") |
| 247 |
return [ |
| 248 |
(text.extr(ep, ' href="', '"'), |
| 249 |
text.extr(ep, ' class="date">', '<')) |
| 250 |
for ep in text.extract_iter(page, ' class="_episodeItem', "</li>") |
| 251 |
] |
| 252 |
|
| 253 |
def _asset_banner(self, page): |
| 254 |
try: |
| 255 |
pos = page.index('<span class="thmb') |
| 256 |
except Exception: |
| 257 |
return |
| 258 |
|
| 259 |
url = _url(text.extract(page, 'src="', '"', pos)[0]) |
| 260 |
return text.nameext_from_url(url, {"url": url, "type": "banner"}) |
| 261 |
|
| 262 |
|
| 263 |
class WebtoonsArtistExtractor(WebtoonsBase, Extractor): |
| 264 |
"""Extractor for webtoons.com artists""" |
| 265 |
subcategory = "artist" |
| 266 |
pattern = BASE_PATTERN + r"/p/community/([^/?#]+)/u/([^/?#]+)" |
| 267 |
example = "https://www.webtoons.com/p/community/LANG/u/ARTIST" |
| 268 |
|
| 269 |
def items(self): |
| 270 |
for comic in self.comics(): |
| 271 |
comic["_extractor"] = WebtoonsComicExtractor |
| 272 |
comic_url = self.root + comic["extra"]["episodeListPath"] |
| 273 |
yield Message.Queue, comic_url, comic |
| 274 |
|
| 275 |
def comics(self): |
| 276 |
lang, artist = self.groups |
| 277 |
language = util.code_to_language(lang).upper() |
| 278 |
|
| 279 |
url = f"{self.root}/p/community/{lang}/u/{artist}" |
| 280 |
page = self.request(url).text |
| 281 |
creator_id = text.extr(page, '\\"creatorId\\":\\"', '\\') |
| 282 |
|
| 283 |
url = f"{self.root}/p/community/api/v1/creator/{creator_id}/titles" |
| 284 |
params = { |
| 285 |
"language": language, |
| 286 |
"nextSize": "50", |
| 287 |
} |
| 288 |
headers = { |
| 289 |
"language": language, |
| 290 |
} |
| 291 |
data = self.request_json(url, params=params, headers=headers) |
| 292 |
|
| 293 |
return data["result"]["titles"] |
| 294 |
|
| 295 |
|
| 296 |
def _url(url): |
| 297 |
return url.replace("://webtoon-phinf.", "://swebtoon-phinf.") |
| 298 |
|
| 299 |
|
| 300 |
def _parse_date(date_string, lang): |
| 301 |
"""Parse a locale-dependent date string from the episode list""" |
| 302 |
try: |
| 303 |
date_string = date_string.strip() |
| 304 |
|
| 305 |
if lang == "en": |
| 306 |
return dt.parse(date_string, "%b %d, %Y") |
| 307 |
if lang == "de": |
| 308 |
return dt.parse(date_string, "%d.%m.%Y") |
| 309 |
if lang == "id": |
| 310 |
return dt.parse(date_string, "%d %b %Y") |
| 311 |
if lang == "zh-hant": |
| 312 |
year, month, day = date_string.replace( |
| 313 |
"年", " ").replace("月", " ").replace("日", "").split() |
| 314 |
return dt.datetime(int(year), int(month), int(day)) |
| 315 |
|
| 316 |
if lang == "fr": |
| 317 |
months = ("janv", "févr", "mars", "avr", "mai", "juin", |
| 318 |
"juil", "août", "sept", "oct", "nov", "déc") |
| 319 |
elif lang == "es": |
| 320 |
months = ("ene", "feb", "mar", "abr", "may", "jun", |
| 321 |
"jul", "ago", "sept", "oct", "nov", "dic") |
| 322 |
elif lang == "th": |
| 323 |
months = ("มค", "กพ", "มีค", "เมย", "พค", "มิย", |
| 324 |
"กค", "สค", "กย", "ตค", "พย", "ธค") |
| 325 |
else: |
| 326 |
return dt.NONE |
| 327 |
day, month, year = date_string.replace(".", "").split() |
| 328 |
month = months.index(month.lower()) + 1 |
| 329 |
return dt.datetime(int(year), month, int(day)) |
| 330 |
except Exception: |
| 331 |
return dt.NONE |