Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(popupLyrics/netease): use PyNCMd API #2516

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions Extensions/popupLyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ function PopupLyrics() {
}

class LyricProviders {
/** Netease PyNCM API
*
* @typedef {{
* result: {
* songs: {
* name: string,
* id: number,
* dt: number, // duration in ms
* al: { // album
* name: string,
* },
* }[],
* },
* }} SearchResponse
*
* @typedef {{
* title: string,
* artist: string,
* album: string,
* duration: number,
* }} Info
*
* @typedef {{
* lrc: {
* lyric: string,
* klyric: undefined, // unimplemented
* },
* }} neteaseLyric
Lseoksee marked this conversation as resolved.
Show resolved Hide resolved
*/

static async fetchSpotify(info) {
const baseURL = "wg://lyrics/v1/track/";
const id = info.uri.split(":")[2];
Expand Down Expand Up @@ -158,27 +188,41 @@ function PopupLyrics() {
}
}

/**
* Search with PyNCM api.
*
* @param {Info} info
* @returns {{
* lyrics: [
* {
* startTime: Number,
* text: string,
* }
* ],
Lseoksee marked this conversation as resolved.
Show resolved Hide resolved
* }}
*/
static async fetchNetease(info) {
const searchURL = `https://music.xianqiao.wang/neteaseapiv2/search?limit=10&type=1&keywords=`;
const lyricURL = `https://music.xianqiao.wang/neteaseapiv2/lyric?id=`;
const requestHeader = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"
};
const searchURL = `https://pyncmd.apis.imouto.in/api/pyncm?module=cloudsearch&method=GetSearchResult&keyword=`;
const lyricURL = `https://pyncmd.apis.imouto.in/api/pyncm?module=track&method=GetTrackLyrics&song_id=`;
afonsojramos marked this conversation as resolved.
Show resolved Hide resolved

const cleanTitle = LyricUtils.removeExtraInfo(LyricUtils.normalize(info.title));
const finalURL = searchURL + encodeURIComponent(`${cleanTitle} ${info.artist}`);

const searchResults = await CosmosAsync.get(finalURL, null, requestHeader);
/** @type {SearchResponse} */
const searchResults = await CosmosAsync.get(finalURL);
const items = searchResults.result.songs;
if (!items || !items.length) {

if (!items) {
return { error: "Cannot find track" };
}

const album = LyricUtils.capitalize(info.album);
let itemId = items.findIndex(val => LyricUtils.capitalize(val.album.name) === album || Math.abs(info.duration - val.duration) < 1000);

let itemId = items.findIndex(val => LyricUtils.capitalize(val.al.name) === album || Math.abs(info.duration - val.dt) < 1000);
if (itemId === -1) return { error: "Cannot find track" };

const meta = await CosmosAsync.get(lyricURL + items[itemId].id, null, requestHeader);
/** @type {neteaseLyric} */
const meta = await CosmosAsync.get(lyricURL + items[itemId].id);
let lyricStr = meta.lrc;

if (!lyricStr || !lyricStr.lyric) {
Expand Down