Skip to content

Commit

Permalink
Improve compound link support to include short links and also ignore …
Browse files Browse the repository at this point in the history
…the playlist carrier video.
  • Loading branch information
itsTheFae committed Feb 2, 2024
1 parent 274bb23 commit df4ff4f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
23 changes: 18 additions & 5 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2795,7 +2795,9 @@ async def _cmd_play_compound_link(
# TODO: maybe add config to auto yes or no and bypass this.
# TODO: this currently will queue the original video twice.

async def _prompt_for_playing(prompt: str, next_url: str) -> None:
async def _prompt_for_playing(
prompt: str, next_url: str, ignore_vid: str = ""
) -> None:
msg = await self.safe_send_message(channel, prompt)
if not msg:
log.warning(
Expand Down Expand Up @@ -2824,6 +2826,7 @@ def _check_react(reaction: discord.Reaction, user: discord.Member) -> bool:
leftover_args,
next_url,
head,
ignore_video_id=ignore_vid,
)
await self.safe_delete_message(msg)
elif reaction.emoji == EMOJI_CROSS_MARK_BUTTON:
Expand All @@ -2834,15 +2837,20 @@ def _check_react(reaction: discord.Reaction, user: discord.Member) -> bool:
# Check for playlist in youtube watch link.
# https://youtu.be/VID?list=PLID
# https://www.youtube.com/watch?v=VID&list=PLID
playlist_regex = r"(?:/watch\?v=.+&(list=[^&]+)|be/[^?&]{6,}\?(list=PL[^&]+))"
matches = re.search(playlist_regex, song_url)
playlist_regex = re.compile(
r"(?:youtube.com/watch\?v=|youtu\.be/)([^?&]{6,})[&?]{1}(list=PL[^&]+)",
re.I | re.X,
)
matches = playlist_regex.search(song_url)
if matches:
pl_url = "https://www.youtube.com/playlist?" + matches.group(1)
pl_url = "https://www.youtube.com/playlist?" + matches.group(2)
ignore_vid = matches.group(1)
asyncio.ensure_future(
_prompt_for_playing(
# TODO: i18n / UI stuff
f"This link contains a Playlist ID:\n`{song_url}`\n\nDo you want to queue the playlist too?",
pl_url,
ignore_vid,
)
)

Expand All @@ -2858,6 +2866,7 @@ async def _cmd_play(
song_url: str,
head: bool,
shuffle_entries: bool = False,
ignore_video_id: str = "",
) -> CommandResponse:
"""
This function handles actually playing any given URL or song subject.
Expand Down Expand Up @@ -3026,7 +3035,11 @@ async def _cmd_play(
# Also have a "verify_entry" hook with the entry as an arg and returns the entry if its ok
start_time = time.time()
entry_list, position = await player.playlist.import_from_info(
info, channel=channel, author=author, head=False
info,
channel=channel,
author=author,
head=False,
ignore_video_id=ignore_video_id,
)

time_taken = time.time() - start_time
Expand Down
15 changes: 14 additions & 1 deletion musicbot/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ async def add_entry_from_info(
return entry, (1 if head else len(self.entries))

async def import_from_info(
self, info: "YtdlpResponseDict", head: bool, **meta: Any
self,
info: "YtdlpResponseDict",
head: bool,
ignore_video_id: str = "",
**meta: Any,
) -> Tuple[List[EntryTypes], int]:
"""
Validates the songs from `info` and queues them to be played.
Expand Down Expand Up @@ -234,6 +238,15 @@ async def import_from_info(
# count tracks regardless of conditions, used for missing track names
# and also defers serialization of the queue for playlists.
track_number += 1
# Ignore playlist entry when it comes from compound links.
if ignore_video_id and ignore_video_id == item.video_id:
log.debug(
"Ignored video from compound playlist link with ID: %s",
item.video_id,
)
baditems += 1
continue

# Exclude entries over max permitted duration.
if (
author_perms
Expand Down

0 comments on commit df4ff4f

Please sign in to comment.