Add update feature

This commit is contained in:
iwumingz 2022-04-11 10:05:12 +08:00
parent 9fb6b582f0
commit 159b029b10
7 changed files with 70 additions and 18 deletions

View File

@ -4,7 +4,7 @@
@File : custom.py
@Time : 2022/04/02 10:17:03
@Author : Viperorz
@Version : 1.0.5
@Version : v1.1.7
@License : (C)Copyright 2021-2022
@Desc : None
"""

View File

@ -134,7 +134,7 @@ restart:
update:
cmd: update
format: -update
format: -update <无|force>
usage: 更新配置文件和sycgram到主分支的最新版本
prefix:
@ -159,5 +159,5 @@ download:
ip:
cmd: ip
format: -ip <IP地址|域名>
format: -ip <IP地址|域名|me>
usage: 查询IP地址或域名的信息

View File

@ -5,7 +5,7 @@ CONTAINER_NAME="sycgram"
GITHUB_IMAGE_NAME="iwumingz/${CONTAINER_NAME}"
GITHUB_IMAGE_PATH="ghcr.io/${GITHUB_IMAGE_NAME}"
PROJECT_PATH="/opt/${CONTAINER_NAME}"
PROJECT_VERSION="v1.1.6"
PROJECT_VERSION="v1.1.7"
red='\033[0;31m'
green='\033[0;32m'

View File

@ -1,8 +1,8 @@
from core import command
from pyrogram import Client
from pyrogram.types import Message
from tools.sessions import session
from tools.helpers import Parameters, show_cmd_tip, show_exception
from tools.sessions import session
@Client.on_message(command('ip'))
@ -10,8 +10,9 @@ async def ip(_: Client, msg: Message):
"""查询ip信息"""
cmd, address = Parameters.get(msg)
if not address:
await show_cmd_tip(msg, cmd)
return
return await show_cmd_tip(msg, cmd)
elif address == "me":
address = ''
async def get_api(api: str) -> str:
async with session.get(api) as resp:

View File

@ -9,9 +9,9 @@ from pyrogram.types import Message
from tools.constants import (SYCGRAM, SYCGRAM_ERROR, SYCGRAM_INFO,
SYCGRAM_WARNING, UPDATE_CMD)
from tools.helpers import Parameters, show_cmd_tip, show_exception
from tools.updates import (get_alias_of_cmds, pull_and_update_command_yml,
reset_cmd_alias, update_cmd_alias,
update_cmd_prefix)
from tools.updates import (get_alias_of_cmds, is_latest_version,
pull_and_update_command_yml, reset_cmd_alias,
update_cmd_alias, update_cmd_prefix)
@Client.on_message(command("restart"))
@ -25,7 +25,23 @@ async def restart(_: Client, msg: Message):
@Client.on_message(command("update"))
async def update(_: Client, msg: Message):
"""更新sycgram到主分支的最新版本"""
text = f"**{SYCGRAM_INFO}**\n> # `It's updating {SYCGRAM} ...`"
# arg - 是否强制更新
_, arg = Parameters.get(msg)
arg = False if arg != "force" else True
version_info = f"**{SYCGRAM_INFO}**\n> # `The current version is the latest.`"
if not arg:
try:
res = await is_latest_version()
except Exception as e:
return await show_exception(msg, e)
if res:
return await msg.edit_text(version_info, parse_mode='md')
else:
text = f"**{SYCGRAM_INFO}**\n> # `Updating to the latest version.`"
else:
text = f"**{SYCGRAM_INFO}**\n> # `Forcing to update to the latest version.`"
await msg.edit_text(text, parse_mode='md')
try:
await pull_and_update_command_yml()
@ -36,7 +52,7 @@ async def update(_: Client, msg: Message):
except Exception as e:
text = f"**{SYCGRAM_ERROR}**\n> # `{e}`"
else:
text = f"**{SYCGRAM_INFO}**\n> # `{SYCGRAM.title()} is already the latest version.`"
text = version_info
finally:
await msg.edit_text(text, parse_mode='md')
@ -66,7 +82,7 @@ async def prefix(_: Client, msg: Message):
logger.error(e)
await msg.edit_text(text, parse_mode='md')
else:
text = f"**{SYCGRAM_INFO}**\n> # `Restarting {SYCGRAM} prefix of all commands.`"
text = f"**{SYCGRAM_INFO}**\n> # `Restarting prefix of all commands.`"
await msg.edit_text(text, parse_mode='md')
sys.exit()
@ -101,7 +117,7 @@ async def alias(_: Client, msg: Message):
logger.error(e)
await msg.edit_text(text, parse_mode='md')
else:
text = f"**{SYCGRAM_INFO}**\n> # `Reset alias of <{source}> ...`"
text = f"**{SYCGRAM_INFO}**\n> # `Resetting alias of <{source}> ...`"
await msg.edit_text(text, parse_mode='md')
sys.exit()

View File

@ -8,8 +8,7 @@ from typing import Any, Dict, Optional, Tuple
from loguru import logger
from tools.constants import INSTALL_SPEEDTEST, SPEEDTEST_PATH_FILE, SYCGRAM_ERROR
from .constants import INSTALL_SPEEDTEST, SPEEDTEST_PATH_FILE, SYCGRAM_ERROR
from .helpers import basher
@ -53,7 +52,7 @@ class Speedtester:
f"Sponsor: {self.get_sponsor()}\n" \
f"Upload: {self.get_speed('upload')}\n" \
f"Download: {self.get_speed('download')}\n" \
f"jitter: {self.get_ping('jitter')}\n" \
f"Jitter: {self.get_ping('jitter')}\n" \
f"Latency: {self.get_ping('latency')}\n" \
f"Time: {self.get_time()}"
return text, f"{self.__output.get('result').get('url')}.png"

View File

@ -1,9 +1,12 @@
import json
import re
from typing import Any, Dict
import yaml
from loguru import logger
from .constants import CMD_YML_REMOTE, COMMAND_YML
from .constants import CMD_YML_REMOTE, COMMAND_YML, SYCGRAM
from .helpers import basher
from .sessions import session
@ -89,3 +92,36 @@ async def pull_and_update_command_yml(is_update: bool = True) -> None:
# 合并到本地,以本地为主
update_cmd_yml(data)
resp.raise_for_status()
async def get_remote_version() -> str:
"""获取远程仓库版本"""
api = "https://api.github.com/repos/iwumingz/sycgram/tags"
async with session.get(api, timeout=9.9) as resp:
if resp.status == 200:
res = await resp.json()
return res[0].get('name')
resp.raise_for_status()
async def get_local_version() -> str:
"""获取本地仓库版本"""
cmd = f"docker inspect ghcr.io/iwumingz/{SYCGRAM}:latest -f '{{json .Config.Labels}}'"
res = await basher(cmd, timeout=10)
if not res.get('error'):
try:
data = json.loads(res.get('output'))
except Exception as e:
raise e
else:
return data.get('org.opencontainers.image.version')
raise ValueError(res.get('error'))
async def is_latest_version() -> bool:
"""检测是否为最新版本镜像"""
remote_v = await get_remote_version()
local_v = await get_local_version()
logger.info(f"Remote image version is {remote_v}")
logger.info(f"Local image version is {local_v}")
return remote_v == local_v