anime-downloader/anime_downloader/cli.py

93 lines
2.9 KiB
Python
Raw Normal View History

import click
2018-02-05 07:01:37 -08:00
import subprocess
2018-05-27 10:01:49 -07:00
import logging
from anime_downloader.sites.nineanime import NineAnime
2018-05-27 10:01:49 -07:00
from anime_downloader.sites.exceptions import NotFoundError
from . import util
echo = click.echo
@click.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-05-27 10:01:49 -07:00
@click.option('--save-playlist', '-p', 'playlist', default=False, type=bool, is_flag=True,
help="If flag is set, saves the stream urls in an m3u file instead of downloading")
@click.option('--url', '-u', default=False, type=bool, is_flag=True,
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-02-05 08:54:30 -08:00
@click.option('--no-download', default=False, is_flag=True,
help="Retrieve without downloading")
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
default='720p',
help='Specify the quality of episode. Default-720p')
2018-05-27 10:01:49 -07:00
@click.option('--force', '-f', is_flag=True, default=False,
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']),
default='INFO',
help='Sets the level of logger')
def cli(anime_url, episode_range, playlist, url, player, no_download, quality,
force, log_level):
""" Anime Downloader
Download your favourite anime.
"""
2018-05-28 12:36:40 -07:00
2018-05-27 10:01:49 -07:00
util.setup_logger(log_level)
2018-05-28 12:36:40 -07:00
# HACK/XXX: Should use a regex. But a dirty hack for now :/
if '9anime' not in anime_url:
anime_url = search(anime_url)
2018-02-04 15:57:10 -08:00
try:
2018-05-27 10:01:49 -07:00
anime = NineAnime(anime_url, quality=quality)
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
2018-05-27 10:01:49 -07:00
if episode_range is None:
episode_range = '1:'+str(len(anime)+1)
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()
if not no_download:
episode.download(force)
2018-02-05 07:01:37 -08:00
print()
2018-05-28 12:36:40 -07:00
def search(query):
search_results = NineAnime.search(query)
print(util.format_search_results(search_results))
val = click.prompt('Enter the anime no: ', type=int, default=1)
url = search_results[val-1].url
title = search_results[val-1].title
logging.info('Selected {}'.format(title))
return url