Skip to content

Commit

Permalink
refactor!: remove PlayerNotFound and handle FileNotFoundError in cli …
Browse files Browse the repository at this point in the history
…play method.
  • Loading branch information
THEGOLDENPRO committed Apr 28, 2024
1 parent 32b3d8b commit 04913c3
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 149 deletions.
15 changes: 11 additions & 4 deletions mov_cli/cli/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,22 @@ def play(media: Media, metadata: Metadata, scraper: Scraper, episode: EpisodeSel
)
mov_cli_logger.debug(f"Streaming with this url -> '{media.url}'")

popen = chosen_player.play(media)
try:
popen = chosen_player.play(media)
except FileNotFoundError as e:
mov_cli_logger.error(
f"The player '{chosen_player.display_name}' was not found! " \
f"Are you sure you have it installed? Are you sure it's in path? \nError: {e}"
)
return None

if popen is None and platform != "iOS":
mov_cli_logger.error(
f"The player '{config.player.__class__.__name__.lower()}' is not supported on this platform ({platform}). " \
"We recommend VLC for iOS and MPV for every other platform."
f"The player '{chosen_player.display_name}' is not supported on this platform ({platform}). " \
"We recommend VLC for iOS, IINA for MacOS and MPV for every other platform."
)

return False
return None

option = watch_options(popen, chosen_player, platform, media, config.fzf_enabled)

Expand Down
2 changes: 2 additions & 0 deletions mov_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def player(self) -> Player:
return players.VLC(platform, self)
elif value.lower() == "syncplay":
return players.SyncPlay(platform, self)
elif value.lower() == "iina":
return players.IINA(platform, self)

return players.CustomPlayer(value)

Expand Down
15 changes: 2 additions & 13 deletions mov_cli/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

if TYPE_CHECKING:
import logging
from .players import Player

from devgoldyutils import Colours
from .logger import mov_cli_logger

__all__ = (
"MovCliException",
"PlayerNotFound"
"MovCliException"
)

class MovCliException(Exception):
Expand All @@ -22,13 +20,4 @@ def __init__(self, message: str, logger: logging.Logger = None):
logger = mov_cli_logger

logger.critical(message)
super().__init__(message)

# NOTE: I might remove this. ~ Goldy
class PlayerNotFound(MovCliException):
"""Raised when player is not found."""
def __init__(self, player: Player) -> None:
super().__init__(
f"The player '{player.__class__.__name__}' was not found. Are you sure you have it installed? " \
"Are you sure the environment variable is set correctly?"
)
super().__init__(message)
2 changes: 2 additions & 0 deletions mov_cli/players/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .player import *

from .mpv import *
from .vlc import *
from .iina import *
from .syncplay import *
from .custom_player import *
14 changes: 5 additions & 9 deletions mov_cli/players/custom_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import subprocess
from devgoldyutils import Colours, LoggerAdapter

from .. import errors
from ..logger import mov_cli_logger
from .player import Player

Expand All @@ -26,11 +25,8 @@ def __init__(self, player_command: str, **kwargs) -> None:

def play(self, media: Media) -> subprocess.Popen:
"""Plays this media in a custom player."""
logger.info(f"Launching your custom media player '{self.player_command}'...")

try:
return subprocess.Popen(
[self.player_command, media.url]
)
except ModuleNotFoundError:
raise errors.PlayerNotFound(self)
logger.debug(f"Launching your custom media player '{self.player_command}'...")

return subprocess.Popen(
[self.player_command, media.url]
)
50 changes: 50 additions & 0 deletions mov_cli/players/iina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Optional
from ..media import Media
from .. import Config
from ..utils.platform import SUPPORTED_PLATFORMS

import subprocess
from devgoldyutils import Colours

from .player import Player

__all__ = ("IINA",)

class IINA(Player):
def __init__(self, platform: SUPPORTED_PLATFORMS, config: Config, **kwargs) -> None:
self.config = config
self.platform = platform

super().__init__(display_name = Colours.GREY.apply("IINA"), **kwargs)

def play(self, media: Media) -> Optional[subprocess.Popen]:
"""Plays this media in the IINA media player for MacOS."""

if self.platform == "Darwin":
args = [
"iina",
"--no-stdin",
"--keep-running",
media.url,
f"--mpv-force-media-title={media.display_name}",
]

if media.referrer is not None:
args.append(f"--mpv-referrer={media.referrer}")

if media.audio_url is not None: # TODO: This will need testing.
args.append(f"--mpv-audio-file={media.audio_url}")

if media.subtitles is not None: # TODO: This will need testing.
args.append(f"--mpv-sub-file={media.subtitles}")

if self.config.resolution is not None:
args.append(f"--mpv-hls-bitrate={self.config.resolution}") # NOTE: This only works when the file is a m3u8

return subprocess.Popen(args)

return None
69 changes: 18 additions & 51 deletions mov_cli/players/mpv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,21 @@
from ..utils.platform import SUPPORTED_PLATFORMS

import subprocess
from devgoldyutils import Colours, LoggerAdapter
from devgoldyutils import Colours

from .. import errors
from ..logger import mov_cli_logger
from .player import Player

__all__ = ("MPV",)

logger = LoggerAdapter(mov_cli_logger, prefix = Colours.PURPLE.apply("MPV"))

class MPV(Player):
def __init__(self, platform: SUPPORTED_PLATFORMS, config: Config, **kwargs) -> None:
self.platform = platform
self.config = config

super().__init__(**kwargs)
super().__init__(display_name = Colours.PURPLE.apply("MPV"), **kwargs)

def play(self, media: Media) -> Optional[subprocess.Popen]:
"""Plays this media in the MPV media player."""
logger.info("Launching MPV Media Player...")

if self.platform == "Android":
return subprocess.Popen(
Expand All @@ -42,54 +37,26 @@ def play(self, media: Media) -> Optional[subprocess.Popen]:
]
)

try:

if self.platform == "Linux" or self.platform == "Windows":
args = [
"mpv",
media.url,
f"--force-media-title={media.display_name}",
"--no-terminal",
]

if media.referrer is not None:
args.append(f"--referrer={media.referrer}")

if media.audio_url is not None:
args.append(f"--audio-file={media.audio_url}")

if media.subtitles is not None:
args.append(f"--sub-file={media.subtitles}")

if self.config.resolution is not None:
args.append(f"--hls-bitrate={self.config.resolution}") # NOTE: This only works when the file is a m3u8

return subprocess.Popen(args)

elif self.platform == "Darwin":
args = [
"iina",
"--no-stdin",
"--keep-running",
media.url,
f"--mpv-force-media-title={media.display_name}",
]

if media.referrer is not None:
args.append(f"--mpv-referrer={media.referrer}")
elif self.platform == "Linux" or self.platform == "Windows" or self.platform == "Darwin":
args = [
"mpv",
media.url,
f"--force-media-title={media.display_name}",
"--no-terminal",
]

if media.audio_url is not None: # TODO: This will need testing.
args.append(f"--mpv-audio-file={media.audio_url}")
if media.referrer is not None:
args.append(f"--referrer={media.referrer}")

if media.subtitles is not None: # TODO: This will need testing.
args.append(f"--mpv-sub-file={media.subtitles}")
if media.audio_url is not None:
args.append(f"--audio-file={media.audio_url}")

if self.config.resolution is not None:
args.append(f"--mpv-hls-bitrate={self.config.resolution}") # NOTE: This only works when the file is a m3u8
if media.subtitles is not None:
args.append(f"--sub-file={media.subtitles}")

return subprocess.Popen(args)
if self.config.resolution is not None:
args.append(f"--hls-bitrate={self.config.resolution}") # NOTE: This only works when the file is a m3u8

except (ModuleNotFoundError, FileNotFoundError):
raise errors.PlayerNotFound(self)
return subprocess.Popen(args)

return None
80 changes: 34 additions & 46 deletions mov_cli/players/syncplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,63 @@
from ..utils.platform import SUPPORTED_PLATFORMS

import subprocess
from devgoldyutils import Colours, LoggerAdapter
from devgoldyutils import Colours

from .. import errors
from ..logger import mov_cli_logger
from .player import Player

__all__ = ("SyncPlay",)

logger = LoggerAdapter(mov_cli_logger, prefix = Colours.PURPLE.apply("SyncPlay"))

class SyncPlay(Player):
def __init__(self, platform: SUPPORTED_PLATFORMS, config: Config, **kwargs) -> None:
self.platform = platform
self.config = config

super().__init__(display_name = "Syncplay", **kwargs)
super().__init__(display_name = Colours.BLUE.apply("Syncplay"), **kwargs)

def play(self, media: Media) -> Optional[subprocess.Popen]:
"""Plays this media in SyncPlay."""
if self.platform == "Windows" or self.platform == "Linux":
args = [
"syncplay",
media.url,
"--",
f"--force-media-title={media.display_name}",
]

logger.info("Launching SyncPlay...")

try:

if self.platform == "Windows" or self.platform == "Linux":
args = [
"syncplay",
media.url,
"--",
f"--force-media-title={media.display_name}",
]

if media.referrer is not None:
args.append(f"--referrer={media.referrer}")

if media.audio_url is not None:
args.append(f"--audio-file={media.audio_url}")
if media.referrer is not None:
args.append(f"--referrer={media.referrer}")

if media.subtitles is not None:
args.append(f"--sub-file={media.subtitles}")
if media.audio_url is not None:
args.append(f"--audio-file={media.audio_url}")

if self.config.resolution is not None:
args.append(f"--hls-bitrate={self.config.resolution}") # NOTE: Only M3U8
if media.subtitles is not None:
args.append(f"--sub-file={media.subtitles}")

return subprocess.Popen(args)
if self.config.resolution is not None:
args.append(f"--hls-bitrate={self.config.resolution}") # NOTE: Only M3U8

elif self.platform == "Darwin": # NOTE: Limits you to IINA
args = [
"syncplay",
media.url,
"--",
f"--mpv-force-media-title={media.display_name}",
]
return subprocess.Popen(args)

if media.referrer is not None:
args.append(f"--mpv-referrer={media.referrer}")
elif self.platform == "Darwin": # NOTE: Limits you to IINA
args = [
"syncplay",
media.url,
"--",
f"--mpv-force-media-title={media.display_name}",
]

if media.audio_url is not None: # TODO: This will need testing.
args.append(f"--mpv-audio-file={media.audio_url}")
if media.referrer is not None:
args.append(f"--mpv-referrer={media.referrer}")

if media.subtitles is not None: # TODO: This will need testing.
args.append(f"--mpv-sub-file={media.subtitles}")
if media.audio_url is not None: # TODO: This will need testing.
args.append(f"--mpv-audio-file={media.audio_url}")

if self.config.resolution is not None: # TODO: This will need testing.
args.append(f"--mpv-hls-bitrate={self.config.resolution}")
if media.subtitles is not None: # TODO: This will need testing.
args.append(f"--mpv-sub-file={media.subtitles}")

return subprocess.Popen(args)
if self.config.resolution is not None: # TODO: This will need testing.
args.append(f"--mpv-hls-bitrate={self.config.resolution}")

except (ModuleNotFoundError, FileNotFoundError):
raise errors.PlayerNotFound(self)
return subprocess.Popen(args)

return None
Loading

0 comments on commit 04913c3

Please sign in to comment.