diff --git a/commands/capoo/random_capoo.py b/commands/capoo/random_capoo.py new file mode 100644 index 0000000..f0bb1cc --- /dev/null +++ b/commands/capoo/random_capoo.py @@ -0,0 +1,21 @@ +import discord +from random import choice + +from commands.capoo.utils import get_videos_from_channel + +CAPOO_YOUTUBE = "https://www.youtube.com/@BugCatCapoo/videos" +BASE_VIDEO_URL = 'https://www.youtube.com/watch?v=' +BASE_SEARCH_URL = 'https://www.googleapis.com/youtube/v3/search?' + + +def register_commands(tree, this_guild: discord.Object): + @tree.command( + name="capoo", + description="Get a random video from Capoo's YouTube channel", + guild=this_guild, + ) + async def random_capoo( + interaction: discord.Interaction, + ): + video_links = get_videos_from_channel("UClr57MMpeX6m_p6hvvhu1Fw") + await interaction.response.send_message(choice(video_links)) diff --git a/commands/capoo/utils.py b/commands/capoo/utils.py new file mode 100644 index 0000000..72acbf9 --- /dev/null +++ b/commands/capoo/utils.py @@ -0,0 +1,25 @@ + +import json +import os + +import urllib3 + + +BASE_VIDEO_URL = 'https://www.youtube.com/watch?v=' +BASE_SEARCH_URL = 'https://www.googleapis.com/youtube/v3/search?' + +def get_videos_from_channel(channel_id, max_results=100): + api_key = os.getenv("YOUTUBE_API_KEY") + + url = BASE_SEARCH_URL + \ + 'key={}&channelId={}&part=snippet,id&order=date&maxResults={}'.format( + api_key, channel_id, max_results) + + video_links = [] + htm_content = urllib3.PoolManager().request('GET', url).data + video_json = json.loads(htm_content) + for video_item in video_json['items']: + if video_item['kind'] == "youtube#searchResult": + video_links.append(BASE_VIDEO_URL + video_item['id']['videoId']) + + return video_links diff --git a/main.py b/main.py index a4c8727..88a7c0d 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,7 @@ from commands.reacttw import react_tw from commands.react_baltics import react_baltics from commands.shiba import random_shiba +from commands.capoo import random_capoo import sys # load environment vars (from .env) @@ -82,6 +83,7 @@ async def test_slash_command(interaction: discord.Interaction): edit_entry_cmd.register_commands(tree, this_guild, client) hgs.register_commands(tree, this_guild) random_shiba.register_commands(tree, this_guild) +random_capoo.register_commands(tree, this_guild) # sync the slash commands to server diff --git a/requirements.txt b/requirements.txt index fb6e32b..55e557a 100644 Binary files a/requirements.txt and b/requirements.txt differ