Skip to content

Commit

Permalink
feat: upload trime nightly build
Browse files Browse the repository at this point in the history
  • Loading branch information
shitlime committed Feb 6, 2024
1 parent 6c1f52a commit 05e4698
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bot_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ modules:
- "modules.Bilibili.get_video_info"
- "modules.Bilibili.get_video_ai_conclusion"
- "modules.scheduler.new_year"
- "modules.scheduler.TrimeNightlyUpload.trime_nightly_upload"

modules_config:
message_queue:
Expand Down Expand Up @@ -78,4 +79,6 @@ modules_config:
send_time: "30 15 * * *" # 遵循crontab的方式 例:'30 15 * * *'表示每天的15:30
new_year:
enable_group: [1145141919810]
enable_friend: []
enable_friend: []
TrimeNightlyUpload:
enable_group: [1145141919810]
59 changes: 59 additions & 0 deletions modules/scheduler/TrimeNightlyUpload/get_trime_nightly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import re
import asyncio
import aiohttp

trime_nightly_release = "https://github.com/osfans/trime/releases/tag/nightly"

async def get_nightly_commit_hash():
"""
访问github获取信息,组装成下载链接返回
"""
commit_hash_pattern = r'data-hovercard-type="commit".+?href="/osfans/trime/commit/(.{8})'
async with aiohttp.ClientSession() as session:
async with session.get(trime_nightly_release) as r:
if r.status == 200:
html = await r.read()
html = html.decode('utf-8')
commit_hash = re.search(commit_hash_pattern, html).group(1)
return commit_hash
else:
return r.start

def generate_nightly_download_link(commit_hash: str) -> list:
url_preix = "https://github.com/osfans/trime/releases/download/nightly/"
return [f"{url_preix}com.osfans.trime-nightly-0-g{commit_hash}-arm64-v8a-release.apk",
f"{url_preix}com.osfans.trime-nightly-0-g{commit_hash}-armeabi-v7a-release.apk",
f"{url_preix}com.osfans.trime-nightly-0-g{commit_hash}-x86-release.apk",
f"{url_preix}com.osfans.trime-nightly-0-g{commit_hash}-x86_64-release.apk"]

async def get_all_file_info() -> dict:
commit_hash = await get_nightly_commit_hash()
if type(commit_hash) == str:
link = generate_nightly_download_link(commit_hash)
# 生成字典格式: {文件名: 下载链接}
d = {}
for l in link:
d[l.split("/")[-1]] = l
print(d)
return d
else:
print(f"获取trime nightly commit hash出错!状态码:{commit_hash}")

async def get_file_bytes(url: str):
"""
通过链接下载文件数据,返回bytes
"""
async with aiohttp.request("GET", url=url) as r:
return await r.read()

async def test():
commit_hash = await get_nightly_commit_hash()
if type(commit_hash) == str:
link = generate_nightly_download_link(commit_hash)
print('\n'.join(link))
data = await get_file_bytes(link[0])
print(data)

if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(test())
37 changes: 37 additions & 0 deletions modules/scheduler/TrimeNightlyUpload/trime_nightly_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import datetime

from bot_init import BOT
from graia.saya import Channel
from graia.scheduler import timers
from graia.ariadne.app import Ariadne
from graia.scheduler.saya import SchedulerSchema
from graia.ariadne.message.chain import MessageChain
from graia.ariadne.message.element import Plain

from .get_trime_nightly import get_all_file_info, get_file_bytes

channel = Channel.current()
channel.meta["name"]="同文每夜构建"
channel.meta["author"]="Shitlime"
channel.meta["description"]="""
定时上传trime nightly build
"""

config = BOT.get_modules_config("TrimeNightlyUpload")

# 配置:
enable_group = config["enable_group"] # list

@channel.use(SchedulerSchema(timer=timers.crontabify("11 0 * * *")))
async def upload_trime_nightly(app: Ariadne):
file_info = get_all_file_info()
for file_name, file_url in file_info:
data = await get_file_bytes(file_url)
path = f"同文原版(Nightly Build,每夜版){datetime.datetime.today()}"
name = file_name + ".删后缀喵"
for group_num in enable_group:
await app.upload_file(
data=data,
target=group_num,
path=path,
name=name)

0 comments on commit 05e4698

Please sign in to comment.