anime-downloader/anime_downloader/cli.py

149 lines
4.6 KiB
Python
Raw Normal View History

import click
2018-02-05 07:01:37 -08:00
import subprocess
import sys
2018-06-02 11:18:05 -07:00
import os
2018-05-27 10:01:49 -07:00
import logging
from anime_downloader.sites import get_anime_class
2018-05-27 10:01:49 -07:00
from anime_downloader.sites.exceptions import NotFoundError
2018-06-01 01:13:44 -07:00
from anime_downloader.players.mpv import mpv
2018-05-27 10:01:49 -07:00
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
from anime_downloader import util
from anime_downloader.config import Config
2018-05-31 04:04:47 -07:00
from anime_downloader import watch as _watch
echo = click.echo
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
@click.group(context_settings=Config.CONTEXT_SETTINGS)
2018-05-30 03:56:27 -07:00
def cli():
2018-05-30 04:29:23 -07:00
"""Anime Downloader
Download or watch your favourite anime
"""
2018-05-30 03:56:27 -07:00
pass
2018-05-31 04:04:47 -07:00
# NOTE: Don't put defaults here. Add them to the dict in config
2018-05-30 03:56:27 -07:00
@cli.command()
@click.argument('anime_url')
2018-05-27 10:01:49 -07:00
@click.option('--episodes', '-e', 'episode_range', metavar='<int>:<int>',
2018-02-05 08:54:30 -08:00
help="Range of anime you want to download in the form <start>:<end>")
2018-06-02 11:18:05 -07:00
@click.option('--save-playlist', '-p', 'save_playlist', type=bool, is_flag=True,
2018-05-27 10:01:49 -07:00
help="If flag is set, saves the stream urls in an m3u file instead of downloading")
2018-05-31 04:04:47 -07:00
@click.option('--url', '-u', type=bool, is_flag=True,
2018-05-27 10:01:49 -07:00
help="If flag is set, prints the stream url instead of downloading")
2018-02-05 07:01:37 -08:00
@click.option('--play', 'player', metavar='PLAYER',
help="Streams in the specified player")
2018-06-02 11:18:05 -07:00
@click.option('--skip-download', is_flag=True,
2018-02-05 08:54:30 -08:00
help="Retrieve without downloading")
2018-05-31 04:04:47 -07:00
@click.option('--download-dir', help="Specifiy the directory to download to")
2018-05-27 10:01:49 -07:00
@click.option('--quality', '-q', type=click.Choice(['360p', '480p', '720p']),
2018-02-05 08:54:30 -08:00
help='Specify the quality of episode. Default-720p')
2018-06-02 11:18:05 -07:00
@click.option('--force-download', '-f', is_flag=True,
help='Force downloads even if file exists')
2018-05-27 10:01:49 -07:00
@click.option('--log-level', '-ll', 'log_level',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'ERROR']),
help='Sets the level of logger')
2018-05-31 04:04:47 -07:00
@click.pass_context
2018-06-02 11:18:05 -07:00
def dl(ctx, anime_url, episode_range, save_playlist, url, player, skip_download, quality,
force_download, log_level, download_dir):
2018-05-30 04:29:23 -07:00
""" Download the anime using the url or search for it.
"""
2018-05-28 12:36:40 -07:00
2018-05-27 10:01:49 -07:00
util.setup_logger(log_level)
2018-06-02 11:18:05 -07:00
if url or player:
skip_download = True
2018-05-27 10:01:49 -07:00
cls = get_anime_class(anime_url)
if not cls:
anime_url = util.search_and_get_url(anime_url)
cls = get_anime_class(anime_url)
2018-05-28 12:36:40 -07:00
2018-02-04 15:57:10 -08:00
try:
anime = cls(anime_url, quality=quality, path=download_dir)
2018-02-04 15:57:10 -08:00
except NotFoundError as e:
echo(e.args[0])
return
2018-05-27 10:01:49 -07:00
if episode_range is None:
episode_range = '1:'+str(len(anime)+1)
2018-06-02 11:18:05 -07:00
if download_dir:
logging.info('Downloading to {}'.format(os.path.abspath(download_dir)))
logging.info('Found anime: {}'.format(anime.title))
try:
2018-05-27 10:01:49 -07:00
start, end = [int(x) for x in episode_range.split(':')]
anime._episodeIds = anime._episodeIds[start-1:end-1]
except ValueError:
# Only one episode specified
2018-05-27 10:01:49 -07:00
anime = [anime[int(episode_range)-1]]
for episode in anime:
if url:
print(episode.stream_url)
continue
2018-02-05 07:01:37 -08:00
if player:
p = subprocess.Popen([player, episode.stream_url])
p.wait()
2018-06-02 11:18:05 -07:00
if not skip_download:
episode.download(force_download)
2018-02-05 07:01:37 -08:00
print()
2018-05-28 12:36:40 -07:00
2018-05-30 03:56:27 -07:00
@cli.command()
2018-05-31 04:04:47 -07:00
@click.argument('anime_name', required=False)
@click.option('--new', '-n', type=bool, is_flag=True,
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")
2018-06-01 01:13:44 -07:00
@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):
"""
WORK IN PROGRESS: MAY NOT WORK
"""
2018-06-01 01:13:44 -07:00
util.setup_logger(log_level)
2018-05-31 04:04:47 -07:00
watcher = _watch.Watcher()
2018-05-30 03:56:27 -07:00
2018-05-31 04:04:47 -07:00
if new:
if anime_name:
query = anime_name
else:
query = click.prompt('Enter a anime name or url', type=str)
2018-05-30 03:56:27 -07:00
2018-05-31 04:04:47 -07:00
url = util.search_and_get_url(query)
2018-05-28 12:36:40 -07:00
2018-05-31 04:04:47 -07:00
watcher.new(url)
sys.exit(0)
2018-05-28 12:36:40 -07:00
2018-05-31 04:04:47 -07:00
if _list:
watcher.list()
sys.exit(0)
if anime_name:
anime = watcher.get(anime_name)
2018-05-28 12:36:40 -07:00
2018-06-01 01:13:44 -07:00
logging.info('Got {}'.format(anime.title))
for idx, episode in enumerate(anime[anime.episodes_done:]):
player = mpv(episode.stream_url)
returncode = player.play()
if returncode == mpv.STOP:
break
# elif returncode == mpv.CONNECT_ERR:
# retry
2018-05-28 12:36:40 -07:00
2018-06-01 01:13:44 -07:00
watcher.update(anime, idx+1)