Skip to content

Commit

Permalink
fix:pylin:autopep8:flake
Browse files Browse the repository at this point in the history
  • Loading branch information
kalanakt committed Jan 27, 2024
1 parent c766c3e commit 565b8db
Show file tree
Hide file tree
Showing 18 changed files with 901 additions and 198 deletions.
571 changes: 571 additions & 0 deletions .pylintrc

Large diffs are not rendered by default.

Empty file added Uploader/__init__.py
Empty file.
108 changes: 45 additions & 63 deletions Uploader/button.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,58 @@
"""module for youtube download"""

import logging
import os
import json
import time
import shutil
import asyncio
from datetime import datetime

from Uploader.functions.display_progress import humanbytes, progress_for_pyrogram
from Uploader.functions.ran_text import random_char
from Uploader.script import Translation
from Uploader.utitles import Mdata01, Mdata02, Mdata03
from config import Config

# Set up logging configuration
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
logging.getLogger("pyrogram").setLevel(logging.WARNING)


logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


async def youtube_dl_call_back(_bot, update):
# Constants
AD_STRING_TO_REPLACE = "please report this issue on https://github.com/kalanakt/All-Url-Uploader/issues"

cb_data = update.data
# youtube_dl extractors
tg_send_type, youtube_dl_format, youtube_dl_ext, ranom = cb_data.split("|")
print(cb_data)
random1 = random_char(5)
save_ytdl_json_path = (
Config.DOWNLOAD_LOCATION + "/" +
str(update.from_user.id) + f"{ranom}" + ".json"
Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + f"{ranom}" + ".json"
)

try:
with open(save_ytdl_json_path, "r", encoding="utf8") as f:
response_json = json.load(f)
except FileNotFoundError as e:
await update.message.edit(f"error: {e}")
await update.message.edit(f"Error: {e}")
await update.message.delete()
return False

youtube_dl_url = update.message.reply_to_message.text
custom_file_name = (
str(response_json.get("title")) + "_" +
youtube_dl_format + "." + youtube_dl_ext
str(response_json.get("title")) + "_" + youtube_dl_format + "." + youtube_dl_ext
)
youtube_dl_username = None
youtube_dl_password = None

if "|" in youtube_dl_url:
url_parts = youtube_dl_url.split("|")
if len(url_parts) == 2:
youtube_dl_url = url_parts[0]
custom_file_name = url_parts[1]
youtube_dl_url, custom_file_name = map(str.strip, url_parts)
elif len(url_parts) == 4:
youtube_dl_url = url_parts[0]
custom_file_name = url_parts[1]
youtube_dl_username = url_parts[2]
youtube_dl_password = url_parts[3]
youtube_dl_url, custom_file_name, youtube_dl_username, youtube_dl_password = map(str.strip, url_parts)
else:
for entity in update.message.reply_to_message.entities:
if entity.type == "text_link":
Expand All @@ -68,17 +61,10 @@ async def youtube_dl_call_back(_bot, update):
o = entity.offset
length = entity.length
youtube_dl_url = youtube_dl_url[o: o + length]
if youtube_dl_url is not None:
youtube_dl_url = youtube_dl_url.strip()
if custom_file_name is not None:
custom_file_name = custom_file_name.strip()
# https://stackoverflow.com/a/761825/4723940
if youtube_dl_username is not None:
youtube_dl_username = youtube_dl_username.strip()
if youtube_dl_password is not None:
youtube_dl_password = youtube_dl_password.strip()
logger.info(youtube_dl_url)
logger.info(custom_file_name)

# Cleaning up inputs
youtube_dl_url, custom_file_name, youtube_dl_username, youtube_dl_password = map(str.strip, [youtube_dl_url, custom_file_name, youtube_dl_username, youtube_dl_password])

else:
for entity in update.message.reply_to_message.entities:
if entity.type == "text_link":
Expand All @@ -87,22 +73,26 @@ async def youtube_dl_call_back(_bot, update):
o = entity.offset
length = entity.length
youtube_dl_url = youtube_dl_url[o: o + length]

await update.message.edit_caption(
caption=Translation.DOWNLOAD_START.format(custom_file_name)
)
description = Translation.CUSTOM_CAPTION_UL_FILE

if "fulltitle" in response_json:
description = response_json["fulltitle"][:1021]
# escape Markdown and special characters

tmp_directory_for_each_user = (
Config.DOWNLOAD_LOCATION + "/" +
str(update.from_user.id) + f"{random1}"
Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + f"{random1}"
)

if not os.path.isdir(tmp_directory_for_each_user):
os.makedirs(tmp_directory_for_each_user)

download_directory = f"{tmp_directory_for_each_user}/{custom_file_name}"

command_to_exec = []

if tg_send_type == "audio":
command_to_exec = [
"yt-dlp",
Expand All @@ -120,7 +110,6 @@ async def youtube_dl_call_back(_bot, update):
download_directory,
]
else:
# command_to_exec = ["yt-dlp", "-f", youtube_dl_format, "--hls-prefer-ffmpeg", "--recode-video", "mp4", "-k", youtube_dl_url, "-o", download_directory]
minus_f_format = youtube_dl_format
if "youtu" in youtube_dl_url:
minus_f_format = f"{youtube_dl_format}+bestaudio"
Expand All @@ -139,33 +128,34 @@ async def youtube_dl_call_back(_bot, update):
]

if Config.HTTP_PROXY != "":
command_to_exec.append("--proxy")
command_to_exec.append(Config.HTTP_PROXY)
command_to_exec.extend(["--proxy", Config.HTTP_PROXY])

if youtube_dl_username is not None:
command_to_exec.append("--username")
command_to_exec.append(youtube_dl_username)
command_to_exec.extend(["--username", youtube_dl_username])

if youtube_dl_password is not None:
command_to_exec.append("--password")
command_to_exec.append(youtube_dl_password)
command_to_exec.append("--no-warnings")
# command_to_exec.append("--quiet")
command_to_exec.extend(["--password", youtube_dl_password])

command_to_exec.extend(["--no-warnings"])

logger.info(command_to_exec)
start = datetime.now()

process = await asyncio.create_subprocess_exec(
*command_to_exec,
# stdout must a pipe to be accessible as process.stdout
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
# Wait for the subprocess to finish

stdout, stderr = await process.communicate()
e_response = stderr.decode().strip()
t_response = stdout.decode().strip()

logger.info(e_response)
logger.info(t_response)
ad_string_to_replace = "please report this issue on https://github.com/kalanakt/All-Url-Uploader/issues"
if e_response and ad_string_to_replace in e_response:
error_message = e_response.replace(ad_string_to_replace, "")

if e_response and AD_STRING_TO_REPLACE in e_response:
error_message = e_response.replace(AD_STRING_TO_REPLACE, "")
await update.message.edit_caption(text=error_message)
return False

Expand All @@ -179,17 +169,15 @@ async def youtube_dl_call_back(_bot, update):
end_one = datetime.now()
time_taken_for_download = (end_one - start).seconds
file_size = Config.TG_MAX_FILE_SIZE + 1

try:
file_size = os.stat(download_directory).st_size
except FileNotFoundError:
download_directory = os.path.splitext(
download_directory)[0] + "." + "mkv"
# https://stackoverflow.com/a/678242/4723940
download_directory = os.path.splitext(download_directory)[0] + "." + "mkv"
file_size = os.stat(download_directory).st_size

download_location = f"{Config.DOWNLOAD_LOCATION}/{update.from_user.id}.jpg"
thumb = download_location if os.path.isfile(
download_location) else None
thumb = download_location if os.path.isfile(download_location) else None

if file_size > Config.TG_MAX_FILE_SIZE:
await update.message.edit_caption(
Expand All @@ -201,19 +189,19 @@ async def youtube_dl_call_back(_bot, update):
await update.message.edit_caption(
caption=Translation.UPLOAD_START.format(custom_file_name)
)

start_time = time.time()

if tg_send_type == "video":
width, height, duration = await Mdata01(download_directory)
await update.message.reply_video(
# chat_id=update.message.chat.id,
video=download_directory,
caption=description,
duration=duration,
width=width,
height=height,
supports_streaming=True,
thumb=thumb,
# reply_to_message_id=update.id,
progress=progress_for_pyrogram,
progress_args=(
Translation.UPLOAD_START,
Expand All @@ -224,12 +212,10 @@ async def youtube_dl_call_back(_bot, update):
elif tg_send_type == "audio":
duration = await Mdata03(download_directory)
await update.message.reply_audio(
# chat_id=update.message.chat.id,
audio=download_directory,
caption=description,
duration=duration,
thumb=thumb,
# reply_to_message_id=update.id,
progress=progress_for_pyrogram,
progress_args=(
Translation.UPLOAD_START,
Expand All @@ -240,12 +226,10 @@ async def youtube_dl_call_back(_bot, update):
elif tg_send_type == "vm":
width, duration = await Mdata02(download_directory)
await update.message.reply_video_note(
# chat_id=update.message.chat.id,
video_note=download_directory,
duration=duration,
length=width,
thumb=thumb,
# reply_to_message_id=update.id,
progress=progress_for_pyrogram,
progress_args=(
Translation.UPLOAD_START,
Expand All @@ -255,11 +239,8 @@ async def youtube_dl_call_back(_bot, update):
)
else:
await update.message.reply_document(
# chat_id=update.message.chat.id,
document=download_directory,
caption=description,
# parse_mode=enums.ParseMode.HTML,
# reply_to_message_id=update.id,
thumb=thumb,
progress=progress_for_pyrogram,
progress_args=(
Expand All @@ -273,11 +254,12 @@ async def youtube_dl_call_back(_bot, update):
time_taken_for_upload = (end_two - end_one).seconds

shutil.rmtree(tmp_directory_for_each_user)

await update.message.edit_caption(
caption=Translation.AFTER_SUCCESSFUL_UPLOAD_MSG_WITH_TS.format(
time_taken_for_download, time_taken_for_upload
)
)

logger.info("Downloaded in: : %s", str(time_taken_for_download))
logger.info("Downloaded in: %s", str(time_taken_for_download))
logger.info("Uploaded in: %s", str(time_taken_for_upload))
8 changes: 5 additions & 3 deletions Uploader/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Module for handling Pyrogram callback queries"""

import logging
from pyrogram import Client
from Uploader.dl_button import ddl_call_back
from Uploader.button import youtube_dl_call_back
from Uploader.script import Translation
from pyrogram import Client
import logging

# Set up logging configuration
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
Expand Down Expand Up @@ -36,6 +39,5 @@ async def button(bot, update):
await youtube_dl_call_back(bot, update)
elif "=" in update.data:
await ddl_call_back(bot, update)

else:
await update.message.delete()
6 changes: 3 additions & 3 deletions Uploader/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Client.on_message(
filters.command("start") & filters.private,
)
async def start_bot(_, m: Message):
async def start_bot(_bot, m: Message):
return await m.reply_text(
Translation.START_TEXT.format(m.from_user.first_name),
reply_markup=Translation.START_BUTTONS,
Expand All @@ -18,7 +18,7 @@ async def start_bot(_, m: Message):
@Client.on_message(
filters.command("help") & filters.private,
)
async def help_bot(_, m: Message):
async def help_bot(_bot, m: Message):
return await m.reply_text(
Translation.HELP_TEXT,
reply_markup=Translation.HELP_BUTTONS,
Expand All @@ -29,7 +29,7 @@ async def help_bot(_, m: Message):
@Client.on_message(
filters.command("about") & filters.private,
)
async def aboutme(_, m: Message):
async def aboutme(_bot, m: Message):
return await m.reply_text(
Translation.ABOUT_TEXT,
reply_markup=Translation.ABOUT_BUTTONS,
Expand Down
Loading

0 comments on commit 565b8db

Please sign in to comment.