Skip to content

Commit

Permalink
feat: add cache and url-chars arguments; fix mkv format
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Jul 25, 2024
2 parents 2ede958 + d69bf3f commit 743fd04
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 28 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.10.0
Jul 25, 2024

- Add `url-chars` cli argument
- Add `cache` cli argument, refs #10
- Add **url_chars** function, refs #12
- Fix _mkv_ extension, refs #13
- Fix **orderby-track** return function, refs #14

## 1.9.0
May 09, 2024

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ To install ``mkpl``, see here:
```console
$ pip install make_playlist # for python enviroment

$ dnf copr enable matteoguadrini/mkpl
$ dnf install python-make_playlist -y # for Red Hat and fedora

$ git clone https://github.com/MatteoGuadrini/mkpl.git && cd mkpl
$ pip install . # for others
```
Expand All @@ -36,6 +33,8 @@ $ pip install . # for others
| -I | --image | Playlist image | Image path |
| -l | --link | Add local or remote files | Files |
| -j | --join | Join one or more other playlist files | Playlist files |
| -n | --cache | Cache playlist results | Seconds |
| -U | --url-chars | Substitute some chars with URL Encoding | |
| -r | --recursive | Recursive search | |
| -a | --absolute | Absolute file name | |
| -s | --shuffle | Casual order | |
Expand Down Expand Up @@ -77,7 +76,7 @@ $ pip install . # for others
4. Create a shuffled playlist with my music collection and exclude dirs

```bash
mkpl -d "my_mp3_collection" "my_mp4_collection" -r -s -e "my_mp3_collection/metallica" "my_mp3_collection/dk" "my music.m3u"
mkpl -d "my_mp3_collection" "my_mp4_collection" -r -s -e "my_mp3_collection/metallica" "my_mp3_collection/dk" -- "my music.m3u"
```

5. Create a TV series playlist with max 15 tracks
Expand Down
139 changes: 118 additions & 21 deletions mkpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from string import capwords

from mutagen import File, MutagenError, id3
from tempcache import TempCache

# endregion

Expand All @@ -59,6 +60,7 @@
"avi",
"xvid",
"divx",
"mkv",
"mpeg",
"mpg",
"mov",
Expand All @@ -71,7 +73,7 @@
"f4a",
}
FILE_FORMAT = AUDIO_FORMAT.union(VIDEO_FORMAT)
__version__ = "1.9.0"
__version__ = "1.10.0"


# endregion
Expand Down Expand Up @@ -228,6 +230,19 @@ def get_args():
help="Order playlist files by length",
action="store_true",
)
orderby_group.add_argument(
"-U",
"--url-chars",
help="Substitute some chars with URL Encoding (Percent Encoding)",
action="store_true",
)
orderby_group.add_argument(
"-n",
"--cache",
help="Cache playlist results",
type=int,
metavar="SECONDS",
)

args = parser.parse_args()

Expand Down Expand Up @@ -339,6 +354,50 @@ def join_playlist(playlist, *others):
print(f"warning: {file} generated error: {err}")


def url_chars(playlist):
"""URL encoding converts characters into a format that can be transmitted over the Internet."""
URL_CHARS = {
" ": "%20",
"!": "%21",
'"': "%22",
"#": "%23",
"$": "%24",
"%": "%25",
"&": "%26",
"'": "%27",
"(": "%28",
")": "%29",
"*": "%2A",
"+": "%2B",
",": "%2C",
"-": "%2D",
":": "%3A",
";": "%3B",
"<": "%3C",
"=": "%3D",
">": "%3E",
"?": "%3F",
"@": "%40",
"[": "%5B",
"]": "%5D",
"^": "%5E",
"_": "%5F",
"`": "%60",
"{": "%7B",
"|": "%7C",
"}": "%7D",
"~": "%7E",
}
ret = []
for file in playlist:
for char in file:
if URL_CHARS.get(char):
file = file.replace(char, URL_CHARS.get(char))
ret.append(file)

return ret


def report_issue(exc):
"""Report issue"""
print(
Expand Down Expand Up @@ -367,7 +426,7 @@ def get_track(file):
file = open_multimedia_file(file)
if file and hasattr(file, "tags"):
default = id3.TRCK(text="0")
return file.tags.get("TRCK", default)[0]
return int(file.tags.get("TRCK", default)[0])


def get_year(file):
Expand Down Expand Up @@ -637,32 +696,66 @@ def main():
f"pattern={args.pattern}, split={args.split}",
)

# Define cache
if args.cache:
cache = TempCache("mkpl", max_age=args.cache)
vprint(args.verbose, f"use cache {cache.path}")
# Clean the cache
cache.clear_items()
else:
cache = None

# Make multimedia list
for directory in args.directories:
directory_files = make_playlist(
directory,
FILE_FORMAT,
args.pattern,
sortby_name=args.orderby_name,
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,
)
if args.cache:
directory_files = cache.cache_result(
make_playlist,
directory,
FILE_FORMAT,
args.pattern,
sortby_name=args.orderby_name,
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,
)
else:
directory_files = make_playlist(
directory,
FILE_FORMAT,
args.pattern,
sortby_name=args.orderby_name,
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,
)

multimedia_files.extend(directory_files)

# Check if you must split into directory playlist
if args.split:
# Substitute chars with URL encoding
if args.url_chars:
multimedia_files = url_chars(directory_files)
playlist_name = basename(normpath(directory))
playlist_ext = ".m3u8" if args.encoding == "UNICODE" else ".m3u"
playlist_path = join(
Expand All @@ -671,6 +764,10 @@ def main():
_process_playlist(directory_files, args, playlist_path)
args.enabled_extensions = False

# Substitute chars with URL encoding
if args.url_chars:
multimedia_files = url_chars(multimedia_files)

_process_playlist(multimedia_files, args)

# Count files into playlist
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ py-modules = ["mkpl"]

[project]
name = "make_playlist"
version = "1.9.0"
version = "1.10.0"
readme = "README.md"

authors = [{ name = "Matteo Guadrini", email = "[email protected]" }]
Expand All @@ -16,13 +16,13 @@ maintainers = [
]

description = "Make M3U format playlist from command line."
requires-python = ">=3.7"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
]
dependencies = ["mutagen"]
dependencies = ["mutagen", "tempcache"]

[project.scripts]
mkpl = "mkpl:main"
Expand Down

0 comments on commit 743fd04

Please sign in to comment.