Browse Source

#11 完了?

ai-migration
みてるぞ 2 weeks ago
parent
commit
6eb191a7f4
1 changed files with 31 additions and 24 deletions
  1. +31
    -24
      test.py

+ 31
- 24
test.py View File

@@ -1,7 +1,9 @@
from __future__ import annotations

import asyncio
import os
import time
from datetime import datetime
from typing import TypedDict

import atproto
@@ -10,7 +12,7 @@ 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 QueryType
from nizika_ai.consts import Character, GPTModel, Platform, QueryType
from nizika_ai.models import Answer, AnsweredFlag, Query, User

TARGET_WORDS = ['deerjika', 'ニジカ', 'ぼっち', '虹夏', '郁代', 'バーカ',
@@ -35,15 +37,17 @@ async def main (
"""

await asyncio.gather (like_posts (),
reply (),
check_mentions ())


async def like_posts (
) -> None:
while True:
for post in fetch_target_posts ():
client.like (**post)
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)

@@ -51,18 +55,21 @@ async def like_posts (
async def check_mentions (
) -> None:
while True:
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)
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)

@@ -100,11 +107,11 @@ def fetch_thread_contents (
while res:
records.append ({ 'strong_ref': atproto.models.create_strong_ref (res.post),
'did': res.post.author.did,
'handle': response.post.author.handle,
'name': response.post.author.display_name,
'datetime': response.post.record.created_at,
'text': response.post.record.text,
'embed': response.post.record.embed })
'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
@@ -118,7 +125,7 @@ def fetch_target_posts (
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.lower ()
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 }))

@@ -147,10 +154,10 @@ def _fetch_user (
did: str,
name: str,
) -> User:
user = User.where ('platform', Platform.BLUESKY).where ('code', did).first ()
user = User.where ('platform', Platform.BLUESKY.value).where ('code', did).first ()
if user is None:
user = User ()
user.platform = Platfrom.BLUESKY
user.platform = Platform.BLUESKY.value
user.code = did
user.name = name
user.save ()


Loading…
Cancel
Save