Skip to content

Commit

Permalink
Feat: Added --quiet to all commands. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
justfoolingaround committed Jul 7, 2021
1 parent 5d712d5 commit c9d7392
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 33 deletions.
2 changes: 0 additions & 2 deletions animdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import click

from core import package_banner
from core.cli.commands import download, stream, continuation, grab, schedule, test
from core.cli.helpers.player import supported_streamers

Expand All @@ -26,5 +25,4 @@ def __animdl_cli__():
pass

if __name__ == '__main__':
print("\x1b[35m{}\x1b[39m".format(package_banner))
__animdl_cli__()
11 changes: 0 additions & 11 deletions core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
from .__version__ import __core__

package_banner = """\
░█████╗░███╗░░██╗██╗███╗░░░███╗██████╗░██╗░░░░░
██╔══██╗████╗░██║██║████╗░████║██╔══██╗██║░░░░░
███████║██╔██╗██║██║██╔████╔██║██║░░██║██║░░░░░
██╔══██║██║╚████║██║██║╚██╔╝██║██║░░██║██║░░░░░
██║░░██║██║░╚███║██║██║░╚═╝░██║██████╔╝███████╗
╚═╝░░╚═╝╚═╝░░╚══╝╚═╝╚═╝░░░░░╚═╝╚═════╝░╚══════╝v{}
A highly efficient anime downloader and streamer
""".format(__core__)
8 changes: 5 additions & 3 deletions core/cli/commands/download.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from core.cli.helpers.fun import bannerify
import os
from pathlib import Path

Expand All @@ -23,7 +24,9 @@
@click.option('--mixed', is_flag=True, default=True, help="Auto-skip mixed fillers/canons (If filler list is configured).")
@click.option('--canon', is_flag=True, default=True, help="Auto-skip canons (If filler list is configured).")
@click.option('--idm', is_flag=True, default=False, help="Download anime using Internet Download Manager")
def animdl_download(query, anonymous, start, end, title, filler_list, offset, filler, mixed, canon, idm):
@click.option('--quiet', help='A flag to silence all the announcements.', is_flag=True, flag_value=True)
@bannerify
def animdl_download(query, anonymous, start, end, title, filler_list, offset, filler, mixed, canon, idm, quiet):
"""
Download call.
"""
Expand All @@ -32,8 +35,7 @@ def animdl_download(query, anonymous, start, end, title, filler_list, offset, fi
session = requests.Session()

anime, provider = process_query(session, query)
ts = lambda x: to_stdout(x, 'animdl-%s-downloader-core' % provider)
tx = lambda x: to_stdout(x, 'animdl-protip')
ts = lambda x: to_stdout(x, 'animdl-%s-downloader-core' % provider) if not quiet else None
content_name = title or anime.get('name')
if not content_name:
content_name = choice(create_random_titles())
Expand Down
23 changes: 16 additions & 7 deletions core/cli/commands/grab.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from core.cli.helpers.fun import bannerify
import click
import requests

Expand All @@ -11,22 +12,30 @@
@click.option('-s', '--start', help="An integer that determines where to begin the grabbing from.", required=False, default=1, show_default=False, type=int)
@click.option('-e', '--end', help="A integer that determines where to end the grabbing at.", required=False, default=0, show_default=False, type=int)
@click.option('-f', '--file', help="File to write all the grabbed content to.", required=False, default='', show_default=False, type=str)
def animdl_grab(query, start, end, file):
@click.option('--quiet', help='A flag to silence all the announcements.', is_flag=True, flag_value=True)
@bannerify
def animdl_grab(query, start, end, file, quiet):
end = end or float('inf')
session = requests.Session()
anime, provider = process_query(session, query)
ts = lambda x: to_stdout(x, 'animdl-%s-grabber-core' % provider)
ts = lambda x: to_stdout(x, 'animdl-%s-grabber-core' % provider) if not quiet else None
anime_associator = Associator(anime.get('anime_url'))
ts("Initializing grabbing session.")
collected_streams = []
collected_streams = []

if file:
file += ".json" if not file.endswith('.json') else ''

for stream_url_caller, episode in anime_associator.raw_fetch_using_check(check=lambda x: end >= x >= start):
stream_url = stream_url_caller()
collected_streams.append({'episode': episode, 'streams': stream_url})
if file:
file += ".json" if not file.endswith('.json') else ''
to_stdout('Write -> "%s"' % file, 'E%02d' % episode)
with open(file, 'w') as json_file_writer:
json.dump(collected_streams, json_file_writer, indent=4)
try:
with open(file, 'w') as json_file_writer:
json.dump(collected_streams, json_file_writer, indent=4)
except WindowsError:
ts("Failed to attempt I/O on the file at the moment; the unwritten values will be written in the next I/O.")
else:
to_stdout(stream_url, 'E%02d' % episode)
to_stdout(json.dumps({'episode': episode, 'streams': stream_url}), ('E%02d' % episode) if not quiet else '')
ts("Grabbing session complete.")
5 changes: 4 additions & 1 deletion core/cli/commands/schedule.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from core.cli.helpers.fun import bannerify
import click
import requests

Expand Down Expand Up @@ -27,7 +28,9 @@ def merge_dicts(dict1, dict2):


@click.command(name='schedule', help="Know which animes are going over the air when.")
def animdl_schedule():
@click.option('--quiet', help='A flag to silence all the announcements.', is_flag=True, flag_value=True)
@bannerify
def animdl_schedule(quiet):

with requests.get(LIVECHART + 'schedule/tv', allow_redirects=True, headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.102 Safari/537.36'}) as livechart_page:
content = arrange_template(htmlparser.fromstring(livechart_page.text))
Expand Down
7 changes: 5 additions & 2 deletions core/cli/commands/stream.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from core.cli.helpers.fun import bannerify
import click
import requests

Expand Down Expand Up @@ -34,8 +35,10 @@ def quality_prompt(stream_list, provider):
@click.option('--filler', is_flag=True, default=True, help="Auto-skip fillers (If filler list is configured).")
@click.option('--mixed', is_flag=True, default=True, help="Auto-skip mixed fillers/canons (If filler list is configured).")
@click.option('--canon', is_flag=True, default=True, help="Auto-skip canons (If filler list is configured).")
@click.option('--quiet', help='A flag to silence all the announcements.', is_flag=True, flag_value=True)
@bannerify
def animdl_stream(query, anonymous, start, title, filler_list, offset,
player_opts, mpv, vlc, filler, mixed, canon):
player_opts, mpv, vlc, filler, mixed, canon, quiet):
"""
Streamer call for animdl streaming session.
"""
Expand All @@ -46,7 +49,7 @@ def animdl_stream(query, anonymous, start, title, filler_list, offset,
return to_stdout('Streaming failed due to selection of a unsupported streamer; please configure the streamer in the config to use it.', caller='animdl-stream-failure')

anime, provider = process_query(session, query)
ts = lambda x: to_stdout(x, 'animdl-%s-streamer-core' % provider)
ts = lambda x: to_stdout(x, 'animdl-%s-streamer-core' % provider) if not quiet else None
ts('Now initiating your stream session')
content_name = title or anime.get('name') or choice(create_random_titles())
ts("Starting stream session @ [%02d/?]" % start)
Expand Down
5 changes: 4 additions & 1 deletion core/cli/commands/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from core.cli.helpers.fun import bannerify
import click
import requests

Expand All @@ -19,7 +20,9 @@
@click.command(name='test', help="Test the scrapability power.")
@click.option('-x', help='A list of certain sites (full anime url) to be explicity used for testing.', default=[], required=False, multiple=True)
@click.option('-e', help='Episode number to bestow the testing upon', default=1, required=False, type=int)
def animdl_test(x, e):
@click.option('--quiet', help='A flag to silence all the announcements.', is_flag=True, flag_value=True)
@bannerify
def animdl_test(x, e, quiet):
if not x:
x = SITE_LIST.values()

Expand Down
28 changes: 25 additions & 3 deletions core/cli/helpers/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
import re
from random import choice

from ...__version__ import __core__

package_banner = """\
░█████╗░███╗░░██╗██╗███╗░░░███╗██████╗░██╗░░░░░
██╔══██╗████╗░██║██║████╗░████║██╔══██╗██║░░░░░
███████║██╔██╗██║██║██╔████╔██║██║░░██║██║░░░░░
██╔══██║██║╚████║██║██║╚██╔╝██║██║░░██║██║░░░░░
██║░░██║██║░╚███║██║██║░╚═╝░██║██████╔╝███████╗
╚═╝░░╚═╝╚═╝░░╚══╝╚═╝╚═╝░░░░░╚═╝╚═════╝░╚══════╝v{}
A highly efficient anime downloader and streamer
""".format(__core__)

URL_REGEX = re.compile(r"(?:https?://)?(?P<base>(?:\S+\.)+[^/]+)/(?:(?:[^/])+/)*(?P<url_end>[^?/]+)")

LANGUAGE = {
Expand All @@ -34,8 +46,10 @@ def create_random_titles():
"%s-in-the-%s" % (choice(noun), choice(noun)),
]

def to_stdout(message, caller='animdl'):
return print("[\x1b[36m%s\x1b[39m] %s" % (caller, message))
def to_stdout(message, caller='animdl', *, color_index=36):
if caller:
message = "[\x1b[{}m{}\x1b[39m] ".format(color_index, caller) + message
return print(message)

def stream_judiciary(url):
"""
Expand All @@ -46,4 +60,12 @@ def stream_judiciary(url):
return "Unknown"

base, url_end = match.group('base', 'url_end')
return "'%s' from %s" % (url_end, LABELS.get(base, base))
return "'%s' from %s" % (url_end, LABELS.get(base, base))

def bannerify(f):
def internal(*args, **kwargs):
quiet_state = kwargs.get('quiet')
if quiet_state is not None and not quiet_state:
print("\x1b[35m{}\x1b[39m".format(package_banner))
return f(*args, **kwargs)
return internal
7 changes: 4 additions & 3 deletions core/cli/helpers/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def generate_download_arguments(session_dict):
session_dict.get('afl', {}).get('offset', 0),
session_dict.get('afl', {}).get('allow-filler', False),
session_dict.get('afl', {}).get('allow-canon', False),
session_dict.get('afl', {}).get('allow-mixed', False),
session_dict.get('afl', {}).get('allow-mixed', False),
False
)

def generate_stream_arguments(session_dict):
u, s, e, i, idm, au, ao, af, ac, am = generate_download_arguments(session_dict)
return u, s, i, au, ao, [], True, False, af, ac, am
u, s, e, i, idm, au, ao, af, ac, am, q = generate_download_arguments(session_dict)
return u, s, i, au, ao, [], True, False, af, ac, am, q

def search_identifiers(session_file, identifer):
for session in load_sessions(session_file):
Expand Down

0 comments on commit c9d7392

Please sign in to comment.