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
config.ini
*.session
*.session*
data/

8
ci.py
View File

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

View File

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

View File

@ -1,9 +1,21 @@
import datetime
from configparser import RawConfigParser
from os.path import exists
from os import remove
from os import remove, sep
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:
@ -25,17 +37,20 @@ class Artifact:
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 download(self, path: str = f"data{sep}Grasscutter.zip"):
if exists(path):
remove(path)
content = await client.get(self.url)
if content.status_code == 404:
raise FileNotFoundError
content = content.content
with open(path, 'wb') as f:
f.write(content)
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:
data = req.json()
if data.get("artifacts", []):
@ -50,4 +65,4 @@ def get_hash() -> str:
if data.get("workflow_runs", []):
return data["workflow_runs"][0]["head_commit"]["id"][:6] + " " + \
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.types import Message
from ci import app, channel_id, sqlite, scheduler
@ -12,9 +14,12 @@ async def run_every_30_minute():
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",
sqlite["artifact_id"] = new_artifact.id
try:
await new_artifact.download()
except FileNotFoundError:
return
await app.send_document(channel_id, f"data{sep}Grasscutter.zip",
caption=new_artifact.hash,
force_document=True)