akashrchandran/spotify-lyrics-api

一个用于从Spotify获取歌词的Rest API,由Musixmatch提供支持。

v2.1.0 2024-01-18 16:59 UTC

This package is auto-updated.

Last update: 2024-09-26 21:44:45 UTC


README

spotify-lyrics-api

一个用于从Spotify获取歌词的Rest API,由Musixmatch提供支持。命令行版本可在akashrchandran/syrics中找到。

警告

该项目可能违反Spotify TOS。请自行承担风险使用。

注意

已将此项目转换为模板仓库,请部署您自己的版本。如需帮助,请随时提出问题。

使用Composer安装

composer require akashrchandran/spotify-lyrics-api

获取歌词

目前它只支持轨道ID或链接。

使用GET请求

您必须使用查询参数发送数据

可用参数

您必须指定trackidurl中的任一,否则将返回错误。

示例

使用trackid

https://:8080/?trackid=5f8eCNwTlr0RJopE9vQ6mB

使用url

https://:8080/?url=https://open.spotify.com/track/5f8eCNwTlr0RJopE9vQ6mB?autoplay=true

响应

{
    "error": false,
    "syncType": "LINE_SYNCED",
    "lines": [
        {
            "startTimeMs": "960",
            "words": "One, two, three, four",
            "syllables": [],
            "endTimeMs": "0"
        },
        {
            "startTimeMs": "4020",
            "words": "Ooh-ooh, ooh-ooh-ooh",
            "syllables": [],
            "endTimeMs": "0"
        }
    ]
}

将格式更改为lrc

https://:8080/?trackid=5f8eCNwTlr0RJopE9vQ6mB&format=lrc

响应

{
    "error": false,
    "syncType": "LINE_SYNCED",
    "lines": [
        {
            "timeTag": "00:00.96",
            "words": "One, two, three, four"
        },
        {
            "timeTag": "00:04.02",
            "words": "Ooh-ooh, ooh-ooh-ooh"
        }
    ]
}

响应

API给出的不同响应在此列出。

如果发生错误,则将错误键的值设置为true,否则设置为false

"error": false //no error occured

大多数歌词都是时间同步的或带有时间标签,而有些则不是时间同步的或没有时间标签。为了区分同步和不同步,我们有一个键syncType

"syncType": "LINE_SYNCED"

Musixmatch支持行同步和词同步类型的时间歌词。行同步是指时间标签是给定行唱到哪个位置,而词同步歌词指定当词在歌曲中出现时的时间。目前Spotify只支持行同步。也许他们会在将来支持词同步:/。

行同步

{
    "error": false,
    "syncType": "LINE_SYNCED",
    "lines": [
        {
            "timeTag": "00:00.96",
            "words": "One, two, three, four"
        },
        {
            "timeTag": "00:04.02",
            "words": "Ooh-ooh, ooh-ooh-ooh"
        }
    ]
}

未同步或未同步

注意timeTags被设置为00:00.00

{
    "error": false,
    "syncType": "UNSYNCED",
    "lines": [
        {
            "timeTag": "00:00.00",
            "words": "jaane nahin denge tuje"
        },
        {
            "timeTag": "00:00.00",
            "words": "chaahe tujh ko rab bulaa le, hum naa rab se darane waale"
        }
    ]
}

错误消息

当trackid和url都没有提供时(400 Bad Request)

错误响应

{
    "error": true,
    "message": "url or trackid parameter is required!"
}

当在Spotify中找不到给定轨道的歌词时(404 Not Found)

错误响应

{
    "error": true,
    "message": "lyrics for this track is not available on spotify!"
}

作为包使用

使用composer require akashrchandran/spotify-lyrics-api安装。

在您的PHP代码中包含包的自动加载文件,并调用类Spotify

<?php
require('./vendor/autoload.php');

$spotify = new SpotifyLyricsApi\Spotify("SP_DC here");
$spotify->checkTokenExpire();
$reponse = $spotify -> getLyrics(track_id: "1418IuVKQPTYqt7QNJ9RXN");
?>

image

部署

想要托管此API的版本,但首先您需要从Spotify获取SP_DC cookie

查找SP_DC

您可以在此处找到详细的指南。

Heroku

Deploy

Vercel

Deploy with Vercel

本地运行

使用git将仓库克隆到您的本地机器,或者您可以下载最新的zip文件并将其解压。

您需要在机器上安装PHP才能运行此程序。

通过终端进入文件夹

cd spotify-lyrics-api

临时设置SP_DC令牌为环境变量

export SP_DC=[token here and remove the square brackets]

启动服务器

php -S localhost:8000 api/index.php

现在打开浏览器并输入localhost:8080,您应该能看到程序正在运行。

鸣谢

-> 对于一切。