-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
formatter.py
532 lines (419 loc) · 14.1 KB
/
formatter.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""
Module for formatting songs into strings.
Contains functions to create search queries and song titles
and file names.
"""
import copy
import logging
import re
from functools import lru_cache
from pathlib import Path
from typing import List, Optional
import pykakasi
from rapidfuzz import fuzz
from slugify import slugify as py_slugify
from yt_dlp.utils import sanitize_filename
from spotdl.types.song import Song
__all__ = [
"VARS",
"JAP_REGEX",
"DISALLOWED_REGEX",
"create_song_title",
"sanitize_string",
"slugify",
"format_query",
"create_search_query",
"create_file_name",
"parse_duration",
"to_ms",
"restrict_filename",
"ratio",
]
VARS = [
"{title}",
"{artists}",
"{artist}",
"{album}",
"{album-artist}",
"{genre}",
"{disc-number}",
"{disc-count}",
"{duration}",
"{year}",
"{original-date}",
"{track-number}",
"{tracks-count}",
"{isrc}",
"{track-id}",
"{publisher}",
"{list-length}",
"{list-position}",
"{list-name}",
"{output-ext}",
]
KKS = pykakasi.kakasi()
JAP_REGEX = re.compile(
"[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]"
)
DISALLOWED_REGEX = re.compile(r"[^-a-zA-Z0-9\!\@\$]+")
logger = logging.getLogger(__name__)
def create_song_title(song_name: str, song_artists: List[str]) -> str:
"""
Create the song title.
### Arguments
- song_name: the name of the song
- song_artists: the list of artists of the song
### Returns
- the song title
### Notes
- Example: "Artist1, Artist2 - Song Name"
"""
joined_artists = ", ".join(song_artists)
if len(song_artists) >= 1:
return f"{joined_artists} - {song_name}"
return song_name
def sanitize_string(string: str) -> str:
"""
Sanitize the filename to be used in the file system.
### Arguments
- string: the string to sanitize
### Returns
- the sanitized string
"""
output = string
# this is windows specific (disallowed chars)
output = "".join(char for char in output if char not in "/?\\*|<>")
# double quotes (") and semi-colons (:) are also disallowed characters but we would
# like to retain their equivalents, so they aren't removed in the prior loop
output = output.replace('"', "'").replace(":", "-")
return output
@lru_cache()
def slugify(string: str) -> str:
"""
Slugify the string.
### Arguments
- string: the string to slugify
### Returns
- the slugified string
"""
# Replace ambiguous characters
if not JAP_REGEX.search(string):
# If string doesn't have japanese characters
# return early
return py_slugify(string, regex_pattern=DISALLOWED_REGEX.pattern)
# Workaround for japanese characters
# because slugify incorrectly converts them
# to latin characters
normal_slug = py_slugify(
string,
regex_pattern=JAP_REGEX.pattern,
)
results = KKS.convert(normal_slug)
result = ""
for index, item in enumerate(results):
result += item["hepburn"]
if not (
item["kana"] == item["hepburn"]
or item["kana"] == item["hepburn"]
or (
item == results[-1]
or results[index + 1]["kana"] == results[index + 1]["hepburn"]
)
):
result += "-"
return py_slugify(result, regex_pattern=DISALLOWED_REGEX.pattern)
def format_query(
song: Song,
template: str,
santitize: bool,
file_extension: Optional[str] = None,
short: bool = False,
) -> str:
"""
Replace template variables with the actual values.
### Arguments
- song: the song object
- template: the template string
- santitize: whether to sanitize the string
- file_extension: the file extension to use
- short: whether to use the short version of the template
### Returns
- the formatted string
"""
if "{output-ext}" in template and file_extension is None:
raise ValueError("file_extension is None, but template contains {output-ext}")
for key, val in [
("{list-length}", song.list_length),
("{list-position}", song.list_position),
("{list-name}", song.list_name),
]:
if not (key in template and val is None):
continue
logger.warning(
"Template contains %s, but it's value is None. Replacing with empty string.",
key,
)
template = template.replace(key, "")
template = template.replace(r"//", r"/")
# If template has only {output-ext}, fix it
if template in ["/.{output-ext}", ".{output-ext}"]:
template = "{artists} - {title}.{output-ext}"
# Remove artists from the list that are already in the title
artists = [
artist for artist in song.artists if slugify(artist) not in slugify(song.name)
]
# Add the main artist again to the list
if len(artists) == 0 or artists[0] != song.artists[0]:
artists.insert(0, song.artists[0])
artists_str = ", ".join(artists)
# the code below is valid, song_list is actually checked for None
formats = {
"{title}": song.name,
"{artists}": song.artists[0] if short is True else artists_str,
"{artist}": song.artists[0],
"{album}": song.album_name,
"{album-artist}": song.album_artist,
"{genre}": song.genres[0] if song.genres else "",
"{disc-number}": song.disc_number,
"{disc-count}": song.disc_count,
"{duration}": song.duration,
"{year}": song.year,
"{original-date}": song.date,
"{track-number}": f"{song.track_number:02d}" if song.track_number else "",
"{tracks-count}": song.tracks_count,
"{isrc}": song.isrc,
"{track-id}": song.song_id,
"{publisher}": song.publisher,
"{output-ext}": file_extension,
"{list-name}": song.list_name,
"{list-position}": str(song.list_position).zfill(len(str(song.list_length))),
"{list-length}": song.list_length,
}
if santitize:
# sanitize the values in formats dict
for key, value in formats.items():
if value is None:
continue
formats[key] = sanitize_string(str(value))
# Replace all the keys with the values
for key, value in formats.items():
template = template.replace(key, str(value))
return template
def create_search_query(
song: Song,
template: str,
santitize: bool,
file_extension: Optional[str] = None,
short: bool = False,
) -> str:
"""
Create the search query for the song.
### Arguments
- song: the song object
- template: the template string
- santitize: whether to sanitize the string
- file_extension: the file extension to use
- short: whether to use the short version of the template
### Returns
- the formatted string
"""
# If template does not contain any of the keys,
# append {artist} - {title} at the beggining of the template
if not any(key in template for key in VARS):
template = "{artist} - {title}" + template
return format_query(song, template, santitize, file_extension, short=short)
def create_file_name(
song: Song,
template: str,
file_extension: str,
restrict: bool = False,
short: bool = False,
file_name_length: Optional[int] = None,
) -> Path:
"""
Create the file name for the song, by replacing template variables with the actual values.
### Arguments
- song: the song object
- template: the template string
- file_extension: the file extension to use
- restrict: whether to sanitize the filename
- short: whether to use the short version of the template
- file_name_length: the maximum length of the file name
### Returns
- the formatted string as a Path object
"""
temp_song = copy.deepcopy(song)
# If template does not contain any of the keys,
# append {artists} - {title}.{output-ext} to it
if not any(key in template for key in VARS) and template != "":
template += "/{artists} - {title}.{output-ext}"
if template == "":
template = "{artists} - {title}.{output-ext}"
# If template ends with a slash. Does not have a file name with extension
# at the end of the template, append {artists} - {title}.{output-ext} to it
if template.endswith("/") or template.endswith(r"\\") or template.endswith("\\\\"):
template += "/{artists} - {title}.{output-ext}"
# If template does not end with {output-ext}, append it to the end of the template
if not template.endswith(".{output-ext}"):
template += ".{output-ext}"
formatted_string = format_query(
song=song,
template=template,
santitize=True,
file_extension=file_extension,
short=short,
)
# Parse template as Path object
file = Path(formatted_string)
santitized_parts = []
for part in file.parts:
match = re.search(r"[^\.*](.*)[^\.*$]", part)
if match and part != ".spotdl":
santitized_parts.append(match.group(0))
else:
santitized_parts.append(part)
# Join the parts of the path
file = Path(*santitized_parts)
length_limit = file_name_length or 255
# Check if the file name length is greater than the limit
if len(file.name) < length_limit:
# Restrict the filename if needed
if restrict:
return restrict_filename(file)
return file
if short is False:
return create_file_name(
song,
template,
file_extension,
restrict=restrict,
short=True,
file_name_length=length_limit,
)
# Path template is already short, but we still can't create a file
# so we reduce it even further
long_artist = len(song.artist) > (length_limit * 0.50)
long_title = len(song.name) > (length_limit * 0.50)
path_separator = "/" if "/" in template else "\\"
name_template_parts = template.rsplit(path_separator, 1)
name_template = (
name_template_parts[1]
if len(name_template_parts) == 1
else name_template_parts[0]
)
if long_artist:
logger.warning(
"%s: File name is too long. Using only part of song artist.",
temp_song.display_name,
)
short_artist = temp_song.artist.split(",")[0]
temp_song.artist = short_artist
if len(temp_song.artist) > (length_limit * 0.50):
temp_song.artist = temp_song.artist.split(" ")[0]
if long_title:
logger.warning(
"%s: File name is too long. Using only part of the song title.",
temp_song.display_name,
)
name_parts = temp_song.name.split(" ")
old_name = temp_song.name
temp_song.name = ""
for part in name_parts:
old_name = temp_song.name
temp_song.name += part + " "
formatted_name = format_query(
song=temp_song,
template=name_template,
santitize=True,
file_extension=file_extension,
short=True,
)
if len(formatted_name.strip()) > length_limit:
temp_song.name = old_name.strip()
break
return create_file_name(
song=temp_song,
template=template,
file_extension=file_extension,
restrict=restrict,
short=short,
file_name_length=length_limit,
)
def parse_duration(duration: Optional[str]) -> float:
"""
Convert string value of time (duration: "25:36:59") to a float value of seconds (92219.0)
### Arguments
- duration: the string value of time
### Returns
- the float value of seconds
"""
if duration is None:
return 0.0
try:
# {(1, "s"), (60, "m"), (3600, "h")}
mapped_increments = zip([1, 60, 3600], reversed(duration.split(":")))
seconds = sum(multiplier * int(time) for multiplier, time in mapped_increments)
return float(seconds)
# This usually occurs when the wrong string is mistaken for the duration
except (ValueError, TypeError, AttributeError):
return 0.0
def to_ms(
string: Optional[str] = None, precision: Optional[int] = None, **kwargs
) -> float:
"""
Convert a string to milliseconds.
### Arguments
- string: the string to convert
- precision: the number of decimals to round to
- kwargs: the keyword args to convert
### Returns
- the milliseconds
### Notes
- You can either pass a string,
- or a set of keyword args ("hour", "min", "sec", "ms") to convert.
- If "precision" is set, the result is rounded to the number of decimals given.
- From: https://gist.github.com/Hellowlol/5f8545e999259b4371c91ac223409209
"""
if string:
hour = int(string[0:2])
minute = int(string[3:5])
sec = int(string[6:8])
milliseconds = int(string[10:11])
else:
hour = int(kwargs.get("hour", 0))
minute = int(kwargs.get("min", 0))
sec = int(kwargs.get("sec", 0))
milliseconds = int(kwargs.get("ms", 0))
result = (
(hour * 60 * 60 * 1000) + (minute * 60 * 1000) + (sec * 1000) + milliseconds
)
if precision and isinstance(precision, int):
return round(result, precision)
return result
def restrict_filename(pathobj: Path) -> Path:
"""
Sanitizes the filename part of a Path object. Returns modified object.
### Arguments
- pathobj: the Path object to sanitize
### Returns
- the modified Path object
### Notes
- Based on the `sanitize_filename` function from yt-dlp
"""
result = sanitize_filename(pathobj.name, True, False)
result = result.replace("_-_", "-")
if not result:
result = "_"
return pathobj.with_name(result)
@lru_cache()
def ratio(string1: str, string2: str) -> float:
"""
Wrapper for fuzz.ratio
with lru_cache
### Arguments
- string1: the first string
- string2: the second string
### Returns
- the ratio
"""
return fuzz.ratio(string1, string2)