video-stream/program/updater.py

117 lines
3.9 KiB
Python
Raw Normal View History

"""
Video + Music Stream Telegram Bot
Copyright (c) 2022-present levina=lab <https://github.com/levina-lab>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/licenses.html>
"""
import os
2022-03-01 14:18:51 +00:00
import shlex
2022-01-31 12:41:47 +00:00
2022-03-01 14:18:51 +00:00
from typing import Tuple
from git.exc import InvalidGitRepositoryError, GitCommandError
2022-03-01 14:18:51 +00:00
from git import Repo
2022-02-11 06:43:58 +00:00
from pyrogram.types import Message
from pyrogram import Client, filters
from program import LOGS
from config import UPSTREAM_BRANCH, UPSTREAM_REPO, BOT_USERNAME
2022-01-31 12:41:47 +00:00
2022-02-11 06:43:58 +00:00
from driver.filters import command
from driver.decorators import bot_creator
from driver.database.dbqueue import get_active_chats, remove_active_chat
2022-01-31 12:41:47 +00:00
2022-02-10 09:46:08 +00:00
2022-03-01 14:16:39 +00:00
async def install_requirements(cmd: str) -> Tuple[str, str, int, int]:
args = shlex.split(cmd)
process = await asyncio.create_subprocess_exec(
*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
return (
stdout.decode("utf-8", "replace").strip(),
stderr.decode("utf-8", "replace").strip(),
process.returncode,
process.pid,
)
@Client.on_message(command(["update", f"update@{BOT_USERNAME}"]) & ~filters.edited)
@bot_creator
async def update_bot(_, message: Message):
chat_id = message.chat.id
msg = await bot.send_message(chat_id, "❖ Checking updates...")
2022-01-31 12:41:47 +00:00
try:
repo = Repo()
except GitCommandError:
await msg.edit("Git command error !")
return
2022-01-31 12:41:47 +00:00
except InvalidGitRepositoryError:
await msg.edit("❖ Checking Git updates...")
2022-02-10 21:56:59 +00:00
repo = Repo.init()
if "origin" in repo.remotes:
origin = repo.remote("origin")
else:
origin = repo.create_remote("origin", UPSTREAM_REPO)
2022-02-10 21:56:59 +00:00
origin.fetch()
repo.create_head(UPSTREAM_BRANCH, origin.refs[UPSTREAM_BRANCH])
repo.heads[UPSTREAM_BRANCH].set_tracking_branch(
origin.refs[UPSTREAM_BRANCH]
)
repo.heads[UPSTREAM_BRANCH].checkout(True)
try:
repo.create_remote("origin", UPSTREAM_REPO)
except BaseException:
pass
nrs = repo.remote("origin")
nrs.fetch(UPSTREAM_BRANCH)
try:
nrs.pull(UPSTREAM_BRANCH)
except GitCommandError:
repo.git.reset("--hard", "FETCH_HEAD")
await install_requirements(
"pip3 install --no-cache-dir -r requirements.txt"
)
await msg.edit("Done, bot has updated !\n\n> now the bot will be rebooted to apply the update and will active again in 5-10 second(s).")
return
served_chats = []
try:
chats = await get_active_chats()
for chat in chats:
served_chats.append(int(chat["chat_id"]))
except BaseException:
pass
for x in served_chats:
try:
await remove_active_chat(x)
except BaseException:
pass
2022-02-10 21:56:59 +00:00
return
os.system(f"kill -9 {os.getpid()} && python3 main.py")
2022-02-10 21:56:59 +00:00
@Client.on_message(command(["restart", f"restart@{BOT_USERNAME}"]) & ~filters.edited)
@bot_creator
2022-01-31 12:41:47 +00:00
async def restart_bot(_, message: Message):
try:
msg = await message.reply_text("❖ Restarting bot...")
LOGS.info("[INFO]: >> BOT SERVER RESTARTED <<")
except BaseException as err:
LOGS.info(f"[ERROR]: {err}")
return
await msg.edit_text("✅ Bot has restarted !\n\n» back active again in 5-10 seconds.")
os.system(f"kill -9 {os.getpid()} && python3 main.py")