anime-downloader/anime_downloader/cli.py

88 lines
2.3 KiB
Python
Raw Normal View History

import click
import sys
2018-06-02 11:18:05 -07:00
import os
2019-02-23 08:23:42 -08:00
import importlib
2018-05-27 10:01:49 -07:00
2018-06-03 10:30:26 -07:00
from anime_downloader.__version__ import __version__
2018-05-27 10:01:49 -07:00
2018-06-02 11:18:05 -07:00
from anime_downloader.config import Config
2019-02-23 08:23:42 -08:00
from anime_downloader import util
echo = click.echo
2018-05-31 04:04:47 -07:00
2021-08-06 13:55:12 -07:00
def check_for_update():
from pkg_resources import parse_version
import requests
import re
version_file = "https://raw.githubusercontent.com/anime-dl/anime-downloader/master/anime_downloader/__version__.py"
2021-08-06 14:00:44 -07:00
regex = r"__version__\s*=\s*[\"'](\d+\.\d+\.\d+)[\"']"
2021-08-06 13:55:12 -07:00
r = requests.get(version_file)
if not r.ok:
return
current_ver = parse_version(__version__)
remote_ver = parse_version(re.match(regex, r.text).group(1))
if remote_ver > current_ver:
print(
"New version (on GitHub) is available: {} -> {}\n".format(
current_ver, remote_ver
)
)
2019-02-23 08:23:42 -08:00
class CLIClass(click.MultiCommand):
2018-06-09 08:24:42 -07:00
2019-02-23 08:23:42 -08:00
def list_commands(self, ctx):
commands_dir = os.path.join(os.path.dirname(__file__), 'commands')
rv = []
for filename in os.listdir(commands_dir):
2019-07-23 09:50:12 -07:00
if filename == '__init__.py':
continue
2019-02-23 08:23:42 -08:00
if filename.endswith('.py'):
rv.append(filename[:-3])
rv.sort()
return rv
2018-06-09 08:24:42 -07:00
2019-02-23 08:23:42 -08:00
def get_command(self, ctx, name):
command = importlib.import_module(
"anime_downloader.commands.{}".format(name))
return command.command
2018-06-09 08:24:42 -07:00
2018-05-28 12:36:40 -07:00
2019-02-23 08:23:42 -08:00
@click.group(cls=CLIClass, context_settings=Config.CONTEXT_SETTINGS)
@click.version_option(version=__version__)
@click.option(
2019-02-23 08:23:42 -08:00
'--log-level', '-ll',
type=click.Choice(['ERROR', 'WARNING', 'INFO', 'DEBUG']),
default='INFO',
help="Log Level"
)
2019-02-23 08:23:42 -08:00
def cli(log_level):
"""Anime Downloader
2019-02-23 08:23:42 -08:00
Download or watch your favourite anime
"""
2018-06-01 01:13:44 -07:00
util.setup_logger(log_level)
# if not util.check_in_path('aria2c'):
# raise logger.ERROR("Aria2 is not in path. Please follow installation instructions: https://github.com/vn-ki/anime-downloader/wiki/Installation")
2018-05-31 04:04:47 -07:00
2018-06-02 13:04:39 -07:00
2019-02-23 08:23:42 -08:00
def main():
2021-08-06 13:55:12 -07:00
try:
check_for_update()
except Exception:
pass
2018-06-03 05:43:19 -07:00
try:
2019-02-23 08:23:42 -08:00
cli()
except Exception as e:
if 'DEBUG' in sys.argv:
raise
click.echo(click.style('ERROR:', fg='black', bg='red') +
' ' + click.style(str(e), fg='red'))
2019-03-10 12:49:12 -07:00
sys.exit(1)