-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen-playlist
executable file
·77 lines (62 loc) · 2.37 KB
/
gen-playlist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2018 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Generate a list of playlists from all the artists available in the Beets database.
"""
import os
import yaml
import beets.library
BEETSCONFIG = "~/.config/beets/config.yaml"
BEETSDB = "~/.config/beets/library.db"
def generate_playlists():
"""Generate list of playlists from all albumartists in the Beets database."""
# Load the Beets library.
libpath = os.path.expanduser(BEETSDB)
lib = beets.library.Library(libpath)
# Get a list of artists.
artists = set()
for album in lib.albums():
artists.add(album.albumartist)
artists = list(artists)
# Generate a list of playlists from the artists in the Beets database.
playlists = []
playlists.append({"name": "All Songs.m3u", "query": ""})
playlists.append({"name": "Top Rated.m3u", "query": "rating:5"})
for artist in artists:
playlists.append(
{
"name": "Best of " + artist.replace('/', '_') + ".m3u",
"query": 'albumartist:"' + artist + '" rating:5',
}
)
return playlists
def write_playlists(p):
"""Write list of playlists to the Beets YAML config file."""
# Load the YAML config file.
configpath = os.path.expanduser(BEETSCONFIG)
with open(configpath, encoding="UTF-8") as f:
data = yaml.safe_load(f)
# Write playlists to the beets YAML config file.
data["smartplaylist"]["playlists"] = p
with open(configpath, "w", encoding="UTF-8") as f:
yaml.dump(data, f, default_flow_style=False)
def main():
playlists = generate_playlists()
write_playlists(playlists)
if __name__ == "__main__":
main()