-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecode_asktheweb.py
executable file
·98 lines (70 loc) · 2.41 KB
/
recode_asktheweb.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
#!/usr/bin/env python3
import argparse
import json.decoder
import re
import sys
import requests
from _utils import find_decodings, get_artisttitle, get_lyrics
HELP = """
Experimental and probably not what you want. Try to find the correct encoding
for a given ultrastar text file. Extracts a part of the lyrics, and put it into
a lyric search engine for all supported encodings. Decode using the encoding
with the most results. Changes the file in place.
"""
non_ascii = re.compile("[^a-zA-Z0-9,. !?-]")
def count_results(lyrics):
response = requests.get("https://songsear.ch/api/search", {"q": lyrics})
try:
data = response.json()
return data["total"]
except KeyError:
raise Exception("API error: " + data["error"])
except json.decoder.JSONDecodeError:
raise Exception("Unexpected API response: " + response.content.decode())
def guess_encoding(content):
best = "ascii"
best_count = 0
for encoding, text in find_decodings(content):
artisttitle = get_artisttitle(text)
lyrics = get_lyrics(text)
non_ascii_lyrics = ""
for line in lyrics:
if line in non_ascii_lyrics:
continue
if non_ascii.search(line):
for word in line.split():
if len(non_ascii_lyrics) + len(word) < 200:
non_ascii_lyrics += word + " "
if non_ascii_lyrics:
search_string = non_ascii_lyrics
elif non_ascii.search(artisttitle):
search_string = artisttitle
if not search_string:
continue
results = count_results(search_string)
if results > best_count:
best = encoding
best_count = results
return best
def fix_encoding(path, dry_run=False):
with open(path, "rb") as f:
content = f.read()
encoding = guess_encoding(content)
content = content.decode(encoding)
print(encoding)
if not dry_run:
with open(path, "w") as f:
f.write(content)
def main(argv):
parser = argparse.ArgumentParser(description=HELP)
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:
fix_encoding(path)
if __name__ == "__main__":
main(sys.argv[1:])