Skip to content

Commit

Permalink
Merge pull request #25 from placeTW/random-capoo
Browse files Browse the repository at this point in the history
feat: random capo
  • Loading branch information
howardt12345 authored Aug 11, 2023
2 parents 7276811 + f693411 commit 2e7382c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions commands/capoo/random_capoo.py
Original file line number Diff line number Diff line change
@@ -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))
25 changes: 25 additions & 0 deletions commands/capoo/utils.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Binary file modified requirements.txt
Binary file not shown.

0 comments on commit 2e7382c

Please sign in to comment.