Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
major rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
LewdHuTao committed May 25, 2024
1 parent 671b1d9 commit 988eecc
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 26 deletions.
106 changes: 103 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "llyrics",
"version": "1.0.6",
"version": "1.0.7",
"description": "A simple package to fetch lyrics from Genius API",
"main": "src/index.js",
"scripts": {
Expand All @@ -9,7 +9,8 @@
"author": "LewdHuTao",
"license": "ISC",
"dependencies": {
"axios": "^1.6.8"
"axios": "^1.6.8",
"node-fetch": "^3.3.2"
},
"repository": {
"type": "git",
Expand All @@ -25,5 +26,8 @@
"bugs": {
"url": "https://github.com/shittybot/llyrics/issues"
},
"homepage": "https://github.com/shittybot/llyrics#readme"
}
"homepage": "https://github.com/shittybot/llyrics#readme",
"devDependencies": {
"dotenv": "^16.4.5"
}
}
82 changes: 63 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
const axios = require("axios");
async function findLyrics({
search_engine: { musixmatch, genius },
song_title,
genius_api,
}) {
let apiBaseUrl = "https://lyrics.lewdhutao.my.eu.org";
let songTitle = song_title;

async function findLyrics(apiKey, songName) {

let api_key = apiKey;
let song_Name = songName;
let lyrics;
let trackName;
let trackName;
let trackArtist;
let artworkUrl;
let searchEngine;

try {
const searchResponse = await axios.get(`https://llyrics-api.vercel.app//lyrics?title=${song_Name}&geniusAPI=${api_key}`)
if (searchResponse.status === 200) {
const searchData = searchResponse.data;
lyrics = searchData.lyrics;
trackName = searchData.trackName;
trackArtist = searchData.trackArtist;
if (!lyrics) throw new Error("No lyrics found.");
findLyrics.lyrics = lyrics;
findLyrics.trackName = trackName;
findLyrics.trackArtist = trackArtist;
}
if (musixmatch === true) {
const musixmatchResponse = await fetch(
`${apiBaseUrl}/musixmatch/lyrics?title=${encodeURIComponent(songTitle)}`
);
const musixmatchData = await musixmatchResponse.json();

trackArtist = musixmatchData.artist_name;
trackName = musixmatchData.track_name;
searchEngine = musixmatchData.search_engine;
artworkUrl = musixmatchData.artwork_url;
lyrics = musixmatchData.lyrics;

if (!lyrics && genius === true) {
const geniusResponse = await fetch(
`${apiBaseUrl}/genius/lyrics?title=${encodeURIComponent(songTitle)}&api_key=${genius_api}`
);
const geniusData = await geniusResponse.json();

trackArtist = geniusData.artist_name;
trackName = geniusData.track_name;
searchEngine = geniusData.search_engine;
artworkUrl = geniusData.artworkUrl;
lyrics = geniusData.lyrics;
}
} else if (genius === true) {
const geniusResponse = await fetch(
`${apiBaseUrl}/genius/lyrics?title=${encodeURIComponent(songTitle)}&api_key=${genius_api}`
);
const geniusData = await geniusResponse.json();

trackArtist = geniusData.artist_name;
trackName = geniusData.track_name;
searchEngine = geniusData.search_engine;
artworkUrl = geniusData.artworkUrl;
lyrics = geniusData.lyrics;
} else {
throw new Error("Both Musixmatch and Genius are disabled.");
}

if (!lyrics) {
throw new Error("No lyrics found.");
}
} catch (error) {
throw new Error(error);
console.error("Error:", error.message);
throw error;
}

return {
trackArtist,
trackName,
searchEngine,
artworkUrl,
lyrics
};
}

module.exports = findLyrics;
module.exports = { findLyrics };
41 changes: 41 additions & 0 deletions src/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { findLyrics } = require("../index");
require("dotenv").config();

async function testFindLyrics() {
try {
console.log("Testing with Musixmatch enabled:");
const musixmatch = await findLyrics({
search_engine: { musixmatch: true, genius: false },
song_title: "fly high",
});

const musixmatch_artist = musixmatch.trackArtist;
const musixmatch_track = musixmatch.trackName;
const musixmatch_lyrics = musixmatch.lyrics;
const musixmatch_data = {
musixmatch_artist,
musixmatch_track,
musixmatch_lyrics,
};

console.log("\nTesting with Genius enabled:");
const genius = await findLyrics({
search_engine: { musixmatch: false, genius: true },
song_title: "fly high",
genius_api: process.env.GENIUS_API,
});

const genius_artist = genius.trackArtist;
const genius_track = genius.trackName;
const genius_lyrics = genius.lyrics;
const genius_data = { genius_artist, genius_track, genius_lyrics };

if (musixmatch_data && genius_data) {
console.log("Test Passed");
}
} catch (error) {
console.error("Error:", error.message);
}
}

testFindLyrics();

0 comments on commit 988eecc

Please sign in to comment.