Skip to content

Commit

Permalink
create a whole new unknown link downloader (refactor required)
Browse files Browse the repository at this point in the history
Will refactor for sure
  • Loading branch information
kytpbs committed Aug 28, 2024
1 parent bd7bad0 commit eb13d57
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions src/download_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import asyncio
import logging
import timeit
from typing import Optional
import discord

from src.other import UnknownAlternateDownloader
from src.downloading_system import get_downloader

def _convert_paths_to_discord_files(paths: list[str]) -> list[discord.File]:
Expand Down Expand Up @@ -48,9 +52,9 @@ async def callback(interaction: discord.Interaction):
async def download_video_command(interaction: discord.Interaction, url: str, is_ephemeral: bool = False, include_title: bool | None = None):
downloader = get_downloader(url)
if downloader is None:
await interaction.response.send_message("Bu link desteklenmiyor", ephemeral=True)
logging.info("Found an unsupported link: %s", url)
return
await interaction.response.defer(ephemeral=True)
return await try_unknown_link(interaction, url, include_title)

await interaction.response.defer(ephemeral=is_ephemeral)

Expand Down Expand Up @@ -83,3 +87,63 @@ async def download_video_command(interaction: discord.Interaction, url: str, is_
caption = returned_content

await interaction.followup.send(caption, files=discord_files, ephemeral=is_ephemeral, view=view)

async def loading_animation(message: discord.WebhookMessage):
original_text = message.content
sleep_time = 0 # we don't actually need to sleep thanks to ``message.edit`` being async
while True:
await message.edit(content=original_text + ".", view=discord.ui.View())
await asyncio.sleep(sleep_time)
await message.edit(content=original_text + "..", view=discord.ui.View())
await asyncio.sleep(sleep_time)
await message.edit(content=original_text + "...", view=discord.ui.View())
await asyncio.sleep(sleep_time)

async def try_unknown_link(interaction: discord.Interaction, url: str, include_title: Optional[bool] = None):
""" edits the sent message if the download is successful, otherwise sends an error message
Args:
interaction (discord.Interaction): the interaction to edit with ``interaction.response.edit_message``
url (str): the url to download the video from
"""

downloader = UnknownAlternateDownloader
sent_message = await interaction.followup.send("Bu link resmi olarak desteklenmiyor, yine de indirmeyi deniyorum", ephemeral=True, wait=True)
loading_task = asyncio.create_task(loading_animation(sent_message))

try:
attachments = await downloader.download_video_from_link(url)
file_paths = [attachment.path for attachment in attachments]
discord_files = _convert_paths_to_discord_files(file_paths)
except Exception as e:
loading_task.cancel()
await sent_message.edit(content="Linki ne yazıkki indiremedim")
raise e # re-raise the exception so we can see what went wrong

if len(attachments) == 0:
loading_task.cancel()
await sent_message.edit(content="Videoyu Bulamadım, lütfen daha sonra tekrar deneyin ya da hatayı bildirin")
return

returned_content = " + ".join(filter(None, [attachment.caption for attachment in attachments]))
default_caption = f"Video{'s' if len(attachments) > 1 else ''} Downloaded"
caption = ""
view = discord.utils.MISSING
shortened_content = _get_shortened_caption(returned_content) + " ***...***"


if include_title is False or not returned_content:
caption = default_caption

elif include_title is True:
caption = returned_content

elif len(shortened_content) < len(returned_content):
view = _get_view(shortened_content, returned_content)
caption = shortened_content
else:
caption = returned_content

loading_task.cancel()
await sent_message.edit(content=f"{url} downloaded")
await interaction.followup.send(content=caption, files=discord_files, view=view)

0 comments on commit eb13d57

Please sign in to comment.