fix issue with pytube,

add limit to search result length
This commit is contained in:
Arthur Lu 2024-09-24 23:21:00 +00:00
parent ccc13700d9
commit 50f993392b
2 changed files with 15 additions and 7 deletions

View File

@ -33,15 +33,20 @@ def get_success(message):
emb = discord.Embed(title='music-bot', description=message,color=0x00FF00) emb = discord.Embed(title='music-bot', description=message,color=0x00FF00)
return emb return emb
def get_search_results(query, results): def get_search_results(query, results, max_results = 10):
desc = 'search for: {0}'.format(query) desc = 'search for: {0}'.format(query)
emb = discord.Embed(title='music-bot', description=desc,color=0x0000FF) emb = discord.Embed(title='music-bot', description=desc,color=0x0000FF)
# need to limit number of results because of embedded message limit
num_results = min(max_results, len(results))
lst = "" lst = ""
for i in range(0, min(10, len(results))): for i in range(0, num_results):
# need to limit length of each search result because of embedded message limit
title = results[i].title title = results[i].title
title_trunc = title[0:70]
author = results[i].author author = results[i].author
lst += "{0}: {1} | {2}\n".format(str(i), author, title) author_trunc = author[0:20]
lst += "{0}: {1} | {2}\n".format(str(i), author_trunc, title_trunc)
if lst == "": if lst == "":
lst = "no results" lst = "no results"
emb.add_field(name="{0} results found: ".format(len(results)), value=lst) emb.add_field(name="{0} results found: ".format(num_results), value=lst)
return emb return emb

View File

@ -4,10 +4,13 @@ from config import *
import discord import discord
from discord.ext import commands from discord.ext import commands
from embed import * from embed import *
from pytube import YouTube, Playlist, Search, exceptions from pytube import Playlist, Search, exceptions
import shutil import shutil
from embed import get_search_results from embed import get_search_results
from pytubefix import YouTube
from pytubefix.cli import on_progress
from music_queue import music_queue from music_queue import music_queue
config_path = "config.json" config_path = "config.json"
@ -147,12 +150,12 @@ async def play(ctx, *args):
if 'list=' in url: if 'list=' in url:
pl = Playlist(url) pl = Playlist(url)
for video in pl: for video in pl:
yt = YouTube(video) yt = YouTube(video, on_progress_callback = on_progress)
bot.queue.enqueue(yt) bot.queue.enqueue(yt)
count += 1 count += 1
await ctx.send(embed=get_success('added {0} tracks to queue'.format(len(pl)))) await ctx.send(embed=get_success('added {0} tracks to queue'.format(len(pl))))
else: else:
yt = YouTube(url) yt = YouTube(url, on_progress_callback = on_progress)
bot.queue.enqueue(yt) bot.queue.enqueue(yt)
await ctx.send(embed=get_success('added {0} to queue'.format(yt.title))) await ctx.send(embed=get_success('added {0} to queue'.format(yt.title)))