| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2019-2025 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Extractors for https://blog.naver.com/""" |
| 10 |
|
| 11 |
from .common import GalleryExtractor, Extractor, Message |
| 12 |
from .. import text, util, dt |
| 13 |
import time |
| 14 |
|
| 15 |
|
| 16 |
class NaverBlogBase(): |
| 17 |
"""Base class for blog.naver.com extractors""" |
| 18 |
category = "naver-blog" |
| 19 |
root = "https://blog.naver.com" |
| 20 |
|
| 21 |
|
| 22 |
class NaverBlogPostExtractor(NaverBlogBase, GalleryExtractor): |
| 23 |
"""Extractor for blog posts on blog.naver.com""" |
| 24 |
subcategory = "post" |
| 25 |
filename_fmt = "{num:>03}.{extension}" |
| 26 |
directory_fmt = ("{category}", "{blog[user]} {blog[id]}", |
| 27 |
"{post[date]:%Y-%m-%d} {post[title]}") |
| 28 |
archive_fmt = "{blog[id]}_{post[num]}_{num}" |
| 29 |
pattern = (r"(?:https?://)?blog\.naver\.com/" |
| 30 |
r"(?:PostView\.n(?:aver|hn)\?blogId=(\w+)&logNo=(\d+)|" |
| 31 |
r"(\w+)/(\d+)/?$)") |
| 32 |
example = "https://blog.naver.com/BLOGID/12345" |
| 33 |
|
| 34 |
def __init__(self, match): |
| 35 |
if blog_id := match[1]: |
| 36 |
self.blog_id = blog_id |
| 37 |
self.post_id = match[2] |
| 38 |
else: |
| 39 |
self.blog_id = match[3] |
| 40 |
self.post_id = match[4] |
| 41 |
|
| 42 |
url = (f"{self.root}/PostView.nhn" |
| 43 |
f"?blogId={self.blog_id}&logNo={self.post_id}") |
| 44 |
GalleryExtractor.__init__(self, match, url) |
| 45 |
|
| 46 |
def metadata(self, page): |
| 47 |
extr = text.extract_from(page) |
| 48 |
data = { |
| 49 |
"post": { |
| 50 |
"title" : text.unescape(extr( |
| 51 |
'"og:title" content="', '"')), |
| 52 |
"description": text.unescape(extr( |
| 53 |
'"og:description" content="', '"')).replace(" ", " "), |
| 54 |
"num" : text.parse_int(self.post_id), |
| 55 |
}, |
| 56 |
"blog": { |
| 57 |
"id" : self.blog_id, |
| 58 |
"num" : text.parse_int(extr("var blogNo = '", "'")), |
| 59 |
"user" : extr("var nickName = '", "'"), |
| 60 |
}, |
| 61 |
} |
| 62 |
|
| 63 |
data["post"]["date"] = self._parse_datetime( |
| 64 |
extr('se_publishDate pcol2">', '<') or |
| 65 |
extr('_postAddDate">', '<')) |
| 66 |
|
| 67 |
return data |
| 68 |
|
| 69 |
def _parse_datetime(self, dt_string): |
| 70 |
if "전" in dt_string: |
| 71 |
ts = time.gmtime() |
| 72 |
return dt.datetime(ts.tm_year, ts.tm_mon, ts.tm_mday) |
| 73 |
return dt.parse(dt_string, "%Y. %m. %d. %H:%M") |
| 74 |
|
| 75 |
def images(self, page): |
| 76 |
files = [] |
| 77 |
self._extract_images(files, page) |
| 78 |
if self.config("videos", True): |
| 79 |
self._extract_videos(files, page) |
| 80 |
return files |
| 81 |
|
| 82 |
def _extract_images(self, files, page): |
| 83 |
for url in text.extract_iter(page, 'data-lazy-src="', '"'): |
| 84 |
url = url.replace("://post", "://blog", 1).partition("?")[0] |
| 85 |
if "\ufffd" in text.unquote(url): |
| 86 |
url = text.unquote(url, encoding="EUC-KR") |
| 87 |
files.append((url, None)) |
| 88 |
|
| 89 |
def _extract_videos(self, files, page): |
| 90 |
for module in text.extract_iter(page, " data-module='", "'"): |
| 91 |
if '"v2_video"' not in module: |
| 92 |
continue |
| 93 |
try: |
| 94 |
media = util.json_loads(module)["data"] |
| 95 |
self._extract_media(files, media) |
| 96 |
except Exception as exc: |
| 97 |
self.log.warning("%s: Failed to extract video '%s' (%s: %s)", |
| 98 |
self.post_id, media.get("vid"), |
| 99 |
exc.__class__.__name__, exc) |
| 100 |
|
| 101 |
def _extract_media(self, files, media): |
| 102 |
url = ("https://apis.naver.com/rmcnmv/rmcnmv/vod/play/v2.0/" + |
| 103 |
media["vid"]) |
| 104 |
params = { |
| 105 |
"key" : media["inkey"], |
| 106 |
"sid" : "2", |
| 107 |
# "pid": "00000000-0000-0000-0000-000000000000", |
| 108 |
"nonce": int(time.time()), |
| 109 |
"devt" : "html5_pc", |
| 110 |
"prv" : "N", |
| 111 |
"aup" : "N", |
| 112 |
"stpb" : "N", |
| 113 |
"cpl" : "ko_KR", |
| 114 |
"providerEnv": "real", |
| 115 |
"adt" : "glad", |
| 116 |
"lc" : "ko_KR", |
| 117 |
} |
| 118 |
data = self.request_json(url, params=params) |
| 119 |
video = max(data["videos"]["list"], |
| 120 |
key=lambda v: v.get("size") or 0) |
| 121 |
files.append((video["source"], video)) |
| 122 |
|
| 123 |
|
| 124 |
class NaverBlogBlogExtractor(NaverBlogBase, Extractor): |
| 125 |
"""Extractor for a user's blog on blog.naver.com""" |
| 126 |
subcategory = "blog" |
| 127 |
categorytransfer = True |
| 128 |
pattern = (r"(?:https?://)?blog\.naver\.com/" |
| 129 |
r"(?:PostList\.n(?:aver|hn)\?(?:[^&#]+&)*blogId=([^&#]+)|" |
| 130 |
r"(\w+)/?$)") |
| 131 |
example = "https://blog.naver.com/BLOGID" |
| 132 |
|
| 133 |
def __init__(self, match): |
| 134 |
Extractor.__init__(self, match) |
| 135 |
self.blog_id = match[1] or match[2] |
| 136 |
|
| 137 |
def items(self): |
| 138 |
# fetch first post number |
| 139 |
url = f"{self.root}/PostList.nhn?blogId={self.blog_id}" |
| 140 |
post_num = text.extr( |
| 141 |
self.request(url).text, 'gnFirstLogNo = "', '"', |
| 142 |
) |
| 143 |
|
| 144 |
# setup params for API calls |
| 145 |
url = self.root + "/PostViewBottomTitleListAsync.nhn" |
| 146 |
params = { |
| 147 |
"blogId" : self.blog_id, |
| 148 |
"logNo" : post_num or "0", |
| 149 |
"viewDate" : "", |
| 150 |
"categoryNo" : "", |
| 151 |
"parentCategoryNo" : "", |
| 152 |
"showNextPage" : "true", |
| 153 |
"showPreviousPage" : "false", |
| 154 |
"sortDateInMilli" : "", |
| 155 |
"isThumbnailViewType": "false", |
| 156 |
"countPerPage" : "", |
| 157 |
} |
| 158 |
|
| 159 |
# loop over all posts |
| 160 |
while True: |
| 161 |
data = self.request_json(url, params=params) |
| 162 |
|
| 163 |
for post in data["postList"]: |
| 164 |
post["url"] = (f"{self.root}/PostView.nhn?blogId=" |
| 165 |
f"{self.blog_id}&logNo={post['logNo']}") |
| 166 |
post["_extractor"] = NaverBlogPostExtractor |
| 167 |
yield Message.Queue, post["url"], post |
| 168 |
|
| 169 |
if not data["hasNextPage"]: |
| 170 |
return |
| 171 |
params["logNo"] = data["nextIndexLogNo"] |
| 172 |
params["sortDateInMilli"] = data["nextIndexSortDate"] |