-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.py
36 lines (33 loc) · 1.28 KB
/
youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests
import json
import discord
import urllib.parse
import os
import token
try:
YOUTUBE_API_KEY = os.environ["YOUTUBE_API_KEY"]
except KeyError:
with open("token.txt") as f:
sample_text = f.readline()
sample_text = f.readline()
YOUTUBE_API_KEY = f.readline()
def getYoutubeVideos(args):
args = list(args)
maxResults = 3
if args and args[-1].isdigit() and 1 <= int(args[-1]) <= 5:
maxResults = args[-1]
args = args[:len(args)-1]
url = f"https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=25&q={urllib.parse.quote(' '.join(args))}&maxResults={maxResults}&type=video&key={YOUTUBE_API_KEY}"
data = requests.get(url).json()["items"]
embed = discord.Embed(
title=f"Top {maxResults} results for '{' '.join(args)}':",
colour=discord.Colour.red()
)
for i in range(len(data)):
description = f"https://youtube.com/watch?v={data[i]['id']['videoId']}\n"
to_add = data[i]["snippet"]["description"]
description += f"{to_add[:min(len(to_add), 80)]}..."
embed.add_field(name=f"{i+1}) {data[i]['snippet']['title']}",value=description, inline=False)
if len(data) > 0:
embed.set_image(url=data[0]["snippet"]["thumbnails"]["high"]["url"])
return embed