| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://chzzk.naver.com/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text, util |
| 11 |
|
| 12 |
|
| 13 |
class NaverChzzkExtractor(Extractor): |
| 14 |
"""Base class for chzzk.naver.com extractors""" |
| 15 |
category = "naver-chzzk" |
| 16 |
filename_fmt = "{uid}_{id}_{num}.{extension}" |
| 17 |
directory_fmt = ("{category}", "{user[userNickname]}") |
| 18 |
archive_fmt = "{uid}_{id}_{num}" |
| 19 |
|
| 20 |
def request_api(self, uid, id=None, params=None): |
| 21 |
return self.request_json( |
| 22 |
f"https://apis.naver.com/nng_main/nng_comment_api/v1/type" |
| 23 |
f"/CHANNEL_POST/id/{uid}/comments/{id or ''}", |
| 24 |
params=params)["content"] |
| 25 |
|
| 26 |
def items(self): |
| 27 |
for comment in self.comments(): |
| 28 |
data = comment["comment"] |
| 29 |
files = data.pop("attaches") or () |
| 30 |
data["id"] = data["commentId"] |
| 31 |
data["uid"] = data["objectId"] |
| 32 |
data["user"] = comment["user"] |
| 33 |
data["count"] = len(files) |
| 34 |
data["date"] = self.parse_datetime( |
| 35 |
data["createdDate"], "%Y%m%d%H%M%S") |
| 36 |
|
| 37 |
yield Message.Directory, "", data |
| 38 |
for data["num"], file in enumerate(files, 1): |
| 39 |
if extra := file.get("extraJson"): |
| 40 |
file.update(util.json_loads(extra)) |
| 41 |
file["date"] = self.parse_datetime_iso( |
| 42 |
file["createdDate"]) |
| 43 |
file["date_updated"] = self.parse_datetime_iso( |
| 44 |
file["updatedDate"]) |
| 45 |
data["file"] = file |
| 46 |
url = file["attachValue"] |
| 47 |
yield Message.Url, url, text.nameext_from_url(url, data) |
| 48 |
|
| 49 |
|
| 50 |
class NaverChzzkCommentExtractor(NaverChzzkExtractor): |
| 51 |
"""Extractor for individual comment from chzzk.naver.com""" |
| 52 |
subcategory = "comment" |
| 53 |
pattern = r"(?:https?://)?chzzk\.naver\.com/(\w+)/community/detail/(\d+)" |
| 54 |
example = "https://chzzk.naver.com/0123456789abcdef/community/detail/12345" |
| 55 |
|
| 56 |
def comments(self): |
| 57 |
uid, id = self.groups |
| 58 |
res = self.request_api(uid, id) |
| 59 |
return ({"comment": res["comment"], "user": res["user"]},) |
| 60 |
|
| 61 |
|
| 62 |
class NaverChzzkCommunityExtractor(NaverChzzkExtractor): |
| 63 |
"""Extractor for comments from chzzk.naver.com""" |
| 64 |
subcategory = "community" |
| 65 |
pattern = r"(?:https?://)?chzzk\.naver\.com/(\w+)/community" |
| 66 |
example = "https://chzzk.naver.com/0123456789abcdef/community" |
| 67 |
request_interval = (0.5, 1.5) |
| 68 |
|
| 69 |
def comments(self): |
| 70 |
uid = self.match[1] |
| 71 |
params = { |
| 72 |
"limit": 10, |
| 73 |
"offset": text.parse_int(self.config("offset")), |
| 74 |
"pagingType": "PAGE", |
| 75 |
} |
| 76 |
while True: |
| 77 |
comments = self.request_api(uid, params=params)["comments"] |
| 78 |
yield from comments["data"] |
| 79 |
if not comments["page"]["next"]: |
| 80 |
return |
| 81 |
params["offset"] += params["limit"] |