-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_language.py
executable file
·73 lines (57 loc) · 1.9 KB
/
guess_language.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
#!/usr/bin/env python3
import argparse
import os
import sys
import traceback
from pathlib import Path
from _utils import get_attribute, set_attribute
from recode_language import guess_lyric_language
HELP = """
Try to find the correct language for a given ultrastar text file and sort its
directory into TARGET/language, e.g. TARGET/en.
$ guess_language.py sorted_songs songs/*/*.txt
moves to
sorted_songs/en/xxx/something_english.txt
sorted_songs/de/xxx/something_german.txt
sorted_songs/es/xxx/something_spanish.txt
"""
def guess_language(path):
with open(path) as f:
text = f.read()
try:
language = guess_lyric_language(text, remove_non_ascii=False)
try:
old_language = get_attribute(text, "LANGUAGE")
except KeyError:
old_language = None
print(f"SUCCESS\t{old_language}\t{language}")
return language
except Exception as ex:
print(f"ERROR\t{ex}\t{path}")
raise
def main(argv):
parser = argparse.ArgumentParser(description=HELP)
parser.add_argument("target")
parser.add_argument("files", nargs="+")
parser.add_argument(
"--dry-run",
action="store_true",
help="just find the encoding, do not change the file.",
)
args = parser.parse_args(argv)
for path in args.files:
print(path)
try:
language = guess_language(path)
song_directory = Path(path).parent
new_name = Path(args.target) / language / song_directory.name
print(f"{song_directory} => {new_name}")
if not args.dry_run:
if not song_directory.exists():
print(f"WARNING directory vanished: {song_directory}")
else:
os.renames(song_directory, new_name)
except Exception as ex:
traceback.print_exc()
if __name__ == "__main__":
main(sys.argv[1:])