Browse Source

#1 ほぼ完了

main
みてるぞ 1 week ago
parent
commit
ad9f5256e5
3 changed files with 38 additions and 12 deletions
  1. +2
    -0
      .gitingore
  2. +21
    -7
      main.py
  3. +15
    -5
      nico.py

+ 2
- 0
.gitingore View File

@@ -0,0 +1,2 @@
/db
/eloquent.pyi

+ 21
- 7
main.py View File

@@ -71,7 +71,8 @@ def main (
client.login (account.USER_ID, account.PASSWORD) client.login (account.USER_ID, account.PASSWORD)


got_kiriban_at: date = datetime.now ().date () - timedelta (days = datetime.now ().hour < 15) 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 ()) kiriban_interval: timedelta = ((get_kiriban_dt_to_update () - datetime.now ())
/ len (kiriban_list)) / len (kiriban_list))
next_kiriban_at = datetime.now () next_kiriban_at = datetime.now ()
@@ -109,8 +110,9 @@ def main (
root = records[-1]['strong_ref'])) root = records[-1]['strong_ref']))


if kiriban_list and datetime.now () >= next_kiriban_at: 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))) 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'] }" uri = f"https://www.nicovideo.jp/watch/{ video_info['contentId'] }"
(title, description, thumbnail) = get_embed_info (uri) (title, description, thumbnail) = get_embed_info (uri)
try: try:
@@ -120,21 +122,33 @@ def main (
thumb = upload.blob thumb = upload.blob
except Timeout: except Timeout:
thumb = None 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 ( embed_external = models.AppBskyEmbedExternal.Main (
external = models.AppBskyEmbedExternal.External ( external = models.AppBskyEmbedExternal.External (
title = title, title = title,
description = description, description = description,
thumb = thumb, thumb = thumb,
uri = uri)) 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 ```html
{ video_info['description'] } { video_info['description'] }
``` ```
このことについて、ニジカちゃんからのお祝いメッセージを下さい。"""),
embed = embed_external)
このことについて、ニジカちゃんからのお祝いメッセージを下さい。
ただし、そのメッセージ内には再生数の数値とその多さに応じたリアクション(「すっご~い!」や「しょぼ~い」など)を添えてください。"""
client.post (Talk.main (prompt), embed = embed_external)
next_kiriban_at += kiriban_interval next_kiriban_at += kiriban_interval
last_posted_at = now last_posted_at = now




+ 15
- 5
nico.py View File

@@ -5,7 +5,7 @@
from __future__ import annotations from __future__ import annotations


import os import os
from datetime import date, timedelta
from datetime import date, datetime, timedelta
from typing import TypedDict, cast from typing import TypedDict, cast


import requests import requests
@@ -13,7 +13,7 @@ from bs4 import BeautifulSoup
from requests.exceptions import Timeout from requests.exceptions import Timeout
from eloquent import DatabaseManager, Model 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', CONFIG: dict[str, DbConfig] = { 'mysql': { 'driver': 'mysql',
'host': 'localhost', 'host': 'localhost',
@@ -124,8 +124,8 @@ def get_video_info (


def get_kiriban_list ( def get_kiriban_list (
base_date: date, 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')) latest_fetched_at = cast (date, VideoHistory.max ('fetched_at'))
previous_fetched_at = cast (date, (VideoHistory previous_fetched_at = cast (date, (VideoHistory
@@ -144,11 +144,21 @@ def get_kiriban_list (
for code in targets: for code in targets:
video_info = get_video_info (code) video_info = get_video_info (code)
if video_info is not None: 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 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): class DbConfig (TypedDict):
driver: str driver: str
host: str host: str


Loading…
Cancel
Save