watch finally works 🎉

master
Vishnunarayan K I 2018-06-03 00:41:43 +05:30
parent 50bdcd2313
commit e9bdb1322b
4 changed files with 46 additions and 21 deletions

View File

@ -104,14 +104,14 @@ def dl(ctx, anime_url, episode_range, save_playlist, url, player, skip_download,
help="Create a new anime to watch")
@click.option('--list', '-l', '_list', type=bool, is_flag=True,
help="List all animes in watch list")
@click.option('--player', metavar='PLAYER',
help="Streams in the specified player")
@click.option('--quality', '-q', type=click.Choice(['360p', '480p', '720p']),
help='Specify the quality of episode.')
@click.option('--log-level', '-ll', 'log_level',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'ERROR']),
help='Sets the level of logger', default='INFO')
def watch(anime_name, new, _list, player, log_level):
def watch(anime_name, new, _list, quality, log_level):
"""
WORK IN PROGRESS: MAY NOT WORK
With watch you can keep track of any anime you watch.
"""
util.setup_logger(log_level)
watcher = _watch.Watcher()
@ -133,16 +133,23 @@ def watch(anime_name, new, _list, player, log_level):
if anime_name:
anime = watcher.get(anime_name)
anime.quality = quality
logging.info('Got {}'.format(anime.title))
logging.info('Found {}'.format(anime.title))
to_watch = anime[anime.episodes_done:]
for idx, episode in enumerate(anime[anime.episodes_done:]):
player = mpv(episode.stream_url)
returncode = player.play()
for idx, episode in enumerate(to_watch):
if returncode == mpv.STOP:
for tries in range(5):
logging.info('Playing episode {}'.format(anime.episodes_done+1))
player = mpv(episode.stream_url)
returncode = player.play()
if returncode == mpv.STOP:
sys.exit(0)
elif returncode == mpv.CONNECT_ERR:
logging.warning("Couldn't connect. Retrying.")
continue
anime.episodes_done += 1
watcher.update(anime)
break
# elif returncode == mpv.CONNECT_ERR:
# retry
watcher.update(anime, idx+1)

View File

@ -14,7 +14,11 @@ DEFAULT_CONFIG = {
'download_dir': '.',
'quality': '720p',
'force_download': False,
'log_level': 'INFO'
'log_level': 'INFO',
},
'watch': {
'quality': '720p',
'log_level': 'INFO',
}
}

View File

@ -44,7 +44,7 @@ class BasePlayer(metaclass=ABCMeta):
def play(self):
cmd = [self._get_executable()] + self.args
logging.debug('Command: {}'.format(cmd))
self.process = subprocess.Popen(cmd)
self.process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
returncode = self.process.wait()
return returncode

View File

@ -25,7 +25,9 @@ class Watcher:
def list(self):
animes = self._read_from_watch_file()
click.echo('{:>5} | {:^35} | {:^8} | {:^10}'.format('SlNo', 'Name', 'Eps', 'Type'))
click.echo('{:>5} | {:^35} | {:^8} | {:^10}'.format(
'SlNo', 'Name', 'Eps', 'Type'
))
click.echo('-'*65)
fmt_str = '{:5} | {:35.35} | {:3}/{:<3} | {meta:10.10}'
@ -37,24 +39,36 @@ class Watcher:
def get(self, anime_name):
animes = self._read_from_watch_file()
match = process.extractOne(anime_name, animes, score_cutoff=10)
return match[0]
def remove(self, anime_name):
animes = self._read_from_watch_file()
animes = [anime for anime in animes if anime.title != anime_name]
self._write_to_watch_file(animes)
def update(self, changed_anime):
animes = self._read_from_watch_file()
animes = [anime for anime in animes
if anime.title != changed_anime.title]
animes = [changed_anime] + animes
self._write_to_watch_file(animes)
def _append_to_watch_file(self, anime):
if not os.path.exists(self.WATCH_FILE):
with open(self.WATCH_FILE, 'wb') as watch_file:
pickle.dump([anime], watch_file)
self._write_to_watch_file([anime])
return
with open(self.WATCH_FILE, 'rb') as watch_file:
data = pickle.load(watch_file)
data.append(anime)
data = [anime] + data
self._write_to_watch_file(data)
def _write_to_watch_file(self, animes):
with open(self.WATCH_FILE, 'wb') as watch_file:
pickle.dump(data, watch_file)
pickle.dump(animes, watch_file)
def _read_from_watch_file(self):
if not os.path.exists(self.WATCH_FILE):