Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #48 from diego-aquino/release/v0.5.8
Browse files Browse the repository at this point in the history
Release: v0.5.8
  • Loading branch information
diego-aquino authored Sep 22, 2021
2 parents cc18817 + fede3df commit 4c2200c
Show file tree
Hide file tree
Showing 26 changed files with 1,176 additions and 1,245 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $(VENV)/bin/activate: setup.py requirements.txt
install:
$(PIP) install -e .

clean: clean-build clean-pyc
clean: clean-build clean-pyc clean-media

clean-build:
rm -rf build/
Expand All @@ -33,8 +33,8 @@ clean-pyc:
find . -name '__pycache__' -exec rm -rf {} +

clean-media:
find . -name '*.mp*' -exec rm -f {} +
find . -name '*.webm' -exec rm -f {} +
find . -name '*.mp*' -not -path "./.github/*" -exec rm -f {} +
find . -name '*.webm' -not -path "./.github/*" -exec rm -f {} +

build: clean
$(PYTHON) setup.py sdist bdist_wheel
Expand Down
53 changes: 21 additions & 32 deletions mediakit/actions/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from pytube import YouTube
from pytube.exceptions import RegexMatchError as PytubeRegexMatchError

from mediakit.streams.arguments import command_args
from mediakit.streams.cli import DownloadCLI
from mediakit.cli.arguments import command_args
from mediakit.cli.download import DownloadCLI
from mediakit.utils.files import (
get_filename_from,
read_video_urls_from,
file_exists
file_exists,
remove_all_temporary_files,
)
from mediakit.constants import FFMPEG_BINARY
from mediakit.globals import global_config
Expand All @@ -21,32 +22,23 @@ def _get_video_urls_to_download(arguments):
if not was_batch_file_provided:
return [arguments.video_url]

if file_exists(global_config.batch_file):
video_urls_to_download = read_video_urls_from(global_config.batch_file)
if not file_exists(global_config.batch_file):
raise exceptions.NoSuchFile(global_config.batch_file)

if len(video_urls_to_download) == 0:
raise exceptions.NoVideoURLsInBatchFile(
global_config.batch_file
)
video_urls_to_download = read_video_urls_from(global_config.batch_file)

return video_urls_to_download
if len(video_urls_to_download) == 0:
raise exceptions.NoVideoURLsInBatchFile(global_config.batch_file)

else:
raise exceptions.NoSuchFile(global_config.batch_file)
return video_urls_to_download


def download():
def on_download_progress(stream, chunk, bytes_remaining):
download_cli.update_download_progress_info(bytes_remaining)

arguments = command_args.parse_download_arguments()

try:
video_urls_to_download = _get_video_urls_to_download(arguments)
except (
exceptions.NoSuchFile,
exceptions.NoVideoURLsInBatchFile
) as exception:
except (exceptions.NoSuchFile, exceptions.NoVideoURLsInBatchFile) as exception:
exception.show_message()
return

Expand All @@ -69,25 +61,20 @@ def on_download_progress(stream, chunk, bytes_remaining):
is_last_video = video_url_index == len(video_urls_to_download) - 1

try:
download_cli.show_loading_label()
download_cli.mark_as_loading(True)

video = YouTube(video_url)
download_cli.register_download_info(
video,
output_path,
filename,
formats
)
download_cli.register_download_info(video, output_path, filename, formats)
download_cli.mark_as_loading(False)

download_cli.show_video_heading()
download_cli.show_download_summary()

if not global_config.answer_yes_to_all_questions:
confirmed = download_cli.ask_for_confirmation_to_download()
if not confirmed:
user_has_confirmed = download_cli.ask_for_confirmation_to_download()
if not user_has_confirmed:
continue

video.register_on_progress_callback(on_download_progress)

download_cli.download_selected_formats()

if global_config.batch_file:
Expand All @@ -98,16 +85,18 @@ def on_download_progress(stream, chunk, bytes_remaining):
except exceptions.NoAvailableSpecifiedFormats as exception:
exception.show_message()
except PytubeRegexMatchError:
download_cli.remove_loading_label()
download_cli.mark_as_loading(False)
exceptions.InvalidVideoURLError().show_message()
except KeyboardInterrupt:
download_cli.terminate()
break
except Exception as exception:
download_cli.remove_loading_label()
download_cli.mark_as_loading(False)
exceptions.UnspecifiedError().show_message()
raise exception
finally:
remove_all_temporary_files(output_path)

if is_last_video:
download_cli.terminate()
else:
Expand Down
9 changes: 3 additions & 6 deletions mediakit/actions/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mediakit.actions import download
from mediakit.streams.arguments import (
from mediakit.cli.arguments import (
command_args,
GlobalArguments,
show_help_message,
show_current_version,
show_arguments_not_recognized_error
show_arguments_not_recognized_error,
)


Expand All @@ -15,10 +15,7 @@ def get_command_actions():
actions.append(show_help_message)
if command_args.has_argument(GlobalArguments.version):
actions.append(show_current_version)
if (
command_args.has_video_url()
or command_args.has_argument(GlobalArguments.batch)
):
if command_args.has_video_url() or command_args.has_argument(GlobalArguments.batch):
actions.append(download)

if len(actions) == 0:
Expand Down
File renamed without changes.
105 changes: 38 additions & 67 deletions mediakit/streams/arguments.py → mediakit/cli/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,79 @@

from mediakit.info import name, version, description
from mediakit.utils import regex
from mediakit.streams.colors import colored, Colors
from mediakit.streams.screen import screen, ContentCategories
from mediakit.cli.colors import colored, Colors
from mediakit.cli.screen import screen, ContentCategories
from mediakit.globals import global_config


class GlobalArguments:
help = ['-h', '--help']
yes = ['-y', '--yes']
version = ['-v', '--version']
no_colors = ['-nc', '--no-colors']
batch = ['-b', '--batch']
help = ["-h", "--help"]
yes = ["-y", "--yes"]
version = ["-v", "--version"]
no_colors = ["-nc", "--no-colors"]
batch = ["-b", "--batch"]


class Parser(ArgumentParser):
def add_global_arguments(self):
self.add_argument(
*GlobalArguments.help,
action='store_true',
help='Show this help message'
*GlobalArguments.help, action="store_true", help="Show this help message"
)
self.add_argument(
*GlobalArguments.yes,
action='store_true',
help='Answer "yes" to all questions beforehand'
action="store_true",
help='Answer "yes" to all questions beforehand',
)
self.add_argument(
*GlobalArguments.version,
action='store_true',
help='Show the current version'
action="store_true",
help="Show the current version",
)
self.add_argument(
*GlobalArguments.no_colors,
dest='no_colors',
action='store_true',
help='Disable the colors of the interface'
dest="no_colors",
action="store_true",
help="Disable the colors of the interface",
)
self.add_argument(
*GlobalArguments.batch,
nargs=1,
help='Download videos from URLs stored in a batch file'
help="Download videos from URLs stored in a batch file",
)

def add_download_arguments(self):
if not global_config.batch_file:
self.add_argument(
'video_url',
help='URL of the YouTube video to download'
)
self.add_argument("video_url", help="URL of the YouTube video to download")
self.add_argument(
'output_path',
nargs='?',
default='./',
help='Destination folder to where to save the downloads'
"output_path",
nargs="?",
default="./",
help="Destination folder to where to save the downloads",
)
self.add_argument(
'-f',
'--formats',
nargs='*',
"-f",
"--formats",
nargs="*",
default=[],
help='Formats to download, separated by spaces (e.g. 1080p 720p 360p)'
help="Formats to download, separated by spaces (e.g. 1080p 720p 360p)",
)


def update_global_config_based_on_arguments(arguments):
answer_yes_to_all_questions = getattr(
arguments,
'yes',
global_config.answer_yes_to_all_questions
arguments, "yes", global_config.answer_yes_to_all_questions
)
ui_colors_disabled = getattr(
arguments,
'no_colors',
global_config.ui_colors_disabled
)
batch_file = getattr(
arguments,
'batch',
global_config.batch_file
arguments, "no_colors", global_config.ui_colors_disabled
)
batch_file = getattr(arguments, "batch", global_config.batch_file)

global_config.answer_yes_to_all_questions = answer_yes_to_all_questions
global_config.ui_colors_disabled = ui_colors_disabled

was_batch_file_provided = (
isinstance(batch_file, list)
and len(batch_file) == 1
)
was_batch_file_provided = isinstance(batch_file, list) and len(batch_file) == 1
global_config.batch_file = (
batch_file[0] if was_batch_file_provided
else global_config.batch_file
batch_file[0] if was_batch_file_provided else global_config.batch_file
)


Expand All @@ -102,8 +85,7 @@ def __init__(self, args=sys.argv, parse_and_update_global_config=True):
self.unique_arguments = set(self.arguments)

self.parser = Parser(
formatter_class=RawDescriptionHelpFormatter,
add_help=False
formatter_class=RawDescriptionHelpFormatter, add_help=False
)

self.parser.add_global_arguments()
Expand All @@ -112,18 +94,12 @@ def __init__(self, args=sys.argv, parse_and_update_global_config=True):
update_global_config_based_on_arguments(self.global_arguments)

self.parser.description = (
colored(
f'{name.lower()} v{version}\n',
style=Colors.style.BRIGHT
)
colored(f"{name.lower()} v{version}\n", style=Colors.style.BRIGHT)
+ description
)

def has_argument(self, possible_arguments):
intersection = (
self.unique_arguments
.intersection(set(possible_arguments))
)
intersection = self.unique_arguments.intersection(set(possible_arguments))

return len(intersection) > 0

Expand All @@ -134,31 +110,26 @@ def has_video_url(self):

return False


def parse_download_arguments(self):
self.parser.add_download_arguments()

arguments = self.parser.parse_args()

return arguments


def show_current_version():
screen.append_content(f'{version}\n')
screen.append_content(f"{version}\n")


def show_help_message():
command_args.parser.add_download_arguments()
screen.append_content(
command_args.parser.format_help()
)
screen.append_content(command_args.parser.format_help())


def show_arguments_not_recognized_error():
screen.append_content(
'\nCould not recognize the provided arguments. '
+ 'Please check your entries and try again.\n\n',
ContentCategories.ERROR
"\nCould not recognize the provided arguments. "
+ "Please check your entries and try again.\n\n",
ContentCategories.ERROR,
)


Expand Down
23 changes: 10 additions & 13 deletions mediakit/streams/colors.py → mediakit/cli/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from mediakit.globals import global_config


class Colors():
class Colors:
fore = colorama.Fore
back = colorama.Back
style = colorama.Style


class ColoredText():
class ColoredText:
def __init__(self, text, fore, back, style):
self.text = text
self.fore = fore
Expand All @@ -18,23 +18,20 @@ def __init__(self, text, fore, back, style):

def __str__(self):
return (
f'{self.fore}{self.back}{self.style}'
f'{self.text}'
f'{Colors.style.RESET_ALL}'
f"{self.fore}{self.back}{self.style}"
f"{self.text}"
f"{Colors.style.RESET_ALL}"
)


def colored(
*values,
fore=Colors.fore.RESET,
back=Colors.back.RESET,
style=Colors.style.NORMAL,
sep=' '
fore: str = Colors.fore.RESET,
back: str = Colors.back.RESET,
style: str = Colors.style.NORMAL,
sep=" ",
):
joined_text = sep.join(map(
lambda value: str(value),
values
))
joined_text = sep.join(map(lambda value: str(value), values))

if global_config.ui_colors_disabled:
return joined_text
Expand Down
1 change: 1 addition & 0 deletions mediakit/cli/download/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .download_cli import DownloadCLI
Loading

0 comments on commit 4c2200c

Please sign in to comment.