mirror of
https://github.com/jarulsamy/Plex-Bot.git
synced 2024-08-19 15:01:55 +02:00
⚡ Switch to PlexAPI for search
This commit is contained in:
parent
7f49c4d958
commit
5f90b17b0e
@ -9,10 +9,11 @@ from async_timeout import timeout
|
|||||||
from discord import FFmpegPCMAudio
|
from discord import FFmpegPCMAudio
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord.ext.commands import command
|
from discord.ext.commands import command
|
||||||
from fuzzywuzzy import fuzz
|
|
||||||
from plexapi.exceptions import Unauthorized
|
from plexapi.exceptions import Unauthorized
|
||||||
from plexapi.server import PlexServer
|
from plexapi.server import PlexServer
|
||||||
|
|
||||||
|
from .exceptions import TrackNotFoundError
|
||||||
|
|
||||||
root_log = logging.getLogger()
|
root_log = logging.getLogger()
|
||||||
plex_log = logging.getLogger("Plex")
|
plex_log = logging.getLogger("Plex")
|
||||||
bot_log = logging.getLogger("Bot")
|
bot_log = logging.getLogger("Bot")
|
||||||
@ -152,32 +153,22 @@ class Plex(commands.Cog):
|
|||||||
self.bot.loop.create_task(self._audio_player_task())
|
self.bot.loop.create_task(self._audio_player_task())
|
||||||
|
|
||||||
def _search_tracks(self, title: str):
|
def _search_tracks(self, title: str):
|
||||||
"""Search the Plex music db
|
"""Search the Plex music db for track
|
||||||
|
|
||||||
Uses a fuzzy search algorithm to find the closest matching song
|
|
||||||
title in the Plex music database.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
title: str title of song to search for
|
title: str title of song to search for
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
plexapi.audio.Track pointing to closest matching title
|
plexapi.audio.Track pointing to closest matching title
|
||||||
None if song can't be found.
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
None
|
TrackNotFoundError: Title of track can't be found in plex db.
|
||||||
"""
|
"""
|
||||||
tracks = self.music.searchTracks()
|
results = self.music.searchTracks(title=title, maxresults=1)
|
||||||
score = [None, -1]
|
try:
|
||||||
for i in tracks:
|
return results[0]
|
||||||
ratio = fuzz.ratio(title.lower(), i.title.lower())
|
except IndexError:
|
||||||
if ratio > score[1]:
|
raise TrackNotFoundError
|
||||||
score[0] = i
|
|
||||||
score[1] = ratio
|
|
||||||
elif ratio == score[1]:
|
|
||||||
score[0] = i
|
|
||||||
|
|
||||||
return score[0]
|
|
||||||
|
|
||||||
async def _play(self):
|
async def _play(self):
|
||||||
"""Heavy lifting of playing songs
|
"""Heavy lifting of playing songs
|
||||||
@ -332,10 +323,9 @@ class Plex(commands.Cog):
|
|||||||
return
|
return
|
||||||
|
|
||||||
title = " ".join(args)
|
title = " ".join(args)
|
||||||
track = self._search_tracks(title)
|
try:
|
||||||
|
track = self._search_tracks(title)
|
||||||
# Fail if song title can't be found
|
except TrackNotFoundError:
|
||||||
if not track:
|
|
||||||
await ctx.send(f"Can't find song: {title}")
|
await ctx.send(f"Can't find song: {title}")
|
||||||
bot_log.debug("Failed to play, can't find song - %s", title)
|
bot_log.debug("Failed to play, can't find song - %s", title)
|
||||||
return
|
return
|
||||||
|
2
PlexBot/exceptions.py
Normal file
2
PlexBot/exceptions.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
class TrackNotFoundError(Exception):
|
||||||
|
pass
|
Loading…
Reference in New Issue
Block a user