Compare commits
No commits in common. "main" and "2.2" have entirely different histories.
|
|
@ -10,8 +10,4 @@ build/
|
||||||
Pipfile.lock
|
Pipfile.lock
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
.vscode/
|
.vscode/
|
||||||
.idea/
|
|
||||||
.venv/
|
.venv/
|
||||||
*.code-workspace
|
|
||||||
*.ini
|
|
||||||
.pypirc
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
# See https://pre-commit.com/hooks.html for more hooks
|
# See https://pre-commit.com/hooks.html for more hooks
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.5.0
|
rev: v4.4.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: check-ast
|
- id: check-ast
|
||||||
- id: check-builtin-literals
|
- id: check-builtin-literals
|
||||||
|
|
@ -11,23 +11,31 @@ repos:
|
||||||
- id: requirements-txt-fixer
|
- id: requirements-txt-fixer
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- repo: https://github.com/psf/black
|
- repo: https://github.com/psf/black
|
||||||
rev: 23.10.1
|
rev: 23.1.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
language_version: python3.13
|
language_version: python3.8
|
||||||
|
- repo: https://github.com/asottile/blacken-docs
|
||||||
|
rev: 1.13.0
|
||||||
|
hooks:
|
||||||
|
- id: blacken-docs
|
||||||
- repo: https://github.com/asottile/pyupgrade
|
- repo: https://github.com/asottile/pyupgrade
|
||||||
rev: v3.15.0
|
rev: v3.3.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: pyupgrade
|
- id: pyupgrade
|
||||||
args: [--py37-plus, --keep-runtime-typing]
|
args: [--py37-plus, --keep-runtime-typing]
|
||||||
- repo: https://github.com/asottile/reorder-python-imports
|
- repo: https://github.com/asottile/reorder_python_imports
|
||||||
rev: v3.12.0
|
rev: v3.9.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: reorder-python-imports
|
- id: reorder-python-imports
|
||||||
- repo: https://github.com/asottile/add-trailing-comma
|
- repo: https://github.com/asottile/add-trailing-comma
|
||||||
rev: v3.1.0
|
rev: v2.4.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: add-trailing-comma
|
- id: add-trailing-comma
|
||||||
|
- repo: https://github.com/hadialqattan/pycln
|
||||||
|
rev: v2.1.3
|
||||||
|
hooks:
|
||||||
|
- id: pycln
|
||||||
|
|
||||||
default_language_version:
|
default_language_version:
|
||||||
python: python3.13
|
python: python3.8
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
3.10.9
|
||||||
3
Pipfile
3
Pipfile
|
|
@ -6,7 +6,6 @@ name = "pypi"
|
||||||
[packages]
|
[packages]
|
||||||
orjson = "*"
|
orjson = "*"
|
||||||
"discord.py" = {extras = ["voice"], version = "*"}
|
"discord.py" = {extras = ["voice"], version = "*"}
|
||||||
websockets = "*"
|
|
||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
mypy = "*"
|
mypy = "*"
|
||||||
|
|
@ -14,8 +13,6 @@ pre-commit = "*"
|
||||||
furo = "*"
|
furo = "*"
|
||||||
sphinx = "*"
|
sphinx = "*"
|
||||||
myst-parser = "*"
|
myst-parser = "*"
|
||||||
black = "*"
|
|
||||||
typing-extensions = "*"
|
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.8"
|
python_version = "3.8"
|
||||||
|
|
|
||||||
15
README.md
15
README.md
|
|
@ -3,16 +3,11 @@
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
[](https://github.com/cloudwithax/pomice/blob/main/LICENSE)  [](https://github.com/psf/black)
|
[](https://github.com/cloudwithax/pomice/blob/main/LICENSE) 
|
||||||
[](https://discord.gg/r64qjTSHG8) [](https://pomice.readthedocs.io/en/latest/)
|
[](https://discord.gg/r64qjTSHG8) [](https://pomice.readthedocs.io/en/latest/)
|
||||||
|
|
||||||
|
|
||||||
Pomice is a fully asynchronous Python library designed for communicating with [Lavalink](https://github.com/freyacodes/Lavalink) seamlessly within the [discord.py](https://github.com/Rapptz/discord.py) library. It features 100% coverage of the [Lavalink](https://github.com/freyacodes/Lavalink) spec that can be accessed with easy-to-understand functions along with Spotify and Apple Music querying capabilities using built-in custom clients, making it easier to develop your next big music bot.
|
Pomice is a fully asynchronous Python library designed for communicating with [Lavalink](https://github.com/freyacodes/Lavalink) seamlessly within the [discord.py](https://github.com/Rapptz/discord.py) library. It features 100% API coverage of the entire [Lavalink](https://github.com/freyacodes/Lavalink) spec that can be accessed with easy-to-understand functions. We also include Spotify and Apple Music querying capabilites using built-in custom clients, making it easier to develop your next big music bot.
|
||||||
|
|
||||||
## Quick Links
|
|
||||||
- [Discord Server](https://discord.gg/r64qjTSHG8)
|
|
||||||
- [Read the Docs](https://pomice.readthedocs.io/en/latest/)
|
|
||||||
- [PyPI Homepage](https://pypi.org/project/pomice/)
|
|
||||||
|
|
||||||
|
|
||||||
# Install
|
# Install
|
||||||
|
|
@ -28,7 +23,7 @@ pip install pomice
|
||||||
pip install git+https://github.com/cloudwithax/pomice
|
pip install git+https://github.com/cloudwithax/pomice
|
||||||
```
|
```
|
||||||
|
|
||||||
# Support And Documentation
|
# Support
|
||||||
|
|
||||||
The official documentation is [here](https://pomice.readthedocs.io/en/latest/)
|
The official documentation is [here](https://pomice.readthedocs.io/en/latest/)
|
||||||
|
|
||||||
|
|
@ -36,7 +31,7 @@ You can join our support server [here](https://discord.gg/r64qjTSHG8)
|
||||||
|
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
In-depth examples are located in the [examples folder](https://github.com/cloudwithax/pomice/tree/main/examples)
|
In-depth examples are located in the examples folder
|
||||||
|
|
||||||
Here's a quick example:
|
Here's a quick example:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
# type: ignore
|
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,11 @@ The classes listed here are as they appear in Pomice. When you use them within y
|
||||||
the way you use them will be different. Here's an example on how you would use the `TrackStartEvent` within an event listener in a cog:
|
the way you use them will be different. Here's an example on how you would use the `TrackStartEvent` within an event listener in a cog:
|
||||||
|
|
||||||
```py
|
```py
|
||||||
|
|
||||||
@commands.Cog.listener
|
@commands.Cog.listener
|
||||||
async def on_pomice_track_start(self, player: Player, track: Track):
|
async def on_pomice_track_start(self, player: Player, track: Track):
|
||||||
...
|
...
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Event definitions
|
## Event definitions
|
||||||
|
|
@ -32,7 +34,7 @@ your application. Here are all the definitions:
|
||||||
|
|
||||||
|
|
||||||
All events related to tracks carry a `Player` object so you can access player-specific functions
|
All events related to tracks carry a `Player` object so you can access player-specific functions
|
||||||
and properties for further evaluation. They also carry a `Track` object so you can access track-specific functions and properties for further evaluation as well.
|
and properties for further evaluation. They also carry a `Track` object so you can access track-specific functions and properites for further evaluation as well.
|
||||||
|
|
||||||
`Event.TrackEndEvent()` carries the reason for the track ending. If the track ends suddenly, you can use the reason provided to determine a solution.
|
`Event.TrackEndEvent()` carries the reason for the track ending. If the track ends suddenly, you can use the reason provided to determine a solution.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,18 +67,6 @@ There are also properties the `Player` class has to access certain values:
|
||||||
- `float`
|
- `float`
|
||||||
- Returns the player’s position in a track in milliseconds.
|
- Returns the player’s position in a track in milliseconds.
|
||||||
|
|
||||||
* - `Player.adjusted_position`
|
|
||||||
- `float`
|
|
||||||
- Returns the player’s position in a track in milliseconds, adjusted for rate if affected.
|
|
||||||
|
|
||||||
* - `Player.adjusted_length`
|
|
||||||
- `float`
|
|
||||||
- Returns the current track length in milliseconds, adjusted for rate if affected.
|
|
||||||
|
|
||||||
* - `Player.rate`
|
|
||||||
- `float`
|
|
||||||
- Returns the players current rate, which represents the speed of the currently playing track. This rate is affected by the `Timescale` filter.
|
|
||||||
|
|
||||||
* - `Player.volume`
|
* - `Player.volume`
|
||||||
- `int`
|
- `int`
|
||||||
- Returns the players current volume.
|
- Returns the players current volume.
|
||||||
|
|
@ -361,27 +349,6 @@ await Player.stop()
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Moving the player to another channel
|
|
||||||
|
|
||||||
To move the player to another channel, we need to use `Player.move_to()`
|
|
||||||
|
|
||||||
```py
|
|
||||||
|
|
||||||
await Player.move_to(...)
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
After you have initialized your function, we need to include the `channel` parameter, which is a `VoiceChannel`:
|
|
||||||
|
|
||||||
```py
|
|
||||||
|
|
||||||
await Player.move_to(channel)
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
After running this function, your player should be in the new voice channel. All voice state updates should also be handled.
|
|
||||||
|
|
||||||
|
|
||||||
## Controlling filters
|
## Controlling filters
|
||||||
|
|
||||||
Pomice has an extensive suite of filter management tools to help you make the most of Lavalink and it's filters.
|
Pomice has an extensive suite of filter management tools to help you make the most of Lavalink and it's filters.
|
||||||
|
|
|
||||||
|
|
@ -66,11 +66,6 @@ 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.
|
- 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.
|
You must have two or more nodes to be able to do this.
|
||||||
|
|
||||||
* - `logger`
|
|
||||||
- `Optional[logging.Logger]`
|
|
||||||
- If you would like to receive logging information from Pomice, set this to your logger class
|
|
||||||
|
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -89,14 +84,13 @@ await NodePool.create_node(
|
||||||
spotify_client_id="<your spotify client id here>",
|
spotify_client_id="<your spotify client id here>",
|
||||||
spotify_client_secret="<your spotify client secret here>"
|
spotify_client_secret="<your spotify client secret here>"
|
||||||
apple_music=<True/False>,
|
apple_music=<True/False>,
|
||||||
fallback=<True/False>,
|
fallback=<True/False>
|
||||||
logger=<your logger here>
|
|
||||||
)
|
)
|
||||||
|
|
||||||
```
|
```
|
||||||
:::{important}
|
:::{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, it is **up to you** on how you decide to handle it, whether it be through your own methods or a Lavalink plugin.
|
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.
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,71 +10,63 @@ import re
|
||||||
|
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
URL_REG = re.compile(r"https?://(?:www\.)?.+")
|
URL_REG = re.compile(r'https?://(?:www\.)?.+')
|
||||||
|
|
||||||
|
|
||||||
class MyBot(commands.Bot):
|
class MyBot(commands.Bot):
|
||||||
def __init__(self) -> None:
|
|
||||||
super().__init__(
|
|
||||||
command_prefix="!",
|
|
||||||
activity=discord.Activity(
|
|
||||||
type=discord.ActivityType.listening, name="to music!"
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.add_cog(Music(self))
|
def __init__(self) -> None:
|
||||||
|
super().__init__(command_prefix='!', activity=discord.Activity(type=discord.ActivityType.listening, name='to music!'))
|
||||||
|
|
||||||
async def on_ready(self) -> None:
|
self.add_cog(Music(self))
|
||||||
print("I'm online!")
|
|
||||||
await self.cogs["Music"].start_nodes()
|
async def on_ready(self) -> None:
|
||||||
|
print("I'm online!")
|
||||||
|
await self.cogs["Music"].start_nodes()
|
||||||
|
|
||||||
|
|
||||||
class Music(commands.Cog):
|
class Music(commands.Cog):
|
||||||
def __init__(self, bot) -> None:
|
|
||||||
self.bot = bot
|
|
||||||
|
|
||||||
self.pomice = pomice.NodePool()
|
def __init__(self, bot) -> None:
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
async def start_nodes(self):
|
self.pomice = pomice.NodePool()
|
||||||
await self.pomice.create_node(
|
|
||||||
bot=self.bot,
|
|
||||||
host="127.0.0.1",
|
|
||||||
port="3030",
|
|
||||||
password="youshallnotpass",
|
|
||||||
identifier="MAIN",
|
|
||||||
)
|
|
||||||
print(f"Node is ready!")
|
|
||||||
|
|
||||||
@commands.command(name="join", aliases=["connect"])
|
async def start_nodes(self):
|
||||||
async def join(
|
await self.pomice.create_node(bot=self.bot, host='127.0.0.1', port='3030',
|
||||||
self, ctx: commands.Context, *, channel: discord.TextChannel = None
|
password='youshallnotpass', identifier='MAIN')
|
||||||
) -> None:
|
print(f"Node is ready!")
|
||||||
if not channel:
|
|
||||||
channel = getattr(ctx.author.voice, "channel", None)
|
|
||||||
|
|
||||||
|
@commands.command(name='join', aliases=['connect'])
|
||||||
|
async def join(self, ctx: commands.Context, *, channel: discord.TextChannel = None) -> None:
|
||||||
|
|
||||||
|
if not channel:
|
||||||
|
channel = getattr(ctx.author.voice, 'channel', None)
|
||||||
if not channel:
|
if not channel:
|
||||||
raise commands.CheckFailure(
|
raise commands.CheckFailure('You must be in a voice channel to use this command'
|
||||||
"You must be in a voice channel to use this command"
|
'without specifying the channel argument.')
|
||||||
"without specifying the channel argument."
|
|
||||||
)
|
|
||||||
|
|
||||||
await ctx.author.voice.channel.connect(cls=pomice.Player)
|
|
||||||
await ctx.send(f"Joined the voice channel `{channel}`")
|
|
||||||
|
|
||||||
@commands.command(name="play")
|
await ctx.author.voice.channel.connect(cls=pomice.Player)
|
||||||
async def play(self, ctx, *, search: str) -> None:
|
await ctx.send(f'Joined the voice channel `{channel}`')
|
||||||
if not ctx.voice_client:
|
|
||||||
|
@commands.command(name='play')
|
||||||
|
async def play(self, ctx, *, search: str) -> None:
|
||||||
|
|
||||||
|
if not ctx.voice_client:
|
||||||
await ctx.invoke(self.join)
|
await ctx.invoke(self.join)
|
||||||
|
|
||||||
player = ctx.voice_client
|
player = ctx.voice_client
|
||||||
|
|
||||||
results = await player.get_tracks(query=f"{search}")
|
results = await player.get_tracks(query=f'{search}')
|
||||||
|
|
||||||
if not results:
|
if not results:
|
||||||
raise commands.CommandError("No results were found for that search term.")
|
raise commands.CommandError('No results were found for that search term.')
|
||||||
|
|
||||||
if isinstance(results, pomice.Playlist):
|
if isinstance(results, pomice.Playlist):
|
||||||
await player.play(track=results.tracks[0])
|
await player.play(track=results.tracks[0])
|
||||||
else:
|
else:
|
||||||
await player.play(track=results[0])
|
await player.play(track=results[0])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,3 @@ discord.py[voice]
|
||||||
furo
|
furo
|
||||||
myst_parser
|
myst_parser
|
||||||
orjson
|
orjson
|
||||||
websockets
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
# type: ignore
|
|
||||||
"""
|
"""
|
||||||
This example aims to show the full capabilities of the library.
|
This example aims to show the full capabilities of the library.
|
||||||
This is in the form of a drop-in cog you can use and modify to your liking.
|
This is in the form of a drop-in cog you can use and modify to your liking.
|
||||||
|
|
@ -101,13 +100,13 @@ class Music(commands.Cog):
|
||||||
await self.pomice.create_node(
|
await self.pomice.create_node(
|
||||||
bot=self.bot,
|
bot=self.bot,
|
||||||
host="127.0.0.1",
|
host="127.0.0.1",
|
||||||
port=3030,
|
port="3030",
|
||||||
password="youshallnotpass",
|
password="youshallnotpass",
|
||||||
identifier="MAIN",
|
identifier="MAIN",
|
||||||
)
|
)
|
||||||
print(f"Node is ready!")
|
print(f"Node is ready!")
|
||||||
|
|
||||||
def required(self, ctx: commands.Context):
|
async def required(self, ctx: commands.Context):
|
||||||
"""Method which returns required votes based on amount of members in a channel."""
|
"""Method which returns required votes based on amount of members in a channel."""
|
||||||
player: Player = ctx.voice_client
|
player: Player = ctx.voice_client
|
||||||
channel = self.bot.get_channel(int(player.channel.id))
|
channel = self.bot.get_channel(int(player.channel.id))
|
||||||
|
|
@ -119,7 +118,7 @@ class Music(commands.Cog):
|
||||||
|
|
||||||
return required
|
return required
|
||||||
|
|
||||||
def is_privileged(self, ctx: commands.Context):
|
async def is_privileged(self, ctx: commands.Context):
|
||||||
"""Check whether the user is an Admin or DJ."""
|
"""Check whether the user is an Admin or DJ."""
|
||||||
player: Player = ctx.voice_client
|
player: Player = ctx.voice_client
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
# type: ignore
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
|
|
@ -36,7 +35,7 @@ class Music(commands.Cog):
|
||||||
await self.pomice.create_node(
|
await self.pomice.create_node(
|
||||||
bot=self.bot,
|
bot=self.bot,
|
||||||
host="127.0.0.1",
|
host="127.0.0.1",
|
||||||
port=3030,
|
port="3030",
|
||||||
password="youshallnotpass",
|
password="youshallnotpass",
|
||||||
identifier="MAIN",
|
identifier="MAIN",
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ Pomice
|
||||||
~~~~~~
|
~~~~~~
|
||||||
The modern Lavalink wrapper designed for discord.py.
|
The modern Lavalink wrapper designed for discord.py.
|
||||||
|
|
||||||
Copyright (c) 2024, cloudwithax
|
Copyright (c) 2023, cloudwithax
|
||||||
|
|
||||||
Licensed under GPL-3.0
|
Licensed under GPL-3.0
|
||||||
"""
|
"""
|
||||||
|
|
@ -20,7 +20,7 @@ if not discord.version_info.major >= 2:
|
||||||
"using 'pip install discord.py'",
|
"using 'pip install discord.py'",
|
||||||
)
|
)
|
||||||
|
|
||||||
__version__ = "2.10.0"
|
__version__ = "2.2"
|
||||||
__title__ = "pomice"
|
__title__ = "pomice"
|
||||||
__author__ = "cloudwithax"
|
__author__ = "cloudwithax"
|
||||||
__license__ = "GPL-3.0"
|
__license__ = "GPL-3.0"
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
"""Apple Music module for Pomice, made possible by cloudwithax 2023"""
|
"""Apple Music module for Pomice, made possible by cloudwithax 2023"""
|
||||||
from .client import *
|
from .client import Client
|
||||||
from .exceptions import *
|
from .exceptions import *
|
||||||
from .objects import *
|
from .objects import *
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,10 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import base64
|
import base64
|
||||||
import logging
|
|
||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import AsyncGenerator
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
@ -20,14 +16,11 @@ from .objects import *
|
||||||
__all__ = ("Client",)
|
__all__ = ("Client",)
|
||||||
|
|
||||||
AM_URL_REGEX = re.compile(
|
AM_URL_REGEX = re.compile(
|
||||||
r"https?://music\.apple\.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+?)/(?P<id>[^/?]+?)(?:/)?(?:\?.*)?$",
|
r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>[^?]+)",
|
||||||
)
|
)
|
||||||
AM_SINGLE_IN_ALBUM_REGEX = re.compile(
|
AM_SINGLE_IN_ALBUM_REGEX = re.compile(
|
||||||
r"https?://music\.apple\.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>[^/?]+)(\?i=)(?P<id2>[^&]+)(?:&.*)?$",
|
r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>.+)(\?i=)(?P<id2>.+)",
|
||||||
)
|
)
|
||||||
|
|
||||||
AM_SCRIPT_REGEX = re.compile(r'<script.*?src="(/assets/index-.*?)"')
|
|
||||||
|
|
||||||
AM_REQ_URL = "https://api.music.apple.com/v1/catalog/{country}/{type}s/{id}"
|
AM_REQ_URL = "https://api.music.apple.com/v1/catalog/{country}/{type}s/{id}"
|
||||||
AM_BASE_URL = "https://api.music.apple.com"
|
AM_BASE_URL = "https://api.music.apple.com"
|
||||||
|
|
||||||
|
|
@ -38,71 +31,40 @@ class Client:
|
||||||
and translating it to a valid Lavalink track. No client auth is required here.
|
and translating it to a valid Lavalink track. No client auth is required here.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *, playlist_concurrency: int = 6) -> None:
|
def __init__(self) -> None:
|
||||||
self.expiry: datetime = datetime(1970, 1, 1)
|
self.expiry: datetime = datetime(1970, 1, 1)
|
||||||
self.token: str = ""
|
self.token: str = ""
|
||||||
self.headers: Dict[str, str] = {}
|
self.headers: Dict[str, str] = {}
|
||||||
self.session: aiohttp.ClientSession = None # type: ignore
|
self.session: aiohttp.ClientSession = None # type: ignore
|
||||||
self._log = logging.getLogger(__name__)
|
|
||||||
# Concurrency knob for parallel playlist page retrieval
|
|
||||||
self._playlist_concurrency = max(1, playlist_concurrency)
|
|
||||||
|
|
||||||
async def _set_session(self, session: aiohttp.ClientSession) -> None:
|
|
||||||
self.session = session
|
|
||||||
|
|
||||||
async def request_token(self) -> None:
|
async def request_token(self) -> None:
|
||||||
# First lets get the raw response from the main page
|
if not self.session:
|
||||||
|
self.session = aiohttp.ClientSession()
|
||||||
|
|
||||||
resp = await self.session.get("https://music.apple.com")
|
async with self.session.get("https://music.apple.com/assets/index.919fe17f.js") as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
raise AppleMusicRequestException(
|
||||||
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
|
)
|
||||||
|
text = await resp.text()
|
||||||
|
match = re.search('"(eyJ.+?)"', text)
|
||||||
|
if not match:
|
||||||
|
raise AppleMusicRequestException(
|
||||||
|
"Could not find token in response.",
|
||||||
|
)
|
||||||
|
result = match.group(1)
|
||||||
|
|
||||||
if resp.status != 200:
|
self.token = result
|
||||||
raise AppleMusicRequestException(
|
self.headers = {
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
"Authorization": f"Bearer {result}",
|
||||||
)
|
"Origin": "https://apple.com",
|
||||||
|
}
|
||||||
# Looking for script tag that fits criteria
|
token_split = self.token.split(".")[1]
|
||||||
|
token_json = base64.b64decode(
|
||||||
text = await resp.text()
|
token_split + "=" * (-len(token_split) % 4),
|
||||||
match = re.search(AM_SCRIPT_REGEX, text)
|
).decode()
|
||||||
|
token_data = json.loads(token_json)
|
||||||
if not match:
|
self.expiry = datetime.fromtimestamp(token_data["exp"])
|
||||||
raise AppleMusicRequestException(
|
|
||||||
"Could not find valid script URL in response.",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Found the script file, lets grab our token
|
|
||||||
|
|
||||||
result = match.group(1)
|
|
||||||
asset_url = result
|
|
||||||
|
|
||||||
resp = await self.session.get("https://music.apple.com" + asset_url)
|
|
||||||
|
|
||||||
if resp.status != 200:
|
|
||||||
raise AppleMusicRequestException(
|
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
|
||||||
)
|
|
||||||
|
|
||||||
text = await resp.text()
|
|
||||||
match = re.search('"(eyJ.+?)"', text)
|
|
||||||
if not match:
|
|
||||||
raise AppleMusicRequestException(
|
|
||||||
"Could not find token in response.",
|
|
||||||
)
|
|
||||||
result = match.group(1)
|
|
||||||
|
|
||||||
self.token = result
|
|
||||||
self.headers = {
|
|
||||||
"Authorization": f"Bearer {result}",
|
|
||||||
"Origin": "https://apple.com",
|
|
||||||
}
|
|
||||||
token_split = self.token.split(".")[1]
|
|
||||||
token_json = base64.b64decode(
|
|
||||||
token_split + "=" * (-len(token_split) % 4),
|
|
||||||
).decode()
|
|
||||||
token_data = json.loads(token_json)
|
|
||||||
self.expiry = datetime.fromtimestamp(token_data["exp"])
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Fetched Apple Music bearer token successfully")
|
|
||||||
|
|
||||||
async def search(self, query: str) -> Union[Album, Playlist, Song, Artist]:
|
async def search(self, query: str) -> Union[Album, Playlist, Song, Artist]:
|
||||||
if not self.token or datetime.utcnow() > self.expiry:
|
if not self.token or datetime.utcnow() > self.expiry:
|
||||||
|
|
@ -128,171 +90,67 @@ class Client:
|
||||||
else:
|
else:
|
||||||
request_url = AM_REQ_URL.format(country=country, type=type, id=id)
|
request_url = AM_REQ_URL.format(country=country, type=type, id=id)
|
||||||
|
|
||||||
resp = await self.session.get(request_url, headers=self.headers)
|
async with self.session.get(request_url, headers=self.headers) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
if resp.status != 200:
|
raise AppleMusicRequestException(
|
||||||
raise AppleMusicRequestException(
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
)
|
||||||
)
|
data: dict = await resp.json(loads=json.loads)
|
||||||
|
|
||||||
data: dict = await resp.json(loads=json.loads)
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"Made request to Apple Music API with status {resp.status} and response {data}",
|
|
||||||
)
|
|
||||||
|
|
||||||
data = data["data"][0]
|
data = data["data"][0]
|
||||||
|
|
||||||
if type == "song":
|
if type == "song":
|
||||||
return Song(data)
|
return Song(data)
|
||||||
|
|
||||||
elif type == "album":
|
if type == "album":
|
||||||
return Album(data)
|
return Album(data)
|
||||||
|
|
||||||
elif type == "artist":
|
if type == "artist":
|
||||||
resp = await self.session.get(
|
async with self.session.get(
|
||||||
f"{request_url}/view/top-songs",
|
f"{request_url}/view/top-songs",
|
||||||
headers=self.headers,
|
headers=self.headers,
|
||||||
)
|
) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise AppleMusicRequestException(
|
raise AppleMusicRequestException(
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
)
|
)
|
||||||
|
top_tracks: dict = await resp.json(loads=json.loads)
|
||||||
top_tracks: dict = await resp.json(loads=json.loads)
|
artist_tracks: dict = top_tracks["data"]
|
||||||
artist_tracks: dict = top_tracks["data"]
|
|
||||||
|
|
||||||
return Artist(data, tracks=artist_tracks)
|
return Artist(data, tracks=artist_tracks)
|
||||||
else:
|
|
||||||
track_data: dict = data["relationships"]["tracks"]
|
|
||||||
album_tracks: List[Song] = [Song(track) for track in track_data["data"]]
|
|
||||||
|
|
||||||
if not len(album_tracks):
|
track_data: dict = data["relationships"]["tracks"]
|
||||||
raise AppleMusicRequestException(
|
album_tracks: List[Song] = [Song(track) for track in track_data["data"]]
|
||||||
"This playlist is empty and therefore cannot be queued.",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Apple Music uses cursor pagination with 'next'. We'll fetch subsequent pages
|
if not len(album_tracks):
|
||||||
# concurrently by first collecting cursors in rolling waves.
|
|
||||||
next_cursor = track_data.get("next")
|
|
||||||
semaphore = asyncio.Semaphore(self._playlist_concurrency)
|
|
||||||
|
|
||||||
async def fetch_page(url: str) -> List[Song]:
|
|
||||||
async with semaphore:
|
|
||||||
resp = await self.session.get(url, headers=self.headers)
|
|
||||||
if resp.status != 200:
|
|
||||||
if self._log:
|
|
||||||
self._log.warning(
|
|
||||||
f"Apple Music page fetch failed {resp.status} {resp.reason} for {url}",
|
|
||||||
)
|
|
||||||
return []
|
|
||||||
pj: dict = await resp.json(loads=json.loads)
|
|
||||||
songs = [Song(track) for track in pj.get("data", [])]
|
|
||||||
# Return songs; we will look for pj.get('next') in streaming iterator variant
|
|
||||||
return songs, pj.get("next") # type: ignore
|
|
||||||
|
|
||||||
# We'll implement a wave-based approach similar to Spotify but need to follow cursors.
|
|
||||||
# Because we cannot know all cursors upfront, we'll iteratively fetch waves.
|
|
||||||
waves: List[List[Song]] = []
|
|
||||||
cursors: List[str] = []
|
|
||||||
if next_cursor:
|
|
||||||
cursors.append(next_cursor)
|
|
||||||
|
|
||||||
# Limit total waves to avoid infinite loops in malformed responses
|
|
||||||
max_waves = 50
|
|
||||||
wave_size = self._playlist_concurrency * 2
|
|
||||||
wave_counter = 0
|
|
||||||
while cursors and wave_counter < max_waves:
|
|
||||||
current = cursors[:wave_size]
|
|
||||||
cursors = cursors[wave_size:]
|
|
||||||
tasks = [
|
|
||||||
fetch_page(AM_BASE_URL + cursor) for cursor in current # type: ignore[arg-type]
|
|
||||||
]
|
|
||||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
||||||
for res in results:
|
|
||||||
if isinstance(res, tuple): # (songs, next)
|
|
||||||
songs, nxt = res
|
|
||||||
if songs:
|
|
||||||
waves.append(songs)
|
|
||||||
if nxt:
|
|
||||||
cursors.append(nxt)
|
|
||||||
wave_counter += 1
|
|
||||||
|
|
||||||
for w in waves:
|
|
||||||
album_tracks.extend(w)
|
|
||||||
|
|
||||||
return Playlist(data, album_tracks)
|
|
||||||
|
|
||||||
async def iter_playlist_tracks(
|
|
||||||
self,
|
|
||||||
*,
|
|
||||||
query: str,
|
|
||||||
batch_size: int = 100,
|
|
||||||
) -> AsyncGenerator[List[Song], None]:
|
|
||||||
"""Stream Apple Music playlist tracks in batches.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
query: str
|
|
||||||
Apple Music playlist URL.
|
|
||||||
batch_size: int
|
|
||||||
Logical grouping size for yielded batches.
|
|
||||||
"""
|
|
||||||
if not self.token or datetime.utcnow() > self.expiry:
|
|
||||||
await self.request_token()
|
|
||||||
|
|
||||||
result = AM_URL_REGEX.match(query)
|
|
||||||
if not result or result.group("type") != "playlist":
|
|
||||||
raise InvalidAppleMusicURL("Provided query is not a valid Apple Music playlist URL.")
|
|
||||||
|
|
||||||
country = result.group("country")
|
|
||||||
playlist_id = result.group("id")
|
|
||||||
request_url = AM_REQ_URL.format(country=country, type="playlist", id=playlist_id)
|
|
||||||
resp = await self.session.get(request_url, headers=self.headers)
|
|
||||||
if resp.status != 200:
|
|
||||||
raise AppleMusicRequestException(
|
raise AppleMusicRequestException(
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
"This playlist is empty and therefore cannot be queued.",
|
||||||
)
|
)
|
||||||
data: dict = await resp.json(loads=json.loads)
|
|
||||||
playlist_data = data["data"][0]
|
|
||||||
track_data: dict = playlist_data["relationships"]["tracks"]
|
|
||||||
|
|
||||||
first_page_tracks = [Song(track) for track in track_data["data"]]
|
_next = track_data.get("next")
|
||||||
for i in range(0, len(first_page_tracks), batch_size):
|
if _next:
|
||||||
yield first_page_tracks[i : i + batch_size]
|
next_page_url = AM_BASE_URL + _next
|
||||||
|
|
||||||
next_cursor = track_data.get("next")
|
while next_page_url is not None:
|
||||||
semaphore = asyncio.Semaphore(self._playlist_concurrency)
|
async with self.session.get(next_page_url, headers=self.headers) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
async def fetch(cursor: str) -> tuple[List[Song], Optional[str]]:
|
raise AppleMusicRequestException(
|
||||||
url = AM_BASE_URL + cursor
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
async with semaphore:
|
|
||||||
r = await self.session.get(url, headers=self.headers)
|
|
||||||
if r.status != 200:
|
|
||||||
if self._log:
|
|
||||||
self._log.warning(
|
|
||||||
f"Skipping Apple Music page due to {r.status} {r.reason}",
|
|
||||||
)
|
)
|
||||||
return [], None
|
|
||||||
pj: dict = await r.json(loads=json.loads)
|
|
||||||
songs = [Song(track) for track in pj.get("data", [])]
|
|
||||||
return songs, pj.get("next")
|
|
||||||
|
|
||||||
# Rolling waves of fetches following cursor chain
|
next_data: dict = await resp.json(loads=json.loads)
|
||||||
max_waves = 50
|
|
||||||
wave_size = self._playlist_concurrency * 2
|
album_tracks.extend(Song(track) for track in next_data["data"])
|
||||||
waves = 0
|
|
||||||
cursors: List[str] = []
|
_next = next_data.get("next")
|
||||||
if next_cursor:
|
if _next:
|
||||||
cursors.append(next_cursor)
|
next_page_url = AM_BASE_URL + _next
|
||||||
while cursors and waves < max_waves:
|
else:
|
||||||
current = cursors[:wave_size]
|
next_page_url = None
|
||||||
cursors = cursors[wave_size:]
|
|
||||||
results = await asyncio.gather(*[fetch(c) for c in current])
|
return Playlist(data, album_tracks)
|
||||||
for songs, nxt in results:
|
|
||||||
if songs:
|
async def close(self) -> None:
|
||||||
for j in range(0, len(songs), batch_size):
|
if self.session:
|
||||||
yield songs[j : j + batch_size]
|
await self.session.close()
|
||||||
if nxt:
|
self.session = None # type: ignore
|
||||||
cursors.append(nxt)
|
|
||||||
waves += 1
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import re
|
import re
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from enum import IntEnum
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
"SearchType",
|
"SearchType",
|
||||||
|
|
@ -11,7 +10,6 @@ __all__ = (
|
||||||
"RouteStrategy",
|
"RouteStrategy",
|
||||||
"RouteIPType",
|
"RouteIPType",
|
||||||
"URLRegex",
|
"URLRegex",
|
||||||
"LogLevel",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -34,11 +32,6 @@ class SearchType(Enum):
|
||||||
ytsearch = "ytsearch"
|
ytsearch = "ytsearch"
|
||||||
ytmsearch = "ytmsearch"
|
ytmsearch = "ytmsearch"
|
||||||
scsearch = "scsearch"
|
scsearch = "scsearch"
|
||||||
other = "other"
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _missing_(cls, value: object) -> "SearchType": # type: ignore[override]
|
|
||||||
return cls.other
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.value
|
return self.value
|
||||||
|
|
@ -57,10 +50,6 @@ class TrackType(Enum):
|
||||||
TrackType.APPLE_MUSIC defines that the track is from Apple Music.
|
TrackType.APPLE_MUSIC defines that the track is from Apple Music.
|
||||||
|
|
||||||
TrackType.HTTP defines that the track is from an HTTP source.
|
TrackType.HTTP defines that the track is from an HTTP source.
|
||||||
|
|
||||||
TrackType.LOCAL defines that the track is from a local source.
|
|
||||||
|
|
||||||
TrackType.OTHER defines that the track is from an unknown source (possible from 3rd-party plugins).
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# We don't have to define anything special for these, since these just serve as flags
|
# We don't have to define anything special for these, since these just serve as flags
|
||||||
|
|
@ -69,12 +58,6 @@ class TrackType(Enum):
|
||||||
SPOTIFY = "spotify"
|
SPOTIFY = "spotify"
|
||||||
APPLE_MUSIC = "apple_music"
|
APPLE_MUSIC = "apple_music"
|
||||||
HTTP = "http"
|
HTTP = "http"
|
||||||
LOCAL = "local"
|
|
||||||
OTHER = "other"
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _missing_(cls, value: object) -> "TrackType": # type: ignore[override]
|
|
||||||
return cls.OTHER
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.value
|
return self.value
|
||||||
|
|
@ -91,8 +74,6 @@ class PlaylistType(Enum):
|
||||||
PlaylistType.SPOTIFY defines that the playlist is from Spotify
|
PlaylistType.SPOTIFY defines that the playlist is from Spotify
|
||||||
|
|
||||||
PlaylistType.APPLE_MUSIC defines that the playlist is from Apple Music.
|
PlaylistType.APPLE_MUSIC defines that the playlist is from Apple Music.
|
||||||
|
|
||||||
PlaylistType.OTHER defines that the playlist is from an unknown source (possible from 3rd-party plugins).
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# We don't have to define anything special for these, since these just serve as flags
|
# We don't have to define anything special for these, since these just serve as flags
|
||||||
|
|
@ -100,11 +81,6 @@ class PlaylistType(Enum):
|
||||||
SOUNDCLOUD = "soundcloud"
|
SOUNDCLOUD = "soundcloud"
|
||||||
SPOTIFY = "spotify"
|
SPOTIFY = "spotify"
|
||||||
APPLE_MUSIC = "apple_music"
|
APPLE_MUSIC = "apple_music"
|
||||||
OTHER = "other"
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _missing_(cls, value: object) -> "PlaylistType": # type: ignore[override]
|
|
||||||
return cls.OTHER
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.value
|
return self.value
|
||||||
|
|
@ -198,7 +174,7 @@ class RouteIPType(Enum):
|
||||||
|
|
||||||
class URLRegex:
|
class URLRegex:
|
||||||
"""
|
"""
|
||||||
The enum for all the URL Regexes in use by Pomice.
|
The enums for all the URL Regexes in use by Pomice.
|
||||||
|
|
||||||
URLRegex.SPOTIFY_URL returns the Spotify URL Regex.
|
URLRegex.SPOTIFY_URL returns the Spotify URL Regex.
|
||||||
|
|
||||||
|
|
@ -218,12 +194,8 @@ class URLRegex:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Spotify share links can include query parameters like ?si=XXXX, a trailing slash,
|
|
||||||
# or an intl locale segment (e.g. /intl-en/). Broaden the regex so we still capture
|
|
||||||
# the type and id while ignoring extra parameters. This prevents the URL from being
|
|
||||||
# treated as a generic Lavalink identifier and ensures internal Spotify handling runs.
|
|
||||||
SPOTIFY_URL = re.compile(
|
SPOTIFY_URL = re.compile(
|
||||||
r"https?://open\.spotify\.com/(?:intl-[a-zA-Z-]+/)?(?P<type>album|playlist|track|artist)/(?P<id>[a-zA-Z0-9]+)(?:/)?(?:\?.*)?$",
|
r"https?://open.spotify.com/(?P<type>album|playlist|track|artist)/(?P<id>[a-zA-Z0-9]+)",
|
||||||
)
|
)
|
||||||
|
|
||||||
DISCORD_MP3_URL = re.compile(
|
DISCORD_MP3_URL = re.compile(
|
||||||
|
|
@ -240,21 +212,22 @@ class URLRegex:
|
||||||
r"^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))/playlist\?list=.*",
|
r"^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))/playlist\?list=.*",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
YOUTUBE_VID_IN_PLAYLIST = re.compile(
|
||||||
|
r"(?P<video>^.*?v.*?)(?P<list>&list.*)",
|
||||||
|
)
|
||||||
|
|
||||||
YOUTUBE_TIMESTAMP = re.compile(
|
YOUTUBE_TIMESTAMP = re.compile(
|
||||||
r"(?P<video>^.*?)(\?t|&start)=(?P<time>\d+)?.*",
|
r"(?P<video>^.*?)(\?t|&start)=(?P<time>\d+)?.*",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Apple Music links sometimes append additional query parameters (e.g. &l=en, &uo=4).
|
|
||||||
# Allow arbitrary query parameters so valid links are captured and parsed.
|
|
||||||
AM_URL = re.compile(
|
AM_URL = re.compile(
|
||||||
r"https?://music\.apple\.com/(?P<country>[a-zA-Z]{2})/"
|
r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/"
|
||||||
r"(?P<type>album|playlist|song|artist)/(?P<name>.+?)/(?P<id>[^/?]+?)(?:/)?(?:\?.*)?$",
|
r"(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>[^?]+)",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Single-in-album links may also carry extra query params beyond the ?i=<trackid> token.
|
|
||||||
AM_SINGLE_IN_ALBUM_REGEX = re.compile(
|
AM_SINGLE_IN_ALBUM_REGEX = re.compile(
|
||||||
r"https?://music\.apple\.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/"
|
r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/"
|
||||||
r"(?P<name>.+)/(?P<id>[^/?]+)(\?i=)(?P<id2>[^&]+)(?:&.*)?$",
|
r"(?P<name>.+)/(?P<id>.+)(\?i=)(?P<id2>.+)",
|
||||||
)
|
)
|
||||||
|
|
||||||
SOUNDCLOUD_URL = re.compile(
|
SOUNDCLOUD_URL = re.compile(
|
||||||
|
|
@ -272,37 +245,3 @@ class URLRegex:
|
||||||
LAVALINK_SEARCH = re.compile(r"(?P<type>ytm?|sc)search:")
|
LAVALINK_SEARCH = re.compile(r"(?P<type>ytm?|sc)search:")
|
||||||
|
|
||||||
BASE_URL = re.compile(r"https?://(?:www\.)?.+")
|
BASE_URL = re.compile(r"https?://(?:www\.)?.+")
|
||||||
|
|
||||||
|
|
||||||
class LogLevel(IntEnum):
|
|
||||||
"""
|
|
||||||
The enum for specifying the logging level within Pomice.
|
|
||||||
This class serves as shorthand for logging.<level>
|
|
||||||
This enum is exclusively for the logging feature in Pomice.
|
|
||||||
If you are not using this feature, this class is not necessary.
|
|
||||||
|
|
||||||
|
|
||||||
LogLevel.DEBUG sets the logging level to "debug".
|
|
||||||
|
|
||||||
LogLevel.INFO sets the logging level to "info".
|
|
||||||
|
|
||||||
LogLevel.WARN sets the logging level to "warn".
|
|
||||||
|
|
||||||
LogLevel.ERROR sets the logging level to "error".
|
|
||||||
|
|
||||||
LogLevel.CRITICAL sets the logging level to "CRITICAL".
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
DEBUG = 10
|
|
||||||
INFO = 20
|
|
||||||
WARN = 30
|
|
||||||
ERROR = 40
|
|
||||||
CRITICAL = 50
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_str(cls, level_str):
|
|
||||||
try:
|
|
||||||
return cls[level_str.upper()]
|
|
||||||
except KeyError:
|
|
||||||
raise ValueError(f"No such log level: {level_str}")
|
|
||||||
|
|
|
||||||
|
|
@ -59,13 +59,14 @@ class TrackStartEvent(PomiceEvent):
|
||||||
|
|
||||||
def __init__(self, data: dict, player: Player):
|
def __init__(self, data: dict, player: Player):
|
||||||
self.player: Player = player
|
self.player: Player = player
|
||||||
self.track: Optional[Track] = self.player._current
|
assert self.player._current is not None
|
||||||
|
self.track: Track = self.player._current
|
||||||
|
|
||||||
# on_pomice_track_start(player, track)
|
# on_pomice_track_start(player, track)
|
||||||
self.handler_args = self.player, self.track
|
self.handler_args = self.player, self.track
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<Pomice.TrackStartEvent player={self.player!r} track={self.track!r}>"
|
return f"<Pomice.TrackStartEvent player={self.player} track_id={self.track.track_id}>"
|
||||||
|
|
||||||
|
|
||||||
class TrackEndEvent(PomiceEvent):
|
class TrackEndEvent(PomiceEvent):
|
||||||
|
|
@ -79,7 +80,8 @@ class TrackEndEvent(PomiceEvent):
|
||||||
|
|
||||||
def __init__(self, data: dict, player: Player):
|
def __init__(self, data: dict, player: Player):
|
||||||
self.player: Player = player
|
self.player: Player = player
|
||||||
self.track: Optional[Track] = self.player._ending_track
|
assert self.player._ending_track is not None
|
||||||
|
self.track: Track = self.player._ending_track
|
||||||
self.reason: str = data["reason"]
|
self.reason: str = data["reason"]
|
||||||
|
|
||||||
# on_pomice_track_end(player, track, reason)
|
# on_pomice_track_end(player, track, reason)
|
||||||
|
|
@ -87,8 +89,8 @@ class TrackEndEvent(PomiceEvent):
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (
|
return (
|
||||||
f"<Pomice.TrackEndEvent player={self.player!r} track_id={self.track!r} "
|
f"<Pomice.TrackEndEvent player={self.player} track_id={self.track.track_id} "
|
||||||
f"reason={self.reason!r}>"
|
f"reason={self.reason}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -104,7 +106,8 @@ class TrackStuckEvent(PomiceEvent):
|
||||||
|
|
||||||
def __init__(self, data: dict, player: Player):
|
def __init__(self, data: dict, player: Player):
|
||||||
self.player: Player = player
|
self.player: Player = player
|
||||||
self.track: Optional[Track] = self.player._ending_track
|
assert self.player._ending_track is not None
|
||||||
|
self.track: Track = self.player._ending_track
|
||||||
self.threshold: float = data["thresholdMs"]
|
self.threshold: float = data["thresholdMs"]
|
||||||
|
|
||||||
# on_pomice_track_stuck(player, track, threshold)
|
# on_pomice_track_stuck(player, track, threshold)
|
||||||
|
|
@ -128,7 +131,8 @@ class TrackExceptionEvent(PomiceEvent):
|
||||||
|
|
||||||
def __init__(self, data: dict, player: Player):
|
def __init__(self, data: dict, player: Player):
|
||||||
self.player: Player = player
|
self.player: Player = player
|
||||||
self.track: Optional[Track] = self.player._ending_track
|
assert self.player._ending_track is not None
|
||||||
|
self.track: Track = self.player._ending_track
|
||||||
# Error is for Lavalink <= 3.3
|
# Error is for Lavalink <= 3.3
|
||||||
self.exception: str = data.get(
|
self.exception: str = data.get(
|
||||||
"error",
|
"error",
|
||||||
|
|
|
||||||
|
|
@ -77,12 +77,6 @@ class Equalizer(Filter):
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<Pomice.EqualizerFilter tag={self.tag} eq={self.eq} raw={self.raw}>"
|
return f"<Pomice.EqualizerFilter tag={self.tag} eq={self.eq} raw={self.raw}>"
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, Equalizer):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return self.raw == __value.raw
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def flat(cls) -> "Equalizer":
|
def flat(cls) -> "Equalizer":
|
||||||
"""Equalizer preset which represents a flat EQ board,
|
"""Equalizer preset which represents a flat EQ board,
|
||||||
|
|
@ -237,16 +231,6 @@ class Timescale(Filter):
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<Pomice.TimescaleFilter tag={self.tag} speed={self.speed} pitch={self.pitch} rate={self.rate}>"
|
return f"<Pomice.TimescaleFilter tag={self.tag} speed={self.speed} pitch={self.pitch} rate={self.rate}>"
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, Timescale):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return (
|
|
||||||
self.speed == __value.speed
|
|
||||||
and self.pitch == __value.pitch
|
|
||||||
and self.rate == __value.rate
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Karaoke(Filter):
|
class Karaoke(Filter):
|
||||||
"""Filter which filters the vocal track from any song and leaves the instrumental.
|
"""Filter which filters the vocal track from any song and leaves the instrumental.
|
||||||
|
|
@ -286,17 +270,6 @@ class Karaoke(Filter):
|
||||||
f"filter_band={self.filter_band} filter_width={self.filter_width}>"
|
f"filter_band={self.filter_band} filter_width={self.filter_width}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, Karaoke):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return (
|
|
||||||
self.level == __value.level
|
|
||||||
and self.mono_level == __value.mono_level
|
|
||||||
and self.filter_band == __value.filter_band
|
|
||||||
and self.filter_width == __value.filter_width
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Tremolo(Filter):
|
class Tremolo(Filter):
|
||||||
"""Filter which produces a wavering tone in the music,
|
"""Filter which produces a wavering tone in the music,
|
||||||
|
|
@ -332,12 +305,6 @@ class Tremolo(Filter):
|
||||||
f"<Pomice.TremoloFilter tag={self.tag} frequency={self.frequency} depth={self.depth}>"
|
f"<Pomice.TremoloFilter tag={self.tag} frequency={self.frequency} depth={self.depth}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, Tremolo):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return self.frequency == __value.frequency and self.depth == __value.depth
|
|
||||||
|
|
||||||
|
|
||||||
class Vibrato(Filter):
|
class Vibrato(Filter):
|
||||||
"""Filter which produces a wavering tone in the music, similar to the Tremolo filter,
|
"""Filter which produces a wavering tone in the music, similar to the Tremolo filter,
|
||||||
|
|
@ -373,12 +340,6 @@ class Vibrato(Filter):
|
||||||
f"<Pomice.VibratoFilter tag={self.tag} frequency={self.frequency} depth={self.depth}>"
|
f"<Pomice.VibratoFilter tag={self.tag} frequency={self.frequency} depth={self.depth}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, Vibrato):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return self.frequency == __value.frequency and self.depth == __value.depth
|
|
||||||
|
|
||||||
|
|
||||||
class Rotation(Filter):
|
class Rotation(Filter):
|
||||||
"""Filter which produces a stereo-like panning effect, which sounds like
|
"""Filter which produces a stereo-like panning effect, which sounds like
|
||||||
|
|
@ -396,12 +357,6 @@ class Rotation(Filter):
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<Pomice.RotationFilter tag={self.tag} rotation_hertz={self.rotation_hertz}>"
|
return f"<Pomice.RotationFilter tag={self.tag} rotation_hertz={self.rotation_hertz}>"
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, Rotation):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return self.rotation_hertz == __value.rotation_hertz
|
|
||||||
|
|
||||||
|
|
||||||
class ChannelMix(Filter):
|
class ChannelMix(Filter):
|
||||||
"""Filter which manually adjusts the panning of the audio, which can make
|
"""Filter which manually adjusts the panning of the audio, which can make
|
||||||
|
|
@ -463,17 +418,6 @@ class ChannelMix(Filter):
|
||||||
f"right_to_left={self.right_to_left} right_to_right={self.right_to_right}>"
|
f"right_to_left={self.right_to_left} right_to_right={self.right_to_right}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, ChannelMix):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return (
|
|
||||||
self.left_to_left == __value.left_to_left
|
|
||||||
and self.left_to_right == __value.left_to_right
|
|
||||||
and self.right_to_left == __value.right_to_left
|
|
||||||
and self.right_to_right == __value.right_to_right
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Distortion(Filter):
|
class Distortion(Filter):
|
||||||
"""Filter which generates a distortion effect. Useful for certain filter implementations where
|
"""Filter which generates a distortion effect. Useful for certain filter implementations where
|
||||||
|
|
@ -535,21 +479,6 @@ class Distortion(Filter):
|
||||||
f"tan_scale={self.tan_scale} offset={self.offset} scale={self.scale}"
|
f"tan_scale={self.tan_scale} offset={self.offset} scale={self.scale}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, Distortion):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return (
|
|
||||||
self.sin_offset == __value.sin_offset
|
|
||||||
and self.sin_scale == __value.sin_scale
|
|
||||||
and self.cos_offset == __value.cos_offset
|
|
||||||
and self.cos_scale == __value.cos_scale
|
|
||||||
and self.tan_offset == __value.tan_offset
|
|
||||||
and self.tan_scale == __value.tan_scale
|
|
||||||
and self.offset == __value.offset
|
|
||||||
and self.scale == __value.scale
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class LowPass(Filter):
|
class LowPass(Filter):
|
||||||
"""Filter which supresses higher frequencies and allows lower frequencies to pass.
|
"""Filter which supresses higher frequencies and allows lower frequencies to pass.
|
||||||
|
|
@ -566,9 +495,3 @@ class LowPass(Filter):
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<Pomice.LowPass tag={self.tag} smoothing={self.smoothing}>"
|
return f"<Pomice.LowPass tag={self.tag} smoothing={self.smoothing}>"
|
||||||
|
|
||||||
def __eq__(self, __value: object) -> bool:
|
|
||||||
if not isinstance(__value, LowPass):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return self.smoothing == __value.smoothing
|
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ class Track:
|
||||||
self.author: str = info.get("author", "Unknown Author")
|
self.author: str = info.get("author", "Unknown Author")
|
||||||
self.uri: str = info.get("uri", "")
|
self.uri: str = info.get("uri", "")
|
||||||
self.identifier: str = info.get("identifier", "")
|
self.identifier: str = info.get("identifier", "")
|
||||||
self.isrc: Optional[str] = info.get("isrc", None)
|
self.isrc: str = info.get("isrc", "")
|
||||||
self.thumbnail: Optional[str] = info.get("thumbnail")
|
self.thumbnail: Optional[str] = info.get("thumbnail")
|
||||||
|
|
||||||
if self.uri and self.track_type is TrackType.YOUTUBE:
|
if self.uri and self.track_type is TrackType.YOUTUBE:
|
||||||
|
|
@ -98,6 +98,9 @@ class Track:
|
||||||
if not isinstance(other, Track):
|
if not isinstance(other, Track):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if self.ctx and other.ctx:
|
||||||
|
return other.track_id == self.track_id and other.ctx.message.id == self.ctx.message.id
|
||||||
|
|
||||||
return other.track_id == self.track_id
|
return other.track_id == self.track_id
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
|
|
|
||||||
257
pomice/player.py
257
pomice/player.py
|
|
@ -1,11 +1,8 @@
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from discord import Client
|
from discord import Client
|
||||||
|
|
@ -13,6 +10,8 @@ from discord import Guild
|
||||||
from discord import VoiceChannel
|
from discord import VoiceChannel
|
||||||
from discord import VoiceProtocol
|
from discord import VoiceProtocol
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
from discord.types.voice import GuildVoiceState
|
||||||
|
from discord.types.voice import VoiceServerUpdate
|
||||||
|
|
||||||
from . import events
|
from . import events
|
||||||
from .enums import SearchType
|
from .enums import SearchType
|
||||||
|
|
@ -25,16 +24,10 @@ from .exceptions import FilterTagInvalid
|
||||||
from .exceptions import TrackInvalidPosition
|
from .exceptions import TrackInvalidPosition
|
||||||
from .exceptions import TrackLoadError
|
from .exceptions import TrackLoadError
|
||||||
from .filters import Filter
|
from .filters import Filter
|
||||||
from .filters import Timescale
|
|
||||||
from .objects import Playlist
|
from .objects import Playlist
|
||||||
from .objects import Track
|
from .objects import Track
|
||||||
from .pool import Node
|
from .pool import Node
|
||||||
from .pool import NodePool
|
from .pool import NodePool
|
||||||
from pomice.utils import LavalinkVersion
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from discord.types.voice import VoiceServerUpdate
|
|
||||||
from discord.types.voice import GuildVoiceState
|
|
||||||
|
|
||||||
__all__ = ("Filters", "Player")
|
__all__ = ("Filters", "Player")
|
||||||
|
|
||||||
|
|
@ -79,35 +72,10 @@ class Filters:
|
||||||
if filter.tag == filter_tag:
|
if filter.tag == filter_tag:
|
||||||
del self._filters[index]
|
del self._filters[index]
|
||||||
|
|
||||||
def edit_filter(self, *, filter_tag: str, to_apply: Filter) -> None:
|
|
||||||
"""Edits a filter in the list of filters applied using its filter tag and replaces it with the new filter."""
|
|
||||||
if not any(f for f in self._filters if f.tag == filter_tag):
|
|
||||||
raise FilterTagInvalid("A filter with that tag was not found.")
|
|
||||||
|
|
||||||
for index, filter in enumerate(self._filters):
|
|
||||||
if filter.tag == filter_tag:
|
|
||||||
if not type(filter) == type(to_apply):
|
|
||||||
raise FilterInvalidArgument(
|
|
||||||
"Edited filter is not the same type as the current filter.",
|
|
||||||
)
|
|
||||||
if self._filters[index] == to_apply:
|
|
||||||
raise FilterInvalidArgument("Edited filter is the same as the current filter.")
|
|
||||||
|
|
||||||
if to_apply.tag != filter_tag:
|
|
||||||
raise FilterInvalidArgument(
|
|
||||||
"Edited filter tag is not the same as the current filter tag.",
|
|
||||||
)
|
|
||||||
|
|
||||||
self._filters[index] = to_apply
|
|
||||||
|
|
||||||
def has_filter(self, *, filter_tag: str) -> bool:
|
def has_filter(self, *, filter_tag: str) -> bool:
|
||||||
"""Checks if a filter exists in the list of filters using its filter tag"""
|
"""Checks if a filter exists in the list of filters using its filter tag"""
|
||||||
return any(f for f in self._filters if f.tag == filter_tag)
|
return any(f for f in self._filters if f.tag == filter_tag)
|
||||||
|
|
||||||
def has_filter_type(self, *, filter_type: Filter) -> bool:
|
|
||||||
"""Checks if any filters applied match the specified filter type."""
|
|
||||||
return any(f for f in self._filters if type(f) == type(filter_type))
|
|
||||||
|
|
||||||
def reset_filters(self) -> None:
|
def reset_filters(self) -> None:
|
||||||
"""Removes all filters from the list"""
|
"""Removes all filters from the list"""
|
||||||
self._filters = []
|
self._filters = []
|
||||||
|
|
@ -148,19 +116,16 @@ class Player(VoiceProtocol):
|
||||||
"_volume",
|
"_volume",
|
||||||
"_paused",
|
"_paused",
|
||||||
"_is_connected",
|
"_is_connected",
|
||||||
|
"_position",
|
||||||
"_last_position",
|
"_last_position",
|
||||||
"_last_update",
|
"_last_update",
|
||||||
"_ending_track",
|
"_ending_track",
|
||||||
"_log",
|
|
||||||
"_voice_state",
|
"_voice_state",
|
||||||
"_player_endpoint_uri",
|
"_player_endpoint_uri",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __call__(self, client: Client, channel: VoiceChannel) -> Player:
|
def __call__(self, client: Client, channel: VoiceChannel) -> "Player":
|
||||||
self.client = client
|
self.__init__(client, channel) # type: ignore
|
||||||
self.channel = channel
|
|
||||||
self._guild = channel.guild
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
@ -172,9 +137,9 @@ class Player(VoiceProtocol):
|
||||||
) -> None:
|
) -> None:
|
||||||
self.client: Client = client
|
self.client: Client = client
|
||||||
self.channel: VoiceChannel = channel
|
self.channel: VoiceChannel = channel
|
||||||
self._guild = channel.guild
|
|
||||||
|
|
||||||
self._bot: Client = client
|
self._bot: Client = client
|
||||||
|
self._guild: Guild = channel.guild
|
||||||
self._node: Node = node if node else NodePool.get_node()
|
self._node: Node = node if node else NodePool.get_node()
|
||||||
self._current: Optional[Track] = None
|
self._current: Optional[Track] = None
|
||||||
self._filters: Filters = Filters()
|
self._filters: Filters = Filters()
|
||||||
|
|
@ -182,10 +147,10 @@ class Player(VoiceProtocol):
|
||||||
self._paused: bool = False
|
self._paused: bool = False
|
||||||
self._is_connected: bool = False
|
self._is_connected: bool = False
|
||||||
|
|
||||||
|
self._position: int = 0
|
||||||
self._last_position: int = 0
|
self._last_position: int = 0
|
||||||
self._last_update: float = 0
|
self._last_update: float = 0
|
||||||
self._ending_track: Optional[Track] = None
|
self._ending_track: Optional[Track] = None
|
||||||
self._log = self._node._log
|
|
||||||
|
|
||||||
self._voice_state: dict = {}
|
self._voice_state: dict = {}
|
||||||
|
|
||||||
|
|
@ -200,12 +165,10 @@ class Player(VoiceProtocol):
|
||||||
@property
|
@property
|
||||||
def position(self) -> float:
|
def position(self) -> float:
|
||||||
"""Property which returns the player's position in a track in milliseconds"""
|
"""Property which returns the player's position in a track in milliseconds"""
|
||||||
if not self.is_playing:
|
if not self.is_playing or not self._current:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
current: Track = self._current # type: ignore
|
current = getattr(self._current, "original", self._current)
|
||||||
if current.original:
|
|
||||||
current = current.original
|
|
||||||
|
|
||||||
if self.is_paused:
|
if self.is_paused:
|
||||||
return min(self._last_position, current.length)
|
return min(self._last_position, current.length)
|
||||||
|
|
@ -213,27 +176,10 @@ class Player(VoiceProtocol):
|
||||||
difference = (time.time() * 1000) - self._last_update
|
difference = (time.time() * 1000) - self._last_update
|
||||||
position = self._last_position + difference
|
position = self._last_position + difference
|
||||||
|
|
||||||
return round(min(position, current.length))
|
if position > current.length:
|
||||||
|
|
||||||
@property
|
|
||||||
def rate(self) -> float:
|
|
||||||
"""Property which returns the player's current rate"""
|
|
||||||
if _filter := next((f for f in self._filters._filters if isinstance(f, Timescale)), None):
|
|
||||||
return _filter.speed or _filter.rate
|
|
||||||
return 1.0
|
|
||||||
|
|
||||||
@property
|
|
||||||
def adjusted_position(self) -> float:
|
|
||||||
"""Property which returns the player's position in a track in milliseconds adjusted for rate"""
|
|
||||||
return self.position / self.rate
|
|
||||||
|
|
||||||
@property
|
|
||||||
def adjusted_length(self) -> float:
|
|
||||||
"""Property which returns the player's track length in milliseconds adjusted for rate"""
|
|
||||||
if not self.is_playing:
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
return self.current.length / self.rate # type: ignore
|
return min(position, current.length)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_playing(self) -> bool:
|
def is_playing(self) -> bool:
|
||||||
|
|
@ -287,19 +233,12 @@ class Player(VoiceProtocol):
|
||||||
"""
|
"""
|
||||||
return self.guild.id not in self._node._players
|
return self.guild.id not in self._node._players
|
||||||
|
|
||||||
def _adjust_end_time(self) -> Optional[str]:
|
|
||||||
if self._node._version >= LavalinkVersion(3, 7, 5):
|
|
||||||
return None
|
|
||||||
|
|
||||||
return "0"
|
|
||||||
|
|
||||||
async def _update_state(self, data: dict) -> None:
|
async def _update_state(self, data: dict) -> None:
|
||||||
state: dict = data.get("state", {})
|
state: dict = data.get("state", {})
|
||||||
self._last_update = int(state.get("time", 0))
|
self._last_update = time.time() * 1000.0
|
||||||
self._is_connected = bool(state.get("connected"))
|
self._is_connected = bool(state.get("connected"))
|
||||||
self._last_position = int(state.get("position", 0))
|
position = state.get("position")
|
||||||
if self._log:
|
self._last_position = int(position) if position else 0
|
||||||
self._log.debug(f"Got player update state with data {state}")
|
|
||||||
|
|
||||||
async def _dispatch_voice_update(self, voice_data: Optional[Dict[str, Any]] = None) -> None:
|
async def _dispatch_voice_update(self, voice_data: Optional[Dict[str, Any]] = None) -> None:
|
||||||
if {"sessionId", "event"} != self._voice_state.keys():
|
if {"sessionId", "event"} != self._voice_state.keys():
|
||||||
|
|
@ -320,11 +259,6 @@ class Player(VoiceProtocol):
|
||||||
data={"voice": data},
|
data={"voice": data},
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"Dispatched voice update to {state['event']['endpoint']} with data {data}",
|
|
||||||
)
|
|
||||||
|
|
||||||
async def on_voice_server_update(self, data: VoiceServerUpdate) -> None:
|
async def on_voice_server_update(self, data: VoiceServerUpdate) -> None:
|
||||||
self._voice_state.update({"event": data})
|
self._voice_state.update({"event": data})
|
||||||
await self._dispatch_voice_update(self._voice_state)
|
await self._dispatch_voice_update(self._voice_state)
|
||||||
|
|
@ -339,10 +273,6 @@ class Player(VoiceProtocol):
|
||||||
return
|
return
|
||||||
|
|
||||||
channel = self.guild.get_channel(int(channel_id))
|
channel = self.guild.get_channel(int(channel_id))
|
||||||
|
|
||||||
if self.channel != channel:
|
|
||||||
self.channel = channel
|
|
||||||
|
|
||||||
if not channel:
|
if not channel:
|
||||||
await self.disconnect()
|
await self.disconnect()
|
||||||
self._voice_state.clear()
|
self._voice_state.clear()
|
||||||
|
|
@ -357,7 +287,7 @@ class Player(VoiceProtocol):
|
||||||
event_type: str = data["type"]
|
event_type: str = data["type"]
|
||||||
event: PomiceEvent = getattr(events, event_type)(data, self)
|
event: PomiceEvent = getattr(events, event_type)(data, self)
|
||||||
|
|
||||||
if isinstance(event, TrackEndEvent) and event.reason not in ("REPLACED", "replaced"):
|
if isinstance(event, TrackEndEvent) and event.reason != "REPLACED":
|
||||||
self._current = None
|
self._current = None
|
||||||
|
|
||||||
event.dispatch(self._bot)
|
event.dispatch(self._bot)
|
||||||
|
|
@ -365,38 +295,33 @@ class Player(VoiceProtocol):
|
||||||
if isinstance(event, TrackStartEvent):
|
if isinstance(event, TrackStartEvent):
|
||||||
self._ending_track = self._current
|
self._ending_track = self._current
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Dispatched event {data['type']} to player.")
|
|
||||||
|
|
||||||
async def _refresh_endpoint_uri(self, session_id: Optional[str]) -> None:
|
|
||||||
self._player_endpoint_uri = f"sessions/{session_id}/players"
|
|
||||||
|
|
||||||
async def _swap_node(self, *, new_node: Node) -> None:
|
async def _swap_node(self, *, new_node: Node) -> None:
|
||||||
|
data: dict = {
|
||||||
|
"position": self.position,
|
||||||
|
}
|
||||||
if self.current:
|
if self.current:
|
||||||
data: dict = {"position": self.position, "encodedTrack": self.current.track_id}
|
data["encodedTrack"] = self.current.track_id
|
||||||
|
|
||||||
del self._node._players[self._guild.id]
|
del self._node._players[self._guild.id]
|
||||||
self._node = new_node
|
self._node = new_node
|
||||||
self._node._players[self._guild.id] = self
|
self._node._players[self._guild.id] = self
|
||||||
# reassign uri to update session id
|
# reassign uri to update session id
|
||||||
await self._refresh_endpoint_uri(new_node._session_id)
|
self._player_endpoint_uri = f"sessions/{self._node._session_id}/players"
|
||||||
|
|
||||||
await self._dispatch_voice_update()
|
await self._dispatch_voice_update()
|
||||||
await self._node.send(
|
await self._node.send(
|
||||||
method="PATCH",
|
method="PATCH",
|
||||||
path=self._player_endpoint_uri,
|
path=self._player_endpoint_uri,
|
||||||
guild_id=self._guild.id,
|
guild_id=self._guild.id,
|
||||||
data=data or None,
|
data=data,
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Swapped all players to new node {new_node._identifier}.")
|
|
||||||
|
|
||||||
async def get_tracks(
|
async def get_tracks(
|
||||||
self,
|
self,
|
||||||
query: str,
|
query: str,
|
||||||
*,
|
*,
|
||||||
ctx: Optional[commands.Context] = None,
|
ctx: Optional[commands.Context] = None,
|
||||||
search_type: SearchType | None = SearchType.ytsearch,
|
search_type: SearchType = SearchType.ytsearch,
|
||||||
filters: Optional[List[Filter]] = None,
|
filters: Optional[List[Filter]] = None,
|
||||||
) -> Optional[Union[List[Track], Playlist]]:
|
) -> Optional[Union[List[Track], Playlist]]:
|
||||||
"""Fetches tracks from the node's REST api to parse into Lavalink.
|
"""Fetches tracks from the node's REST api to parse into Lavalink.
|
||||||
|
|
@ -413,21 +338,8 @@ class Player(VoiceProtocol):
|
||||||
"""
|
"""
|
||||||
return await self._node.get_tracks(query, ctx=ctx, search_type=search_type, filters=filters)
|
return await self._node.get_tracks(query, ctx=ctx, search_type=search_type, filters=filters)
|
||||||
|
|
||||||
async def build_track(self, identifier: str, ctx: Optional[commands.Context] = None) -> Track:
|
|
||||||
"""
|
|
||||||
Builds a track using a valid track identifier
|
|
||||||
|
|
||||||
You can also pass in a discord.py Context object to get a
|
|
||||||
Context object on the track it builds.
|
|
||||||
"""
|
|
||||||
|
|
||||||
return await self._node.build_track(identifier, ctx=ctx)
|
|
||||||
|
|
||||||
async def get_recommendations(
|
async def get_recommendations(
|
||||||
self,
|
self, *, track: Track, ctx: Optional[commands.Context] = None
|
||||||
*,
|
|
||||||
track: Track,
|
|
||||||
ctx: Optional[commands.Context] = None,
|
|
||||||
) -> Optional[Union[List[Track], Playlist]]:
|
) -> Optional[Union[List[Track], Playlist]]:
|
||||||
"""
|
"""
|
||||||
Gets recommendations from either YouTube or Spotify.
|
Gets recommendations from either YouTube or Spotify.
|
||||||
|
|
@ -437,12 +349,7 @@ class Player(VoiceProtocol):
|
||||||
return await self._node.get_recommendations(track=track, ctx=ctx)
|
return await self._node.get_recommendations(track=track, ctx=ctx)
|
||||||
|
|
||||||
async def connect(
|
async def connect(
|
||||||
self,
|
self, *, timeout: float, reconnect: bool, self_deaf: bool = False, self_mute: bool = False
|
||||||
*,
|
|
||||||
timeout: float,
|
|
||||||
reconnect: bool,
|
|
||||||
self_deaf: bool = False,
|
|
||||||
self_mute: bool = False,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
await self.guild.change_voice_state(
|
await self.guild.change_voice_state(
|
||||||
channel=self.channel,
|
channel=self.channel,
|
||||||
|
|
@ -462,9 +369,6 @@ class Player(VoiceProtocol):
|
||||||
data={"encodedTrack": None},
|
data={"encodedTrack": None},
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Player has been stopped.")
|
|
||||||
|
|
||||||
async def disconnect(self, *, force: bool = False) -> None:
|
async def disconnect(self, *, force: bool = False) -> None:
|
||||||
"""Disconnects the player from voice."""
|
"""Disconnects the player from voice."""
|
||||||
try:
|
try:
|
||||||
|
|
@ -472,7 +376,7 @@ class Player(VoiceProtocol):
|
||||||
finally:
|
finally:
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
self._is_connected = False
|
self._is_connected = False
|
||||||
self.channel = None # type: ignore
|
del self.channel
|
||||||
|
|
||||||
async def destroy(self) -> None:
|
async def destroy(self) -> None:
|
||||||
"""Disconnects and destroys the player, and runs internal cleanup."""
|
"""Disconnects and destroys the player, and runs internal cleanup."""
|
||||||
|
|
@ -481,34 +385,22 @@ class Player(VoiceProtocol):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# 'NoneType' has no attribute '_get_voice_client_key' raised by self.cleanup() ->
|
# 'NoneType' has no attribute '_get_voice_client_key' raised by self.cleanup() ->
|
||||||
# assume we're already disconnected and cleaned up
|
# assume we're already disconnected and cleaned up
|
||||||
assert self.channel is None and not self.is_connected
|
assert not self.is_connected and not self.channel
|
||||||
|
|
||||||
self._node._players.pop(self.guild.id)
|
self._node._players.pop(self.guild.id)
|
||||||
if self.node.is_connected:
|
await self._node.send(
|
||||||
await self._node.send(
|
method="DELETE",
|
||||||
method="DELETE",
|
path=self._player_endpoint_uri,
|
||||||
path=self._player_endpoint_uri,
|
guild_id=self._guild.id,
|
||||||
guild_id=self._guild.id,
|
)
|
||||||
)
|
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug("Player has been destroyed.")
|
|
||||||
|
|
||||||
async def play(
|
async def play(
|
||||||
self,
|
self, track: Track, *, start: int = 0, end: int = 0, ignore_if_playing: bool = False
|
||||||
track: Track,
|
|
||||||
*,
|
|
||||||
start: int = 0,
|
|
||||||
end: int = 0,
|
|
||||||
ignore_if_playing: bool = False,
|
|
||||||
) -> Track:
|
) -> Track:
|
||||||
"""Plays a track. If a Spotify track is passed in, it will be handled accordingly."""
|
"""Plays a track. If a Spotify track is passed in, it will be handled accordingly."""
|
||||||
|
|
||||||
if not track._search_type:
|
|
||||||
track.original = track
|
|
||||||
|
|
||||||
# Make sure we've never searched the track before
|
# Make sure we've never searched the track before
|
||||||
if track._search_type and track.original is None:
|
if track.original is None:
|
||||||
# First lets try using the tracks ISRC, every track has one (hopefully)
|
# First lets try using the tracks ISRC, every track has one (hopefully)
|
||||||
try:
|
try:
|
||||||
if not track.isrc:
|
if not track.isrc:
|
||||||
|
|
@ -538,7 +430,7 @@ class Player(VoiceProtocol):
|
||||||
data = {
|
data = {
|
||||||
"encodedTrack": search.track_id,
|
"encodedTrack": search.track_id,
|
||||||
"position": str(start),
|
"position": str(start),
|
||||||
"endTime": self._adjust_end_time(),
|
"endTime": str(track.length),
|
||||||
}
|
}
|
||||||
track.original = search
|
track.original = search
|
||||||
track.track_id = search.track_id
|
track.track_id = search.track_id
|
||||||
|
|
@ -547,7 +439,7 @@ class Player(VoiceProtocol):
|
||||||
data = {
|
data = {
|
||||||
"encodedTrack": track.track_id,
|
"encodedTrack": track.track_id,
|
||||||
"position": str(start),
|
"position": str(start),
|
||||||
"endTime": self._adjust_end_time(),
|
"endTime": str(track.length),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Lets set the current track before we play it so any
|
# Lets set the current track before we play it so any
|
||||||
|
|
@ -572,10 +464,10 @@ class Player(VoiceProtocol):
|
||||||
for filter in track.filters:
|
for filter in track.filters:
|
||||||
await self.add_filter(_filter=filter)
|
await self.add_filter(_filter=filter)
|
||||||
|
|
||||||
# Lavalink v3.7.5 changed the way the end time parameter works
|
# Lavalink v4 changed the way the end time parameter works
|
||||||
# so now the end time cannot be zero.
|
# so now the end time cannot be zero.
|
||||||
# If it isnt zero, it'll be set to None.
|
# If it isnt zero, it'll match the length of the track,
|
||||||
# Otherwise, it'll be set here:
|
# otherwise itll be set here:
|
||||||
|
|
||||||
if end > 0:
|
if end > 0:
|
||||||
data["endTime"] = str(end)
|
data["endTime"] = str(end)
|
||||||
|
|
@ -588,11 +480,6 @@ class Player(VoiceProtocol):
|
||||||
query=f"noReplace={ignore_if_playing}",
|
query=f"noReplace={ignore_if_playing}",
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"Playing {track.title} from uri {track.uri} with a length of {track.length}",
|
|
||||||
)
|
|
||||||
|
|
||||||
return self._current
|
return self._current
|
||||||
|
|
||||||
async def seek(self, position: float) -> float:
|
async def seek(self, position: float) -> float:
|
||||||
|
|
@ -611,10 +498,7 @@ class Player(VoiceProtocol):
|
||||||
guild_id=self._guild.id,
|
guild_id=self._guild.id,
|
||||||
data={"position": position},
|
data={"position": position},
|
||||||
)
|
)
|
||||||
|
return self._position
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Seeking to {position}.")
|
|
||||||
return self.position
|
|
||||||
|
|
||||||
async def set_pause(self, pause: bool) -> bool:
|
async def set_pause(self, pause: bool) -> bool:
|
||||||
"""Sets the pause state of the currently playing track."""
|
"""Sets the pause state of the currently playing track."""
|
||||||
|
|
@ -625,9 +509,6 @@ class Player(VoiceProtocol):
|
||||||
data={"paused": pause},
|
data={"paused": pause},
|
||||||
)
|
)
|
||||||
self._paused = pause
|
self._paused = pause
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Player has been {'paused' if pause else 'resumed'}.")
|
|
||||||
return self._paused
|
return self._paused
|
||||||
|
|
||||||
async def set_volume(self, volume: int) -> int:
|
async def set_volume(self, volume: int) -> int:
|
||||||
|
|
@ -639,20 +520,8 @@ class Player(VoiceProtocol):
|
||||||
data={"volume": volume},
|
data={"volume": volume},
|
||||||
)
|
)
|
||||||
self._volume = volume
|
self._volume = volume
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Player volume has been adjusted to {volume}")
|
|
||||||
return self._volume
|
return self._volume
|
||||||
|
|
||||||
async def move_to(self, channel: VoiceChannel) -> None:
|
|
||||||
"""Moves the player to a new voice channel."""
|
|
||||||
|
|
||||||
await self.guild.change_voice_state(channel=channel)
|
|
||||||
|
|
||||||
self.channel = channel
|
|
||||||
|
|
||||||
await self._dispatch_voice_update()
|
|
||||||
|
|
||||||
async def add_filter(self, _filter: Filter, fast_apply: bool = False) -> Filters:
|
async def add_filter(self, _filter: Filter, fast_apply: bool = False) -> Filters:
|
||||||
"""Adds a filter to the player. Takes a pomice.Filter object.
|
"""Adds a filter to the player. Takes a pomice.Filter object.
|
||||||
This will only work if you are using a version of Lavalink that supports filters.
|
This will only work if you are using a version of Lavalink that supports filters.
|
||||||
|
|
@ -669,12 +538,7 @@ class Player(VoiceProtocol):
|
||||||
guild_id=self._guild.id,
|
guild_id=self._guild.id,
|
||||||
data={"filters": payload},
|
data={"filters": payload},
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Filter has been applied to player with tag {_filter.tag}")
|
|
||||||
if fast_apply:
|
if fast_apply:
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Fast apply passed, now applying filter instantly.")
|
|
||||||
await self.seek(self.position)
|
await self.seek(self.position)
|
||||||
|
|
||||||
return self._filters
|
return self._filters
|
||||||
|
|
@ -695,44 +559,7 @@ class Player(VoiceProtocol):
|
||||||
guild_id=self._guild.id,
|
guild_id=self._guild.id,
|
||||||
data={"filters": payload},
|
data={"filters": payload},
|
||||||
)
|
)
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Filter has been removed from player with tag {filter_tag}")
|
|
||||||
if fast_apply:
|
if fast_apply:
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Fast apply passed, now removing filter instantly.")
|
|
||||||
await self.seek(self.position)
|
|
||||||
|
|
||||||
return self._filters
|
|
||||||
|
|
||||||
async def edit_filter(
|
|
||||||
self,
|
|
||||||
*,
|
|
||||||
filter_tag: str,
|
|
||||||
edited_filter: Filter,
|
|
||||||
fast_apply: bool = False,
|
|
||||||
) -> Filters:
|
|
||||||
"""Edits a filter from the player using its filter tag and a new filter of the same type.
|
|
||||||
The filter to be replaced must have the same tag as the one you are replacing it with.
|
|
||||||
This will only work if you are using a version of Lavalink that supports filters.
|
|
||||||
|
|
||||||
If you would like for the filter to apply instantly, set the `fast_apply` arg to `True`.
|
|
||||||
|
|
||||||
(You must have a song playing in order for `fast_apply` to work.)
|
|
||||||
"""
|
|
||||||
|
|
||||||
self._filters.edit_filter(filter_tag=filter_tag, to_apply=edited_filter)
|
|
||||||
payload = self._filters.get_all_payloads()
|
|
||||||
await self._node.send(
|
|
||||||
method="PATCH",
|
|
||||||
path=self._player_endpoint_uri,
|
|
||||||
guild_id=self._guild.id,
|
|
||||||
data={"filters": payload},
|
|
||||||
)
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Filter with tag {filter_tag} has been edited to {edited_filter!r}")
|
|
||||||
if fast_apply:
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Fast apply passed, now editing filter instantly.")
|
|
||||||
await self.seek(self.position)
|
await self.seek(self.position)
|
||||||
|
|
||||||
return self._filters
|
return self._filters
|
||||||
|
|
@ -756,10 +583,6 @@ class Player(VoiceProtocol):
|
||||||
guild_id=self._guild.id,
|
guild_id=self._guild.id,
|
||||||
data={"filters": {}},
|
data={"filters": {}},
|
||||||
)
|
)
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"All filters have been removed from player.")
|
|
||||||
|
|
||||||
if fast_apply:
|
if fast_apply:
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Fast apply passed, now removing all filters instantly.")
|
|
||||||
await self.seek(self.position)
|
await self.seek(self.position)
|
||||||
|
|
|
||||||
519
pomice/pool.py
519
pomice/pool.py
|
|
@ -1,12 +1,8 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import time
|
|
||||||
from os import path
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
@ -17,24 +13,14 @@ from typing import Union
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import orjson as json
|
|
||||||
from discord import Client
|
from discord import Client
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord.utils import MISSING
|
|
||||||
|
|
||||||
try:
|
|
||||||
from websockets.legacy import client # websockets >= 10.0
|
|
||||||
except ImportError:
|
|
||||||
import websockets.client as client # websockets < 10.0 # type: ignore
|
|
||||||
|
|
||||||
from websockets import exceptions
|
|
||||||
from websockets import typing as wstype
|
|
||||||
|
|
||||||
from . import __version__
|
from . import __version__
|
||||||
from . import applemusic
|
from . import applemusic
|
||||||
from . import spotify
|
from . import spotify
|
||||||
from .enums import *
|
from .enums import *
|
||||||
from .enums import LogLevel
|
from .exceptions import AppleMusicNotEnabled
|
||||||
from .exceptions import InvalidSpotifyClientAuthorization
|
from .exceptions import InvalidSpotifyClientAuthorization
|
||||||
from .exceptions import LavalinkVersionIncompatible
|
from .exceptions import LavalinkVersionIncompatible
|
||||||
from .exceptions import NodeConnectionFailure
|
from .exceptions import NodeConnectionFailure
|
||||||
|
|
@ -48,7 +34,6 @@ from .objects import Playlist
|
||||||
from .objects import Track
|
from .objects import Track
|
||||||
from .routeplanner import RoutePlanner
|
from .routeplanner import RoutePlanner
|
||||||
from .utils import ExponentialBackoff
|
from .utils import ExponentialBackoff
|
||||||
from .utils import LavalinkVersion
|
|
||||||
from .utils import NodeStats
|
from .utils import NodeStats
|
||||||
from .utils import Ping
|
from .utils import Ping
|
||||||
|
|
||||||
|
|
@ -60,8 +45,6 @@ __all__ = (
|
||||||
"NodePool",
|
"NodePool",
|
||||||
)
|
)
|
||||||
|
|
||||||
VERSION_REGEX = re.compile(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:[a-zA-Z0-9_-]+)?")
|
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
"""The base class for a node.
|
"""The base class for a node.
|
||||||
|
|
@ -79,11 +62,8 @@ class Node:
|
||||||
"_password",
|
"_password",
|
||||||
"_identifier",
|
"_identifier",
|
||||||
"_heartbeat",
|
"_heartbeat",
|
||||||
"_resume_key",
|
|
||||||
"_resume_timeout",
|
|
||||||
"_secure",
|
"_secure",
|
||||||
"_fallback",
|
"_fallback",
|
||||||
"_log_level",
|
|
||||||
"_websocket_uri",
|
"_websocket_uri",
|
||||||
"_rest_uri",
|
"_rest_uri",
|
||||||
"_session",
|
"_session",
|
||||||
|
|
@ -100,7 +80,6 @@ class Node:
|
||||||
"_spotify_client",
|
"_spotify_client",
|
||||||
"_apple_music_client",
|
"_apple_music_client",
|
||||||
"_route_planner",
|
"_route_planner",
|
||||||
"_log",
|
|
||||||
"_stats",
|
"_stats",
|
||||||
"available",
|
"available",
|
||||||
)
|
)
|
||||||
|
|
@ -115,20 +94,14 @@ class Node:
|
||||||
password: str,
|
password: str,
|
||||||
identifier: str,
|
identifier: str,
|
||||||
secure: bool = False,
|
secure: bool = False,
|
||||||
heartbeat: int = 120,
|
heartbeat: int = 30,
|
||||||
resume_key: Optional[str] = None,
|
|
||||||
resume_timeout: int = 60,
|
|
||||||
loop: Optional[asyncio.AbstractEventLoop] = None,
|
loop: Optional[asyncio.AbstractEventLoop] = None,
|
||||||
session: Optional[aiohttp.ClientSession] = None,
|
session: Optional[aiohttp.ClientSession] = None,
|
||||||
spotify_client_id: Optional[str] = None,
|
spotify_client_id: Optional[int] = None,
|
||||||
spotify_client_secret: Optional[str] = None,
|
spotify_client_secret: Optional[str] = None,
|
||||||
apple_music: bool = False,
|
apple_music: bool = False,
|
||||||
fallback: bool = False,
|
fallback: bool = False,
|
||||||
logger: Optional[logging.Logger] = None,
|
|
||||||
):
|
):
|
||||||
if not isinstance(port, int):
|
|
||||||
raise TypeError("Port must be an integer")
|
|
||||||
|
|
||||||
self._bot: commands.Bot = bot
|
self._bot: commands.Bot = bot
|
||||||
self._host: str = host
|
self._host: str = host
|
||||||
self._port: int = port
|
self._port: int = port
|
||||||
|
|
@ -136,8 +109,6 @@ class Node:
|
||||||
self._password: str = password
|
self._password: str = password
|
||||||
self._identifier: str = identifier
|
self._identifier: str = identifier
|
||||||
self._heartbeat: int = heartbeat
|
self._heartbeat: int = heartbeat
|
||||||
self._resume_key: Optional[str] = resume_key
|
|
||||||
self._resume_timeout: int = resume_timeout
|
|
||||||
self._secure: bool = secure
|
self._secure: bool = secure
|
||||||
self._fallback: bool = fallback
|
self._fallback: bool = fallback
|
||||||
|
|
||||||
|
|
@ -146,15 +117,14 @@ class Node:
|
||||||
|
|
||||||
self._session: aiohttp.ClientSession = session # type: ignore
|
self._session: aiohttp.ClientSession = session # type: ignore
|
||||||
self._loop: asyncio.AbstractEventLoop = loop or asyncio.get_event_loop()
|
self._loop: asyncio.AbstractEventLoop = loop or asyncio.get_event_loop()
|
||||||
self._websocket: client.WebSocketClientProtocol
|
self._websocket: aiohttp.ClientWebSocketResponse
|
||||||
self._task: asyncio.Task = None # type: ignore
|
self._task: asyncio.Task = None # type: ignore
|
||||||
|
|
||||||
self._session_id: Optional[str] = None
|
self._session_id: Optional[str] = None
|
||||||
self._available: bool = False
|
self._available: bool = False
|
||||||
self._version: LavalinkVersion = LavalinkVersion(0, 0, 0)
|
self._version: int = 0
|
||||||
|
|
||||||
self._route_planner = RoutePlanner(self)
|
self._route_planner = RoutePlanner(self)
|
||||||
self._log = logger
|
|
||||||
|
|
||||||
if not self._bot.user:
|
if not self._bot.user:
|
||||||
raise NodeCreationError("Bot user is not ready yet.")
|
raise NodeCreationError("Bot user is not ready yet.")
|
||||||
|
|
@ -169,14 +139,13 @@ class Node:
|
||||||
|
|
||||||
self._players: Dict[int, Player] = {}
|
self._players: Dict[int, Player] = {}
|
||||||
|
|
||||||
self._spotify_client: Optional[spotify.Client] = None
|
self._spotify_client_id: Optional[int] = spotify_client_id
|
||||||
self._apple_music_client: Optional[applemusic.Client] = None
|
|
||||||
|
|
||||||
self._spotify_client_id: Optional[str] = spotify_client_id
|
|
||||||
self._spotify_client_secret: Optional[str] = spotify_client_secret
|
self._spotify_client_secret: Optional[str] = spotify_client_secret
|
||||||
|
|
||||||
|
self._apple_music_client: Optional[applemusic.Client] = None
|
||||||
|
|
||||||
if self._spotify_client_id and self._spotify_client_secret:
|
if self._spotify_client_id and self._spotify_client_secret:
|
||||||
self._spotify_client = spotify.Client(
|
self._spotify_client: spotify.Client = spotify.Client(
|
||||||
self._spotify_client_id,
|
self._spotify_client_id,
|
||||||
self._spotify_client_secret,
|
self._spotify_client_secret,
|
||||||
)
|
)
|
||||||
|
|
@ -215,7 +184,7 @@ class Node:
|
||||||
@property
|
@property
|
||||||
def player_count(self) -> int:
|
def player_count(self) -> int:
|
||||||
"""Property which returns how many players are connected to this node"""
|
"""Property which returns how many players are connected to this node"""
|
||||||
return len(self.players.values())
|
return len(self.players)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pool(self) -> Type[NodePool]:
|
def pool(self) -> Type[NodePool]:
|
||||||
|
|
@ -232,44 +201,6 @@ class Node:
|
||||||
"""Alias for `Node.latency`, returns the latency of the node"""
|
"""Alias for `Node.latency`, returns the latency of the node"""
|
||||||
return self.latency
|
return self.latency
|
||||||
|
|
||||||
async def _handle_version_check(self, version: str) -> None:
|
|
||||||
if version.endswith("-SNAPSHOT"):
|
|
||||||
# we're just gonna assume all snapshot versions correlate with v4
|
|
||||||
self._version = LavalinkVersion(major=4, minor=0, fix=0)
|
|
||||||
return
|
|
||||||
|
|
||||||
_version_rx = VERSION_REGEX.match(version)
|
|
||||||
if not _version_rx:
|
|
||||||
self._available = False
|
|
||||||
raise LavalinkVersionIncompatible(
|
|
||||||
"The Lavalink version you're using is incompatible. "
|
|
||||||
"Lavalink version 3.7.0 or above is required to use this library.",
|
|
||||||
)
|
|
||||||
|
|
||||||
_version_groups = _version_rx.groups()
|
|
||||||
major, minor, fix = (
|
|
||||||
int(_version_groups[0] or 0),
|
|
||||||
int(_version_groups[1] or 0),
|
|
||||||
int(_version_groups[2] or 0),
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Parsed Lavalink version: {major}.{minor}.{fix}")
|
|
||||||
self._version = LavalinkVersion(major=major, minor=minor, fix=fix)
|
|
||||||
if self._version < LavalinkVersion(3, 7, 0):
|
|
||||||
self._available = False
|
|
||||||
raise LavalinkVersionIncompatible(
|
|
||||||
"The Lavalink version you're using is incompatible. "
|
|
||||||
"Lavalink version 3.7.0 or above is required to use this library.",
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _set_ext_client_session(self, session: aiohttp.ClientSession) -> None:
|
|
||||||
if self._spotify_client:
|
|
||||||
await self._spotify_client._set_session(session=session)
|
|
||||||
|
|
||||||
if self._apple_music_client:
|
|
||||||
await self._apple_music_client._set_session(session=session)
|
|
||||||
|
|
||||||
async def _update_handler(self, data: dict) -> None:
|
async def _update_handler(self, data: dict) -> None:
|
||||||
await self._bot.wait_until_ready()
|
await self._bot.wait_until_ready()
|
||||||
|
|
||||||
|
|
@ -304,59 +235,25 @@ class Node:
|
||||||
|
|
||||||
await self.disconnect()
|
await self.disconnect()
|
||||||
|
|
||||||
async def _configure_resuming(self) -> None:
|
|
||||||
if not self._resume_key:
|
|
||||||
return
|
|
||||||
|
|
||||||
data: Dict[str, Union[int, str, bool]] = {"timeout": self._resume_timeout}
|
|
||||||
|
|
||||||
if self._version.major == 3:
|
|
||||||
data["resumingKey"] = self._resume_key
|
|
||||||
elif self._version.major == 4:
|
|
||||||
if self._log:
|
|
||||||
self._log.warning("Using a resume key with Lavalink v4 is deprecated.")
|
|
||||||
data["resuming"] = True
|
|
||||||
|
|
||||||
await self.send(
|
|
||||||
method="PATCH",
|
|
||||||
path=f"sessions/{self._session_id}",
|
|
||||||
include_version=True,
|
|
||||||
data=data,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _listen(self) -> None:
|
async def _listen(self) -> None:
|
||||||
|
backoff = ExponentialBackoff(base=7)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
msg = await self._websocket.receive()
|
||||||
msg = await self._websocket.recv()
|
if msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.CLOSING):
|
||||||
data = json.loads(msg)
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Recieved raw websocket message {msg}")
|
|
||||||
self._loop.create_task(self._handle_ws_msg(data=data))
|
|
||||||
except exceptions.ConnectionClosed:
|
|
||||||
if self.player_count > 0:
|
|
||||||
for _player in self.players.values():
|
|
||||||
self._loop.create_task(_player.destroy())
|
|
||||||
|
|
||||||
if self._fallback:
|
if self._fallback:
|
||||||
self._loop.create_task(self._handle_node_switch())
|
await self._handle_node_switch()
|
||||||
|
|
||||||
self._loop.create_task(self._websocket.close())
|
|
||||||
|
|
||||||
backoff = ExponentialBackoff(base=7)
|
|
||||||
retry = backoff.delay()
|
retry = backoff.delay()
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"Retrying connection to Node {self._identifier} in {retry} secs",
|
|
||||||
)
|
|
||||||
await asyncio.sleep(retry)
|
await asyncio.sleep(retry)
|
||||||
|
|
||||||
if not self.is_connected:
|
if not self.is_connected:
|
||||||
self._loop.create_task(self.connect(reconnect=True))
|
self._loop.create_task(self.connect())
|
||||||
|
else:
|
||||||
|
self._loop.create_task(self._handle_payload(msg.json()))
|
||||||
|
|
||||||
async def _handle_ws_msg(self, data: dict) -> None:
|
async def _handle_payload(self, data: dict) -> None:
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Recieved raw payload from Node {self._identifier} with data {data}")
|
|
||||||
op = data.get("op", None)
|
op = data.get("op", None)
|
||||||
|
if not op:
|
||||||
|
return
|
||||||
|
|
||||||
if op == "stats":
|
if op == "stats":
|
||||||
self._stats = NodeStats(data)
|
self._stats = NodeStats(data)
|
||||||
|
|
@ -364,20 +261,21 @@ class Node:
|
||||||
|
|
||||||
if op == "ready":
|
if op == "ready":
|
||||||
self._session_id = data["sessionId"]
|
self._session_id = data["sessionId"]
|
||||||
await self._configure_resuming()
|
|
||||||
|
|
||||||
if not "guildId" in data:
|
if not "guildId" in data:
|
||||||
return
|
return
|
||||||
|
|
||||||
player: Optional[Player] = self._players.get(int(data["guildId"]))
|
player = self._players.get(int(data["guildId"]))
|
||||||
if not player:
|
if not player:
|
||||||
return
|
return
|
||||||
|
|
||||||
if op == "event":
|
if op == "event":
|
||||||
return await player._dispatch_event(data)
|
await player._dispatch_event(data)
|
||||||
|
return
|
||||||
|
|
||||||
if op == "playerUpdate":
|
if op == "playerUpdate":
|
||||||
return await player._update_state(data)
|
await player._update_state(data)
|
||||||
|
return
|
||||||
|
|
||||||
async def send(
|
async def send(
|
||||||
self,
|
self,
|
||||||
|
|
@ -396,126 +294,85 @@ class Node:
|
||||||
|
|
||||||
uri: str = (
|
uri: str = (
|
||||||
f"{self._rest_uri}/"
|
f"{self._rest_uri}/"
|
||||||
f'{f"v{self._version.major}/" if include_version else ""}'
|
f'{f"v{self._version}/" if include_version else ""}'
|
||||||
f"{path}"
|
f"{path}"
|
||||||
f'{f"/{guild_id}" if guild_id else ""}'
|
f'{f"/{guild_id}" if guild_id else ""}'
|
||||||
f'{f"?{query}" if query else ""}'
|
f'{f"?{query}" if query else ""}'
|
||||||
)
|
)
|
||||||
|
|
||||||
resp = await self._session.request(
|
async with self._session.request(
|
||||||
method=method,
|
method=method,
|
||||||
url=uri,
|
url=uri,
|
||||||
headers=self._headers,
|
headers=self._headers,
|
||||||
json=data or {},
|
json=data or {},
|
||||||
)
|
) as resp:
|
||||||
if self._log:
|
if resp.status >= 300:
|
||||||
self._log.debug(
|
resp_data: dict = await resp.json()
|
||||||
f"Making REST request to Node {self._identifier} with method {method} to {uri}",
|
raise NodeRestException(
|
||||||
)
|
f'Error fetching from Lavalink REST api: {resp.status} {resp.reason}: {resp_data["message"]}',
|
||||||
if resp.status >= 300:
|
|
||||||
resp_data: dict = await resp.json()
|
|
||||||
raise NodeRestException(
|
|
||||||
f'Error from Node {self._identifier} fetching from Lavalink REST api: {resp.status} {resp.reason}: {resp_data["message"]}',
|
|
||||||
)
|
|
||||||
|
|
||||||
if method == "DELETE" or resp.status == 204:
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"REST request to Node {self._identifier} with method {method} to {uri} completed sucessfully and returned no data.",
|
|
||||||
)
|
)
|
||||||
return await resp.json(content_type=None)
|
|
||||||
|
|
||||||
if resp.content_type == "text/plain":
|
if method == "DELETE" or resp.status == 204:
|
||||||
if self._log:
|
return await resp.json(content_type=None)
|
||||||
self._log.debug(
|
|
||||||
f"REST request to Node {self._identifier} with method {method} to {uri} completed sucessfully and returned text with body {await resp.text()}",
|
|
||||||
)
|
|
||||||
return await resp.text()
|
|
||||||
|
|
||||||
if self._log:
|
if resp.content_type == "text/plain":
|
||||||
self._log.debug(
|
return await resp.text()
|
||||||
f"REST request to Node {self._identifier} with method {method} to {uri} completed sucessfully and returned JSON with body {await resp.json()}",
|
|
||||||
)
|
return await resp.json()
|
||||||
return await resp.json()
|
|
||||||
|
|
||||||
def get_player(self, guild_id: int) -> Optional[Player]:
|
def get_player(self, guild_id: int) -> Optional[Player]:
|
||||||
"""Takes a guild ID as a parameter. Returns a pomice Player object or None."""
|
"""Takes a guild ID as a parameter. Returns a pomice Player object or None."""
|
||||||
return self._players.get(guild_id, None)
|
return self._players.get(guild_id, None)
|
||||||
|
|
||||||
async def connect(self, *, reconnect: bool = False) -> Node:
|
async def connect(self) -> "Node":
|
||||||
"""Initiates a connection with a Lavalink node and adds it to the node pool."""
|
"""Initiates a connection with a Lavalink node and adds it to the node pool."""
|
||||||
await self._bot.wait_until_ready()
|
await self._bot.wait_until_ready()
|
||||||
|
|
||||||
start = time.perf_counter()
|
|
||||||
|
|
||||||
if not self._session:
|
if not self._session:
|
||||||
# Configure connection pooling for optimal concurrent request performance
|
self._session = aiohttp.ClientSession()
|
||||||
connector = aiohttp.TCPConnector(
|
|
||||||
limit=100, # Total connection limit
|
|
||||||
limit_per_host=30, # Per-host connection limit
|
|
||||||
ttl_dns_cache=300, # DNS cache TTL in seconds
|
|
||||||
)
|
|
||||||
timeout = aiohttp.ClientTimeout(total=30, connect=10)
|
|
||||||
self._session = aiohttp.ClientSession(
|
|
||||||
connector=connector,
|
|
||||||
timeout=timeout,
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not reconnect:
|
version: str = await self.send(
|
||||||
version: str = await self.send(
|
method="GET",
|
||||||
method="GET",
|
path="version",
|
||||||
path="version",
|
ignore_if_available=True,
|
||||||
ignore_if_available=True,
|
include_version=False,
|
||||||
include_version=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._handle_version_check(version=version)
|
|
||||||
await self._set_ext_client_session(session=self._session)
|
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"Version check from Node {self._identifier} successful. Returned version {version}",
|
|
||||||
)
|
|
||||||
|
|
||||||
self._websocket = await client.connect( # type: ignore
|
|
||||||
f"{self._websocket_uri}/v{self._version.major}/websocket",
|
|
||||||
extra_headers=self._headers,
|
|
||||||
ping_interval=self._heartbeat,
|
|
||||||
)
|
)
|
||||||
|
version = version.replace(".", "")
|
||||||
if reconnect:
|
if not version.endswith("-SNAPSHOT") and int(version) < 370:
|
||||||
if self._log:
|
self._available = False
|
||||||
self._log.debug(f"Trying to reconnect to Node {self._identifier}...")
|
raise LavalinkVersionIncompatible(
|
||||||
if self.player_count:
|
"The Lavalink version you're using is incompatible. "
|
||||||
for player in self.players.values():
|
"Lavalink version 3.7.0 or above is required to use this library.",
|
||||||
await player._refresh_endpoint_uri(self._session_id)
|
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"Node {self._identifier} successfully connected to websocket using {self._websocket_uri}/v{self._version.major}/websocket",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if version.endswith("-SNAPSHOT"):
|
||||||
|
# we're just gonna assume all snapshot versions correlate with v4
|
||||||
|
self._version = 4
|
||||||
|
else:
|
||||||
|
self._version = int(version[:1])
|
||||||
|
|
||||||
|
self._websocket = await self._session.ws_connect(
|
||||||
|
f"{self._websocket_uri}/v{self._version}/websocket",
|
||||||
|
headers=self._headers,
|
||||||
|
heartbeat=self._heartbeat,
|
||||||
|
)
|
||||||
|
|
||||||
if not self._task:
|
if not self._task:
|
||||||
self._task = self._loop.create_task(self._listen())
|
self._task = self._loop.create_task(self._listen())
|
||||||
|
|
||||||
self._available = True
|
self._available = True
|
||||||
|
|
||||||
end = time.perf_counter()
|
|
||||||
|
|
||||||
if self._log:
|
|
||||||
self._log.info(f"Connected to node {self._identifier}. Took {end - start:.3f}s")
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
except (aiohttp.ClientConnectorError, OSError, ConnectionRefusedError):
|
except (aiohttp.ClientConnectorError, ConnectionRefusedError):
|
||||||
raise NodeConnectionFailure(
|
raise NodeConnectionFailure(
|
||||||
f"The connection to node '{self._identifier}' failed.",
|
f"The connection to node '{self._identifier}' failed.",
|
||||||
) from None
|
) from None
|
||||||
except exceptions.InvalidHandshake:
|
except aiohttp.WSServerHandshakeError:
|
||||||
raise NodeConnectionFailure(
|
raise NodeConnectionFailure(
|
||||||
f"The password for node '{self._identifier}' is invalid.",
|
f"The password for node '{self._identifier}' is invalid.",
|
||||||
) from None
|
) from None
|
||||||
except exceptions.InvalidURI:
|
except aiohttp.InvalidURL:
|
||||||
raise NodeConnectionFailure(
|
raise NodeConnectionFailure(
|
||||||
f"The URI for node '{self._identifier}' is invalid.",
|
f"The URI for node '{self._identifier}' is invalid.",
|
||||||
) from None
|
) from None
|
||||||
|
|
@ -524,29 +381,21 @@ class Node:
|
||||||
"""Disconnects a connected Lavalink node and removes it from the node pool.
|
"""Disconnects a connected Lavalink node and removes it from the node pool.
|
||||||
This also destroys any players connected to the node.
|
This also destroys any players connected to the node.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
start = time.perf_counter()
|
|
||||||
|
|
||||||
for player in self.players.copy().values():
|
for player in self.players.copy().values():
|
||||||
await player.destroy()
|
await player.destroy()
|
||||||
if self._log:
|
|
||||||
self._log.debug("All players disconnected from node.")
|
|
||||||
|
|
||||||
await self._websocket.close()
|
await self._websocket.close()
|
||||||
await self._session.close()
|
await self._session.close()
|
||||||
if self._log:
|
if self._spotify_client:
|
||||||
self._log.debug("Websocket and http session closed.")
|
await self._spotify_client.close()
|
||||||
|
|
||||||
|
if self._apple_music_client:
|
||||||
|
await self._apple_music_client.close()
|
||||||
|
|
||||||
del self._pool._nodes[self._identifier]
|
del self._pool._nodes[self._identifier]
|
||||||
self.available = False
|
self.available = False
|
||||||
self._task.cancel()
|
self._task.cancel()
|
||||||
|
|
||||||
end = time.perf_counter()
|
|
||||||
if self._log:
|
|
||||||
self._log.info(
|
|
||||||
f"Successfully disconnected from node {self._identifier} and closed all sessions. Took {end - start:.3f}s",
|
|
||||||
)
|
|
||||||
|
|
||||||
async def build_track(self, identifier: str, ctx: Optional[commands.Context] = None) -> Track:
|
async def build_track(self, identifier: str, ctx: Optional[commands.Context] = None) -> Track:
|
||||||
"""
|
"""
|
||||||
Builds a track using a valid track identifier
|
Builds a track using a valid track identifier
|
||||||
|
|
@ -558,16 +407,13 @@ class Node:
|
||||||
data: dict = await self.send(
|
data: dict = await self.send(
|
||||||
method="GET",
|
method="GET",
|
||||||
path="decodetrack",
|
path="decodetrack",
|
||||||
query=f"encodedTrack={quote(identifier)}",
|
query=f"encodedTrack={identifier}",
|
||||||
)
|
)
|
||||||
|
|
||||||
track_info = data["info"] if self._version.major >= 4 else data
|
|
||||||
|
|
||||||
return Track(
|
return Track(
|
||||||
track_id=identifier,
|
track_id=identifier,
|
||||||
ctx=ctx,
|
ctx=ctx,
|
||||||
info=track_info,
|
info=data,
|
||||||
track_type=TrackType(track_info["sourceName"]),
|
track_type=TrackType(data["sourceName"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
async def get_tracks(
|
async def get_tracks(
|
||||||
|
|
@ -575,7 +421,7 @@ class Node:
|
||||||
query: str,
|
query: str,
|
||||||
*,
|
*,
|
||||||
ctx: Optional[commands.Context] = None,
|
ctx: Optional[commands.Context] = None,
|
||||||
search_type: Optional[SearchType] = SearchType.ytsearch,
|
search_type: SearchType = SearchType.ytsearch,
|
||||||
filters: Optional[List[Filter]] = None,
|
filters: Optional[List[Filter]] = None,
|
||||||
) -> Optional[Union[Playlist, List[Track]]]:
|
) -> Optional[Union[Playlist, List[Track]]]:
|
||||||
"""Fetches tracks from the node's REST api to parse into Lavalink.
|
"""Fetches tracks from the node's REST api to parse into Lavalink.
|
||||||
|
|
@ -592,17 +438,20 @@ class Node:
|
||||||
|
|
||||||
timestamp = None
|
timestamp = None
|
||||||
|
|
||||||
|
if not URLRegex.BASE_URL.match(query) and not re.match(r"(?:ytm?|sc)search:.", query):
|
||||||
|
query = f"{search_type}:{query}"
|
||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
for filter in filters:
|
for filter in filters:
|
||||||
filter.set_preload()
|
filter.set_preload()
|
||||||
|
|
||||||
# Due to the inclusion of plugins in the v4 update
|
if URLRegex.AM_URL.match(query):
|
||||||
# we are doing away with raising an error if pomice detects
|
if not self._apple_music_client:
|
||||||
# either a Spotify or Apple Music URL and the respective client
|
raise AppleMusicNotEnabled(
|
||||||
# is not enabled. Instead, we will just only parse the URL
|
"You must have Apple Music functionality enabled in order to play Apple Music tracks."
|
||||||
# if the client is enabled and the URL is valid.
|
"Please set apple_music to True in your Node class.",
|
||||||
|
)
|
||||||
|
|
||||||
if self._apple_music_client and URLRegex.AM_URL.match(query):
|
|
||||||
apple_music_results = await self._apple_music_client.search(query=query)
|
apple_music_results = await self._apple_music_client.search(query=query)
|
||||||
if isinstance(apple_music_results, applemusic.Song):
|
if isinstance(apple_music_results, applemusic.Song):
|
||||||
return [
|
return [
|
||||||
|
|
@ -610,7 +459,7 @@ class Node:
|
||||||
track_id=apple_music_results.id,
|
track_id=apple_music_results.id,
|
||||||
ctx=ctx,
|
ctx=ctx,
|
||||||
track_type=TrackType.APPLE_MUSIC,
|
track_type=TrackType.APPLE_MUSIC,
|
||||||
search_type=search_type or SearchType.ytsearch,
|
search_type=search_type,
|
||||||
filters=filters,
|
filters=filters,
|
||||||
info={
|
info={
|
||||||
"title": apple_music_results.name,
|
"title": apple_music_results.name,
|
||||||
|
|
@ -632,7 +481,7 @@ class Node:
|
||||||
track_id=track.id,
|
track_id=track.id,
|
||||||
ctx=ctx,
|
ctx=ctx,
|
||||||
track_type=TrackType.APPLE_MUSIC,
|
track_type=TrackType.APPLE_MUSIC,
|
||||||
search_type=search_type or SearchType.ytsearch,
|
search_type=search_type,
|
||||||
filters=filters,
|
filters=filters,
|
||||||
info={
|
info={
|
||||||
"title": track.name,
|
"title": track.name,
|
||||||
|
|
@ -661,8 +510,15 @@ class Node:
|
||||||
uri=apple_music_results.url,
|
uri=apple_music_results.url,
|
||||||
)
|
)
|
||||||
|
|
||||||
elif self._spotify_client and URLRegex.SPOTIFY_URL.match(query):
|
elif URLRegex.SPOTIFY_URL.match(query):
|
||||||
spotify_results = await self._spotify_client.search(query=query) # type: ignore
|
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/",
|
||||||
|
)
|
||||||
|
|
||||||
|
spotify_results = await self._spotify_client.search(query=query)
|
||||||
|
|
||||||
if isinstance(spotify_results, spotify.Track):
|
if isinstance(spotify_results, spotify.Track):
|
||||||
return [
|
return [
|
||||||
|
|
@ -670,7 +526,7 @@ class Node:
|
||||||
track_id=spotify_results.id,
|
track_id=spotify_results.id,
|
||||||
ctx=ctx,
|
ctx=ctx,
|
||||||
track_type=TrackType.SPOTIFY,
|
track_type=TrackType.SPOTIFY,
|
||||||
search_type=search_type or SearchType.ytsearch,
|
search_type=search_type,
|
||||||
filters=filters,
|
filters=filters,
|
||||||
info={
|
info={
|
||||||
"title": spotify_results.name,
|
"title": spotify_results.name,
|
||||||
|
|
@ -692,7 +548,7 @@ class Node:
|
||||||
track_id=track.id,
|
track_id=track.id,
|
||||||
ctx=ctx,
|
ctx=ctx,
|
||||||
track_type=TrackType.SPOTIFY,
|
track_type=TrackType.SPOTIFY,
|
||||||
search_type=search_type or SearchType.ytsearch,
|
search_type=search_type,
|
||||||
filters=filters,
|
filters=filters,
|
||||||
info={
|
info={
|
||||||
"title": track.name,
|
"title": track.name,
|
||||||
|
|
@ -721,21 +577,45 @@ class Node:
|
||||||
uri=spotify_results.uri,
|
uri=spotify_results.uri,
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
elif discord_url := URLRegex.DISCORD_MP3_URL.match(query):
|
||||||
if (
|
data: dict = await self.send(
|
||||||
search_type
|
method="GET",
|
||||||
and not URLRegex.BASE_URL.match(query)
|
path="loadtracks",
|
||||||
and not re.match(r"(?:[a-z]+?)search:.", query)
|
query=f"identifier={quote(query)}",
|
||||||
and not URLRegex.DISCORD_MP3_URL.match(query)
|
)
|
||||||
and not path.exists(path.dirname(query))
|
|
||||||
):
|
|
||||||
query = f"{search_type}:{query}"
|
|
||||||
|
|
||||||
|
track: dict = data["tracks"][0]
|
||||||
|
info: dict = track["info"]
|
||||||
|
|
||||||
|
return [
|
||||||
|
Track(
|
||||||
|
track_id=track["track"],
|
||||||
|
info={
|
||||||
|
"title": discord_url.group("file"),
|
||||||
|
"author": "Unknown",
|
||||||
|
"length": info["length"],
|
||||||
|
"uri": info["uri"],
|
||||||
|
"position": info["position"],
|
||||||
|
"identifier": info["identifier"],
|
||||||
|
},
|
||||||
|
ctx=ctx,
|
||||||
|
track_type=TrackType.HTTP,
|
||||||
|
filters=filters,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
else:
|
||||||
# If YouTube url contains a timestamp, capture it for use later.
|
# If YouTube url contains a timestamp, capture it for use later.
|
||||||
|
|
||||||
if match := URLRegex.YOUTUBE_TIMESTAMP.match(query):
|
if match := URLRegex.YOUTUBE_TIMESTAMP.match(query):
|
||||||
timestamp = float(match.group("time"))
|
timestamp = float(match.group("time"))
|
||||||
|
|
||||||
|
# If query is a video thats part of a playlist, get the video and queue that instead
|
||||||
|
# (I can't tell you how much i've wanted to implement this in here)
|
||||||
|
|
||||||
|
if match := URLRegex.YOUTUBE_VID_IN_PLAYLIST.match(query):
|
||||||
|
query = match.group("video")
|
||||||
|
|
||||||
data = await self.send(
|
data = await self.send(
|
||||||
method="GET",
|
method="GET",
|
||||||
path="loadtracks",
|
path="loadtracks",
|
||||||
|
|
@ -744,31 +624,21 @@ class Node:
|
||||||
|
|
||||||
load_type = data.get("loadType")
|
load_type = data.get("loadType")
|
||||||
|
|
||||||
# Lavalink v4 changed the name of the key from "tracks" to "data"
|
|
||||||
# so lets account for that
|
|
||||||
data_type = "data" if self._version.major >= 4 else "tracks"
|
|
||||||
|
|
||||||
if not load_type:
|
if not load_type:
|
||||||
raise TrackLoadError(
|
raise TrackLoadError(
|
||||||
"There was an error while trying to load this track.",
|
"There was an error while trying to load this track.",
|
||||||
)
|
)
|
||||||
|
|
||||||
elif load_type in ("LOAD_FAILED", "error"):
|
elif load_type == "LOAD_FAILED":
|
||||||
exception = data["data"] if self._version.major >= 4 else data["exception"]
|
exception = data["exception"]
|
||||||
raise TrackLoadError(
|
raise TrackLoadError(
|
||||||
f"{exception['message']} [{exception['severity']}]",
|
f"{exception['message']} [{exception['severity']}]",
|
||||||
)
|
)
|
||||||
|
|
||||||
elif load_type in ("NO_MATCHES", "empty"):
|
elif load_type == "NO_MATCHES":
|
||||||
return None
|
return None
|
||||||
|
|
||||||
elif load_type in ("PLAYLIST_LOADED", "playlist"):
|
elif load_type == "PLAYLIST_LOADED":
|
||||||
if self._version.major >= 4:
|
|
||||||
track_list = data[data_type]["tracks"]
|
|
||||||
playlist_info = data[data_type]["info"]
|
|
||||||
else:
|
|
||||||
track_list = data[data_type]
|
|
||||||
playlist_info = data["playlistInfo"]
|
|
||||||
tracks = [
|
tracks = [
|
||||||
Track(
|
Track(
|
||||||
track_id=track["encoded"],
|
track_id=track["encoded"],
|
||||||
|
|
@ -776,60 +646,17 @@ class Node:
|
||||||
ctx=ctx,
|
ctx=ctx,
|
||||||
track_type=TrackType(track["info"]["sourceName"]),
|
track_type=TrackType(track["info"]["sourceName"]),
|
||||||
)
|
)
|
||||||
for track in track_list
|
for track in data["tracks"]
|
||||||
]
|
]
|
||||||
return Playlist(
|
return Playlist(
|
||||||
playlist_info=playlist_info,
|
playlist_info=data["playlistInfo"],
|
||||||
tracks=tracks,
|
tracks=tracks,
|
||||||
playlist_type=PlaylistType(tracks[0].track_type.value),
|
playlist_type=PlaylistType(tracks[0].track_type.value),
|
||||||
thumbnail=tracks[0].thumbnail,
|
thumbnail=tracks[0].thumbnail,
|
||||||
uri=query,
|
uri=query,
|
||||||
)
|
)
|
||||||
|
|
||||||
elif load_type in ("SEARCH_RESULT", "TRACK_LOADED", "track", "search"):
|
elif load_type == "SEARCH_RESULT" or load_type == "TRACK_LOADED":
|
||||||
if self._version.major >= 4 and isinstance(data[data_type], dict):
|
|
||||||
data[data_type] = [data[data_type]]
|
|
||||||
|
|
||||||
if path.exists(path.dirname(query)):
|
|
||||||
local_file = Path(query)
|
|
||||||
|
|
||||||
return [
|
|
||||||
Track(
|
|
||||||
track_id=track["encoded"],
|
|
||||||
info={
|
|
||||||
"title": local_file.name,
|
|
||||||
"author": "Unknown",
|
|
||||||
"length": track["info"]["length"],
|
|
||||||
"uri": quote(local_file.as_uri()),
|
|
||||||
"position": track["info"]["position"],
|
|
||||||
"identifier": track["info"]["identifier"],
|
|
||||||
},
|
|
||||||
ctx=ctx,
|
|
||||||
track_type=TrackType.LOCAL,
|
|
||||||
filters=filters,
|
|
||||||
)
|
|
||||||
for track in data[data_type]
|
|
||||||
]
|
|
||||||
|
|
||||||
elif discord_url := URLRegex.DISCORD_MP3_URL.match(query):
|
|
||||||
return [
|
|
||||||
Track(
|
|
||||||
track_id=track["encoded"],
|
|
||||||
info={
|
|
||||||
"title": discord_url.group("file"),
|
|
||||||
"author": "Unknown",
|
|
||||||
"length": track["info"]["length"],
|
|
||||||
"uri": track["info"]["uri"],
|
|
||||||
"position": track["info"]["position"],
|
|
||||||
"identifier": track["info"]["identifier"],
|
|
||||||
},
|
|
||||||
ctx=ctx,
|
|
||||||
track_type=TrackType.HTTP,
|
|
||||||
filters=filters,
|
|
||||||
)
|
|
||||||
for track in data[data_type]
|
|
||||||
]
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
Track(
|
Track(
|
||||||
track_id=track["encoded"],
|
track_id=track["encoded"],
|
||||||
|
|
@ -839,7 +666,7 @@ class Node:
|
||||||
filters=filters,
|
filters=filters,
|
||||||
timestamp=timestamp,
|
timestamp=timestamp,
|
||||||
)
|
)
|
||||||
for track in data[data_type]
|
for track in data["tracks"]
|
||||||
]
|
]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -848,10 +675,7 @@ class Node:
|
||||||
)
|
)
|
||||||
|
|
||||||
async def get_recommendations(
|
async def get_recommendations(
|
||||||
self,
|
self, *, track: Track, ctx: Optional[commands.Context] = None
|
||||||
*,
|
|
||||||
track: Track,
|
|
||||||
ctx: Optional[commands.Context] = None,
|
|
||||||
) -> Optional[Union[List[Track], Playlist]]:
|
) -> Optional[Union[List[Track], Playlist]]:
|
||||||
"""
|
"""
|
||||||
Gets recommendations from either YouTube or Spotify.
|
Gets recommendations from either YouTube or Spotify.
|
||||||
|
|
@ -861,7 +685,7 @@ class Node:
|
||||||
Context object on all tracks that get recommended.
|
Context object on all tracks that get recommended.
|
||||||
"""
|
"""
|
||||||
if track.track_type == TrackType.SPOTIFY:
|
if track.track_type == TrackType.SPOTIFY:
|
||||||
results = await self._spotify_client.get_recommendations(query=track.uri) # type: ignore
|
results = await self._spotify_client.get_recommendations(query=track.uri)
|
||||||
tracks = [
|
tracks = [
|
||||||
Track(
|
Track(
|
||||||
track_id=track.id,
|
track_id=track.id,
|
||||||
|
|
@ -896,57 +720,6 @@ class Node:
|
||||||
"The specfied track must be either a YouTube or Spotify track to recieve recommendations.",
|
"The specfied track must be either a YouTube or Spotify track to recieve recommendations.",
|
||||||
)
|
)
|
||||||
|
|
||||||
async def search_spotify_recommendations(
|
|
||||||
self,
|
|
||||||
query: str,
|
|
||||||
*,
|
|
||||||
ctx: Optional[commands.Context] = None,
|
|
||||||
filters: Optional[List[Filter]] = None,
|
|
||||||
) -> Optional[Union[List[Track], Playlist]]:
|
|
||||||
"""
|
|
||||||
Searches for recommendations on Spotify and returns a list of tracks based on the query.
|
|
||||||
You must have Spotify enabled for this to work.
|
|
||||||
You can pass in a discord.py Context object to get a
|
|
||||||
Context object on all tracks that get recommended.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if not self._spotify_client:
|
|
||||||
raise InvalidSpotifyClientAuthorization(
|
|
||||||
"You must have Spotify enabled to use this feature.",
|
|
||||||
)
|
|
||||||
|
|
||||||
results = await self._spotify_client.track_search(query=query) # type: ignore
|
|
||||||
if not results:
|
|
||||||
raise TrackLoadError(
|
|
||||||
"Unable to find any tracks based on the query.",
|
|
||||||
)
|
|
||||||
|
|
||||||
tracks = [
|
|
||||||
Track(
|
|
||||||
track_id=track.id,
|
|
||||||
ctx=ctx,
|
|
||||||
track_type=TrackType.SPOTIFY,
|
|
||||||
info={
|
|
||||||
"title": track.name,
|
|
||||||
"author": track.artists,
|
|
||||||
"length": track.length,
|
|
||||||
"identifier": track.id,
|
|
||||||
"uri": track.uri,
|
|
||||||
"isStream": False,
|
|
||||||
"isSeekable": True,
|
|
||||||
"position": 0,
|
|
||||||
"thumbnail": track.image,
|
|
||||||
"isrc": track.isrc,
|
|
||||||
},
|
|
||||||
requester=self.bot.user,
|
|
||||||
)
|
|
||||||
for track in results
|
|
||||||
]
|
|
||||||
|
|
||||||
track = tracks[0]
|
|
||||||
|
|
||||||
return await self.get_recommendations(track=track, ctx=ctx)
|
|
||||||
|
|
||||||
|
|
||||||
class NodePool:
|
class NodePool:
|
||||||
"""The base class for the node pool.
|
"""The base class for the node pool.
|
||||||
|
|
@ -1028,16 +801,13 @@ class NodePool:
|
||||||
password: str,
|
password: str,
|
||||||
identifier: str,
|
identifier: str,
|
||||||
secure: bool = False,
|
secure: bool = False,
|
||||||
heartbeat: int = 120,
|
heartbeat: int = 30,
|
||||||
resume_key: Optional[str] = None,
|
|
||||||
resume_timeout: int = 60,
|
|
||||||
loop: Optional[asyncio.AbstractEventLoop] = None,
|
loop: Optional[asyncio.AbstractEventLoop] = None,
|
||||||
spotify_client_id: Optional[str] = None,
|
spotify_client_id: Optional[int] = None,
|
||||||
spotify_client_secret: Optional[str] = None,
|
spotify_client_secret: Optional[str] = None,
|
||||||
session: Optional[aiohttp.ClientSession] = None,
|
session: Optional[aiohttp.ClientSession] = None,
|
||||||
apple_music: bool = False,
|
apple_music: bool = False,
|
||||||
fallback: bool = False,
|
fallback: bool = False,
|
||||||
logger: Optional[logging.Logger] = None,
|
|
||||||
) -> Node:
|
) -> Node:
|
||||||
"""Creates a Node object to be then added into the node pool.
|
"""Creates a Node object to be then added into the node pool.
|
||||||
For Spotify searching capabilites, pass in valid Spotify API credentials.
|
For Spotify searching capabilites, pass in valid Spotify API credentials.
|
||||||
|
|
@ -1056,15 +826,12 @@ class NodePool:
|
||||||
identifier=identifier,
|
identifier=identifier,
|
||||||
secure=secure,
|
secure=secure,
|
||||||
heartbeat=heartbeat,
|
heartbeat=heartbeat,
|
||||||
resume_key=resume_key,
|
|
||||||
resume_timeout=resume_timeout,
|
|
||||||
loop=loop,
|
loop=loop,
|
||||||
spotify_client_id=spotify_client_id,
|
spotify_client_id=spotify_client_id,
|
||||||
session=session,
|
session=session,
|
||||||
spotify_client_secret=spotify_client_secret,
|
spotify_client_secret=spotify_client_secret,
|
||||||
apple_music=apple_music,
|
apple_music=apple_music,
|
||||||
fallback=fallback,
|
fallback=fallback,
|
||||||
logger=logger,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
await node.connect()
|
await node.connect()
|
||||||
|
|
|
||||||
|
|
@ -203,13 +203,9 @@ class Queue(Iterable[Track]):
|
||||||
raise QueueEmpty("No items in the queue.")
|
raise QueueEmpty("No items in the queue.")
|
||||||
|
|
||||||
if self._loop_mode == LoopMode.QUEUE:
|
if self._loop_mode == LoopMode.QUEUE:
|
||||||
# set current item to first track in queue if not set already
|
# recurse if the item isnt in the queue
|
||||||
# otherwise exception will be raised
|
if self._current_item not in self._queue:
|
||||||
if not self._current_item or self._current_item not in self._queue:
|
self.get()
|
||||||
if self._queue:
|
|
||||||
item = self._queue[0]
|
|
||||||
else:
|
|
||||||
raise QueueEmpty("No items in the queue.")
|
|
||||||
|
|
||||||
# set current item to first track in queue if not set already
|
# set current item to first track in queue if not set already
|
||||||
if not self._current_item:
|
if not self._current_item:
|
||||||
|
|
@ -352,23 +348,7 @@ class Queue(Iterable[Track]):
|
||||||
track.filters = None
|
track.filters = None
|
||||||
|
|
||||||
def jump(self, item: Track) -> None:
|
def jump(self, item: Track) -> None:
|
||||||
"""
|
"""Removes all tracks before the."""
|
||||||
Jumps to the item specified in the queue.
|
|
||||||
|
|
||||||
If the queue is not looping, the queue will be mutated.
|
|
||||||
Otherwise, the current item will be adjusted to the item
|
|
||||||
before the specified track.
|
|
||||||
|
|
||||||
The queue is adjusted so that the next item that is retrieved
|
|
||||||
is the track that is specified, effectively 'jumping' the queue.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if self._loop_mode == LoopMode.TRACK:
|
|
||||||
raise QueueException("Jumping the queue whilst looping a track is not allowed.")
|
|
||||||
|
|
||||||
index = self.find_position(item)
|
index = self.find_position(item)
|
||||||
if self._loop_mode == LoopMode.QUEUE:
|
new_queue = self._queue[index : self.size]
|
||||||
self._current_item = self._queue[index - 1]
|
self._queue = new_queue
|
||||||
else:
|
|
||||||
new_queue = self._queue[index : self.size]
|
|
||||||
self._queue = new_queue
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,12 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import logging
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
from base64 import b64encode
|
from base64 import b64encode
|
||||||
from typing import AsyncGenerator
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from urllib.parse import quote
|
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import orjson as json
|
import orjson as json
|
||||||
|
|
@ -24,10 +20,8 @@ __all__ = ("Client",)
|
||||||
|
|
||||||
GRANT_URL = "https://accounts.spotify.com/api/token"
|
GRANT_URL = "https://accounts.spotify.com/api/token"
|
||||||
REQUEST_URL = "https://api.spotify.com/v1/{type}s/{id}"
|
REQUEST_URL = "https://api.spotify.com/v1/{type}s/{id}"
|
||||||
# Keep this in sync with URLRegex.SPOTIFY_URL (enums.py). Accept intl locale segment,
|
|
||||||
# optional trailing slash, and query parameters.
|
|
||||||
SPOTIFY_URL_REGEX = re.compile(
|
SPOTIFY_URL_REGEX = re.compile(
|
||||||
r"https?://open\.spotify\.com/(?:intl-[a-zA-Z-]+/)?(?P<type>album|playlist|track|artist)/(?P<id>[a-zA-Z0-9]+)(?:/)?(?:\?.*)?$",
|
r"https?://open.spotify.com/(?P<type>album|playlist|track|artist)/(?P<id>[a-zA-Z0-9]+)",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -37,49 +31,35 @@ class Client:
|
||||||
for any Spotify URL you throw at it.
|
for any Spotify URL you throw at it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, client_id: int, client_secret: str) -> None:
|
||||||
self,
|
self._client_id: int = client_id
|
||||||
client_id: str,
|
self._client_secret: str = client_secret
|
||||||
client_secret: str,
|
|
||||||
*,
|
|
||||||
playlist_concurrency: int = 10,
|
|
||||||
playlist_page_limit: Optional[int] = None,
|
|
||||||
) -> None:
|
|
||||||
self._client_id = client_id
|
|
||||||
self._client_secret = client_secret
|
|
||||||
|
|
||||||
# HTTP session will be injected by Node
|
self.session: aiohttp.ClientSession = None # type: ignore
|
||||||
self.session: Optional[aiohttp.ClientSession] = None
|
|
||||||
|
|
||||||
self._bearer_token: Optional[str] = None
|
self._bearer_token: Optional[str] = None
|
||||||
self._expiry: float = 0.0
|
self._expiry: float = 0.0
|
||||||
self._auth_token = b64encode(f"{self._client_id}:{self._client_secret}".encode())
|
self._auth_token = b64encode(
|
||||||
self._grant_headers = {"Authorization": f"Basic {self._auth_token.decode()}"}
|
f"{self._client_id}:{self._client_secret}".encode(),
|
||||||
|
)
|
||||||
|
self._grant_headers = {
|
||||||
|
"Authorization": f"Basic {self._auth_token.decode()}",
|
||||||
|
}
|
||||||
self._bearer_headers: Optional[Dict] = None
|
self._bearer_headers: Optional[Dict] = None
|
||||||
self._log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
# Performance tuning knobs
|
|
||||||
self._playlist_concurrency = max(1, playlist_concurrency)
|
|
||||||
self._playlist_page_limit = playlist_page_limit
|
|
||||||
|
|
||||||
async def _set_session(self, session: aiohttp.ClientSession) -> None:
|
|
||||||
self.session = session
|
|
||||||
|
|
||||||
async def _fetch_bearer_token(self) -> None:
|
async def _fetch_bearer_token(self) -> None:
|
||||||
_data = {"grant_type": "client_credentials"}
|
_data = {"grant_type": "client_credentials"}
|
||||||
|
|
||||||
if not self.session:
|
if not self.session:
|
||||||
raise SpotifyRequestException("HTTP session not initialized for Spotify client.")
|
self.session = aiohttp.ClientSession()
|
||||||
resp = await self.session.post(GRANT_URL, data=_data, headers=self._grant_headers)
|
|
||||||
|
|
||||||
if resp.status != 200:
|
async with self.session.post(GRANT_URL, data=_data, headers=self._grant_headers) as resp:
|
||||||
raise SpotifyRequestException(
|
if resp.status != 200:
|
||||||
f"Error fetching bearer token: {resp.status} {resp.reason}",
|
raise SpotifyRequestException(
|
||||||
)
|
f"Error fetching bearer token: {resp.status} {resp.reason}",
|
||||||
|
)
|
||||||
|
|
||||||
data: dict = await resp.json(loads=json.loads)
|
data: dict = await resp.json(loads=json.loads)
|
||||||
if self._log:
|
|
||||||
self._log.debug(f"Fetched Spotify bearer token successfully")
|
|
||||||
|
|
||||||
self._bearer_token = data["access_token"]
|
self._bearer_token = data["access_token"]
|
||||||
self._expiry = time.time() + (int(data["expires_in"]) - 10)
|
self._expiry = time.time() + (int(data["expires_in"]) - 10)
|
||||||
|
|
@ -100,212 +80,63 @@ class Client:
|
||||||
|
|
||||||
request_url = REQUEST_URL.format(type=spotify_type, id=spotify_id)
|
request_url = REQUEST_URL.format(type=spotify_type, id=spotify_id)
|
||||||
|
|
||||||
if not self.session:
|
async with self.session.get(request_url, headers=self._bearer_headers) as resp:
|
||||||
raise SpotifyRequestException("HTTP session not initialized for Spotify client.")
|
if resp.status != 200:
|
||||||
resp = await self.session.get(request_url, headers=self._bearer_headers)
|
raise SpotifyRequestException(
|
||||||
if resp.status != 200:
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
raise SpotifyRequestException(
|
)
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
|
||||||
)
|
|
||||||
|
|
||||||
data: dict = await resp.json(loads=json.loads)
|
data: dict = await resp.json(loads=json.loads)
|
||||||
if self._log:
|
|
||||||
self._log.debug(
|
|
||||||
f"Made request to Spotify API with status {resp.status} and response {data}",
|
|
||||||
)
|
|
||||||
|
|
||||||
if spotify_type == "track":
|
if spotify_type == "track":
|
||||||
return Track(data)
|
return Track(data)
|
||||||
elif spotify_type == "album":
|
elif spotify_type == "album":
|
||||||
return Album(data)
|
return Album(data)
|
||||||
elif spotify_type == "artist":
|
elif spotify_type == "artist":
|
||||||
if not self.session:
|
async with self.session.get(
|
||||||
raise SpotifyRequestException("HTTP session not initialized for Spotify client.")
|
|
||||||
resp = await self.session.get(
|
|
||||||
f"{request_url}/top-tracks?market=US",
|
f"{request_url}/top-tracks?market=US",
|
||||||
headers=self._bearer_headers,
|
headers=self._bearer_headers,
|
||||||
)
|
) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise SpotifyRequestException(
|
raise SpotifyRequestException(
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
)
|
)
|
||||||
|
|
||||||
track_data: dict = await resp.json(loads=json.loads)
|
track_data: dict = await resp.json(loads=json.loads)
|
||||||
tracks = track_data["tracks"]
|
tracks = track_data["tracks"]
|
||||||
return Artist(data, tracks)
|
return Artist(data, tracks)
|
||||||
else:
|
else:
|
||||||
# For playlists we optionally use a reduced fields payload to shrink response sizes.
|
|
||||||
# NB: We cannot apply fields filter to initial request because original metadata is needed.
|
|
||||||
tracks = [
|
tracks = [
|
||||||
Track(track["track"])
|
Track(track["track"])
|
||||||
for track in data["tracks"]["items"]
|
for track in data["tracks"]["items"]
|
||||||
if track["track"] is not None
|
if track["track"] is not None
|
||||||
]
|
]
|
||||||
|
|
||||||
if not tracks:
|
if not len(tracks):
|
||||||
raise SpotifyRequestException(
|
raise SpotifyRequestException(
|
||||||
"This playlist is empty and therefore cannot be queued.",
|
"This playlist is empty and therefore cannot be queued.",
|
||||||
)
|
)
|
||||||
|
|
||||||
total_tracks = data["tracks"]["total"]
|
next_page_url = data["tracks"]["next"]
|
||||||
limit = data["tracks"]["limit"]
|
|
||||||
|
|
||||||
# Short‑circuit small playlists (single page)
|
while next_page_url is not None:
|
||||||
if total_tracks <= limit:
|
async with self.session.get(next_page_url, headers=self._bearer_headers) as resp:
|
||||||
return Playlist(data, tracks)
|
if resp.status != 200:
|
||||||
|
raise SpotifyRequestException(
|
||||||
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
|
)
|
||||||
|
|
||||||
# Build remaining page URLs; Spotify supports offset-based pagination.
|
next_data: dict = await resp.json(loads=json.loads)
|
||||||
remaining_offsets = range(limit, total_tracks, limit)
|
|
||||||
page_urls: List[str] = []
|
|
||||||
fields_filter = (
|
|
||||||
"items(track(name,duration_ms,id,is_local,external_urls,external_ids,artists(name),album(images)))"
|
|
||||||
",next"
|
|
||||||
)
|
|
||||||
for idx, offset in enumerate(remaining_offsets):
|
|
||||||
if self._playlist_page_limit is not None and idx >= self._playlist_page_limit:
|
|
||||||
break
|
|
||||||
page_urls.append(
|
|
||||||
f"{request_url}/tracks?offset={offset}&limit={limit}&fields={quote(fields_filter)}",
|
|
||||||
)
|
|
||||||
|
|
||||||
if page_urls:
|
tracks += [
|
||||||
semaphore = asyncio.Semaphore(self._playlist_concurrency)
|
Track(track["track"])
|
||||||
|
for track in next_data["items"]
|
||||||
async def fetch_page(url: str) -> Optional[List[Track]]:
|
if track["track"] is not None
|
||||||
async with semaphore:
|
]
|
||||||
if not self.session:
|
next_page_url = next_data["next"]
|
||||||
raise SpotifyRequestException(
|
|
||||||
"HTTP session not initialized for Spotify client.",
|
|
||||||
)
|
|
||||||
resp = await self.session.get(url, headers=self._bearer_headers)
|
|
||||||
if resp.status != 200:
|
|
||||||
if self._log:
|
|
||||||
self._log.warning(
|
|
||||||
f"Page fetch failed {resp.status} {resp.reason} for {url}",
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
page_json: dict = await resp.json(loads=json.loads)
|
|
||||||
return [
|
|
||||||
Track(item["track"])
|
|
||||||
for item in page_json.get("items", [])
|
|
||||||
if item.get("track") is not None
|
|
||||||
]
|
|
||||||
|
|
||||||
# Chunk gather in waves to avoid creating thousands of tasks at once
|
|
||||||
aggregated: List[Track] = []
|
|
||||||
wave_size = self._playlist_concurrency * 2
|
|
||||||
for i in range(0, len(page_urls), wave_size):
|
|
||||||
wave = page_urls[i : i + wave_size]
|
|
||||||
results = await asyncio.gather(
|
|
||||||
*[fetch_page(url) for url in wave],
|
|
||||||
return_exceptions=False,
|
|
||||||
)
|
|
||||||
for result in results:
|
|
||||||
if result:
|
|
||||||
aggregated.extend(result)
|
|
||||||
|
|
||||||
tracks.extend(aggregated)
|
|
||||||
|
|
||||||
return Playlist(data, tracks)
|
return Playlist(data, tracks)
|
||||||
|
|
||||||
async def iter_playlist_tracks(
|
|
||||||
self,
|
|
||||||
*,
|
|
||||||
query: str,
|
|
||||||
batch_size: int = 100,
|
|
||||||
) -> AsyncGenerator[List[Track], None]:
|
|
||||||
"""Stream playlist tracks in batches without waiting for full materialization.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
query: str
|
|
||||||
Spotify playlist URL.
|
|
||||||
batch_size: int
|
|
||||||
Number of tracks yielded per batch (logical grouping after fetch). Does not alter API page size.
|
|
||||||
"""
|
|
||||||
if not self._bearer_token or time.time() >= self._expiry:
|
|
||||||
await self._fetch_bearer_token()
|
|
||||||
|
|
||||||
match = SPOTIFY_URL_REGEX.match(query)
|
|
||||||
if not match or match.group("type") != "playlist":
|
|
||||||
raise InvalidSpotifyURL("Provided query is not a valid Spotify playlist URL.")
|
|
||||||
|
|
||||||
playlist_id = match.group("id")
|
|
||||||
request_url = REQUEST_URL.format(type="playlist", id=playlist_id)
|
|
||||||
if not self.session:
|
|
||||||
raise SpotifyRequestException("HTTP session not initialized for Spotify client.")
|
|
||||||
resp = await self.session.get(request_url, headers=self._bearer_headers)
|
|
||||||
if resp.status != 200:
|
|
||||||
raise SpotifyRequestException(
|
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
|
||||||
)
|
|
||||||
data: dict = await resp.json(loads=json.loads)
|
|
||||||
|
|
||||||
# Yield first page immediately
|
|
||||||
first_page_tracks = [
|
|
||||||
Track(item["track"])
|
|
||||||
for item in data["tracks"]["items"]
|
|
||||||
if item.get("track") is not None
|
|
||||||
]
|
|
||||||
# Batch yield
|
|
||||||
for i in range(0, len(first_page_tracks), batch_size):
|
|
||||||
yield first_page_tracks[i : i + batch_size]
|
|
||||||
|
|
||||||
total = data["tracks"]["total"]
|
|
||||||
limit = data["tracks"]["limit"]
|
|
||||||
remaining_offsets = range(limit, total, limit)
|
|
||||||
fields_filter = (
|
|
||||||
"items(track(name,duration_ms,id,is_local,external_urls,external_ids,artists(name),album(images)))"
|
|
||||||
",next"
|
|
||||||
)
|
|
||||||
|
|
||||||
semaphore = asyncio.Semaphore(self._playlist_concurrency)
|
|
||||||
|
|
||||||
async def fetch(offset: int) -> List[Track]:
|
|
||||||
url = (
|
|
||||||
f"{request_url}/tracks?offset={offset}&limit={limit}&fields={quote(fields_filter)}"
|
|
||||||
)
|
|
||||||
async with semaphore:
|
|
||||||
if not self.session:
|
|
||||||
raise SpotifyRequestException(
|
|
||||||
"HTTP session not initialized for Spotify client.",
|
|
||||||
)
|
|
||||||
r = await self.session.get(url, headers=self._bearer_headers)
|
|
||||||
if r.status != 200:
|
|
||||||
if self._log:
|
|
||||||
self._log.warning(
|
|
||||||
f"Skipping page offset={offset} due to {r.status} {r.reason}",
|
|
||||||
)
|
|
||||||
return []
|
|
||||||
pj: dict = await r.json(loads=json.loads)
|
|
||||||
return [
|
|
||||||
Track(item["track"])
|
|
||||||
for item in pj.get("items", [])
|
|
||||||
if item.get("track") is not None
|
|
||||||
]
|
|
||||||
|
|
||||||
# Fetch pages in rolling waves; yield promptly as soon as a wave completes.
|
|
||||||
wave_size = self._playlist_concurrency * 2
|
|
||||||
for i, offset in enumerate(remaining_offsets):
|
|
||||||
# Build wave
|
|
||||||
if i % wave_size == 0:
|
|
||||||
wave_offsets = list(
|
|
||||||
o for o in remaining_offsets if o >= offset and o < offset + wave_size
|
|
||||||
)
|
|
||||||
results = await asyncio.gather(*[fetch(o) for o in wave_offsets])
|
|
||||||
for page_tracks in results:
|
|
||||||
if not page_tracks:
|
|
||||||
continue
|
|
||||||
for j in range(0, len(page_tracks), batch_size):
|
|
||||||
yield page_tracks[j : j + batch_size]
|
|
||||||
# Skip ahead in iterator by adjusting enumerate drive (consume extras)
|
|
||||||
# Fast-forward the generator manually
|
|
||||||
for _ in range(len(wave_offsets) - 1):
|
|
||||||
try:
|
|
||||||
next(remaining_offsets) # type: ignore
|
|
||||||
except StopIteration:
|
|
||||||
break
|
|
||||||
|
|
||||||
async def get_recommendations(self, *, query: str) -> List[Track]:
|
async def get_recommendations(self, *, query: str) -> List[Track]:
|
||||||
if not self._bearer_token or time.time() >= self._expiry:
|
if not self._bearer_token or time.time() >= self._expiry:
|
||||||
await self._fetch_bearer_token()
|
await self._fetch_bearer_token()
|
||||||
|
|
@ -327,34 +158,19 @@ class Client:
|
||||||
id=f"?seed_tracks={spotify_id}",
|
id=f"?seed_tracks={spotify_id}",
|
||||||
)
|
)
|
||||||
|
|
||||||
if not self.session:
|
async with self.session.get(request_url, headers=self._bearer_headers) as resp:
|
||||||
raise SpotifyRequestException("HTTP session not initialized for Spotify client.")
|
if resp.status != 200:
|
||||||
resp = await self.session.get(request_url, headers=self._bearer_headers)
|
raise SpotifyRequestException(
|
||||||
if resp.status != 200:
|
f"Error while fetching results: {resp.status} {resp.reason}",
|
||||||
raise SpotifyRequestException(
|
)
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
|
||||||
)
|
data: dict = await resp.json(loads=json.loads)
|
||||||
|
|
||||||
data: dict = await resp.json(loads=json.loads)
|
|
||||||
tracks = [Track(track) for track in data["tracks"]]
|
tracks = [Track(track) for track in data["tracks"]]
|
||||||
|
|
||||||
return tracks
|
return tracks
|
||||||
|
|
||||||
async def track_search(self, *, query: str) -> List[Track]:
|
async def close(self) -> None:
|
||||||
if not self._bearer_token or time.time() >= self._expiry:
|
if self.session:
|
||||||
await self._fetch_bearer_token()
|
await self.session.close()
|
||||||
|
self.session = None # type: ignore
|
||||||
request_url = f"https://api.spotify.com/v1/search?q={quote(query)}&type=track"
|
|
||||||
|
|
||||||
if not self.session:
|
|
||||||
raise SpotifyRequestException("HTTP session not initialized for Spotify client.")
|
|
||||||
resp = await self.session.get(request_url, headers=self._bearer_headers)
|
|
||||||
if resp.status != 200:
|
|
||||||
raise SpotifyRequestException(
|
|
||||||
f"Error while fetching results: {resp.status} {resp.reason}",
|
|
||||||
)
|
|
||||||
|
|
||||||
data: dict = await resp.json(loads=json.loads)
|
|
||||||
tracks = [Track(track) for track in data["tracks"]["items"]]
|
|
||||||
|
|
||||||
return tracks
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class Track:
|
||||||
self.length: float = data["duration_ms"]
|
self.length: float = data["duration_ms"]
|
||||||
self.id: str = data["id"]
|
self.id: str = data["id"]
|
||||||
|
|
||||||
self.isrc: Optional[str] = None
|
self.issrc: Optional[str] = None
|
||||||
if data.get("external_ids"):
|
if data.get("external_ids"):
|
||||||
self.isrc = data["external_ids"]["isrc"]
|
self.isrc = data["external_ids"]["isrc"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ from typing import Any
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
from typing import NamedTuple
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from .enums import RouteIPType
|
from .enums import RouteIPType
|
||||||
|
|
@ -20,7 +19,6 @@ __all__ = (
|
||||||
"FailingIPBlock",
|
"FailingIPBlock",
|
||||||
"RouteStats",
|
"RouteStats",
|
||||||
"Ping",
|
"Ping",
|
||||||
"LavalinkVersion",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -226,53 +224,3 @@ class Ping:
|
||||||
s_runtime = 1000 * (cost_time)
|
s_runtime = 1000 * (cost_time)
|
||||||
|
|
||||||
return s_runtime
|
return s_runtime
|
||||||
|
|
||||||
|
|
||||||
class LavalinkVersion(NamedTuple):
|
|
||||||
major: int
|
|
||||||
minor: int
|
|
||||||
fix: int
|
|
||||||
|
|
||||||
def __eq__(self, other: object) -> bool:
|
|
||||||
if not isinstance(other, LavalinkVersion):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return (
|
|
||||||
(self.major == other.major) and (self.minor == other.minor) and (self.fix == other.fix)
|
|
||||||
)
|
|
||||||
|
|
||||||
def __ne__(self, other: object) -> bool:
|
|
||||||
if not isinstance(other, LavalinkVersion):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return not (self == other)
|
|
||||||
|
|
||||||
def __lt__(self, other: object) -> bool:
|
|
||||||
if not isinstance(other, LavalinkVersion):
|
|
||||||
return False
|
|
||||||
|
|
||||||
if self.major > other.major:
|
|
||||||
return False
|
|
||||||
if self.minor > other.minor:
|
|
||||||
return False
|
|
||||||
if self.fix > other.fix:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __gt__(self, other: object) -> bool:
|
|
||||||
if not isinstance(other, LavalinkVersion):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return not (self < other)
|
|
||||||
|
|
||||||
def __le__(self, other: object) -> bool:
|
|
||||||
if not isinstance(other, LavalinkVersion):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return (self < other) or (self == other)
|
|
||||||
|
|
||||||
def __ge__(self, other: object) -> bool:
|
|
||||||
if not isinstance(other, LavalinkVersion):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return (self > other) or (self == other)
|
|
||||||
|
|
|
||||||
3
setup.py
3
setup.py
|
|
@ -1,10 +1,9 @@
|
||||||
# type: ignore
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import setuptools
|
import setuptools
|
||||||
|
|
||||||
version = ""
|
version = ""
|
||||||
requirements = ["aiohttp>=3.7.4,<4", "orjson", "websockets"]
|
requirements = ["discord.py>=2.0.0", "aiohttp>=3.7.4,<4", "orjson"]
|
||||||
with open("pomice/__init__.py") as f:
|
with open("pomice/__init__.py") as f:
|
||||||
version = re.search(
|
version = re.search(
|
||||||
r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
|
r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue