This commit is contained in:
45
garfmain.py
45
garfmain.py
@@ -1,7 +1,9 @@
|
||||
import re
|
||||
import config
|
||||
import asyncio
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from urllib.parse import urlparse, parse_qs, urlencode
|
||||
|
||||
from garfpy import (
|
||||
help,
|
||||
@@ -40,6 +42,36 @@ kroger = Kroger()
|
||||
weather = WeatherAPI()
|
||||
|
||||
|
||||
URL_PATTERNS = [
|
||||
r'https?://(?:www\.)?youtube\.com/watch\?[^\s]*',
|
||||
r'https?://youtu\.be/[^\s]*',
|
||||
r'https?://(?:open\.)?spotify\.com/[^\s]*',
|
||||
]
|
||||
|
||||
def clean_url(url):
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
|
||||
if 'youtube.com' in parsed.hostname:
|
||||
params = parse_qs(parsed.query)
|
||||
video_id = params.get('v', [None])[0]
|
||||
if not video_id:
|
||||
return None
|
||||
# timestamp = params.get('t', [None])[0]
|
||||
# if timestamp:
|
||||
# return f"https://www.youtube.com/watch?v={video_id}&t={timestamp}"
|
||||
return f"https://www.youtube.com/watch?v={video_id}"
|
||||
|
||||
if 'youtu.be' in parsed.hostname:
|
||||
return f"https://youtu.be{parsed.path}"
|
||||
|
||||
if 'spotify.com' in parsed.hostname:
|
||||
return f"https://open.spotify.com{parsed.path}"
|
||||
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
@garfbot.event
|
||||
async def on_ready():
|
||||
try:
|
||||
@@ -151,6 +183,19 @@ async def on_message(message):
|
||||
content = message.content.strip()
|
||||
lower = content.lower()
|
||||
|
||||
# Remove tracking stuff from youtube and spotify links
|
||||
cleaned_urls = []
|
||||
|
||||
for pattern in URL_PATTERNS:
|
||||
for match in re.finditer(pattern, message.content):
|
||||
cleaned = clean_url(match.group(0))
|
||||
if cleaned and cleaned != match.group(0):
|
||||
cleaned_urls.append(cleaned)
|
||||
|
||||
if cleaned_urls:
|
||||
links = '\n'.join(cleaned_urls)
|
||||
await message.reply(f"🔗 Cleaned link{'s' if len(cleaned_urls) > 1 else ''}:\n{links}")
|
||||
|
||||
# Chats & pics
|
||||
if lower.startswith("hey garfield") or isinstance(
|
||||
message.channel, discord.DMChannel
|
||||
|
||||
Reference in New Issue
Block a user