-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlrc.py
177 lines (152 loc) · 6.69 KB
/
lrc.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
from flask import Flask, request, Response, jsonify
import requests
import re
import os
from difflib import SequenceMatcher
app = Flask(__name__)
LRC_DIRECTORY = os.environ.get('LRC_DIRECTORY', '/default/path/to/lrcs')
def search_song(keyword):
api_url = f"https://music.163.com/api/search/get?s={keyword}&type=1&limit=50"
response = requests.get(api_url)
if response.status_code != 200:
return None
return response.json().get('result')
def download_lyrics(song_id):
url = "https://music.163.com/api/song/lyric"
params = {"tv": "-1", "lv": "-1", "kv": "-1", "id": song_id}
response = requests.get(url, params=params)
if response.status_code != 200:
return None, None
data = response.json()
lrc = data.get('lrc', {}).get('lyric', '')
tlyric = data.get('tlyric', {}).get('lyric', '')
return lrc, tlyric
def attempt_to_download_lyrics_from_songs(songs):
print("Starting to attempt to download lyrics for songs list:")
print(songs)
for index, song in enumerate(songs):
print(f"Trying song {index + 1}/{len(songs)} with id {song['id']}")
try:
lyrics_content, trans_lyrics_content = download_lyrics(song['id'])
if lyrics_content:
lines = lyrics_content.count('\n') + 1
if lines > 5:
print(f"Lyrics with more than 5 lines found for song with id {song['id']}")
print(lyrics_content)
return lyrics_content, trans_lyrics_content
else:
print(f"Lyrics found for song with id {song['id']} but less than 6 lines")
else:
print(f"No lyrics found for song with id {song['id']}")
except Exception as e:
print(f"Error downloading lyrics for song with id {song['id']}: {e}")
print("No suitable lyrics found for any songs in the list.")
return None, None
def parse_lyrics(lyrics):
lyrics_dict = {}
unformatted_lines = []
pattern = re.compile(r'\[(\d{2}):(\d{2})([.:]\d{2,3})?\](.*)')
for line in lyrics.split('\n'):
match = pattern.match(line)
if match:
minute, second, millisecond, lyric = match.groups()
millisecond = millisecond if millisecond else '.000'
millisecond = millisecond.replace(':', '.')
time_stamp = f"[{minute}:{second}{millisecond}]"
lyrics_dict[time_stamp] = lyric
else:
unformatted_lines.append(line)
return lyrics_dict, unformatted_lines
def merge_lyrics(lrc_dict, tlyric_dict, unformatted_lines):
merged_lyrics = unformatted_lines
all_time_stamps = sorted(set(lrc_dict.keys()).union(tlyric_dict.keys()))
for time_stamp in all_time_stamps:
original_line = lrc_dict.get(time_stamp, '')
translated_line = tlyric_dict.get(time_stamp, '')
merged_lyrics.append(f"{time_stamp}{original_line}")
if translated_line:
merged_lyrics.append(f"{time_stamp}{translated_line}")
return '\n'.join(merged_lyrics)
def check_local_lyrics(title):
for file in os.listdir(LRC_DIRECTORY):
if file.startswith(title) and file.endswith('.lrc'):
with open(os.path.join(LRC_DIRECTORY, file), 'r', encoding='utf-8') as f:
return f.read()
return None
def get_similarity(a, b):
return SequenceMatcher(None, a, b).ratio()
def get_aligned_lyrics(title, artist, album, duration):
search_keywords = [
f"{artist} - {album} - {title}",
f"{album} - {title}",
f"{artist} - {title}"
]
results = []
for keyword in search_keywords:
search_result = search_song(keyword)
if not search_result:
continue
songs = search_result.get('songs', [])
songs = [song for song in songs if abs(song['duration'] / 1000 - duration) <= 3]
for song in songs[:3]:
lyrics_content, trans_lyrics_content = download_lyrics(song['id'])
if lyrics_content:
lrc_dict, unformatted_lines = parse_lyrics(lyrics_content)
if len(lrc_dict) >= 5:
tlyric_dict, _ = parse_lyrics(trans_lyrics_content if trans_lyrics_content else '')
merged = merge_lyrics(lrc_dict, tlyric_dict, unformatted_lines)
similarity = (
get_similarity(title, song['name']) +
get_similarity(artist, ', '.join([artist['name'] for artist in song['artists']])) +
get_similarity(album, song.get('album', {}).get('name', ''))
) / 3
results.append({
"id": str(song['id']),
"title": song['name'],
"artist": ', '.join([artist['name'] for artist in song['artists']]),
"lyrics": merged,
"similarity": similarity
})
results.sort(key=lambda x: x['similarity'], reverse=True)
return results
@app.route('/lyrics', methods=['GET'])
def lyrics():
title = request.args.get('title')
artist = request.args.get('artist')
album = request.args.get('album')
duration = request.args.get('duration', type=float, default=0)
local_lyrics = check_local_lyrics(title)
if local_lyrics:
response = Response(local_lyrics, mimetype='text/plain')
response.headers['Content-Type'] = 'application/json'
return response
aligned_lyrics = get_aligned_lyrics(title, artist, album, duration)
if aligned_lyrics:
response = jsonify(aligned_lyrics)
response.headers['Content-Type'] = 'application/json'
return response
else:
response = Response('', status=404)
response.headers['Content-Type'] = 'application/json'
return response
@app.route('/lyrics/confirm', methods=['POST'])
def confirm_lyrics():
data = request.get_json()
path = data.get('path')
lyrics = data.get('lyrics')
if not path or not lyrics:
response = Response('{"message": "Invalid request data"}', status=400, mimetype='application/json')
return response
lrc_path = re.sub(r'\.\w+$', '.lrc', path)
try:
with open(lrc_path, 'w', encoding='utf-8') as f:
f.write(lyrics)
response = Response('{"message": "Lyrics saved successfully"}', status=200, mimetype='application/json')
return response
except Exception as e:
print(f"Error saving lyrics to {lrc_path}: {e}")
response = Response(f'{{"message": "Error saving lyrics: {e}"}}', status=500, mimetype='application/json')
return response
if __name__ == '__main__':
port = int(os.environ.get("PORT", 51232))
app.run(host='0.0.0.0', port=port)