From 8b39987d6992a2e9e80c581ca6db591efee7a5fc Mon Sep 17 00:00:00 2001 From: Hafitz Setya <71178188+breakdowns@users.noreply.github.com> Date: Thu, 13 May 2021 03:54:24 +0700 Subject: [PATCH] v4.7.3 - Auto set bot command - Added mediainfo command Co-authored-by: brutewooorse Co-authored-by: code-rgb --- Dockerfile | 2 +- README.md | 24 ++---------------- bot/__main__.py | 27 ++++++++++++++++++-- bot/helper/__init__.py | 43 ++++++++++++++++++++++++++++++++ bot/modules/mediainfo.py | 54 ++++++++++++++++++++++++++++++++++++++++ config_sample.env | 2 +- requirements.txt | 1 + 7 files changed, 127 insertions(+), 26 deletions(-) create mode 100644 bot/modules/mediainfo.py diff --git a/Dockerfile b/Dockerfile index 010cb7c..8c6c1ab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ RUN apt-get -qq update && \ rm -rf /var/lib/apt/lists/* && \ apt-add-repository non-free && \ apt-get -qq update && \ - apt-get -qq install -y p7zip-full p7zip-rar aria2 curl pv jq ffmpeg locales python3-lxml && \ + apt-get -qq install -y p7zip-full mediainfo p7zip-rar aria2 curl pv jq ffmpeg locales python3-lxml && \ apt-get purge -y software-properties-common COPY requirements.txt . diff --git a/README.md b/README.md index 47b449b..8645604 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This is a Telegram bot writen in Python for mirroring files on the Internet to o ## Additional Features - Mirroring Uptobox.com links to Google Drive (Uptobox account must be premium) +- Get detailed info about replied media - Nyaa.si and Sukebei Torrent search - Speedtest with picture results - Limiting Torrent size support @@ -16,7 +17,7 @@ This is a Telegram bot writen in Python for mirroring files on the Internet to o - Shell and Executor - Stickers module -## From Source Repos +## From Original Repos - Mirroring direct download links, Torrent, and Telegram files to Google Drive - Mirroring Mega.nz links to Google Drive (If your Mega account not premium, it will limit 4-5gb/day) - Copy files from someone's Drive to your Drive (Using Autorclone) @@ -156,27 +157,6 @@ sudo docker run mirrorbot ## Deploying on Heroku with heroku-cli and Goorm IDE

-## Bot commands to be set in [@BotFather](https://t.me/BotFather) - -``` -mirror - Start Mirroring -tarmirror - Upload tar (zipped) file -unzipmirror - Extract files -clone - Copy file/folder to Drive -watch - Mirror YT-DL support link -tarwatch - Mirror Youtube playlist link as tar -cancel - Cancel a task -cancelall - Cancel all tasks -del - Delete file from Drive -list - [query] Searches files in G-Drive -status - Get Mirror Status message -stats - Bot Usage Stats -help - Get Detailed Help -speedtest - Check Speed of the host -log - Bot Log [owner only] -restart - restart bot [owner only] -repo - Get the bot repo -``` ## Using Service Accounts for uploading to avoid user rate limit For Service Account to work, you must set **USE_SERVICE_ACCOUNTS=**"True" in config file or environment variables, diff --git a/bot/__main__.py b/bot/__main__.py index 6d7350c..ef7644b 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -8,7 +8,7 @@ from sys import executable from datetime import datetime import pytz import time -from telegram import ParseMode, InlineKeyboardMarkup, InlineKeyboardButton +from telegram import ParseMode, BotCommand, InlineKeyboardMarkup, InlineKeyboardButton from telegram.ext import CommandHandler, run_async from bot import dispatcher, updater, botStartTime, AUTHORIZED_CHATS, IMAGE_URL from bot.helper.ext_utils import fs_utils @@ -16,7 +16,7 @@ from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.message_utils import * from .helper.ext_utils.bot_utils import get_readable_file_size, get_readable_time from .helper.telegram_helper.filters import CustomFilters -from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clone, watch, shell, eval, anime, stickers, search, delete, speedtest, usage +from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clone, watch, shell, eval, anime, stickers, search, delete, speedtest, usage, mediainfo now=datetime.now(pytz.timezone('Asia/Jakarta')) @@ -133,6 +133,8 @@ def bot_help(update, context): /{BotCommands.RepoCommand}: Get the bot repo. +/mediainfo: Get detailed info about replied media. + /tshelp: Get help for Torrent search module. /weebhelp: Get help for Anime, Manga, and Character module. @@ -142,6 +144,26 @@ def bot_help(update, context): sendMessage(help_string, context.bot, update) +botcmds = [ +BotCommand(f'{BotCommands.MirrorCommand}', 'Start Mirroring'), +BotCommand(f'{BotCommands.TarMirrorCommand}','Upload tar (zipped) file'), +BotCommand(f'{BotCommands.UnzipMirrorCommand}','Extract files'), +BotCommand(f'{BotCommands.CloneCommand}','Copy file/folder to Drive'), +BotCommand(f'{BotCommands.WatchCommand}','Mirror YT-DL support link'), +BotCommand(f'{BotCommands.TarWatchCommand}','Mirror Youtube playlist link as tar'), +BotCommand(f'{BotCommands.CancelMirror}','Cancel a task'), +BotCommand(f'{BotCommands.CancelAllCommand}','Cancel all tasks'), +BotCommand(f'{BotCommands.DeleteCommand}','Delete file from Drive'), +BotCommand(f'{BotCommands.ListCommand}',' [query] Searches files in G-Drive'), +BotCommand(f'{BotCommands.StatusCommand}','Get Mirror Status message'), +BotCommand(f'{BotCommands.StatsCommand}','Bot Usage Stats'), +BotCommand(f'{BotCommands.HelpCommand}','Get Detailed Help'), +BotCommand(f'{BotCommands.SpeedCommand}','Check Speed of the host'), +BotCommand(f'{BotCommands.LogCommand}','Bot Log [owner only]'), +BotCommand(f'{BotCommands.RestartCommand}','Restart bot [owner only]'), +BotCommand(f'{BotCommands.RepoCommand}','Get the bot repo')] + + def main(): fs_utils.start_cleanup() # Check if the bot is restarting @@ -151,6 +173,7 @@ def main(): restart_message.edit_text("Restarted Successfully!") LOGGER.info('Restarted Successfully!') remove('restart.pickle') + bot.set_my_commands(botcmds) start_handler = CommandHandler(BotCommands.StartCommand, start, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) diff --git a/bot/helper/__init__.py b/bot/helper/__init__.py index e69de29..ceafed0 100644 --- a/bot/helper/__init__.py +++ b/bot/helper/__init__.py @@ -0,0 +1,43 @@ +import asyncio +import os +import shlex +from typing import Tuple +from html_telegraph_poster import TelegraphPoster + +def post_to_telegraph(a_title: str, content: str) -> str: + """ Create a Telegram Post using HTML Content """ + post_client = TelegraphPoster(use_api=True) + auth_name = "slam-mirrorbot" + post_client.create_api_token(auth_name) + post_page = post_client.post( + title=a_title, + author=auth_name, + author_url="https://github.com/breakdowns/slam-mirrorbot", + text=content, + ) + return post_page["url"] + + +async def runcmd(cmd: str) -> Tuple[str, str, int, int]: + """ run command in terminal """ + 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, + ) + + +# Solves ValueError: No closing quotation by removing ' or " in file name +def safe_filename(path_): + if path_ is None: + return + safename = path_.replace("'", "").replace('"', "") + if safename != path_: + os.rename(path_, safename) + return safename diff --git a/bot/modules/mediainfo.py b/bot/modules/mediainfo.py new file mode 100644 index 0000000..b8946f4 --- /dev/null +++ b/bot/modules/mediainfo.py @@ -0,0 +1,54 @@ +# Suggested by - @d0n0t (https://github.com/code-rgb/USERGE-X/issues/9) +# Copyright (C) 2020 BY - GitHub.com/code-rgb [TG - @deleteduser420] +# All rights reserved. + +import os +from pyrogram import filters +from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup +from bot import app +from bot.helper import post_to_telegraph, runcmd, safe_filename + +@app.on_message(filters.command(['mediainfo'])) +async def mediainfo(client, message): + reply = message.reply_to_message + if not reply: + await message.reply_text("Reply to Media first") + return + process = await message.reply_text("`Processing...`") + x_media = None + available_media = ( + "audio", + "document", + "photo", + "sticker", + "animation", + "video", + "voice", + "video_note", + "new_chat_photo", + ) + for kind in available_media: + x_media = getattr(reply, kind, None) + if x_media is not None: + break + if x_media is None: + await process.edit_text("Reply To a Valid Media Format") + return + media_type = str(type(x_media)).split("'")[1] + file_path = safe_filename(await reply.download()) + output_ = await runcmd(f'mediainfo "{file_path}"') + out = None + if len(output_) != 0: + out = output_[0] + body_text = f""" +

JSON

+
{x_media}
+
+ +

DETAILS

+
{out or 'Not Supported'}
+""" + text_ = media_type.split(".")[-1].upper() + link = post_to_telegraph(media_type, body_text) + markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=text_, url=link)]]) + await process.edit_text("ℹ️ MEDIA INFO", reply_markup=markup) diff --git a/config_sample.env b/config_sample.env index cbc1257..6c0d44e 100644 --- a/config_sample.env +++ b/config_sample.env @@ -28,7 +28,7 @@ IMAGE_URL = "https://telegra.ph/file/db03910496f06094f1f7a.jpg" # Change ENABLE_FILESIZE_LIMIT = True if you want to use MAX_TORRENT_SIZE ENABLE_FILESIZE_LIMIT = false MAX_TORRENT_SIZE = 15 -# Fill only if you deploying on heroku-cli +# Fill only if you deploying with heroku-cli and Goorm IDE HEROKU_API_KEY = "" HEROKU_APP_NAME = "" # Add more buttons (two buttons are already added of file link and index link, you can add extra buttons too, these are optional) diff --git a/requirements.txt b/requirements.txt index f105d2d..149f542 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,3 +24,4 @@ js2py lxml telegraph pytz +html-telegraph-poster>=0.2.31