統合 AI への移行 #16

マージ済み
みてるぞ が 6 個のコミットを ai-migration から main へマージ 2025-10-22 21:50:02 +09:00
4個のファイルの変更245行の追加289行の削除
コミット 3e8c2c5fe0 の変更だけを表示してゐます - すべてのコミットを表示
+3
ファイルの表示
@@ -4,3 +4,6 @@
[submodule "nizika_ai"] [submodule "nizika_ai"]
path = nizika_ai path = nizika_ai
url = https://git.miteruzo.com/miteruzo/nizika_ai.git url = https://git.miteruzo.com/miteruzo/nizika_ai.git
[submodule "nicolib"]
path = nicolib
url = https://git.miteruzo.com/miteruzo/nicolib.git
+222 -87
ファイルの表示
@@ -1,68 +1,142 @@
"""
Bluesky のニジカがいろいろする.
(近々機能ごとにファイル分けて systemd でイベント管理する予定)
"""
from __future__ import annotations from __future__ import annotations
import sys import asyncio
import os
import time import time
from datetime import datetime, timedelta from datetime import datetime
from io import BytesIO
from typing import TypedDict from typing import TypedDict
from atproto import Client, models import atproto
import requests
from atproto import Client
from atproto.models import AppBskyEmbedExternal, AppBskyEmbedImages
from atproto.models.AppBskyFeedPost import ReplyRef
from atproto.models.app.bsky.embed import images
from atproto.models.com.atproto.repo import strong_ref
from atproto_client.models.app.bsky.feed.get_timeline import Response from atproto_client.models.app.bsky.feed.get_timeline import Response
from requests.exceptions import Timeout
import account import account
import nicolib
from nizika_ai.consts import Character, GPTModel, Platform, QueryType
from nizika_ai.models import Answer, AnsweredFlag, Query, User
TARGET_WORDS = ['deerjika', 'ニジカ', 'ぼっち', '虹夏', '郁代', 'バーカ',
'kfif', 'kita-flatten-ikuyo-flatten', 'ラマ田', 'ゴートう',
'ぼざクリ', 'オオミソカ', '伊地知', '喜多ちゃん',
'喜タイ', '洗澡鹿', 'シーザオ', '今日は本当に',
'ダイソーで', '変なチンチン', 'daisoで', 'だね~(笑)',
'おやつタイム', 'わさしが', 'わさび県', 'たぬマ', 'にくまる',
'ルイズマリー', '', 'ニジゴ', 'ゴニジ', 'ニジニジ',
'新年だよね', 'うんこじゃん', 'ほくほくのジャガイモ']
def main (
) -> None:
time.sleep (60) time.sleep (60)
client = Client (base_url = 'https://bsky.social') client = Client (base_url = 'https://bsky.social')
client.login (account.USER_ID, account.PASSWORD) client.login (account.USER_ID, account.PASSWORD)
last_posted_at = datetime.now () - timedelta (hours = 6)
has_got_snack_time = False async def main (
has_taken_hot_spring = False ) -> None:
"""
メーン処理
"""
await asyncio.gather (like_posts (),
check_mentions (),
answer ())
async def like_posts (
) -> None:
while True: while True:
now = datetime.now () try:
for post in fetch_target_posts ():
client.like (**post)
except Exception as e:
print (f"[like_posts] { type (e).__name__ }: { e }")
for uri in check_notifications (client): await asyncio.sleep (60)
records = get_thread_contents (client, uri, 20)
async def check_mentions (
) -> None:
while True:
try:
for uri in check_notifications ():
records = fetch_thread_contents (uri, 20)
if records: if records:
answer = Talk.main ((records[0]['text'] record = records[0]
if (records[0]['embed'] is None content = record['text']
or not hasattr (records[0]['embed'], image_url: str | None = None
'images')) if record['embed'] and hasattr (record['embed'], 'images'):
else [ image_url = ('https://cdn.bsky.app/img/feed_fullsize/plain'
{ 'type': 'text', 'text': records[0]['text'] }, f"/{ record['did'] }"
{ 'type': 'image_url', 'image_url': { f"/{ record['embed'].images[0].image.ref.link }")
'url': (f"https://cdn.bsky.app/img/feed_fullsize" user = _fetch_user (record['did'], record['name'])
f"/plain/{ records[0]['did'] }" _add_query (user, record['text'], image_url, {
f"/{ records[0]['embed'].images[0].image.ref.link }") } }]), 'uri': record['strong_ref']['uri'],
records[0]['name'], 'cid': record['strong_ref']['cid'] })
[*map (lambda record: { except Exception as e:
'role': ('assistant' print (f"[check_mentions] { type (e).__name__ }: { e }")
if (record['handle']
== account.USER_ID)
else 'user'),
'content':
record['text']},
reversed (records[1:]))])
client.post (answer,
reply_to = models.AppBskyFeedPost.ReplyRef (
parent = records[0]['strong_ref'],
root = records[-1]['strong_ref']))
if now.hour == 15: await asyncio.sleep (60)
if not has_got_snack_time:
async def answer (
) -> None:
answered_flags = (
AnsweredFlag
.where ('platform', Platform.BLUESKY.value)
.where ('answered', False)
.get ())
for answered_flag in answered_flags:
answer = answered_flag.answer
match QueryType (answer.query.query_type):
case QueryType.BLUESKY_COMMENT:
td = answer.query.transfer_data or { }
uri: str | None = td.get ('uri')
cid: str | None = td.get ('cid')
if (not uri) or (not cid):
continue
sref = { 'uri': uri, 'cid': cid }
strong_ref = atproto.models.create_strong_ref (sref)
reply_ref = ReplyRef (root = strong_ref, parent = strong_ref)
client.post (answer.content, reply_to = reply_ref)
flag.answered = True
flag.save ()
case QueryType.KIRIBAN | QueryType.NICO_REPORT:
td = answer.query.transfer_data or { }
video_code: str | None = td.get ('video_code')
if not video_code:
continue
uri = f"https://www.nicovideo.jp/watch/{ video_code }"
(title, description, thumbnail) = nicolib.fetch_embed_info (uri)
try:
upload = client.com.atproto.repo.upload_blob (
BytesIO (requests.get (thumbnail, timeout = 60).content))
thumb = upload.blob
except Timeout:
thumb = None
external = AppBskyEmbedExternal.External (
title = title,
description = description,
thumb = thumb,
uri = uri)
embed_external = AppBskyEmbedExternal.Main (external = external)
client.post (answer.content, embed = embed_external)
flag.answered = True
flag.save ()
case QueryType.SNACK_TIME:
try: try:
with open ('./assets/snack-time.jpg', 'rb') as f: with open ('./assets/snack-time.jpg', 'rb') as f:
image = models.AppBskyEmbedImages.Image ( image = AppBskyEmbedImages.Image (
alt = ('左に喜多ちゃん、右に人面鹿のニジカが' alt = (
'左に喜多ちゃん、右に人面鹿のニジカが'
'V字に並んでいる。' 'V字に並んでいる。'
'喜多ちゃんは右手でピースサインをして' '喜多ちゃんは右手でピースサインをして'
'片目をウインクしている。' '片目をウインクしている。'
@@ -74,21 +148,16 @@ def main (
'下部に「おやつタイムだ!!!!」という' '下部に「おやつタイムだ!!!!」という'
'日本語のテキストが表示されている。'), '日本語のテキストが表示されている。'),
image = client.com.atproto.repo.upload_blob (f).blob) image = client.com.atproto.repo.upload_blob (f).blob)
client.post (Talk.main ('おやつタイムだ!!!!'), client.post (answer.content,
embed = models.app.bsky.embed.images.Main ( embed = AppBskyEmbedImages.Main (images = [image]))
images = [image])) flag.answered = True
last_posted_at = now flag.save ()
except Exception: except Exception:
pass pass
has_got_snack_time = True case QueryType.HOT_SPRING:
if now.hour == 20 and has_taken_hot_spring:
has_taken_hot_spring = False
if now.hour == 21 and not has_taken_hot_spring:
try: try:
with open ('./assets/hot-spring.jpg', 'rb') as f: with open ('./assets/hot-spring.jpg', 'rb') as f:
image = models.AppBskyEmbedImages.Image ( image = AppBskyEmbedImages.Image (
alt = ('左に喜多ちゃん、右にわさび県産滋賀県が' alt = ('左に喜多ちゃん、右にわさび県産滋賀県が'
'V字に並んでいる。' 'V字に並んでいる。'
'喜多ちゃんは右手でピースサインをして' '喜多ちゃんは右手でピースサインをして'
@@ -100,28 +169,26 @@ def main (
'下部に「温泉に入ろう!!!」という' '下部に「温泉に入ろう!!!」という'
'日本語のテキストが表示されている。'), '日本語のテキストが表示されている。'),
image = client.com.atproto.repo.upload_blob (f).blob) image = client.com.atproto.repo.upload_blob (f).blob)
client.post (Talk.main ('温泉に入ろう!!!'), client.post (answer.content,
embed = models.app.bsky.embed.images.Main ( embed = AppBskyEmbedImages.Main (images = [image]))
images = [image])) flag.answered = True
last_posted_at = now flag.save ()
except Exception: except Exception:
pass pass
has_taken_hot_spring = True
time.sleep (60)
def check_notifications ( def check_notifications (
client: Client, ) -> list[str]:
) -> list: uris: list[str] = []
(uris, last_seen_at) = ([], client.get_current_time_iso ()) last_seen_at = client.get_current_time_iso ()
for notification in (client.app.bsky.notification.list_notifications () notifications = client.app.bsky.notification.list_notifications ()
.notifications): for notification in notifications.notifications:
if not notification.is_read: if not notification.is_read:
if notification.reason in ['mention', 'reply', 'quote']: match notification.reason:
uris += [notification.uri] case 'mention' | 'reply' | 'quote':
elif notification.reason == 'follow': uris.append (notification.uri)
case 'follow':
client.follow (notification.author.did) client.follow (notification.author.did)
client.app.bsky.notification.update_seen ({ 'seen_at': last_seen_at }) client.app.bsky.notification.update_seen ({ 'seen_at': last_seen_at })
@@ -129,32 +196,100 @@ def check_notifications (
return uris return uris
def get_thread_contents ( def fetch_thread_contents (
client: Client,
uri: str, uri: str,
parent_height: int, parent_height: int,
) -> list: ) -> list[Record]:
response = (client.get_post_thread (uri = uri, post_thread = client.get_post_thread (uri = uri, parent_height = parent_height)
parent_height = parent_height) if not post_thread:
.thread) return []
records = []
while response is not None: res = post_thread.thread
records += [{ 'strong_ref': models.create_strong_ref (response.post),
'did': response.post.author.did, records: list[Record] = []
'handle': response.post.author.handle, while res:
'name': response.post.author.display_name, records.append ({ 'strong_ref': { 'uri': res.post.uri, 'cid': res.post.cid },
'datetime': response.post.record.created_at, 'did': res.post.author.did,
'text': response.post.record.text, 'handle': res.post.author.handle,
'embed': response.post.record.embed }] 'name': res.post.author.display_name,
response = response.parent 'datetime': res.post.record.created_at,
'text': res.post.record.text,
'embed': res.post.record.embed })
res = res.parent
return records return records
def fetch_target_posts (
) -> list[LikeParams]:
posts: list[LikeParams] = []
timeline: Response = client.get_timeline ()
for feed in timeline.feed:
me = getattr (client, 'me', None)
my_did = me.did if me else ''
if (feed.post.author.did != my_did
and (feed.post.viewer.like is None)
and any (target_word in (feed.post.record.text or '').casefold ()
for target_word in TARGET_WORDS)):
posts.append (LikeParams ({ 'uri': feed.post.uri, 'cid': feed.post.cid }))
return posts
def _add_query (
user: User,
content: str,
image_url: str | None = None,
transfer_data: dict | None = None,
) -> None:
query = Query ()
query.user_id = user.id
query.target_character = Character.DEERJIKA.value
query.content = content
query.image_url = image_url or None
query.query_type = QueryType.BLUESKY_COMMENT.value
query.model = GPTModel.GPT3_TURBO.value
query.sent_at = datetime.now ()
query.answered = False
query.transfer_data = transfer_data
query.save ()
# TODO: 履歴情報の追加
def _fetch_user (
did: str,
name: str,
) -> User:
user = User.where ('platform', Platform.BLUESKY.value).where ('code', did).first ()
if user is None:
user = User ()
user.platform = Platform.BLUESKY.value
user.code = did
user.name = name
user.save ()
return user
class LikeParams (TypedDict): class LikeParams (TypedDict):
uri: str uri: str
cid: str cid: str
class Record (TypedDict):
strong_ref: StrongRef
did: str
handle: str
name: str
datetime: str
text: str
embed: object
class StrongRef (TypedDict):
uri: str
cid: str
if __name__ == '__main__': if __name__ == '__main__':
main (*sys.argv[1:]) asyncio.run (main ())
サブモジュール
+1
サブモジュール nicolib32ecf2d00f で追加されました
-183
ファイルの表示
@@ -1,183 +0,0 @@
from __future__ import annotations
import asyncio
import os
import time
from datetime import datetime
from typing import TypedDict
import atproto
from atproto import Client
from atproto_client.models.app.bsky.feed.get_timeline import Response
from atproto_client.models.com.atproto.repo.strong_ref import Main
import account
from nizika_ai.consts import Character, GPTModel, Platform, QueryType
from nizika_ai.models import Answer, AnsweredFlag, Query, User
TARGET_WORDS = ['deerjika', 'ニジカ', 'ぼっち', '虹夏', '郁代', 'バーカ',
'kfif', 'kita-flatten-ikuyo-flatten', 'ラマ田', 'ゴートう',
'ぼざクリ', 'オオミソカ', '伊地知', '喜多ちゃん',
'喜タイ', '洗澡鹿', 'シーザオ', '今日は本当に',
'ダイソーで', '変なチンチン', 'daisoで', 'だね~(笑)',
'おやつタイム', 'わさしが', 'わさび県', 'たぬマ', 'にくまる',
'ルイズマリー', '', 'ニジゴ', 'ゴニジ', 'ニジニジ',
'新年だよね', 'うんこじゃん', 'ほくほくのジャガイモ']
time.sleep (60)
client = Client (base_url = 'https://bsky.social')
client.login (account.USER_ID, account.PASSWORD)
async def main (
) -> None:
"""
メーン処理
"""
await asyncio.gather (like_posts (),
check_mentions ())
async def like_posts (
) -> None:
while True:
try:
for post in fetch_target_posts ():
client.like (**post)
except Exception as e:
print (f"[like_posts] { type (e).__name__ }: { e }")
await asyncio.sleep (60)
async def check_mentions (
) -> None:
while True:
try:
for uri in check_notifications ():
records = fetch_thread_contents (uri, 20)
if records:
record = records[0]
content = record['text']
image_url: str | None = None
if record['embed'] and hasattr (record['embed'], 'images'):
image_url = ('https://cdn.bsky.app/img/feed_fullsize/plain'
f"/{ record['did'] }"
f"/{ record['embed'].images[0].image.ref.link }")
user = _fetch_user (record['did'], record['name'])
_add_query (user, record['text'], image_url)
except Exception as e:
print (f"[check_mentions] { type (e).__name__ }: { e }")
await asyncio.sleep (60)
def check_notifications (
) -> list[str]:
uris: list[str] = []
last_seen_at = client.get_current_time_iso ()
notifications = client.app.bsky.notification.list_notifications()
for notification in notifications.notifications:
if not notification.is_read:
match notification.reason:
case 'mention' | 'reply' | 'quote':
uris.append (notification.uri)
case 'follow':
client.follow (notification.author.did)
client.app.bsky.notification.update_seen ({ 'seen_at': last_seen_at })
return uris
def fetch_thread_contents (
uri: str,
parent_height: int,
) -> list[Record]:
post_thread = client.get_post_thread (uri = uri, parent_height = parent_height)
if not post_thread:
return []
res = post_thread.thread
records: list[Record] = []
while res:
records.append ({ 'strong_ref': atproto.models.create_strong_ref (res.post),
'did': res.post.author.did,
'handle': res.post.author.handle,
'name': res.post.author.display_name,
'datetime': res.post.record.created_at,
'text': res.post.record.text,
'embed': res.post.record.embed })
res = res.parent
return records
def fetch_target_posts (
) -> list[LikeParams]:
posts: list[LikeParams] = []
timeline: Response = client.get_timeline ()
for feed in timeline.feed:
if (feed.post.author.did != client.me.did
and (feed.post.viewer.like is None)
and any (target_word in (feed.post.record.text or '').casefold ()
for target_word in TARGET_WORDS)):
posts.append (LikeParams ({ 'uri': feed.post.uri, 'cid': feed.post.cid }))
return posts
def _add_query (
user: User,
content: str,
image_url: str | None = None,
) -> None:
query = Query ()
query.user_id = user.id
query.target_character = Character.DEERJIKA.value
query.content = content
query.image_url = image_url or None
query.query_type = QueryType.BLUESKY_COMMENT.value
query.model = GPTModel.GPT3_TURBO.value
query.sent_at = datetime.now ()
query.answered = False
query.save ()
# TODO: 履歴情報の追加
def _fetch_user (
did: str,
name: str,
) -> User:
user = User.where ('platform', Platform.BLUESKY.value).where ('code', did).first ()
if user is None:
user = User ()
user.platform = Platform.BLUESKY.value
user.code = did
user.name = name
user.save ()
return user
class LikeParams (TypedDict):
uri: str
cid: str
class Record (TypedDict):
strong_ref: Main
did: str
handle: str
name: str
datetime: str
text: str
embed: object
if __name__ == '__main__':
asyncio.run (main ())