Created
June 27, 2025 18:44
-
-
Save Vocaned/8f2ef539007132531e5a83b7668e33e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import requests | |
from urllib.parse import urlencode | |
from mutagen.flac import FLAC | |
from pathlib import Path | |
from math import floor | |
token = '' | |
url = 'https://apic-desktop.musixmatch.com/ws/1.1/macro.subtitles.get' | |
headers = { | |
'authority': 'apic-desktop.musixmatch.com', | |
'cookie': 'x-mxm-token-guid=' | |
} | |
Path('.ignore_lrc').touch(exist_ok=True) | |
with open('.ignore_lrc', 'r') as f: | |
ignorelist = f.read() | |
for p in Path('..').glob('**/*'): | |
if not p.is_file() or p.suffix != '.flac': | |
# only target flacs | |
continue | |
if p.with_suffix('.lrc').exists(): | |
# skip any existing lyrics | |
continue | |
if str(p) in ignorelist: | |
# no lyrics found | |
continue | |
tags = FLAC(p) | |
artist = tags['artist'][0] | |
track = tags['title'][0] | |
duration = tags.info.length | |
if not artist or not track or not duration: | |
print('Broken metadata for file', p) | |
continue | |
print('---') | |
print(f'{artist} - {track}') | |
params = { | |
'app_id': 'web-desktop-app-v1.0', | |
'format': 'json', | |
'usertoken': token, | |
'namespace': 'lyrics_richsynched', | |
'subtitle_format': 'lrc', | |
'q_artist': artist, | |
'q_track': track, | |
'q_duration': duration, | |
'f_subtitle_length': floor(duration) | |
} | |
res = requests.get(url, params=params, headers=headers) | |
try: | |
j = res.json() | |
body = j['message']['body']['macro_calls'] | |
if body['matcher.track.get']['message']['header']['status_code'] == 401: | |
print('ratelimit') | |
raise Exception | |
if body['matcher.track.get']['message']['header']['status_code'] == 404: | |
print('No track found') | |
with open('.ignore_lrc', 'a') as f: | |
f.write(str(p) + '\n') | |
continue | |
if 'track.subtitles.get' in body and body['track.subtitles.get']['message']['header']['status_code'] != 404 and 'subtitle_list' in body['track.subtitles.get']['message']['body'] : | |
if len(body['track.subtitles.get']['message']['body']['subtitle_list']) > 1: | |
print('Multiple subtitles? What do') | |
raise Exception | |
lrc = body['track.subtitles.get']['message']['body']['subtitle_list'][0]['subtitle']['subtitle_body'] | |
print(lrc) | |
with open(p.with_suffix('.lrc'), 'w') as f: | |
f.write(lrc) | |
else: | |
print('No lyrics found') | |
print(body) | |
with open('.ignore_lrc', 'a') as f: | |
f.write(str(p) + '\n') | |
except Exception: | |
print(res.text) | |
raise | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment