diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/web.py | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/cogs/web.py b/cogs/web.py index fcec8f9..e003399 100644 --- a/cogs/web.py +++ b/cogs/web.py | |||
| @@ -9,27 +9,27 @@ from discord.ext import commands | |||
| 9 | import requests | 9 | import requests |
| 10 | from bs4 import BeautifulSoup | 10 | from bs4 import BeautifulSoup |
| 11 | import urllib.parse | 11 | import urllib.parse |
| 12 | import pytube | 12 | import yt_dlp |
| 13 | import logging | 13 | import logging |
| 14 | 14 | ||
| 15 | logger = logging.getLogger(__name__) | 15 | logger = logging.getLogger(__name__) |
| 16 | 16 | ||
| 17 | |||
| 17 | class Web(commands.Cog): | 18 | class Web(commands.Cog): |
| 18 | def __init__(self, bot): | 19 | def __init__(self, bot): |
| 19 | self.bot = bot | 20 | self.bot = bot |
| 20 | 21 | ||
| 21 | def _extract_youtube_title(self, url): | 22 | def _extract_youtube_title(self, url): |
| 22 | """Extract YouTube video title if URL is a YouTube link.""" | 23 | """Extract YouTube video title if URL is a YouTube link.""" |
| 23 | if not url.startswith('https://www.youtube.com/'): | 24 | if "youtube.com/watch" not in url: |
| 24 | return None | 25 | return None |
| 25 | 26 | ||
| 26 | logger.info("youtube link") | 27 | logger.info("youtube link") |
| 27 | try: | 28 | try: |
| 28 | parsed_url = urllib.parse.urlparse(url) | 29 | ydl_opts = {"quiet": True, "nowarnings": True} |
| 29 | video_id = urllib.parse.parse_qs(parsed_url.query).get('v', [None])[0] | 30 | with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
| 30 | if video_id: | 31 | info = ydl.extract_info(url, download=False) |
| 31 | yt = pytube.YouTube(f'https://www.youtube.com/watch?v={video_id}') | 32 | return info.get("title") if info else None |
| 32 | return yt.title | ||
| 33 | except Exception: | 33 | except Exception: |
| 34 | pass | 34 | pass |
| 35 | return None | 35 | return None |
| @@ -37,10 +37,10 @@ class Web(commands.Cog): | |||
| 37 | def _create_button(self, index, link): | 37 | def _create_button(self, index, link): |
| 38 | """Create a button for sending search result.""" | 38 | """Create a button for sending search result.""" |
| 39 | button = discord.ui.Button(label=f"Send Result {index}") | 39 | button = discord.ui.Button(label=f"Send Result {index}") |
| 40 | 40 | ||
| 41 | async def button_callback(interaction: discord.Interaction): | 41 | async def button_callback(interaction: discord.Interaction): |
| 42 | await interaction.response.send_message(link.get('href')) | 42 | await interaction.response.send_message(link.get("href")) |
| 43 | 43 | ||
| 44 | button.callback = button_callback | 44 | button.callback = button_callback |
| 45 | return button | 45 | return button |
| 46 | 46 | ||
| @@ -48,17 +48,17 @@ class Web(commands.Cog): | |||
| 48 | """Process search results and create embed with buttons.""" | 48 | """Process search results and create embed with buttons.""" |
| 49 | embed = Embed(title="Search Results", description=f"Top results for: {query}", color=0x1E90FF) | 49 | embed = Embed(title="Search Results", description=f"Top results for: {query}", color=0x1E90FF) |
| 50 | view = discord.ui.View(disable_on_timeout=True) | 50 | view = discord.ui.View(disable_on_timeout=True) |
| 51 | 51 | ||
| 52 | for i, link in enumerate(links[:5], start=1): | 52 | for i, link in enumerate(links[:5], start=1): |
| 53 | title = link.text | 53 | title = link.text |
| 54 | youtube_title = self._extract_youtube_title(link.get('href')) | 54 | youtube_title = self._extract_youtube_title(link.get("href")) |
| 55 | if youtube_title: | 55 | if youtube_title: |
| 56 | title = youtube_title | 56 | title = youtube_title |
| 57 | 57 | ||
| 58 | embed.add_field(name=f"Result {i}", value=f"[{title}]({link.get('href')})", inline=False) | 58 | embed.add_field(name=f"Result {i}", value=f"[{title}]({link.get('href')})", inline=False) |
| 59 | button = self._create_button(i, link) | 59 | button = self._create_button(i, link) |
| 60 | view.add_item(button) | 60 | view.add_item(button) |
| 61 | 61 | ||
| 62 | return embed, view | 62 | return embed, view |
| 63 | 63 | ||
| 64 | @commands.slash_command( | 64 | @commands.slash_command( |
| @@ -71,7 +71,7 @@ class Web(commands.Cog): | |||
| 71 | query: str | 71 | query: str |
| 72 | ): | 72 | ): |
| 73 | await ctx.defer(ephemeral=True) | 73 | await ctx.defer(ephemeral=True) |
| 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'} | 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"} |
| 75 | encoded_query = urllib.parse.quote_plus(query) | 75 | encoded_query = urllib.parse.quote_plus(query) |
| 76 | url = f"https://duckduckgo.com/html/?q={encoded_query}" | 76 | url = f"https://duckduckgo.com/html/?q={encoded_query}" |
| 77 | response = requests.get(url, headers=headers) | 77 | response = requests.get(url, headers=headers) |
| @@ -83,9 +83,9 @@ class Web(commands.Cog): | |||
| 83 | logger.error(f"Error: {response.status_code}") | 83 | logger.error(f"Error: {response.status_code}") |
| 84 | return | 84 | return |
| 85 | 85 | ||
| 86 | soup = BeautifulSoup(response.text, 'html.parser') | 86 | soup = BeautifulSoup(response.text, "html.parser") |
| 87 | links = soup.find_all('a', class_='result__a') | 87 | links = soup.find_all("a", class_="result__a") |
| 88 | 88 | ||
| 89 | if not links: | 89 | if not links: |
| 90 | await ctx.respond(content="No results found.", ephemeral=True) | 90 | await ctx.respond(content="No results found.", ephemeral=True) |
| 91 | logger.warning("No results found") | 91 | logger.warning("No results found") |
| @@ -95,5 +95,6 @@ class Web(commands.Cog): | |||
| 95 | await ctx.respond(embed=embed, view=view, ephemeral=True) | 95 | await ctx.respond(embed=embed, view=view, ephemeral=True) |
| 96 | logger.info("Search results sent") | 96 | logger.info("Search results sent") |
| 97 | 97 | ||
| 98 | |||
| 98 | def setup(bot): | 99 | def setup(bot): |
| 99 | bot.add_cog(Web(bot)) | 100 | bot.add_cog(Web(bot)) \ No newline at end of file |
