diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/web.py | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/cogs/web.py b/cogs/web.py deleted file mode 100644 index fd22f9f..0000000 --- a/cogs/web.py +++ /dev/null | |||
| @@ -1,90 +0,0 @@ | |||
| 1 | # This file is part of Senator Sperma | ||
| 2 | # Licensed under the GPL-3.0 License | ||
| 3 | # Copyright (C) 2026 Salmonik @ salmonik@tuta.io | ||
| 4 | # https://codeberg.org/salmonik/senator-sperma | ||
| 5 | |||
| 6 | import discord | ||
| 7 | from discord import Embed | ||
| 8 | from discord.ext import commands | ||
| 9 | import requests | ||
| 10 | from bs4 import BeautifulSoup | ||
| 11 | import urllib.parse | ||
| 12 | import yt_dlp | ||
| 13 | import logging | ||
| 14 | from cogs.utils.url_helper import clean_url_for_ytdlp | ||
| 15 | |||
| 16 | logger = logging.getLogger(__name__) | ||
| 17 | |||
| 18 | |||
| 19 | class Web(commands.Cog): | ||
| 20 | def __init__(self, bot): | ||
| 21 | self.bot = bot | ||
| 22 | |||
| 23 | def _extract_youtube_title(self, url): | ||
| 24 | """Extract YouTube video title if URL is a YouTube link.""" | ||
| 25 | if "youtube.com/watch" not in url: | ||
| 26 | return None | ||
| 27 | |||
| 28 | url = clean_url_for_ytdlp(url) | ||
| 29 | logger.info("youtube link") | ||
| 30 | try: | ||
| 31 | ydl_opts = {"quiet": True, "nowarnings": True} | ||
| 32 | with yt_dlp.YoutubeDL(ydl_opts) as ydl: | ||
| 33 | info = ydl.extract_info(url, download=False) | ||
| 34 | return info.get("title") if info else None | ||
| 35 | except Exception: | ||
| 36 | pass | ||
| 37 | return None | ||
| 38 | |||
| 39 | def _process_search_results(self, links, query): | ||
| 40 | """Process search results and create embed.""" | ||
| 41 | embed = Embed(title="Search Results", description=f"Top results for: {query}", color=0x1E90FF) | ||
| 42 | view = discord.ui.View(disable_on_timeout=True) | ||
| 43 | |||
| 44 | for i, link in enumerate(links[:5], start=1): | ||
| 45 | title = link.text | ||
| 46 | youtube_title = self._extract_youtube_title(link.get("href")) | ||
| 47 | if youtube_title: | ||
| 48 | title = youtube_title | ||
| 49 | |||
| 50 | embed.add_field(name=f"Result {i}", value=f"[{title}]({link.get('href')})", inline=False) | ||
| 51 | |||
| 52 | return embed, view | ||
| 53 | |||
| 54 | @commands.slash_command( | ||
| 55 | name="web", | ||
| 56 | description="Search the web using DuckDuckGo" | ||
| 57 | ) | ||
| 58 | async def web( | ||
| 59 | self, | ||
| 60 | ctx: discord.ApplicationContext, | ||
| 61 | query: str | ||
| 62 | ): | ||
| 63 | await ctx.defer() | ||
| 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"} | ||
| 65 | encoded_query = urllib.parse.quote_plus(query) | ||
| 66 | url = f"https://duckduckgo.com/html/?q={encoded_query}" | ||
| 67 | response = requests.get(url, headers=headers) | ||
| 68 | |||
| 69 | logger.info(f"Query: {query}") | ||
| 70 | |||
| 71 | if response.status_code != 200: | ||
| 72 | await ctx.respond(content=f"Error: {response.status_code}", ephemeral=True) | ||
| 73 | logger.error(f"Error: {response.status_code}") | ||
| 74 | return | ||
| 75 | |||
| 76 | soup = BeautifulSoup(response.text, "html.parser") | ||
| 77 | links = soup.find_all("a", class_="result__a") | ||
| 78 | |||
| 79 | if not links: | ||
| 80 | await ctx.respond(content="No results found.", ephemeral=True) | ||
| 81 | logger.warning("No results found") | ||
| 82 | return | ||
| 83 | |||
| 84 | embed, view = self._process_search_results(links, query) | ||
| 85 | await ctx.respond(embed=embed, view=view) | ||
| 86 | logger.info("Search results sent") | ||
| 87 | |||
| 88 | |||
| 89 | def setup(bot): | ||
| 90 | bot.add_cog(Web(bot)) | ||
