minya_hotomoe/modules/CherryCheck.py

70 lines
3.1 KiB
Python
Raw Permalink Normal View History

from module_interface import ModuleInterface
from mipa.ext import tasks
import aiohttp
# CherryChk V1 by Rera*C
2023-10-12 14:00:37 +02:00
# Check's CherryPick's Docker image version and notify if there's a new version. (10 minutes interval)
# Version is checked with tag name, 'latest' and 'develop' tag is ignored.
class CherryChk(ModuleInterface):
def __init__(self):
super().__init__()
self.name = "CherryChk"
self.regex_pattern = None
self.last_checked_time = None
self._cherrypick_docker_image = "noridev/cherrypick"
print(
"[CherryChk] CherryPick Docker version checker module, CherryChk V1 loaded."
)
def module_ready(self):
print("[CherryChk] Module Ready.")
self.update_checker.start()
self.last_checked_time = self.manager.require("MinyaDB").get_func(
"get_module_db"
)(self.name, "last_checked_time")
2023-10-12 14:00:37 +02:00
@tasks.loop(seconds=(60.0 * 10))
async def update_checker(self):
try:
# get latest version from dockerhub
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://hub.docker.com/v2/repositories/{self._cherrypick_docker_image}/tags"
) as resp:
if resp.status == 200:
json = await resp.json()
# get all tags after last_checked_time
for tag in json["results"]:
if (
not self.last_checked_time
or tag["last_updated"] > self.last_checked_time
):
if tag["name"] != "latest" and tag["name"] != "develop":
print(
f"[CherryChk] New version found! ({tag['name']})"
)
self.last_checked_time = tag["last_updated"]
self.manager.require("MinyaDB").get_func(
"set_module_db"
)(
self.name,
"last_checked_time",
tag["last_updated"],
)
await self.manager.bot.client.note.action.send(
2024-05-14 19:06:56 +02:00
content=f"새로운 버전의 체리픽 도커 이미지가 나온 것 같아요! ({tag['name']}) @cocoa \n"
+ f"https://hub.docker.com/r/{self._cherrypick_docker_image}/tags",
visibility="home",
)
break
else:
print(
f"[CherryChk] Failed to get version from DockerHub. ({resp.status})"
)
except Exception as e:
print(f"[CherryChk] Failed to get version from DockerHub. ({e})")