diff options
Diffstat (limited to 'cogs/web.py')
| -rw-r--r-- | cogs/web.py | 20 |
1 files changed, 5 insertions, 15 deletions
diff --git a/cogs/web.py b/cogs/web.py index b072e99..fd22f9f 100644 --- a/cogs/web.py +++ b/cogs/web.py | |||
| @@ -11,6 +11,7 @@ from bs4 import BeautifulSoup | |||
| 11 | import urllib.parse | 11 | import urllib.parse |
| 12 | import yt_dlp | 12 | import yt_dlp |
| 13 | import logging | 13 | import logging |
| 14 | from cogs.utils.url_helper import clean_url_for_ytdlp | ||
| 14 | 15 | ||
| 15 | logger = logging.getLogger(__name__) | 16 | logger = logging.getLogger(__name__) |
| 16 | 17 | ||
| @@ -24,6 +25,7 @@ class Web(commands.Cog): | |||
| 24 | if "youtube.com/watch" not in url: | 25 | if "youtube.com/watch" not in url: |
| 25 | return None | 26 | return None |
| 26 | 27 | ||
| 28 | url = clean_url_for_ytdlp(url) | ||
| 27 | logger.info("youtube link") | 29 | logger.info("youtube link") |
| 28 | try: | 30 | try: |
| 29 | ydl_opts = {"quiet": True, "nowarnings": True} | 31 | ydl_opts = {"quiet": True, "nowarnings": True} |
| @@ -34,18 +36,8 @@ class Web(commands.Cog): | |||
| 34 | pass | 36 | pass |
| 35 | return None | 37 | return None |
| 36 | 38 | ||
| 37 | def _create_button(self, index, link): | ||
| 38 | """Create a button for sending search result.""" | ||
| 39 | button = discord.ui.Button(label=f"Send Result {index}") | ||
| 40 | |||
| 41 | async def button_callback(interaction: discord.Interaction): | ||
| 42 | await interaction.response.send_message(link.get("href")) | ||
| 43 | |||
| 44 | button.callback = button_callback | ||
| 45 | return button | ||
| 46 | |||
| 47 | def _process_search_results(self, links, query): | 39 | def _process_search_results(self, links, query): |
| 48 | """Process search results and create embed with buttons.""" | 40 | """Process search results and create embed.""" |
| 49 | embed = Embed(title="Search Results", description=f"Top results for: {query}", color=0x1E90FF) | 41 | embed = Embed(title="Search Results", description=f"Top results for: {query}", color=0x1E90FF) |
| 50 | view = discord.ui.View(disable_on_timeout=True) | 42 | view = discord.ui.View(disable_on_timeout=True) |
| 51 | 43 | ||
| @@ -56,8 +48,6 @@ class Web(commands.Cog): | |||
| 56 | title = youtube_title | 48 | title = youtube_title |
| 57 | 49 | ||
| 58 | embed.add_field(name=f"Result {i}", value=f"[{title}]({link.get('href')})", inline=False) | 50 | embed.add_field(name=f"Result {i}", value=f"[{title}]({link.get('href')})", inline=False) |
| 59 | button = self._create_button(i, link) | ||
| 60 | view.add_item(button) | ||
| 61 | 51 | ||
| 62 | return embed, view | 52 | return embed, view |
| 63 | 53 | ||
| @@ -70,7 +60,7 @@ class Web(commands.Cog): | |||
| 70 | ctx: discord.ApplicationContext, | 60 | ctx: discord.ApplicationContext, |
| 71 | query: str | 61 | query: str |
| 72 | ): | 62 | ): |
| 73 | await ctx.defer(ephemeral=True) | 63 | await ctx.defer() |
| 74 | headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"} | 64 | headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"} |
| 75 | encoded_query = urllib.parse.quote_plus(query) | 65 | encoded_query = urllib.parse.quote_plus(query) |
| 76 | url = f"https://duckduckgo.com/html/?q={encoded_query}" | 66 | url = f"https://duckduckgo.com/html/?q={encoded_query}" |
| @@ -92,7 +82,7 @@ class Web(commands.Cog): | |||
| 92 | return | 82 | return |
| 93 | 83 | ||
| 94 | embed, view = self._process_search_results(links, query) | 84 | embed, view = self._process_search_results(links, query) |
| 95 | await ctx.respond(embed=embed, view=view, ephemeral=True) | 85 | await ctx.respond(embed=embed, view=view) |
| 96 | logger.info("Search results sent") | 86 | logger.info("Search results sent") |
| 97 | 87 | ||
| 98 | 88 | ||
