Browse Source

投稿者情報追加(#17) (#18)

#17

#17

Co-authored-by: miteruzo <miteruzo@naver.com>
Reviewed-on: #18
main
みてるぞ 4 hours ago
parent
commit
b2adf62090
4 changed files with 25 additions and 0 deletions
  1. +9
    -0
      db/models.py
  2. +2
    -0
      get_videos.py
  3. +3
    -0
      migrations/2026_03_05_122300_add_user_id_to_videos.sql
  4. +11
    -0
      update_db.py

+ 9
- 0
db/models.py View File

@@ -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,


+ 2
- 0
get_videos.py View File

@@ -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]


+ 3
- 0
migrations/2026_03_05_122300_add_user_id_to_videos.sql View File

@@ -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;

+ 11
- 0
update_db.py View File

@@ -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


Loading…
Cancel
Save