Skip to content

Commit

Permalink
✨ weibo dynamic push use forward-node
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Nov 8, 2024
1 parent 8e9c99c commit 834f004
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 18 deletions.
4 changes: 4 additions & 0 deletions app/database/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from dataclasses import dataclass

from sqlalchemy import MetaData
from sqlalchemy.ext.asyncio import AsyncAttrs
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
Expand All @@ -17,6 +19,7 @@ class Base(AsyncAttrs, DeclarativeBase):
metadata = MetaData(naming_convention=_NAMING_CONVENTION)


@dataclass
class User(Base):
__tablename__ = "user"

Expand All @@ -30,6 +33,7 @@ class User(Base):
"""用户信任度"""


@dataclass
class Group(Base):
__tablename__ = "group"

Expand Down
1 change: 1 addition & 0 deletions app/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Statistic:
time: datetime = field(default_factory=datetime.now)


@dataclass
class StatisticTable(Base):
__tablename__ = "statistic"

Expand Down
2 changes: 2 additions & 0 deletions config/plugins/weibo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 动态推送是否使用合并转发
dynamic_forward: false
3 changes: 3 additions & 0 deletions plugins/admin/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from dataclasses import dataclass

from sqlalchemy import Boolean, String
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


@dataclass
class BlacklistCache(Base):
__tablename__ = "blacklist"

Expand Down
3 changes: 3 additions & 0 deletions plugins/coc/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from dataclasses import dataclass

from sqlalchemy import ForeignKey, Integer
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base, Group


@dataclass
class CocRule(Base):
__tablename__ = "coc"

Expand Down
3 changes: 3 additions & 0 deletions plugins/draw/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from dataclasses import dataclass

from sqlalchemy import DateTime, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base, User


@dataclass
class DrawRecord(Base):
__tablename__ = "draw"

Expand Down
3 changes: 3 additions & 0 deletions plugins/gacha/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from dataclasses import dataclass

from sqlalchemy import ForeignKey, Integer
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base, User


@dataclass
class ArkgachaRecord(Base):
__tablename__ = "arkgacha"

Expand Down
3 changes: 3 additions & 0 deletions plugins/learn_repeat/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from dataclasses import dataclass

from sqlalchemy import JSON, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base, Group


@dataclass
class Learn(Base):
__tablename__ = "learn_repeat"

Expand Down
2 changes: 2 additions & 0 deletions plugins/sign/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, Integer
Expand All @@ -6,6 +7,7 @@
from app.database import Base, User


@dataclass
class SignRecord(Base):
__tablename__ = "sign"

Expand Down
3 changes: 3 additions & 0 deletions plugins/sk_autosign/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from datetime import datetime

from sqlalchemy import JSON, DateTime, ForeignKey, String
Expand All @@ -6,6 +7,7 @@
from app.database import Base, User


@dataclass
class SKAutoSignRecord(Base):
__tablename__ = "sk_autosign"

Expand All @@ -16,6 +18,7 @@ class SKAutoSignRecord(Base):
"""森空岛token"""


@dataclass
class SKAutoSignResultRecord(Base):
__tablename__ = "sk_autosign_result"

Expand Down
9 changes: 9 additions & 0 deletions plugins/weibo/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from app.config import BasePluginConfig


class Config(BasePluginConfig, domain="global"):
dynamic_forward: bool = False
"""动态推送是否使用合并转发"""


WeiboConfig = Config
51 changes: 33 additions & 18 deletions plugins/weibo/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import random
from datetime import datetime
from secrets import token_hex

from arclet.alconna import Alconna, Arg, CommandMeta, Field, Option
Expand Down Expand Up @@ -35,10 +36,11 @@
from app.shortcut import accessable, allow, exclusive, picture, record
from library.weibo import WeiboAPI, WeiboDynamic

from .config import WeiboConfig
from .model import WeiboFollower

bot = RaianBotService.current()

weibo_config = bot.config.plugin.get(WeiboConfig)
weibo_fetch = Alconna(
"微博",
Arg(
Expand Down Expand Up @@ -108,14 +110,16 @@ async def _handle_dynamic(
url = await bot.upload_to_cos(await resp.read(), f"weibo_dym_{token_hex(16)}.png")
imgs.append(Picture(UrlResource(url)))
return first, imgs
# nodes: List[MessageChain] = [first, MessageChain(*imgs)] if imgs else [first]
# if data.video_url:
# nodes.append(MessageChain(f"视频链接: {data.video_url}"))
# if data.retweet:
# # nodes.extend(await _handle_dynamic(app, data.retweet, time, target, name, method))
# nodes.append(MessageChain(Forward(*(await _handle_dynamic(app, data.retweet, time, target, name, method)))))
# # return nodes
# return [ForwardNode(target=target, name=name, time=time, message=i) for i in nodes]


async def _handle_dynamic_forward(data: WeiboDynamic, pw: PlaywrightService, uid: str, name: str, url_imgs: bool = False):
first, imgs = await _handle_dynamic(data, pw, url_imgs)
nodes: list[MessageChain] = [MessageChain([first]), MessageChain(imgs)] if imgs else [MessageChain([first])]
if data.video_url:
nodes.append(MessageChain(f"视频链接: {data.video_url}"))
if data.retweet:
nodes.extend(node.content for node in await _handle_dynamic_forward(data.retweet, pw, uid, name, url_imgs)) # type: ignore
return [Node(uid=uid, name=name, time=datetime.now(), content=i) for i in nodes]


@alcommand(weibo_fetch, comp_session={}, post=True)
Expand Down Expand Up @@ -351,11 +355,7 @@ async def update(avilla: Avilla):
wp = wp.copy()
try:
if res := await api.update(int(uid)):
dynamics[int(uid)] = (
await _handle_dynamic(res, pw, url_imgs=True),
res.user.name if res.user else "",
)
await asyncio.sleep(5)
dynamics[int(uid)] = res
else:
continue
except Exception as e:
Expand Down Expand Up @@ -390,13 +390,28 @@ async def update(avilla: Avilla):
if not accounts:
continue
choose = random.choice(accounts)
self_info = next(
(info for info in bot.config.bots if info.ensure(choose)), None # type: ignore
)
if not self_info:
continue
ctx = choose.get_context(Selector().land("qq").group(group.id))
for uid in mapping[group.id]:
dy, name = dynamics[uid]
if uid not in dynamics:
continue
slot = dynamics[uid]
if isinstance(slot, WeiboDynamic):
name = slot.user.name if slot.user else f"微博用户{uid}"
if weibo_config.dynamic_forward:
nodes = await _handle_dynamic_forward(slot, pw, self_info.account, self_info.name)
dy = Forward(nodes=nodes)
else:
dy, _ = await _handle_dynamic(slot, pw, True)
dynamics[uid] = (dy, name)
else:
dy, name = slot # type: ignore
await ctx.scene.send_message(f"{name} 有一条新动态!请查收!")
await ctx.scene.send_message(dy[0])
# if dy[1]:
# await ctx.scene.send_message([*(Picture(UrlResource(url)) for url in dy[1])])
await ctx.scene.send_message(dy)
await asyncio.sleep(10)

dynamics.clear()
Expand Down
3 changes: 3 additions & 0 deletions plugins/weibo/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from dataclasses import dataclass

from sqlalchemy import ForeignKey, Integer
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base, Group


@dataclass
class WeiboFollower(Base):
__tablename__ = "weibo"

Expand Down

0 comments on commit 834f004

Please sign in to comment.