akashrchandran/spotify-lyrics-api
Composer 安装命令:
composer require akashrchandran/spotify-lyrics-api
包简介
A Rest API for fetching lyrics from Spotify which is powered by Musixmatch.
README 文档
README
A Rest API for fetching lyrics from Spotify which is powered by Musixmatch. Commandline version is available akashrchandran/syrics.
Warning
This project is probably against Spotify TOS. Use at your own risks.
Note
Changed this project into a template repository, deploy your own version. If you need help, don't hesitate to open an issue.
Quick Start with Docker
The easiest way to run the API is using Docker:
docker run -d -p 8080:8080 -e SP_DC=your_sp_dc_cookie akashrchandran/spotify-lyrics-api
Then access the API at http://localhost:8080/?trackid=YOUR_TRACK_ID
Docker Compose
services: spotify-lyrics-api: image: akashrchandran/spotify-lyrics-api:latest ports: - "8080:8080" environment: - SP_DC=your_sp_dc_cookie restart: unless-stopped
Install using Composer
composer require akashrchandran/spotify-lyrics-api
Fetching Lyrics
For now it only supports track id or link.
Using GET Requests
You have to use query paramters to send data
Available Parameters:
| Parameter | Default value | Type | Description |
|---|---|---|---|
trackid |
None | String | The trackid from spotify. |
url |
None | String | The url of the track from spotify. |
format |
"id3" |
String | The format of lyrics required. Options: id3, lrc, srt, raw. |
You must specify either trackid or url, otherwise it will return error.
Examples
Using trackid
http://localhost:8080/?trackid=5f8eCNwTlr0RJopE9vQ6mB
Using url
http://localhost:8080/?url=https://open.spotify.com/track/5f8eCNwTlr0RJopE9vQ6mB?autoplay=true
response:
{
"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"
}
]
}
Changing format to lrc
http://localhost:8080/?trackid=5f8eCNwTlr0RJopE9vQ6mB&format=lrc
response:
{
"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"
}
]
}
Responses
Different Responses given out by the API, are listed here.
If any error occurs the value of the error key will be set to true else false
"error": false //no error occured
Most of the lyrics are time synced or have timetags and some aren't time synced or have timetags. To differentiate between synced and unsynced we have key syncType.
"syncType": "LINE_SYNCED"
Musixmatch supports Line synced and Word synced type of timed lyrics. Line Synced is the timetag is given till which the line is sang and the word synced lyrics time specifed when the word comes up in the song. For now Spotify only supports line synced. Maybe they would support word synced in the future :/.
LINE Synced
{
"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"
}
]
}
NOT Synced or Unsynced
Note the
timeTagsis set to00: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"
}
]
}
Error Messages
When trackid and url both are not given (400 Bad Request)
error response:
{
"error": true,
"message": "url or trackid parameter is required!"
}
When no lyrics found on spotify for given track (404 Not Found)
error response:
{
"error": true,
"message": "lyrics for this track is not available on spotify!"
}
Using as package
Install using
composer require akashrchandran/spotify-lyrics-api.
Include the package's autoloader file in your PHP code and call class Spotify().
<?php require './vendor/autoload.php'; use SpotifyLyricsApi\Spotify; use SpotifyLyricsApi\SpotifyException; $spotify = new Spotify("SP_DC here"); try { $spotify->checkTokenExpire(); $lyrics = $spotify->getLyrics(track_id: "1418IuVKQPTYqt7QNJ9RXN"); // $lyrics contains the parsed lyrics data echo "Sync Type: " . $lyrics['lyrics']['syncType'] . "\n"; foreach ($lyrics['lyrics']['lines'] as $line) { echo "[" . $line['startTimeMs'] . "] " . $line['words'] . "\n"; } } catch (SpotifyException $e) { // Handle errors (rate limiting, not found, etc.) echo "Error: " . $e->getMessage() . "\n"; echo "Status Code: " . $e->getCode() . "\n"; }
Formatting Lyrics
// Get lyrics in LRC format $lrcLines = $spotify->getLrcLyrics($lyrics['lyrics']['lines']); // Get lyrics in SRT format $srtLines = $spotify->getSrtLyrics($lyrics['lyrics']['lines']); // Get raw lyrics (plain text) $rawLines = $spotify->getRawLyrics($lyrics['lyrics']['lines']);
Deployment
Want to host your own version of this API, But first you need SP_DC cookie from spotify
Finding SP_DC
You will find the detailed guide here.
Heroku
Vercel
Run locally
use git to clone the repo to your local machine or you can download the latest zip file and extract it.
You need to have PHP installed on you machine to run this program.
Enter into the folder via terminal
cd spotify-lyrics-api
Set SP_DC token as environment variable temprorily
export SP_DC=[token here and remove the square brackets]
Start the server
php -S localhost:8080 api/index.php
now open your browser and type localhost:8080 and you should see the program running.
Credits
• Me -> For everything.
akashrchandran/spotify-lyrics-api 适用场景与选型建议
akashrchandran/spotify-lyrics-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 669 次下载、GitHub Stars 达 624, 最近一次更新时间为 2023 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 akashrchandran/spotify-lyrics-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 akashrchandran/spotify-lyrics-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 669
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 625
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-only
- 更新时间: 2023-01-12
