Skip to content

Commit

Permalink
chore: add join argument
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Aug 2, 2023
1 parent c985074 commit c9fa474
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ $ python setup.py install # for others
| -t | --title | Playlist title | Title string |
| -g | --encoding | Text encoding | UTF-8,ASCII,UNICODE |
| -I | --image | Playlist image | Image path |
| -l | --link | Add remote file links | Links |
| -l | --link | Add local or remote files | Files |
| -j | --join | Join one or more other playlist files | Playlist files |
| -r | --recursive | Recursive search | |
| -a | --absolute | Absolute file name | |
| -s | --shuffle | Casual order | |
Expand Down Expand Up @@ -152,6 +153,11 @@ $ python setup.py install # for others
mkpl -d "new_collection" -r "my music.m3u" -y
```
16. Join the _"First playlist.m3u"_ and _"Second playlist.m3u8"_ with new **"Third playlist.m3u"**:
```bash
mkpl -d "new_collection" -r "Third playlist" -j "First playlist.m3u" "Second playlist.m3u8"
## Use it like Python module
`mkpl` can also be used as a Python module to customize your scripts.
Expand Down
30 changes: 28 additions & 2 deletions mkpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_args():
global FILE_FORMAT

parser = argparse.ArgumentParser(
description="Command line tool to create media playlists in M3U format.",
description="Command line tool to creates playlist file in M3U format.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
epilog="See latest release from https://github.com/MatteoGuadrini/mkpl",
)
Expand Down Expand Up @@ -131,8 +131,17 @@ def get_args():
parser.add_argument(
"-l",
"--link",
help="Add remote file links",
help="Add local or remote file links",
nargs=argparse.ONE_OR_MORE,
metavar='FILES',
default=[],
)
parser.add_argument(
"-j",
"--join",
help="Join one or more other playlist files",
nargs=argparse.ONE_OR_MORE,
metavar='PLAYLISTS',
default=[],
)
parser.add_argument(
Expand Down Expand Up @@ -254,6 +263,19 @@ def file_in_playlist(playlist, file, root=None):
return True


def join_playlist(playlist, *others):
"""Join current playlist with others"""
for file in 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('#')])
except FileNotFoundError:
print(f"warning: {file} file not found")
except OSError as err:
print(f"warning: {file} generated error: {err}")


def report_issue(exc):
"""Report issue"""
print(
Expand Down Expand Up @@ -483,6 +505,10 @@ def add_extension(filelist, cli_args, verbose=False):
def _process_playlist(files, cli_args, other_playlist=None):
"""Private function cli only for process arguments and make playlist"""

# Join other playlist files
if cli_args.join:
join_playlist(files, *cli_args.join)

# Add link
files.extend(cli_args.link)

Expand Down

0 comments on commit c9fa474

Please sign in to comment.