Skip to content

Commit

Permalink
fix logging and made spotify + apple music optional because of v4
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwithax committed Feb 2, 2024
1 parent 9b18759 commit bd78f47
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 53 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ repos:
hooks:
- id: black
language_version: python3.11
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
Expand Down
13 changes: 5 additions & 8 deletions docs/hdi/pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ After you have initialized your function, we need to fill in the proper paramete
- Set this value to `True` if you want Pomice to automatically switch all players to another available node if one disconnects.
You must have two or more nodes to be able to do this.

* - `log_level`
- `LogLevel`
- The logging level for the node. The default logging level is `LogLevel.INFO`.
* - `logger`
- `Optional[logging.Logger]`
- If you would like to receive logging information from Pomice, set this to your logger class

* - `log_handler`
- `Optional[logging.Handler]`
- The logging handler for the node. Set to `None` to default to the built-in logging handler.

:::

Expand All @@ -93,13 +90,13 @@ await NodePool.create_node(
spotify_client_secret="<your spotify client secret here>"
apple_music=<True/False>,
fallback=<True/False>,
log_level=<optional LogLevel here>
logger=<your logger here>
)

```
:::{important}

For features like Spotify and Apple Music, you are **not required** to fill in anything for them if you do not want to use them. If you do end up queuing a Spotify or Apple Music track anyway, they will **not work** because these options are not enabled.
For features like Spotify and Apple Music, you are **not required** to fill in anything for them if you do not want to use them. If you do end up queuing a Spotify or Apple Music track, it is **up to you** on how you decide to handle it, whether it be through your own methods or a Lavalink plugin.

:::

Expand Down
52 changes: 11 additions & 41 deletions pomice/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
from . import spotify
from .enums import *
from .enums import LogLevel
from .exceptions import AppleMusicNotEnabled
from .exceptions import InvalidSpotifyClientAuthorization
from .exceptions import LavalinkVersionIncompatible
from .exceptions import NodeConnectionFailure
from .exceptions import NodeCreationError
Expand Down Expand Up @@ -97,7 +95,6 @@ class Node:
"_apple_music_client",
"_route_planner",
"_log",
"_log_handler",
"_stats",
"available",
)
Expand All @@ -121,8 +118,7 @@ def __init__(
spotify_client_secret: Optional[str] = None,
apple_music: bool = False,
fallback: bool = False,
log_level: LogLevel = LogLevel.INFO,
log_handler: Optional[logging.Handler] = None,
logger: Optional[logging.Logger] = None,
):
if not isinstance(port, int):
raise TypeError("Port must be an integer")
Expand All @@ -138,8 +134,6 @@ def __init__(
self._resume_timeout: int = resume_timeout
self._secure: bool = secure
self._fallback: bool = fallback
self._log_level: LogLevel = log_level
self._log_handler = log_handler

self._websocket_uri: str = f"{'wss' if self._secure else 'ws'}://{self._host}:{self._port}"
self._rest_uri: str = f"{'https' if self._secure else 'http'}://{self._host}:{self._port}"
Expand All @@ -154,7 +148,7 @@ def __init__(
self._version: LavalinkVersion = LavalinkVersion(0, 0, 0)

self._route_planner = RoutePlanner(self)
self._log = self._setup_logging(self._log_level)
self._log = logger

if not self._bot.user:
raise NodeCreationError("Bot user is not ready yet.")
Expand Down Expand Up @@ -232,21 +226,6 @@ def ping(self) -> float:
"""Alias for `Node.latency`, returns the latency of the node"""
return self.latency

def _setup_logging(self, level: LogLevel) -> logging.Logger:
logger = logging.getLogger("pomice")

handler = None

if self._log_handler:
handler = self._log_handler
logger.setLevel(handler.level)

if handler:
logger.handlers.clear()
logger.addHandler(handler)

return logger

async def _handle_version_check(self, version: str) -> None:
if version.endswith("-SNAPSHOT"):
# we're just gonna assume all snapshot versions correlate with v4
Expand Down Expand Up @@ -580,13 +559,13 @@ async def get_tracks(
for filter in filters:
filter.set_preload()

if URLRegex.AM_URL.match(query):
if not self._apple_music_client:
raise AppleMusicNotEnabled(
"You must have Apple Music functionality enabled in order to play Apple Music tracks."
"Please set apple_music to True in your Node class.",
)
# Due to the inclusion of plugins in the v4 update
# we are doing away with raising an error if pomice detects
# either a Spotify or Apple Music URL and the respective client
# is not enabled. Instead, we will just only parse the URL
# if the client is enabled and the URL is valid.

if self._apple_music_client and URLRegex.AM_URL.match(query):
apple_music_results = await self._apple_music_client.search(query=query)
if isinstance(apple_music_results, applemusic.Song):
return [
Expand Down Expand Up @@ -645,14 +624,7 @@ async def get_tracks(
uri=apple_music_results.url,
)

elif URLRegex.SPOTIFY_URL.match(query):
if not self._spotify_client_id and not self._spotify_client_secret:
raise InvalidSpotifyClientAuthorization(
"You did not provide proper Spotify client authorization credentials. "
"If you would like to use the Spotify searching feature, "
"please obtain Spotify API credentials here: https://developer.spotify.com/",
)

elif self._spotify_client and URLRegex.SPOTIFY_URL.match(query):
spotify_results = await self._spotify_client.search(query=query) # type: ignore

if isinstance(spotify_results, spotify.Track):
Expand Down Expand Up @@ -985,8 +957,7 @@ async def create_node(
session: Optional[aiohttp.ClientSession] = None,
apple_music: bool = False,
fallback: bool = False,
log_level: LogLevel = LogLevel.INFO,
log_handler: Optional[logging.Handler] = None,
logger: Optional[logging.Logger] = None,
) -> Node:
"""Creates a Node object to be then added into the node pool.
For Spotify searching capabilites, pass in valid Spotify API credentials.
Expand All @@ -1013,8 +984,7 @@ async def create_node(
spotify_client_secret=spotify_client_secret,
apple_music=apple_music,
fallback=fallback,
log_level=log_level,
log_handler=log_handler,
logger=logger,
)

await node.connect()
Expand Down

0 comments on commit bd78f47

Please sign in to comment.