diff options
Diffstat (limited to 'cogs/web.py')
| -rw-r--r-- | cogs/web.py | 89 |
1 files changed, 57 insertions, 32 deletions
diff --git a/cogs/web.py b/cogs/web.py index a9f8fbb..fcec8f9 100644 --- a/cogs/web.py +++ b/cogs/web.py | |||
| @@ -18,6 +18,49 @@ class Web(commands.Cog): | |||
| 18 | def __init__(self, bot): | 18 | def __init__(self, bot): |
| 19 | self.bot = bot | 19 | self.bot = bot |
| 20 | 20 | ||
| 21 | def _extract_youtube_title(self, url): | ||
| 22 | """Extract YouTube video title if URL is a YouTube link.""" | ||
| 23 | if not url.startswith('https://www.youtube.com/'): | ||
| 24 | return None | ||
| 25 | |||
| 26 | logger.info("youtube link") | ||
| 27 | try: | ||
| 28 | parsed_url = urllib.parse.urlparse(url) | ||
| 29 | video_id = urllib.parse.parse_qs(parsed_url.query).get('v', [None])[0] | ||
| 30 | if video_id: | ||
| 31 | yt = pytube.YouTube(f'https://www.youtube.com/watch?v={video_id}') | ||
| 32 | return yt.title | ||
| 33 | except Exception: | ||
| 34 | pass | ||
| 35 | return None | ||
| 36 | |||
| 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): | ||
| 48 | """Process search results and create embed with buttons.""" | ||
| 49 | embed = Embed(title="Search Results", description=f"Top results for: {query}", color=0x1E90FF) | ||
| 50 | view = discord.ui.View(disable_on_timeout=True) | ||
| 51 | |||
| 52 | for i, link in enumerate(links[:5], start=1): | ||
| 53 | title = link.text | ||
| 54 | youtube_title = self._extract_youtube_title(link.get('href')) | ||
| 55 | if youtube_title: | ||
| 56 | title = youtube_title | ||
| 57 | |||
| 58 | 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 | |||
| 62 | return embed, view | ||
| 63 | |||
| 21 | @commands.slash_command( | 64 | @commands.slash_command( |
| 22 | name="web", | 65 | name="web", |
| 23 | description="Search the web using DuckDuckGo" | 66 | description="Search the web using DuckDuckGo" |
| @@ -35,40 +78,22 @@ class Web(commands.Cog): | |||
| 35 | 78 | ||
| 36 | logger.info(f"Query: {query}") | 79 | logger.info(f"Query: {query}") |
| 37 | 80 | ||
| 38 | if response.status_code == 200: | 81 | if response.status_code != 200: |
| 39 | soup = BeautifulSoup(response.text, 'html.parser') | ||
| 40 | links = soup.find_all('a', class_='result__a') | ||
| 41 | if links: | ||
| 42 | embed = Embed(title="Search Results", description=f"Top results for: {query}", color=0x1E90FF) | ||
| 43 | view = discord.ui.View(disable_on_timeout=True) | ||
| 44 | for i, link in enumerate(links[:5], start=1): | ||
| 45 | title = link.text | ||
| 46 | if link.get('href').startswith('https://www.youtube.com/'): | ||
| 47 | logger.info("youtube link") | ||
| 48 | try: | ||
| 49 | parsed_url = urllib.parse.urlparse(link.get('href')) | ||
| 50 | video_id = urllib.parse.parse_qs(parsed_url.query).get('v', [None])[0] | ||
| 51 | if video_id: | ||
| 52 | yt = pytube.YouTube(f'https://www.youtube.com/watch?v={video_id}') | ||
| 53 | title = yt.title | ||
| 54 | except Exception: | ||
| 55 | pass | ||
| 56 | |||
| 57 | embed.add_field(name=f"Result {i}", value=f"[{title}]({link.get('href')})", inline=False) | ||
| 58 | button = discord.ui.Button(label=f"Send Result {i}") | ||
| 59 | |||
| 60 | async def button_callback(interaction: discord.Interaction, link=link): | ||
| 61 | await interaction.response.send_message(link.get('href')) | ||
| 62 | button.callback = button_callback | ||
| 63 | view.add_item(button) | ||
| 64 | await ctx.respond(embed=embed, view=view, ephemeral=True) | ||
| 65 | logger.info("Search results sent") | ||
| 66 | else: | ||
| 67 | await ctx.respond(content="No results found.", ephemeral=True) | ||
| 68 | logger.warning("No results found") | ||
| 69 | else: | ||
| 70 | await ctx.respond(content=f"Error: {response.status_code}", ephemeral=True) | 82 | await ctx.respond(content=f"Error: {response.status_code}", ephemeral=True) |
| 71 | logger.error(f"Error: {response.status_code}") | 83 | logger.error(f"Error: {response.status_code}") |
| 84 | return | ||
| 85 | |||
| 86 | soup = BeautifulSoup(response.text, 'html.parser') | ||
| 87 | links = soup.find_all('a', class_='result__a') | ||
| 88 | |||
| 89 | if not links: | ||
| 90 | await ctx.respond(content="No results found.", ephemeral=True) | ||
| 91 | logger.warning("No results found") | ||
| 92 | return | ||
| 93 | |||
| 94 | embed, view = self._process_search_results(links, query) | ||
| 95 | await ctx.respond(embed=embed, view=view, ephemeral=True) | ||
| 96 | logger.info("Search results sent") | ||
| 72 | 97 | ||
| 73 | def setup(bot): | 98 | def setup(bot): |
| 74 | bot.add_cog(Web(bot)) | 99 | bot.add_cog(Web(bot)) |
