anime-downloader/anime_downloader/cli.py

60 lines
1.9 KiB
Python
Raw Normal View History

import click
2018-02-05 07:01:37 -08:00
import subprocess
2018-02-04 15:57:10 -08:00
from .anime import Anime, NotFoundError
echo = click.echo
@click.command()
@click.argument('anime_url')
2018-02-05 08:54:30 -08:00
@click.option('--range', 'range_', metavar='<int>:<int>',
help="Range of anime you want to download in the form <start>:<end>")
@click.option('--playlist', default=False, type=bool, is_flag=True,
help="If flag is set, saves the stream urls in an m3u file")
@click.option('--url', default=False, type=bool, is_flag=True,
help="If flag is set, prints the stream url and not download")
2018-02-05 07:01:37 -08:00
@click.option('--play', 'player', metavar='PLAYER',
help="Streams in the specified player")
2018-02-05 08:54:30 -08:00
@click.option('--no-download', default=False, is_flag=True,
help="Retrieve without downloading")
2018-02-05 07:01:37 -08:00
@click.option('--quality', type=click.Choice(['360p', '480p', '720p']),
2018-02-05 08:54:30 -08:00
default='720p',
help='Specify the quality of episode. Default-720p')
2018-02-05 07:01:37 -08:00
def cli(anime_url, range_, playlist, url, player, no_download, quality):
""" Anime Downloader
Download your favourite anime.
"""
2018-02-04 15:57:10 -08:00
try:
2018-02-05 07:01:37 -08:00
anime = Anime(anime_url, quality=quality,
callback=lambda message: print('[INFO] '+message))
2018-02-04 15:57:10 -08:00
except NotFoundError as e:
echo(e.args[0])
return
2018-02-05 07:01:37 -08:00
if url or player:
no_download = True
if range is None:
2018-02-05 07:01:37 -08:00
range_ = '1:'+str(len(anime)+1)
try:
2018-02-05 07:01:37 -08:00
start, end = [int(x) for x in range_.split(':')]
anime._episodeIds = anime._episodeIds[start-1:end-1]
except ValueError:
# Only one episode specified
2018-02-05 07:01:37 -08:00
anime = [anime[int(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()
if not no_download:
episode.download()
print()