Fix a bug

This commit is contained in:
xtaodada 2022-04-27 17:08:14 +08:00
parent 8b5ad8c9e6
commit e09d0bf09d
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
5 changed files with 41 additions and 18 deletions

2
.gitignore vendored
View File

@ -154,5 +154,5 @@ cython_debug/
# data # data
config.ini config.ini
*.session *.session*
data/ data/

8
ci.py
View File

@ -1,7 +1,7 @@
from configparser import RawConfigParser from configparser import RawConfigParser
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
from httpx import get from httpx import get, AsyncClient
from os.path import exists from os.path import exists
from os import mkdir, sep from os import mkdir, sep
from pyrogram import Client from pyrogram import Client
@ -19,11 +19,13 @@ config.read("config.ini")
bot_token: str = "" bot_token: str = ""
api_id: int = 0 api_id: int = 0
api_hash: str = "" api_hash: str = ""
ghp: str = ""
channel_id: int = 0 channel_id: int = 0
bot_token = config.get("pyrogram", "bot_token", fallback=bot_token) bot_token = config.get("basic", "bot_token", fallback=bot_token)
api_id = config.get("pyrogram", "api_id", fallback=api_id) api_id = config.get("pyrogram", "api_id", fallback=api_id)
api_hash = config.get("basic", "api_hash", fallback=api_hash) api_hash = config.get("pyrogram", "api_hash", fallback=api_hash)
channel_id = config.get("basic", "channel_id", fallback=channel_id) channel_id = config.get("basic", "channel_id", fallback=channel_id)
ghp = config.get("basic", "ghp", fallback=ghp)
# 自定义类型 # 自定义类型

View File

@ -5,6 +5,7 @@ api_hash = 0123456789abc0123456789abc
[basic] [basic]
bot_token = 111:abc bot_token = 111:abc
channel_id = 0 channel_id = 0
ghp = ghp_xxx
[plugins] [plugins]
root = plugins root = plugins

View File

@ -1,9 +1,21 @@
import datetime import datetime
from configparser import RawConfigParser
from os.path import exists from os.path import exists
from os import remove from os import remove, sep
from typing import Optional from typing import Optional
from httpx import get, AsyncClient
from httpx import get, stream """ Init httpx client """
# 使用自定义 UA
config = RawConfigParser()
config.read("config.ini")
ghp: str = ""
ghp = config.get("basic", "ghp", fallback=ghp)
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
"Authorization": f"token {ghp}",
}
client = AsyncClient(timeout=10.0, headers=headers, follow_redirects=True)
class Artifact: class Artifact:
@ -25,17 +37,20 @@ class Artifact:
self.created_time: str = self.created_at.strftime("%Y-%m-%d %H:%M:%S") self.created_time: str = self.created_at.strftime("%Y-%m-%d %H:%M:%S")
self.hash: str = get_hash() self.hash: str = get_hash()
async def download(self, path: str = "Grasscutter.zip"): async def download(self, path: str = f"data{sep}Grasscutter.zip"):
if exists("Grasscutter.zip"): if exists(path):
remove("Grasscutter.zip") remove(path)
async with stream("GET", self.url, allow_redirects=True) as response:
for chunk in response.iter_bytes(): content = await client.get(self.url)
with open(path, "ab") as f: if content.status_code == 404:
f.write(chunk) raise FileNotFoundError
content = content.content
with open(path, 'wb') as f:
f.write(content)
async def get_artifact() -> Optional[Artifact]: async def get_artifact() -> Optional[Artifact]:
req = get("https://api.github.com/repos/Grasscutters/Grasscutter/actions/artifacts?per_page=1") req = await client.get("https://api.github.com/repos/Grasscutters/Grasscutter/actions/artifacts?per_page=1")
if req.status_code == 200: if req.status_code == 200:
data = req.json() data = req.json()
if data.get("artifacts", []): if data.get("artifacts", []):
@ -50,4 +65,4 @@ def get_hash() -> str:
if data.get("workflow_runs", []): if data.get("workflow_runs", []):
return data["workflow_runs"][0]["head_commit"]["id"][:6] + " " + \ return data["workflow_runs"][0]["head_commit"]["id"][:6] + " " + \
data["workflow_runs"][0]["head_commit"]["message"][:50] data["workflow_runs"][0]["head_commit"]["message"][:50]
return "" return "Unknown."

View File

@ -1,3 +1,5 @@
from os import sep
from pyrogram import Client, filters from pyrogram import Client, filters
from pyrogram.types import Message from pyrogram.types import Message
from ci import app, channel_id, sqlite, scheduler from ci import app, channel_id, sqlite, scheduler
@ -12,9 +14,12 @@ async def run_every_30_minute():
return return
if not channel_id: if not channel_id:
return return
sqlite.set("artifact_id", new_artifact.id) sqlite["artifact_id"] = new_artifact.id
await new_artifact.download() try:
await app.send_document(channel_id, "Grasscutter.zip", await new_artifact.download()
except FileNotFoundError:
return
await app.send_document(channel_id, f"data{sep}Grasscutter.zip",
caption=new_artifact.hash, caption=new_artifact.hash,
force_document=True) force_document=True)