mirror of
https://github.com/ethanrusz/youtube-url-corrector.git
synced 2024-11-21 09:37:47 -05:00
First working bot.
This commit is contained in:
parent
815d52f074
commit
d88d344e4e
5 changed files with 82 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -152,6 +152,9 @@ dmypy.json
|
||||||
# Cython debug symbols
|
# Cython debug symbols
|
||||||
cython_debug/
|
cython_debug/
|
||||||
|
|
||||||
|
compose.yml
|
||||||
|
.idea/
|
||||||
|
|
||||||
# PyCharm
|
# PyCharm
|
||||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
10
Dockerfile
Normal file
10
Dockerfile
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
FROM python:3.11.6
|
||||||
|
|
||||||
|
WORKDIR /usr/src/units
|
||||||
|
|
||||||
|
COPY requiremtns.txt .
|
||||||
|
RUN pip install -r requirements.com
|
||||||
|
|
||||||
|
COPY bot.py .
|
||||||
|
|
||||||
|
CMD ["python", "bot.py"]
|
51
bot.py
Normal file
51
bot.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import os
|
||||||
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
from contextlib import suppress
|
||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
import re
|
||||||
|
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.members = True
|
||||||
|
intents.message_content = True
|
||||||
|
|
||||||
|
bot = commands.Bot(command_prefix='?', intents=intents)
|
||||||
|
|
||||||
|
PIPED_URL = os.getenv('PIPED_URL')
|
||||||
|
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
|
||||||
|
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_ready():
|
||||||
|
print("Ready!")
|
||||||
|
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_message(message):
|
||||||
|
regex = re.compile(
|
||||||
|
r'https://(music\.)?(www\.)?youtu(be)?\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)')
|
||||||
|
|
||||||
|
result = regex.search(message.content)
|
||||||
|
if result is not None:
|
||||||
|
youtube_url = result.group(0)
|
||||||
|
video_id = await get_youtube_id(youtube_url)
|
||||||
|
|
||||||
|
if video_id is not None:
|
||||||
|
await message.reply(f"I think you meant {PIPED_URL}{video_id}.", mention_author=False)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_youtube_id(url: str, ignore_playlist=True) -> str:
|
||||||
|
query = urlparse(url)
|
||||||
|
if query.hostname == 'youtu.be': return query.path[1:]
|
||||||
|
if query.hostname in {'www.youtube.com', 'youtube.com', 'music.youtube.com'}:
|
||||||
|
if not ignore_playlist:
|
||||||
|
# use case: get playlist id not current video in playlist
|
||||||
|
with suppress(KeyError):
|
||||||
|
return parse_qs(query.query)['list'][0]
|
||||||
|
if query.path == '/watch': return parse_qs(query.query)['v'][0]
|
||||||
|
if query.path[:7] == '/watch/': return query.path.split('/')[1]
|
||||||
|
if query.path[:7] == '/embed/': return query.path.split('/')[2]
|
||||||
|
if query.path[:3] == '/v/': return query.path.split('/')[2]
|
||||||
|
|
||||||
|
|
||||||
|
bot.run(DISCORD_TOKEN)
|
10
requiremtns.txt
Normal file
10
requiremtns.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
aiohttp==3.8.6
|
||||||
|
aiosignal==1.3.1
|
||||||
|
async-timeout==4.0.3
|
||||||
|
attrs==23.1.0
|
||||||
|
charset-normalizer==3.3.2
|
||||||
|
discord.py==2.3.2
|
||||||
|
frozenlist==1.4.0
|
||||||
|
idna==3.4
|
||||||
|
multidict==6.0.4
|
||||||
|
yarl==1.9.2
|
Loading…
Reference in a new issue