Skip to content

Commit

Permalink
Merge pull request #16 from idoknow/version/1.0.0
Browse files Browse the repository at this point in the history
Release: 1.0.0
  • Loading branch information
RockChinQ authored Sep 2, 2024
2 parents 93ee82d + 0a6cc66 commit 0e70b62
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 117 deletions.
23 changes: 0 additions & 23 deletions .env.example

This file was deleted.

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
venv
cache.json
metadata.json
data/
/tests
24 changes: 11 additions & 13 deletions campux/api/api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from __future__ import annotations

import aiohttp
import nonebot

from . import errors
from ..common import entity
from ..core import app


config = nonebot.get_driver().config
class CampuxAPI:

ap: app.Application

class CampuxAPI:
def __init__(self):
pass
def __init__(self, ap: app.Application):
self.ap = ap

async def data(
self,
Expand All @@ -22,10 +25,10 @@ async def data(
async with aiohttp.ClientSession() as session:
async with session.request(
method,
f"{config.campux_api}{path}",
f"{self.ap.config.data['campux_api']}{path}",
params=params,
json=body,
headers={"Authorization": f"Bearer {config.campux_token}"}
headers={"Authorization": f"Bearer {self.ap.config.data['campux_token']}"}
) as resp:
return await self.assert_data(resp)

Expand All @@ -39,10 +42,10 @@ async def read(
async with aiohttp.ClientSession() as session:
async with session.request(
method,
f"{config.campux_api}{path}",
f"{self.ap.config.data['campux_api']}{path}",
params=params,
json=body,
headers={"Authorization": f"Bearer {config.campux_token}"}
headers={"Authorization": f"Bearer {self.ap.config.data['campux_token']}"}
) as resp:
return await resp.read()

Expand Down Expand Up @@ -190,8 +193,3 @@ async def submit_post_verbose(
"values": values
}
)

campux_api = None

if campux_api is None:
campux_api = CampuxAPI()
53 changes: 36 additions & 17 deletions campux/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ class Application:
cache: cache_mgr.CacheManager

meta: config_mgr.ConfigManager

@property
def cpx_api(self) -> api.CampuxAPI:
return api.campux_api

@property
def config(self) -> nonebot.config.Config:
return nonebot.get_driver().config
config: config_mgr.ConfigManager

cpx_api: api.CampuxAPI

mq: redis.RedisStreamMQ

redis_name_proxy: redis.RedisNameProxy

social: social_mgr.SocialPlatformManager

imbot: imbot_mgr.IMBotManager
Expand All @@ -46,31 +44,52 @@ def nonebot_thread():

async def create_app() -> Application:

# 注册适配器
driver = nonebot.get_driver()
driver.register_adapter(OnebotAdapter)

# 在这里加载插件
nonebot.load_plugin("campux.imbot.nbmod") # 本地插件

# 元数据
meta = await config_mgr.load_json_config("data/metadata.json", template_data={
"post_publish_text": "'#' + str(post_id)",
})
meta = await config_mgr.load_json_config(
"data/metadata.json",
template_name="templates/metadata.json"
)

await meta.load_config()
await meta.dump_config()

# 配置文件
config = await config_mgr.load_json_config(
"data/config.json",
template_name="templates/config.json"
)

await config.load_config()
await config.dump_config()

# 缓存管理器
cache = cache_mgr.CacheManager()
cache.load()

ap = Application()
ap.cache = cache
ap.meta = meta
ap.config = config

cpx_api = api.CampuxAPI(ap=ap)
ap.cpx_api = cpx_api

import nonebot
# 初始化 NoneBot
nonebot.init(
**config.data
)

# 注册适配器
driver = nonebot.get_driver()
driver.register_adapter(OnebotAdapter)

# 在这里加载插件
nonebot.load_plugin("campux.imbot.nbmod") # 本地插件

ap.bot_event_loop = asyncio.get_event_loop()

ap.redis_name_proxy = redis.RedisNameProxy(ap)
ap.mq = redis.RedisStreamMQ(ap)
await ap.mq.initialize()
ap.social = social_mgr.SocialPlatformManager(ap)
Expand Down
8 changes: 4 additions & 4 deletions campux/imbot/mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def send_new_post_notify(

logger.info(f"新稿件:{post}")

if self.ap.config.campux_qq_group_review:
if self.ap.config.data['campux_qq_group_review']:

# 获取所有图片
images = [
Expand All @@ -77,7 +77,7 @@ async def send_new_post_notify(
)

asyncio.create_task(self.ap.imbot.send_group_message(
self.ap.config.campux_review_qq_group_id,
self.ap.config.data['campux_review_qq_group_id'],
msg
))

Expand All @@ -87,13 +87,13 @@ async def send_post_cancel_notify(
):
logger.info(f"稿件已取消:{post_id}")

if self.ap.config.campux_qq_group_review:
if self.ap.config.data['campux_qq_group_review']:

msg = [
message.MessageSegment.text(f"稿件已取消: #{post_id}"),
]

asyncio.create_task(self.ap.imbot.send_group_message(
self.ap.config.campux_review_qq_group_id,
self.ap.config.data['campux_review_qq_group_id'],
msg
))
16 changes: 8 additions & 8 deletions campux/imbot/nbmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async def is_private(event: Event):
return type(event) == ob11_event.PrivateMessageEvent

async def not_bot_self(event: Event):
return int(event.get_user_id()) != ap.config.campux_qq_bot_uin
return int(event.get_user_id()) != ap.config.data['campux_qq_bot_uin']

help_message_sent_record: dict[int, int] = {} # user_id, time

Expand Down Expand Up @@ -77,13 +77,13 @@ async def reset_password_func(event: Event):
async def relogin_qzone_func(event: Event):
user_id = int(event.get_user_id())

if user_id != ap.config.campux_qq_admin_uin:
if user_id != ap.config.data['campux_qq_admin_uin']:
await relogin_qzone.finish("无权限")
return

async def qrcode_callback(content: bytes):
asyncio.create_task(ap.imbot.send_private_message(
ap.config.campux_qq_admin_uin,
ap.config.data['campux_qq_admin_uin'],
message=[
message.MessageSegment.text("请使用QQ扫描以下二维码以登录QQ空间:"),
message.MessageSegment.image(content)
Expand Down Expand Up @@ -117,7 +117,7 @@ async def is_group(event: Event):
async def is_review_allow(event: Event):
if type(event) == ob11_event.PrivateMessageEvent:
return False
return ap.config.campux_qq_group_review and int(event.group_id) == int(ap.config.campux_review_qq_group_id)
return ap.config.data['campux_qq_group_review'] and int(event.group_id) == int(ap.config.data['campux_review_qq_group_id'])

# #通过 [id]
approve_post = on_command("通过", rule=to_me() & is_group & is_review_allow, priority=10, block=True)
Expand Down Expand Up @@ -164,7 +164,7 @@ async def approve_post_func(event: Event):
else:
await approve_post.finish(f"稿件 #{post_id} 状态不是待审核")
else:
await approve_post.finish(ap.config.campux_review_help_message)
await approve_post.finish(ap.config.data['campux_review_help_message'])
except Exception as e:
if isinstance(e, nonebot.exception.FinishedException):
return
Expand Down Expand Up @@ -203,7 +203,7 @@ async def reject_post_func(event: Event):
else:
await reject_post.finish(f"稿件 #{post_id} 状态不是待审核")
else:
await reject_post.finish(ap.config.campux_review_help_message)
await reject_post.finish(ap.config.data['campux_review_help_message'])
except Exception as e:
if isinstance(e, nonebot.exception.FinishedException):
return
Expand All @@ -229,7 +229,7 @@ async def resend_post_func(event: Event):
# 添加到ap.bot_event_loop中
ap.bot_event_loop.create_task(ap.social.publish_post(post_id))
else:
await resend_post.finish(ap.config.campux_review_help_message)
await resend_post.finish(ap.config.data['campux_review_help_message'])
except Exception as e:
if isinstance(e, nonebot.exception.FinishedException):
return
Expand All @@ -238,4 +238,4 @@ async def resend_post_func(event: Event):

@any_message_group.handle()
async def any_message_group_func(event: Event):
await any_message_group.finish(ap.config.campux_review_help_message)
await any_message_group.finish(ap.config.data['campux_review_help_message'])
Loading

0 comments on commit 0e70b62

Please sign in to comment.