Skip to content

Commit

Permalink
feat: add orderby arguments and opus file support; refs #4 and #5
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Mar 7, 2023
2 parents 166dbe8 + 3f649fd commit 676cb47
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release notes

## 1.6.0
Mar 07, 2023

- Add `-o` or `--orderby-name` cli argument: see issue #4
- Add `-O` or `--orderby-date` cli argument: see issue #4
- Add support for _'opus'_ file format: see issue #5
- Fix check playlist extension for _m3u8_ format
- Fix asterisk and dot char into include argument

## 1.5.0
Jan 13, 2023

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ $ python setup.py install # for others
| -w | --windows | Windows style folder separator | |
| -v | --verbose | Enable verbosity (debug mode) | |
| -S | --split | Split playlist by directories | |
| -o | --orderby-name | Order playlist files by name | |
| -O | --orderby-date | Order playlist files by creation date | |

## Examples

Expand Down Expand Up @@ -139,6 +141,13 @@ $ python setup.py install # for others
...
```
15. Sort playlist files by name (`-o`) or by creation date (`-O`):
```bash
mkpl -d "new_collection" -r "my music.m3u" -o
mkpl -d "new_collection" -r "my music.m3u" -O
```
## Use it like Python module
`mkpl` can also be used as a Python module to customize your scripts.
Expand Down
2 changes: 1 addition & 1 deletion __info__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

"""Information variable used by modules on this package."""

__version__ = '1.5.0'
__version__ = '1.6.0'
__author__ = 'Matteo Guadrini'
__email__ = 'matteo.guadrini@hotmail.it'
__homepage__ = 'https://github.com/MatteoGuadrini/mkpl'
40 changes: 29 additions & 11 deletions mkpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# created by: matteo.guadrini
# mkpl -- mkpl
#
# Copyright (C) 2022 Matteo Guadrini <matteo.guadrini@hotmail.it>
# Copyright (C) 2023 Matteo Guadrini <matteo.guadrini@hotmail.it>
#
# 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
Expand All @@ -29,14 +29,15 @@
from filecmp import cmp
from pathlib import Path
from random import shuffle
from os.path import join, exists, isdir, getsize, normpath, basename, dirname
from os.path import (join, exists, isdir, getsize,
normpath, basename, dirname, getctime)

# endregion

# region globals
FILE_FORMAT = {'mp1', 'mp2', 'mp3', 'mp4', 'aac', 'ogg', 'wav', 'wma', 'm4a', 'aiff',
'avi', 'xvid', 'divx', 'mpeg', 'mpg', 'mov', 'wmv', 'flac', 'alac'}
__version__ = '1.5.0'
'avi', 'xvid', 'divx', 'mpeg', 'mpg', 'mov', 'wmv', 'flac', 'alac', 'opus'}
__version__ = '1.6.0'


# endregion
Expand All @@ -61,10 +62,13 @@ def get_args():
parser.add_argument("-e", "--exclude-dirs", help="Exclude directory paths", nargs=argparse.ONE_OR_MORE, default=[])
parser.add_argument("-i", "--include", help="Include other file format", nargs=argparse.ONE_OR_MORE,
metavar='FORMAT')
parser.add_argument("-p", "--pattern", help="Regular expression inclusion pattern", default='.*')
parser.add_argument("-p", "--pattern",
help="Regular expression inclusion pattern", default='.*')
parser.add_argument("-f", "--format", help="Select only a file format", type=str, choices=FILE_FORMAT)
parser.add_argument("-z", "--size", help="Start size in bytes", type=int, default=1, metavar='BYTES')
parser.add_argument("-m", "--max-tracks", help="Maximum number of tracks", type=int, default=None, metavar='NUMBER')
parser.add_argument("-z", "--size", help="Start size in bytes", type=int,
default=1, metavar='BYTES')
parser.add_argument("-m", "--max-tracks", help="Maximum number of tracks",
type=int, default=None, metavar='NUMBER')
parser.add_argument("-t", "--title", help="Playlist title", default=None)
parser.add_argument("-g", "--encoding", help="Text encoding", choices=('UTF-8', 'ASCII', 'UNICODE'), default=None)
parser.add_argument("-I", "--image", help="Playlist image", default=None)
Expand All @@ -74,15 +78,19 @@ def get_args():
parser.add_argument("-s", "--shuffle", help="Casual order", action='store_true')
parser.add_argument("-u", "--unique", help="The same files are not placed in the playlist", action='store_true')
parser.add_argument("-c", "--append", help="Continue playlist instead of override it", action='store_true')
parser.add_argument("-w", "--windows", help="Windows style folder separator", action='store_true')
parser.add_argument("-w", "--windows", help="Windows style folder separator",
action='store_true')
parser.add_argument("-S", "--split", help="Split playlist by directories", action='store_true')
parser.add_argument("-o", "--orderby-name", help="Order playlist files by name", action='store_true')
parser.add_argument("-O", "--orderby-date", help="Order playlist files by date", action='store_true')

args = parser.parse_args()

# Check extension of playlist file
if not args.playlist.endswith('.m3u'):
if args.encoding == 'UNICODE':
args.playlist += '.m3u8'
if not args.playlist.endswith('.m3u8'):
args.playlist += '.m3u8'
else:
args.playlist += '.m3u'

Expand Down Expand Up @@ -121,7 +129,7 @@ def get_args():

# Extend files format
if args.include:
FILE_FORMAT.update(set(args.include))
FILE_FORMAT.update(set([fmt.strip('*').strip('.') for fmt in args.include]))

# Select only one format
if args.format:
Expand Down Expand Up @@ -177,6 +185,8 @@ def write_playlist(playlist,
def make_playlist(directory,
pattern,
file_formats,
sortby_name=False,
sortby_date=False,
recursive=False,
exclude_dirs=None,
unique=False,
Expand All @@ -201,7 +211,13 @@ def make_playlist(directory,
for fmt in file_formats:
# Check recursive
folder = '**/*' if recursive else '*'
for file in path.glob(folder + f'.{fmt}'):
files = path.glob(folder + f'.{fmt}')
# Check sort
if sortby_name:
files = sorted(files)
if sortby_date:
files = sorted(files, key=getctime)
for file in files:
# Check if in exclude dirs
if any([e_path in str(file) for e_path in exclude_dirs]):
continue
Expand Down Expand Up @@ -307,6 +323,8 @@ def main():
directory_files = make_playlist(directory,
args.pattern,
FILE_FORMAT,
sortby_name=args.orderby_name,
sortby_date=args.orderby_date,
recursive=args.recursive,
exclude_dirs=args.exclude_dirs,
unique=args.unique,
Expand Down

0 comments on commit 676cb47

Please sign in to comment.