From b2adf6209081fb8d073cd03aa27543c8f09fa1b5 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, 5 Mar 2026 21:03:16 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8A=95=E7=A8=BF=E8=80=85=E6=83=85=E5=A0=B1?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=EF=BC=88#17=EF=BC=89=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #17 #17 Co-authored-by: miteruzo Reviewed-on: https://git.miteruzo.com/miteruzo/nizika_nico/pulls/18 --- db/models.py | 9 +++++++++ get_videos.py | 2 ++ .../2026_03_05_122300_add_user_id_to_videos.sql | 3 +++ update_db.py | 11 +++++++++++ 4 files changed, 25 insertions(+) create mode 100644 migrations/2026_03_05_122300_add_user_id_to_videos.sql diff --git a/db/models.py b/db/models.py index fbaffc7..b254c1b 100644 --- a/db/models.py +++ b/db/models.py @@ -81,6 +81,7 @@ class User (Model): class Video (Model): id: int code: str + user_id: int | None title: str description: str uploaded_at: datetime @@ -88,6 +89,14 @@ class Video (Model): __timestamps__ = False + @property + def user ( + self, + ) -> User | None: + if self.user_id is None: + return None + return self.belongs_to (User) + @property def video_histories ( self, diff --git a/get_videos.py b/get_videos.py index 2cb7c3e..f4dc67a 100644 --- a/get_videos.py +++ b/get_videos.py @@ -27,6 +27,7 @@ def main ( deleted_at = row.deleted_at.date () if row.deleted_at else None video: VideoDict = { 'id': row.id, 'code': row.code, + 'user': getattr (row.user, 'code', None), 'title': row.title, 'description': row.description, 'tags': [], @@ -52,6 +53,7 @@ class DbConfig (TypedDict): class VideoDict (TypedDict): id: int code: str + user: str | None title: str description: str tags: list[str] diff --git a/migrations/2026_03_05_122300_add_user_id_to_videos.sql b/migrations/2026_03_05_122300_add_user_id_to_videos.sql new file mode 100644 index 0000000..d92a691 --- /dev/null +++ b/migrations/2026_03_05_122300_add_user_id_to_videos.sql @@ -0,0 +1,3 @@ +ALTER TABLE `videos` ADD `user_id` BIGINT NULL DEFAULT NULL COMMENT 'ユーザ Id.' AFTER `code`; +ALTER TABLE `videos` ADD INDEX(`user_id`); +ALTER TABLE `videos` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/update_db.py b/update_db.py index 1564a9d..d4bd1b8 100644 --- a/update_db.py +++ b/update_db.py @@ -55,8 +55,16 @@ def update_tables ( for datum in api_data: tag_names: list[str] = datum['tags'].split () + user: User | None = None + if datum['userId']: + user = User.where('code', str (datum['userId'])).first () + if user is None: + user = User () + user.code = str (datum['userId']) + user.save () video = Video () video.code = datum['contentId'] + video.user_id = user.id if user else None video.title = datum['title'] video.description = datum['description'] or '' video.uploaded_at = datetime.fromisoformat (datum['startTime']) @@ -199,6 +207,7 @@ def search_nico_by_tags ( 'targets': 'tagsExact', '_sort': '-viewCounter', 'fields': ('contentId,' + 'userId,' 'title,' 'tags,' 'description,' @@ -220,6 +229,7 @@ def search_nico_by_tags ( video_data = fetch_video_data (video.code)['data'] result_data.append ({ 'contentId': video.code, + 'userId': video_data['video']['userId'], 'title': video_data['video']['title'], 'tags': ' '.join (map (lambda t: t['name'], video_data['tag']['items'])), @@ -243,6 +253,7 @@ class VideoSearchParam (TypedDict): class VideoResult (TypedDict): contentId: str + userId: int | None title: str tags: str description: str | None