move NodePool to node.py
This commit is contained in:
parent
7c9dec116d
commit
7588c5d5e9
|
|
@ -8,6 +8,5 @@ from .exceptions import *
|
||||||
from .events import *
|
from .events import *
|
||||||
from .filters import *
|
from .filters import *
|
||||||
from .objects import *
|
from .objects import *
|
||||||
from .pool import NodePool
|
from .node import Node, NodePool
|
||||||
from .node import Node
|
|
||||||
from .player import Player
|
from .player import Player
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,23 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import random
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import aiohttp
|
|
||||||
import time
|
import time
|
||||||
from typing import Optional, Type
|
from typing import Optional, Type
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
from discord.ext import commands
|
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
from . import __version__, objects, spotify, NodePool
|
from . import __version__, objects, spotify, NodePool
|
||||||
from .exceptions import (
|
from .exceptions import (
|
||||||
InvalidSpotifyClientAuthorization,
|
InvalidSpotifyClientAuthorization,
|
||||||
NodeConnectionFailure,
|
NodeConnectionFailure,
|
||||||
|
NodeCreationError,
|
||||||
NodeNotAvailable,
|
NodeNotAvailable,
|
||||||
|
NoNodesAvailable,
|
||||||
SpotifyAlbumLoadFailed,
|
SpotifyAlbumLoadFailed,
|
||||||
SpotifyPlaylistLoadFailed,
|
SpotifyPlaylistLoadFailed,
|
||||||
SpotifyTrackLoadFailed,
|
SpotifyTrackLoadFailed,
|
||||||
|
|
@ -398,3 +402,64 @@ class Node:
|
||||||
)
|
)
|
||||||
for track in data["tracks"]
|
for track in data["tracks"]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class NodePool:
|
||||||
|
"""The base class for the node pool.
|
||||||
|
This holds all the nodes that are to be used by the bot.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_nodes: dict = {}
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Pomice.NodePool node_count={self.node_count}>"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nodes(self):
|
||||||
|
"""Property which returns a dict with the node identifier and the Node object."""
|
||||||
|
return self._nodes
|
||||||
|
|
||||||
|
@property
|
||||||
|
def node_count(self):
|
||||||
|
return len(self._nodes.values())
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_node(self, *, identifier: str = None) -> Node:
|
||||||
|
"""Fetches a node from the node pool using it's identifier.
|
||||||
|
If no identifier is provided, it will choose a node at random.
|
||||||
|
"""
|
||||||
|
available_nodes = {identifier: node for identifier, node in self._nodes.items()}
|
||||||
|
if not available_nodes:
|
||||||
|
raise NoNodesAvailable('There are no nodes available.')
|
||||||
|
|
||||||
|
if identifier is None:
|
||||||
|
return random.choice(list(available_nodes.values()))
|
||||||
|
|
||||||
|
return available_nodes.get(identifier, None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def create_node(
|
||||||
|
self,
|
||||||
|
bot: Type[discord.Client],
|
||||||
|
host: str,
|
||||||
|
port: str,
|
||||||
|
password: str,
|
||||||
|
identifier: str,
|
||||||
|
spotify_client_id: Optional[str] = None,
|
||||||
|
spotify_client_secret: Optional[str] = None
|
||||||
|
) -> Node:
|
||||||
|
"""Creates a Node object to be then added into the node pool.
|
||||||
|
For Spotify searching capabilites, pass in valid Spotify API credentials.
|
||||||
|
"""
|
||||||
|
if identifier in self._nodes.keys():
|
||||||
|
raise NodeCreationError(f"A node with identifier '{identifier}' already exists.")
|
||||||
|
|
||||||
|
node = Node(
|
||||||
|
pool=self, bot=bot, host=host, port=port, password=password,
|
||||||
|
identifier=identifier, spotify_client_id=spotify_client_id,
|
||||||
|
spotify_client_secret=spotify_client_secret
|
||||||
|
)
|
||||||
|
|
||||||
|
await node.connect()
|
||||||
|
self._nodes[node._identifier] = node
|
||||||
|
return node
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ from discord.ext import commands
|
||||||
|
|
||||||
from . import events, filters, objects
|
from . import events, filters, objects
|
||||||
from .exceptions import TrackInvalidPosition
|
from .exceptions import TrackInvalidPosition
|
||||||
from .pool import NodePool
|
from .node import NodePool
|
||||||
|
|
||||||
|
|
||||||
class Player(VoiceProtocol):
|
class Player(VoiceProtocol):
|
||||||
|
|
|
||||||
|
|
@ -1,68 +0,0 @@
|
||||||
import random
|
|
||||||
from typing import Optional, Type
|
|
||||||
|
|
||||||
import discord
|
|
||||||
|
|
||||||
from .exceptions import NodeCreationError, NoNodesAvailable
|
|
||||||
from .node import Node
|
|
||||||
|
|
||||||
|
|
||||||
class NodePool:
|
|
||||||
"""The base class for the node pool.
|
|
||||||
This holds all the nodes that are to be used by the bot.
|
|
||||||
"""
|
|
||||||
|
|
||||||
_nodes: dict = {}
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return f"<Pomice.NodePool node_count={self.node_count}>"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def nodes(self):
|
|
||||||
"""Property which returns a dict with the node identifier and the Node object."""
|
|
||||||
return self._nodes
|
|
||||||
|
|
||||||
@property
|
|
||||||
def node_count(self):
|
|
||||||
return len(self._nodes.values())
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def get_node(self, *, identifier: str = None) -> Node:
|
|
||||||
"""Fetches a node from the node pool using it's identifier.
|
|
||||||
If no identifier is provided, it will choose a node at random.
|
|
||||||
"""
|
|
||||||
available_nodes = {identifier: node for identifier, node in self._nodes.items()}
|
|
||||||
if not available_nodes:
|
|
||||||
raise NoNodesAvailable('There are no nodes available.')
|
|
||||||
|
|
||||||
if identifier is None:
|
|
||||||
return random.choice(list(available_nodes.values()))
|
|
||||||
|
|
||||||
return available_nodes.get(identifier, None)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
async def create_node(
|
|
||||||
self,
|
|
||||||
bot: Type[discord.Client],
|
|
||||||
host: str,
|
|
||||||
port: str,
|
|
||||||
password: str,
|
|
||||||
identifier: str,
|
|
||||||
spotify_client_id: Optional[str] = None,
|
|
||||||
spotify_client_secret: Optional[str] = None
|
|
||||||
) -> Node:
|
|
||||||
"""Creates a Node object to be then added into the node pool.
|
|
||||||
For Spotify searching capabilites, pass in valid Spotify API credentials.
|
|
||||||
"""
|
|
||||||
if identifier in self._nodes.keys():
|
|
||||||
raise NodeCreationError(f"A node with identifier '{identifier}' already exists.")
|
|
||||||
|
|
||||||
node = Node(
|
|
||||||
pool=self, bot=bot, host=host, port=port, password=password,
|
|
||||||
identifier=identifier, spotify_client_id=spotify_client_id,
|
|
||||||
spotify_client_secret=spotify_client_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
await node.connect()
|
|
||||||
self._nodes[node._identifier] = node
|
|
||||||
return node
|
|
||||||
Loading…
Reference in New Issue