-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrate-song
More file actions
executable file
·164 lines (143 loc) · 4.45 KB
/
rate-song
File metadata and controls
executable file
·164 lines (143 loc) · 4.45 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2018 - 2024 sudorook <daemon@nullcodon.com>
#
# 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/>.
"""
Rate the currently playing song in MPD.
"""
import os
import sys
import re
import argparse
from mpd import MPDClient
import beets.library
#
# Functions
#
def get_current_song():
"""Get the current song from MPD. Returns an Item."""
# Connect to MPD.
client = MPDClient()
client.connect("localhost", 6600)
info = client.currentsong()
if bool(info) is False:
print("MPD not playing. Exiting.")
sys.exit()
client.disconnect()
# Load the Beets library
libpath = os.path.expanduser("~/.config/beets/library.db")
lib = beets.library.Library(libpath)
# Find the song in the beets database based on the title, artist, and album
# information provided by MPD.
item = lib.items(
'"title::^'
+ re.escape(info["title"])
+ '$"'
+ ' "artist::^'
+ re.escape(info["artist"])
+ '$"'
+ ' "album::^'
+ re.escape(info["album"])
+ '$"'
)
if len(item) > 1:
print("Multiple files found. Exiting.")
for i in item:
print(
"'"
+ i["title"]
+ "' from '"
+ i["album"]
+ "' by "
+ i["artist"]
)
sys.exit()
elif len(item) == 0:
print("First pass parsing failed. Trying again.")
item = lib.items(
'"title::^'
+ re.escape(info["title"])
+ '$"'
+ ' "artist::^'
+ re.escape(info["artist"])
+ '$"'
)
if len(item) == 1:
return item[0]
print("Second pass parsing failed. Trying again.")
item = lib.items(
'"title::^'
+ re.escape(info["title"])
+ '$"'
+ ' "album::^'
+ re.escape(info["album"])
+ '$"'
)
if len(item) == 1:
return item[0]
sys.exit(
"Parsing failed. See below:\n"
f"Title: {info['title']} -> {re.escape(info['title'])}\n"
f"Artist: {info['artist']} -> {re.escape(info['artist'])}\n"
f"Album: {info['album']} -> {re.escape(info['album'])}\n"
)
else:
return item[0]
def get_song_from_file(file):
"""Get a song from the database based on its path in the filesystem."""
# Load the Beets library
libpath = os.path.expanduser("~/.config/beets/library.db")
lib = beets.library.Library(libpath)
item = lib.items('"path:' + file + '"')
if len(item) > 1:
sys.exit("Multiple files found. Exiting.")
else:
return item[0]
def prompt_user_rating():
"""Prompt for user rating and make sure entry is valid."""
_rating = int(input("Rating (1-5): "))
if 1 <= int(_rating) <= 5:
return _rating
sys.exit("Invalid rating. Exiting.")
def main():
# Command line parsing
parser = argparse.ArgumentParser(
description="Rate songs in your beets database."
)
parser.add_argument("-f", "--file", help="Song file", required=False)
parser.add_argument("-r", "--rating", help="Rating", required=False)
args = parser.parse_args()
if args.file is None:
song = get_current_song()
print(
f"Rating '{song['title']}' from "
f"'{song['album']}' by '{song['artist']}'"
)
else:
song = get_song_from_file(args.file)
if args.rating is None:
rating = prompt_user_rating()
else:
if 0 <= int(args.rating) <= 5:
rating = int(args.rating)
else:
print("Invalid rating. Exiting.")
sys.exit()
song.load()
song["rating"] = rating
song.store()
if __name__ == "__main__":
main()