2023-07-30 更新 2293 阅读

音乐列表用了网易云的歌单,因为没做缓存每次加载都很慢,会导致播放的时候添加歌曲失败,写了一个脚本自动同步并抓取歌词,如下:

import requests
import json

def get_lyric_content(url):
    response = requests.get(url)
    if response.status_code == 200:
        return response.text
    else:
        return None

url = "https://api.yezi.me/music/?type=playlist&id=7785232779"

response = requests.get(url)
if response.status_code == 200:
    data = response.json()

    for item in data:
        lyric_url = item["lyric"]
        lyric_content = get_lyric_content(lyric_url)
        if lyric_content is not None:
            item["lyric"] = lyric_content

    # Remove the "pic" key from each item in the list
    for item in data:
        del item["pic"]

    # Save the data to MusicList.json
    with open("MusicList.json", "w", encoding="utf-8") as file:
        json.dump(data, file, ensure_ascii=False, indent=2)
else:
    print("Failed to fetch data from the API.")

年少有为 - 李荣浩-music

妈妈再也不用担心我的歌单了,服务器设置一个定时任务,实现自动更新就行了,完美~