Created
October 1, 2024 15:45
-
-
Save yayoimizuha/6d6425482b87c9e8ff8fcbe03bc26674 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
import re | |
from glob import glob | |
from os import path, environ | |
from pprint import pprint | |
from mutagen.mp4 import MP4 | |
from googleapiclient.discovery import build | |
import requests | |
from bs4 import BeautifulSoup | |
MUSIC_ROOT = r"C:\Users\tomokazu\Music\iTunes\iTunes Media\Music" | |
cse = build(serviceName="customsearch", version="v1", developerKey=environ["GOOGLE_API_KEY"]) | |
for file in glob(path.join(MUSIC_ROOT, "**", "*.m4a"), recursive=True): | |
audio = MP4(file) | |
title = audio.tags.get('\xa9nam', [None])[0] | |
singer = audio.tags.get('\xa9ART', [None])[0] | |
if audio.tags.get('\xa9lyr', [None])[0] is not None: | |
audio.tags['\xa9lyr'] = audio.tags.get('\xa9lyr', [None])[0].replace("&", "&") | |
print(re.findall(r"&[A-z].*;", audio.tags.get('\xa9lyr', [None])[0])) | |
print(audio.tags.get('\xa9lyr', [None])) | |
print("already added lyric...skip...", title, singer) | |
continue | |
if "nstrumental" in title: | |
print("Instrumental,skip....", title, singer) | |
continue | |
print(file, title, singer) | |
# title = re.sub(r"\(.*?\)", "", title) | |
query = f"{title} {singer} 歌詞 uta-net.com/song" | |
print(query) | |
search_res = cse.cse().list(q=query, cx=environ["CSE_ID"], num=3).execute() | |
try: | |
search_res["items"] | |
except: | |
print("search result not found...skip...") | |
continue | |
for search_res_item in search_res["items"]: | |
print(search_res_item["link"]) | |
if "uta-net.com/song/" in search_res_item["link"]: | |
uta_net_html = requests.get(search_res_item["link"]).text | |
soup = BeautifulSoup(uta_net_html, "lxml") | |
song = soup.select_one("#kashi_area").decode_contents() | |
print( | |
" ".join([text.strip() for text in soup.select_one("div.song-infoboard").findAll(string=True)]).strip()) | |
print(song) | |
if "n" in input(): | |
break | |
audio.tags["\xa9lyr"] = song.replace("<br/>", "\n") | |
audio.save() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment