✨ Support ci
This commit is contained in:
parent
dcca00c4a1
commit
8b5ad8c9e6
1
.gitignore
vendored
1
.gitignore
vendored
@ -155,3 +155,4 @@ cython_debug/
|
|||||||
# data
|
# data
|
||||||
config.ini
|
config.ini
|
||||||
*.session
|
*.session
|
||||||
|
data/
|
||||||
|
30
ci.py
30
ci.py
@ -1,11 +1,29 @@
|
|||||||
from configparser import RawConfigParser
|
from configparser import RawConfigParser
|
||||||
from httpx import get
|
|
||||||
from pyrogram import Client
|
|
||||||
|
|
||||||
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
|
from httpx import get
|
||||||
|
from os.path import exists
|
||||||
|
from os import mkdir, sep
|
||||||
|
from pyrogram import Client
|
||||||
|
from sqlitedict import SqliteDict
|
||||||
|
|
||||||
|
if not exists("data"):
|
||||||
|
mkdir("data")
|
||||||
|
sqlite = SqliteDict(f"data{sep}sqlite.db", autocommit=True)
|
||||||
|
# 结构如下
|
||||||
|
# {
|
||||||
|
# "artifact_id": 0,
|
||||||
|
# }
|
||||||
config = RawConfigParser()
|
config = RawConfigParser()
|
||||||
config.read("config.ini")
|
config.read("config.ini")
|
||||||
bot_token: str = ""
|
bot_token: str = ""
|
||||||
bot_token = config.get("basic", "bot_token", fallback=bot_token)
|
api_id: int = 0
|
||||||
|
api_hash: str = ""
|
||||||
|
channel_id: int = 0
|
||||||
|
bot_token = config.get("pyrogram", "bot_token", fallback=bot_token)
|
||||||
|
api_id = config.get("pyrogram", "api_id", fallback=api_id)
|
||||||
|
api_hash = config.get("basic", "api_hash", fallback=api_hash)
|
||||||
|
channel_id = config.get("basic", "channel_id", fallback=channel_id)
|
||||||
|
|
||||||
|
|
||||||
# 自定义类型
|
# 自定义类型
|
||||||
@ -18,4 +36,8 @@ class Bot:
|
|||||||
|
|
||||||
me = Bot(get(f"https://api.telegram.org/bot{bot_token}/getme").json()["result"])
|
me = Bot(get(f"https://api.telegram.org/bot{bot_token}/getme").json()["result"])
|
||||||
# 初始化客户端
|
# 初始化客户端
|
||||||
app = Client("bot", bot_token=bot_token)
|
scheduler = AsyncIOScheduler()
|
||||||
|
if not scheduler.running:
|
||||||
|
scheduler.configure(timezone="Asia/ShangHai")
|
||||||
|
scheduler.start()
|
||||||
|
app = Client("bot", api_id=api_id, api_hash=api_hash, bot_token=bot_token, plugins=dict(root="plugins"))
|
||||||
|
@ -4,6 +4,7 @@ api_hash = 0123456789abc0123456789abc
|
|||||||
|
|
||||||
[basic]
|
[basic]
|
||||||
bot_token = 111:abc
|
bot_token = 111:abc
|
||||||
|
channel_id = 0
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
root = plugins
|
root = plugins
|
||||||
|
53
defs/ci.py
Normal file
53
defs/ci.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import datetime
|
||||||
|
from os.path import exists
|
||||||
|
from os import remove
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from httpx import get, stream
|
||||||
|
|
||||||
|
|
||||||
|
class Artifact:
|
||||||
|
def __init__(self, data: dict):
|
||||||
|
self.id: int = data.get("id", 0)
|
||||||
|
self.name: str = "Grasscutter.zip"
|
||||||
|
self.url: str = data.get("archive_download_url", "")
|
||||||
|
size: int = data.get("archive_size", 0)
|
||||||
|
self.size: str = f"{size / 1024 / 1024:.2f} MB"
|
||||||
|
self.expired: bool = data.get("expired", False)
|
||||||
|
created_at: str = data.get("created_at", "")
|
||||||
|
# 2022-04-27T06:31:46Z
|
||||||
|
try:
|
||||||
|
self.created_at: datetime.datetime = datetime.datetime.strptime(
|
||||||
|
created_at, "%Y-%m-%dT%H:%M:%SZ"
|
||||||
|
) + datetime.timedelta(hours=8)
|
||||||
|
except ValueError:
|
||||||
|
self.created_at: datetime.datetime = datetime.datetime.utcnow() + datetime.timedelta(hours=8)
|
||||||
|
self.created_time: str = self.created_at.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
self.hash: str = get_hash()
|
||||||
|
|
||||||
|
async def download(self, path: str = "Grasscutter.zip"):
|
||||||
|
if exists("Grasscutter.zip"):
|
||||||
|
remove("Grasscutter.zip")
|
||||||
|
async with stream("GET", self.url, allow_redirects=True) as response:
|
||||||
|
for chunk in response.iter_bytes():
|
||||||
|
with open(path, "ab") as f:
|
||||||
|
f.write(chunk)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_artifact() -> Optional[Artifact]:
|
||||||
|
req = get("https://api.github.com/repos/Grasscutters/Grasscutter/actions/artifacts?per_page=1")
|
||||||
|
if req.status_code == 200:
|
||||||
|
data = req.json()
|
||||||
|
if data.get("artifacts", []):
|
||||||
|
return Artifact(data["artifacts"][0])
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_hash() -> str:
|
||||||
|
req = get("https://api.github.com/repos/Grasscutters/Grasscutter/actions/runs?per_page=1&status=success")
|
||||||
|
if req.status_code == 200:
|
||||||
|
data = req.json()
|
||||||
|
if data.get("workflow_runs", []):
|
||||||
|
return data["workflow_runs"][0]["head_commit"]["id"][:6] + " " + \
|
||||||
|
data["workflow_runs"][0]["head_commit"]["message"][:50]
|
||||||
|
return ""
|
26
plugins/update.py
Normal file
26
plugins/update.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
from pyrogram import Client, filters
|
||||||
|
from pyrogram.types import Message
|
||||||
|
from ci import app, channel_id, sqlite, scheduler
|
||||||
|
from defs.ci import get_artifact
|
||||||
|
|
||||||
|
|
||||||
|
@scheduler.scheduled_job("cron", minute="*/30", id="0")
|
||||||
|
async def run_every_30_minute():
|
||||||
|
old_artifact_id = sqlite.get("artifact_id", 0)
|
||||||
|
new_artifact = await get_artifact()
|
||||||
|
if old_artifact_id == new_artifact.id:
|
||||||
|
return
|
||||||
|
if not channel_id:
|
||||||
|
return
|
||||||
|
sqlite.set("artifact_id", new_artifact.id)
|
||||||
|
await new_artifact.download()
|
||||||
|
await app.send_document(channel_id, "Grasscutter.zip",
|
||||||
|
caption=new_artifact.hash,
|
||||||
|
force_document=True)
|
||||||
|
|
||||||
|
|
||||||
|
@Client.on_message(filters.incoming & filters.private &
|
||||||
|
filters.command(["force_update", ]))
|
||||||
|
async def force_update(_: Client, message: Message):
|
||||||
|
await run_every_30_minute()
|
||||||
|
await message.reply("Updated!", quote=True)
|
@ -1,3 +1,5 @@
|
|||||||
pyrogram==1.4.16
|
pyrogram==2.0.13
|
||||||
tgcrypto
|
tgcrypto
|
||||||
httpx
|
httpx
|
||||||
|
sqlitedict
|
||||||
|
apscheduler>=3.8.1
|
||||||
|
Loading…
Reference in New Issue
Block a user