Skip to content

Commit

Permalink
Add multiple player basics and IINA support (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strosel committed Oct 16, 2021
1 parent 913e6bc commit 34ba40e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
50 changes: 27 additions & 23 deletions anime_downloader/commands/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from anime_downloader import util
from anime_downloader.__version__ import __version__
from anime_downloader.players.mpv import mpv
from anime_downloader.players import Players
from anime_downloader import watch as _watch
from anime_downloader.config import Config
from anime_downloader.sites import get_anime_class, ALL_ANIME_SITES
Expand Down Expand Up @@ -221,6 +221,9 @@ def list_animes(watcher, quality, download_dir, imp=None, _filter=None):
elif inp == 'watch':
anime.quality = quality
watch_anime(watcher, anime, quality, download_dir)
elif inp.startswith('watch '):
anime.quality = quality
watch_anime(watcher, anime, quality, download_dir, player_class=inp.split('watch ')[-1])

elif inp.startswith('download'):
# You can use download 3:10 for selected episodes
Expand Down Expand Up @@ -299,7 +302,7 @@ def list_animes(watcher, quality, download_dir, imp=None, _filter=None):
watcher.update(anime)


def watch_anime(watcher, anime, quality, download_dir):
def watch_anime(watcher, anime, quality, download_dir, player_name=Config['watch']['default_player']):
autoplay = Config['watch']['autoplay_next']
to_watch = anime[anime.episodes_done:]
logger.debug('Sliced episodes: {}'.format(to_watch._episode_urls))
Expand All @@ -313,30 +316,31 @@ def watch_anime(watcher, anime, quality, download_dir):
'Playing episode {}'.format(episode.ep_no)
)
try:
player = mpv(episode)
player = Players[player_name](episode)

returncode = player.play()
if returncode == player.STOP:
# Returns to watch.
return

elif returncode == player.CONNECT_ERR:
logger.warning("Couldn't connect. Retrying. "
"Attempt #{}".format(tries + 1))
continue

elif returncode == player.PREV:
anime.episodes_done -= 2
watcher.update(anime)
break
# If no other return codes, basically when the player finishes.
# Can't find the returncode for success.
elif autoplay:
break
else:
return
except Exception as e:
anime.episodes_done -= 1
watcher.update(anime)
logger.error(str(e))
sys.exit(1)

returncode = player.play()
if returncode == player.STOP:
# Returns to watch.
return

elif returncode == player.CONNECT_ERR:
logger.warning("Couldn't connect. Retrying. "
"Attempt #{}".format(tries + 1))
continue

elif returncode == player.PREV:
anime.episodes_done -= 2
watcher.update(anime)
break
# If no other return codes, basically when the player finishes.
# Can't find the returncode for success.
elif autoplay:
break
else:
return
4 changes: 3 additions & 1 deletion anime_downloader/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
'log_level': 'INFO',
'provider': 'twist.moe',
'autoplay_next': True,
'mpv_arguments': ''
'mpv_arguments': '',
'iina_arguments': '',
'default_player': 'mpv'
},
'gui': {
'player': 'mpv'
Expand Down
7 changes: 7 additions & 0 deletions anime_downloader/players/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from anime_downloader.players.mpv import mpv
from anime_downloader.players.iina import iina

Players = {
'mpv': mpv,
'iina': iina
}
35 changes: 35 additions & 0 deletions anime_downloader/players/iina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from anime_downloader.players.baseplayer import BasePlayer
from anime_downloader.players.mpv import get_mpv_configfile
from anime_downloader import config
from anime_downloader.config import Config

import os


class iina(BasePlayer):
name = 'iina'

STOP = 50
NEXT = 51
CONNECT_ERR = 2

def _get_executable_windows(self):
return 'iina.exe'

def _get_executable_posix(self):
return 'iina'

@property
def args(self):
# Doesnt use the referer if it's none
launchArgs = Config['watch']['iina_arguments']
if self.episode.source().referer:
return ['--keep-running',
'--input-conf=' + get_mpv_configfile(),
'--http-header-fields=referer: ' + str(self.episode.source().referer),
self.episode.source().stream_url, launchArgs]
else:
return ['--keep-running',
'--input-conf=' + get_mpv_configfile(),
self.episode.source().stream_url, launchArgs]

0 comments on commit 34ba40e

Please sign in to comment.