From a3d9d0bfd7f35a6f0bcb03b7477c18d7a2edc347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BF=E3=81=A6=E3=82=8B=E3=81=9E?= Date: Thu, 1 Jan 2026 14:00:11 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=82=BF=E3=82=B0=E3=81=A8=E9=96=A2?= =?UTF-8?q?=E4=BF=82=E3=81=AA=E3=81=97=E3=81=AB=E8=BF=BD=E8=B7=A1=E3=81=99?= =?UTF-8?q?=E3=82=8B=E5=8B=95=E7=94=BB=E3=83=AA=E3=82=B9=E3=83=88=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=20(#16)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #15 タグ関係なく追跡する動画リスト Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/nizika_nico/pulls/16 --- db/models.py | 7 ++++ ...026_01_01_041600_create_tracked_videos.sql | 2 + update_db.py | 42 ++++++++++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 migrations/2026_01_01_041600_create_tracked_videos.sql diff --git a/db/models.py b/db/models.py index 37d25e8..fbaffc7 100644 --- a/db/models.py +++ b/db/models.py @@ -58,6 +58,13 @@ class Tag (Model): return self.has_many (VideoTag) +class TrackedVideo (Model): + id: int + code: str + + __timestamps__ = False + + class User (Model): id: int code: str diff --git a/migrations/2026_01_01_041600_create_tracked_videos.sql b/migrations/2026_01_01_041600_create_tracked_videos.sql new file mode 100644 index 0000000..77123cf --- /dev/null +++ b/migrations/2026_01_01_041600_create_tracked_videos.sql @@ -0,0 +1,2 @@ +CREATE TABLE `nizika_nico`.`tracked_videos` (`id` BIGINT NOT NULL AUTO_INCREMENT , `code` VARCHAR(16) NOT NULL COMMENT '動画コード' , PRIMARY KEY (`id`)) ENGINE = InnoDB COMMENT = '追跡対象動画'; +ALTER TABLE `tracked_videos` ADD UNIQUE(`code`); diff --git a/update_db.py b/update_db.py index 395b280..1564a9d 100644 --- a/update_db.py +++ b/update_db.py @@ -21,7 +21,13 @@ import requests from eloquent import DatabaseManager, Model from db.config import DB -from db.models import Comment, Tag, User, Video, VideoHistory, VideoTag +from db.models import (Comment, + Tag, + TrackedVideo, + User, + Video, + VideoHistory, + VideoTag) def main ( @@ -115,9 +121,9 @@ def update_tables ( video.save () -def fetch_comments ( +def fetch_video_data ( video_code: str, -) -> list[CommentResult]: +) -> dict[str, Any]: time.sleep (1.2) headers = { 'X-Frontend-Id': '6', @@ -132,10 +138,14 @@ def fetch_comments ( url = (f"https://www.nicovideo.jp/api/watch/v3_guest/{ video_code }" + f"?actionTrackId={ action_track_id }") - res = requests.post (url, headers = headers, timeout = 60).json () + return requests.post (url, headers = headers, timeout = 60).json () + +def fetch_comments ( + video_code: str, +) -> list[CommentResult]: try: - nv_comment = res['data']['comment']['nvComment'] + nv_comment = fetch_video_data (video_code)['data']['comment']['nvComment'] except KeyError: return [] if nv_comment is None: @@ -162,12 +172,6 @@ def fetch_comments ( return [] -def search_nico_by_tag ( - tag: str, -) -> list[VideoResult]: - return search_nico_by_tags ([tag]) - - def search_nico_by_tags ( tags: list[str], ) -> list[VideoResult]: @@ -209,6 +213,22 @@ def search_nico_by_tags ( pass to = until + timedelta (days = 1) + for video in TrackedVideo.get (): + if video.code in map (lambda v: v['contentId'], result_data): + continue + try: + video_data = fetch_video_data (video.code)['data'] + result_data.append ({ + 'contentId': video.code, + 'title': video_data['video']['title'], + 'tags': ' '.join (map (lambda t: t['name'], + video_data['tag']['items'])), + 'description': video_data['video']['description'], + 'viewCounter': video_data['video']['count']['view'], + 'startTime': video_data['video']['registeredAt'] }) + except Exception: + pass + return result_data