-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add orderby-size, orderby-length, count and interactive arguments
- Loading branch information
Showing
4 changed files
with
166 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
# created by: matteo.guadrini | ||
# mkpl -- mkpl | ||
# | ||
# Copyright (C) 2023 Matteo Guadrini <[email protected]> | ||
# Copyright (C) 2024 Matteo Guadrini <[email protected]> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
|
@@ -71,7 +71,7 @@ | |
"f4a", | ||
} | ||
FILE_FORMAT = AUDIO_FORMAT.union(VIDEO_FORMAT) | ||
__version__ = "1.8.0" | ||
__version__ = "1.9.0" | ||
|
||
|
||
# endregion | ||
|
@@ -151,15 +151,15 @@ def get_args(): | |
"--link", | ||
help="Add local or remote file links", | ||
nargs=argparse.ONE_OR_MORE, | ||
metavar='FILES', | ||
metavar="FILES", | ||
default=[], | ||
) | ||
parser.add_argument( | ||
"-j", | ||
"--join", | ||
help="Join one or more other playlist files", | ||
nargs=argparse.ONE_OR_MORE, | ||
metavar='PLAYLISTS', | ||
metavar="PLAYLISTS", | ||
default=[], | ||
) | ||
parser.add_argument( | ||
|
@@ -186,6 +186,15 @@ def get_args(): | |
parser.add_argument( | ||
"-S", "--split", help="Split playlist by directories", action="store_true" | ||
) | ||
parser.add_argument( | ||
"-R", | ||
"--interactive", | ||
help="Asks each file for confirmation", | ||
action="store_true", | ||
) | ||
parser.add_argument( | ||
"-C", "--count", help="Count elements into playlist", action="store_true" | ||
) | ||
orderby_group.add_argument( | ||
"-s", "--shuffle", help="Casual order", action="store_true" | ||
) | ||
|
@@ -207,6 +216,18 @@ def get_args(): | |
help="Order playlist files by year", | ||
action="store_true", | ||
) | ||
orderby_group.add_argument( | ||
"-Z", | ||
"--orderby-size", | ||
help="Order playlist files by size", | ||
action="store_true", | ||
) | ||
orderby_group.add_argument( | ||
"-L", | ||
"--orderby-length", | ||
help="Order playlist files by length", | ||
action="store_true", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
|
@@ -265,6 +286,28 @@ def get_args(): | |
return args | ||
|
||
|
||
def confirm(file, default="y"): | ||
"""Ask user to enter Y or N (case-insensitive) | ||
:file: file to add into playlist | ||
:default: default answer | ||
:return: True if the answer is Y. | ||
:rtype: bool | ||
""" | ||
while ( | ||
answer := input( | ||
"Add file {0} to playlist? {1}:".format( | ||
file, "[Y/n]" if default == "y" else "[y/N]" | ||
) | ||
).lower() | ||
) not in ("y", "n"): | ||
# Check if default | ||
if not answer: | ||
answer = default | ||
break | ||
return answer == "y" | ||
|
||
|
||
def file_in_playlist(playlist, file, root=None): | ||
"""Check if file is in the playlist""" | ||
for f in playlist: | ||
|
@@ -287,7 +330,9 @@ def join_playlist(playlist, *others): | |
try: | ||
# open playlist, remove extensions and extend current playlist file | ||
lines = open(file).readlines() | ||
playlist.extend([line.rstrip() for line in lines if not line.startswith('#')]) | ||
playlist.extend( | ||
[line.rstrip() for line in lines if not line.startswith("#")] | ||
) | ||
except FileNotFoundError: | ||
print(f"warning: {file} file not found") | ||
except OSError as err: | ||
|
@@ -333,6 +378,13 @@ def get_year(file): | |
return file.tags.get("TDOR", default)[0] | ||
|
||
|
||
def get_length(file): | ||
"""Get file by length for sort""" | ||
file = open_multimedia_file(file) | ||
if file and hasattr(file, "info"): | ||
return file.info.length if hasattr(file.info, "length") else 0.0 | ||
|
||
|
||
def find_pattern(pattern, path): | ||
"""Find patter in a file and tags""" | ||
global AUDIO_FORMAT | ||
|
@@ -344,10 +396,10 @@ def find_pattern(pattern, path): | |
if pattern.findall(path): | ||
return True | ||
# Check type of file | ||
ext = os.path.splitext(path)[1].replace('.', '').lower() | ||
ext = os.path.splitext(path)[1].replace(".", "").lower() | ||
if ext in AUDIO_FORMAT: | ||
file = open_multimedia_file(path) | ||
# Check supports of ID3 tagsadd compiled pattern | ||
# Check supports of ID3 tags add compiled pattern | ||
if file and hasattr(file, "ID3"): | ||
# Check pattern into title | ||
if file.tags.get("TIT2"): | ||
|
@@ -381,23 +433,23 @@ def unix_to_dos(path, viceversa=False): | |
|
||
|
||
def write_playlist( | ||
playlist, | ||
open_mode, | ||
files, | ||
encoding, | ||
enabled_extensions=False, | ||
image=None, | ||
ext_part=None, | ||
max_tracks=None, | ||
verbose=False, | ||
playlist, | ||
open_mode, | ||
files, | ||
encoding, | ||
enabled_extensions=False, | ||
image=None, | ||
ext_part=None, | ||
max_tracks=None, | ||
verbose=False, | ||
): | ||
"""Write playlist into file""" | ||
if playlist: | ||
with open( | ||
playlist, | ||
mode=open_mode, | ||
encoding="UTF-8" if encoding == "UNICODE" else encoding, | ||
errors="ignore", | ||
playlist, | ||
mode=open_mode, | ||
encoding="UTF-8" if encoding == "UNICODE" else encoding, | ||
errors="ignore", | ||
) as pl: | ||
if image and enabled_extensions: | ||
vprint(verbose, f"set image {image}") | ||
|
@@ -414,20 +466,23 @@ def write_playlist( | |
|
||
|
||
def make_playlist( | ||
directory, | ||
file_formats, | ||
pattern=None, | ||
sortby_name=False, | ||
sortby_date=False, | ||
sortby_track=False, | ||
sortby_year=False, | ||
recursive=False, | ||
exclude_dirs=None, | ||
unique=False, | ||
absolute=False, | ||
min_size=1, | ||
windows=False, | ||
verbose=False, | ||
directory, | ||
file_formats, | ||
pattern=None, | ||
sortby_name=False, | ||
sortby_date=False, | ||
sortby_track=False, | ||
sortby_year=False, | ||
sortby_size=False, | ||
sortby_length=False, | ||
recursive=False, | ||
exclude_dirs=None, | ||
unique=False, | ||
absolute=False, | ||
min_size=1, | ||
windows=False, | ||
interactive=False, | ||
verbose=False, | ||
): | ||
"""Make playlist list""" | ||
filelist = list() | ||
|
@@ -465,12 +520,15 @@ def make_playlist( | |
# Check if file is in playlist | ||
if unique: | ||
if file_in_playlist( | ||
filelist, file, root=root if not absolute else None | ||
filelist, file, root=root if not absolute else None | ||
): | ||
continue | ||
# Check file size | ||
if size <= min_size: | ||
continue | ||
if interactive: | ||
if not confirm(file): | ||
continue | ||
vprint(verbose, f"add multimedia file {file}") | ||
filelist.append(unix_to_dos(file) if windows else file) | ||
# Check sort | ||
|
@@ -482,6 +540,10 @@ def make_playlist( | |
filelist = sorted(filelist, key=get_track) | ||
elif sortby_year: | ||
filelist = sorted(filelist, key=get_year) | ||
elif sortby_size: | ||
filelist = sorted(filelist, key=os.path.getsize) | ||
elif sortby_length: | ||
filelist = sorted(filelist, key=get_length) | ||
return filelist | ||
|
||
|
||
|
@@ -585,12 +647,15 @@ def main(): | |
sortby_date=args.orderby_date, | ||
sortby_track=args.orderby_track, | ||
sortby_year=args.orderby_year, | ||
sortby_size=args.orderby_size, | ||
sortby_length=args.orderby_length, | ||
recursive=args.recursive, | ||
exclude_dirs=args.exclude_dirs, | ||
unique=args.unique, | ||
absolute=args.absolute, | ||
min_size=args.size, | ||
windows=args.windows, | ||
interactive=args.interactive, | ||
verbose=args.verbose, | ||
) | ||
|
||
|
@@ -608,6 +673,10 @@ def main(): | |
|
||
_process_playlist(multimedia_files, args) | ||
|
||
# Count files into playlist | ||
if args.count: | ||
print(len([file for file in multimedia_files if not file.startswith("#")])) | ||
|
||
except Exception as err: | ||
report_issue(err) | ||
|
||
|
Oops, something went wrong.