Skip to content

base

PlayCallback

Bases: Protocol

Callback that gets called upon playing a title, it accepts a anime and the stream being played.

__call__(anime, stream)

Parameters:

Name Type Description Default
anime Anime

The anime argument passed to the callback. This is the currently playing anime.

required
stream ProviderStream

The stream argument passed to the callback. That is the currently playing stream.

required
Source code in api/src/anipy_api/player/base.py
def __call__(self, anime: "Anime", stream: "ProviderStream"):
    """
    Args:
        anime: The anime argument passed to the callback. This is the currently playing anime.
        stream: The stream argument passed to the callback. That is the currently playing stream.
    """
    ...

PlayerBase(play_callback=None)

Bases: ABC

The abstract base class for all the players.

To list available players or get one by name, use list_players and get_player respectively.

Parameters:

Name Type Description Default
play_callback Optional[PlayCallback]

Callback called upon starting to play a title with play_title

None
Source code in api/src/anipy_api/player/base.py
def __init__(self, play_callback: Optional[PlayCallback] = None):
    """__init__ of PlayerBase

    Args:
        play_callback: Callback called upon starting to play a title with `play_title`
    """
    self._play_callback = play_callback

kill_player() abstractmethod

Kill the player.

Source code in api/src/anipy_api/player/base.py
@abstractmethod
def kill_player(self):
    """Kill the player."""
    ...

play_file(path) abstractmethod

Play any file.

Parameters:

Name Type Description Default
path str

The path to the file

required
Source code in api/src/anipy_api/player/base.py
@abstractmethod
def play_file(self, path: str):
    """Play any file.

    Args:
        path: The path to the file
    """
    ...

play_title(anime, stream) abstractmethod

Play a stream of an anime.

Parameters:

Name Type Description Default
anime Anime

The anime

required
stream ProviderStream

The stream

required
Source code in api/src/anipy_api/player/base.py
@abstractmethod
def play_title(self, anime: "Anime", stream: "ProviderStream"):
    """Play a stream of an anime.

    Args:
        anime: The anime
        stream: The stream
    """
    ...

wait() abstractmethod

Wait for the player to stop/close.

Source code in api/src/anipy_api/player/base.py
@abstractmethod
def wait(self):
    """Wait for the player to stop/close."""
    ...

SubProcessPlayerBase(player_path, extra_args, play_callback=None)

Bases: PlayerBase

The base class for all players that are run through a sub process.

For documentation of the other functions look at the base class.

Example

Here is how you might implement such a player on your own:

class Mpv(SubProcessPlayerBase):
    def __init__(self, player_path: str, extra_args: List[str] = [], rpc_client=None):
        self.player_args_template = [ # (1)
            "{stream_url}",
            "--force-media-title={media_title}",
            "--force-window=immediate",
            *extra_args,
        ]

        super().__init__(
            rpc_client=rpc_client,
            player_path=player_path,
            extra_args=extra_args
        )

  1. This is the important part, those arguments will later be passed to the player. There are two format fields you can use {stream_url} and {media_title}.

Attributes:

Name Type Description
player_args_template List[str]

A list of arguments that are passed to the player command. Fields that are replaced are {media_title} and {stream_url}. This is only important if you are implementing your own player.

Parameters:

Name Type Description Default
player_path str

The path to the player's executable

required
extra_args List[str]

Extra arguments to be passed to the player

required
play_callback Optional[PlayCallback]

Callback called upon starting to play a title with play_title

None
Source code in api/src/anipy_api/player/base.py
@abstractmethod
def __init__(
    self,
    player_path: str,
    extra_args: List[str],
    play_callback: Optional[PlayCallback] = None,
):
    """__init__ for SubProcessPlayerBase

    Args:
        player_path: The path to the player's executable
        extra_args: Extra arguments to be passed to the player
        play_callback: Callback called upon starting to play a title with `play_title`
    """
    super().__init__(play_callback)

    self._sub_proc = None
    self._player_exec = player_path