diff --git a/.gitingore b/.gitingore new file mode 100644 index 0000000..b51353c --- /dev/null +++ b/.gitingore @@ -0,0 +1,2 @@ +/db +/eloquent.pyi diff --git a/main.py b/main.py index 0fb39f0..d9a560c 100644 --- a/main.py +++ b/main.py @@ -71,7 +71,8 @@ def main ( client.login (account.USER_ID, account.PASSWORD) got_kiriban_at: date = datetime.now ().date () - timedelta (days = datetime.now ().hour < 15) - kiriban_list: list[tuple[int, nico.VideoInfo]] = nico.get_kiriban_list (got_kiriban_at) + kiriban_list: list[tuple[int, nico.VideoInfo, datetime]] = ( + nico.get_kiriban_list (got_kiriban_at)) kiriban_interval: timedelta = ((get_kiriban_dt_to_update () - datetime.now ()) / len (kiriban_list)) next_kiriban_at = datetime.now () @@ -109,8 +110,9 @@ def main ( root = records[-1]['strong_ref'])) if kiriban_list and datetime.now () >= next_kiriban_at: - (views_count, video_info) = ( + (views_count, video_info, uploaded_at) = ( kiriban_list.pop (random.randint (0, len (kiriban_list) - 1))) + since_posted = datetime.now () - uploaded_at uri = f"https://www.nicovideo.jp/watch/{ video_info['contentId'] }" (title, description, thumbnail) = get_embed_info (uri) try: @@ -120,21 +122,33 @@ def main ( thumb = upload.blob except Timeout: thumb = None + comments = nico.get_comments (video_info['contentId']) + popular_comments = sorted (comments, + key = lambda c: c.nico_count, + reverse = True)[:10] + latest_comments = sorted (comments, + key = lambda c: c.posted_at, + reverse = True)[:10] embed_external = models.AppBskyEmbedExternal.Main ( external = models.AppBskyEmbedExternal.External ( title = title, description = description, thumb = thumb, uri = uri)) - client.post (Talk.main (f""" -ニコニコの『{ video_info['title'] }』という動画が{ views_count }再生を突破しました。 -つけられたタグは「{ '」、「'.join (video_info['tags']) }」です。 + prompt = f"{ since_posted.days }日と{ since_posted.seconds }秒前にニコニコに投稿された『{ video_info['title'] }』という動画が{ views_count }再生を突破しました。\n" + if video_info['tags']: + prompt += f"つけられたタグは「{ '」、「'.join (video_info['tags']) }」です。\n" + if comments: + prompt += f"人気のコメントは次の通りです:「{ '」、「'.join (c.content for c in popular_comments) }」\n" + prompt += f"最新のコメントは次の通りです:「{ '」、「'.join (c.content for c in latest_comments) }」\n" + prompt += f""" 概要には次のように書かれています: ```html { video_info['description'] } ``` -このことについて、ニジカちゃんからのお祝いメッセージを下さい。"""), - embed = embed_external) +このことについて、ニジカちゃんからのお祝いメッセージを下さい。 +ただし、そのメッセージ内には再生数の数値とその多さに応じたリアクション(「すっご~い!」や「しょぼ~い」など)を添えてください。""" + client.post (Talk.main (prompt), embed = embed_external) next_kiriban_at += kiriban_interval last_posted_at = now diff --git a/nico.py b/nico.py index 94afc34..c62b724 100644 --- a/nico.py +++ b/nico.py @@ -5,7 +5,7 @@ from __future__ import annotations import os -from datetime import date, timedelta +from datetime import date, datetime, timedelta from typing import TypedDict, cast import requests @@ -13,7 +13,7 @@ from bs4 import BeautifulSoup from requests.exceptions import Timeout from eloquent import DatabaseManager, Model -from db.models import Tag, Video, VideoHistory, VideoTag +from db.models import Comment, Tag, Video, VideoHistory, VideoTag CONFIG: dict[str, DbConfig] = { 'mysql': { 'driver': 'mysql', 'host': 'localhost', @@ -124,8 +124,8 @@ def get_video_info ( def get_kiriban_list ( base_date: date, -) -> list[tuple[int, VideoInfo]]: - kiriban_list: list[tuple[int, VideoInfo]] = [] +) -> list[tuple[int, VideoInfo, datetime]]: + kiriban_list: list[tuple[int, VideoInfo, datetime]] = [] latest_fetched_at = cast (date, VideoHistory.max ('fetched_at')) previous_fetched_at = cast (date, (VideoHistory @@ -144,11 +144,21 @@ def get_kiriban_list ( for code in targets: video_info = get_video_info (code) if video_info is not None: - kiriban_list.append ((kiriban_views_count, video_info)) + kiriban_list.append ((kiriban_views_count, video_info, + cast (Video, Video.where ('code', code).first ()).uploaded_at)) return kiriban_list +def get_comments ( + video_code: str, +) -> list[Comment]: + video = Video.where ('code', video_code).first () + if video is None: + return [] + return video.comments + + class DbConfig (TypedDict): driver: str host: str