Skip to content

Commit

Permalink
genius
Browse files Browse the repository at this point in the history
  • Loading branch information
DerTyp7214 committed May 31, 2022
1 parent b98a697 commit f5684b4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "youtube-music",
"productName": "YouTube Music",
"version": "1.17.5",
"version": "1.17.6",
"description": "YouTube Music Desktop App - including custom plugins",
"license": "MIT",
"repository": {
Expand Down
23 changes: 21 additions & 2 deletions plugins/extract-audio-data/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,33 @@ const sampleRate = 24000

const barCount = fftSize / 2

const manipulators = _createCurve([.7, .75, .8, .9, .95, 1, 1.1, 1.15], barCount)
const pinkNoise = [
1.0548608488838, 0.76054078751554, 0.61124787706261, 0.52188737442096,
0.47582581340335, 0.442985940855, 0.39506604448116, 0.38179901474466,
0.3791498265819, 0.35862620105656, 0.34117808276167, 0.31407858754586,
0.32956896818321, 0.32649587026332, 0.32553041354753, 0.33023063745582,
0.33723850113961, 0.32845876137105, 0.32345077632073, 0.33371703524763,
0.33559351013352, 0.32755038614695, 0.33723270172874, 0.33152196761531,
0.34253960054833, 0.33996676648346, 0.35007384375669, 0.34140414964718,
0.35276302794926, 0.45428847576802, 0.57092841582994, 0.56249676873287,
0.64297260455787, 0.64261475342015, 0.72339198663831, 0.73733259583513,
0.83130048006773, 0.86110594108701, 0.93924222866694, 0.97183918188016,
1.0510377466679, 1.1248085597157, 1.1805661781629, 1.2060520313183,
1.2870901748538, 1.3467060487469, 1.419748566548, 1.4930113442739,
1.5233661865195, 1.6291546697418, 1.6687760437528, 1.7517802578211,
1.7828743148843, 1.8640559593836, 1.9024009352922, 1.9445452898741,
2.0042892436186, 2.0429756359259, 2.0702872782946, 2.0901099761327,
2.0997672257821, 2.1029779444138, 2.0654643664757, 2.0357843961318
]

const manipulators = _createCurve(pinkNoise, barCount)

function _createCurve(manipulator, count) {
const times = count / manipulator.length
if (times < 1) return manipulator
const curve = []
for (let i = 0; i < manipulator.length; i++) for (let j = 0; j < times; j++) curve.push(
curve.length > 0 ? (curve[curve.length - 1] + manipulator[i] + .000001) / 2 : manipulator[i]
curve.length > 0 ? (curve[curve.length - 1] + manipulator[i]) / 2 : manipulator[i]
)
return curve
}
Expand Down
63 changes: 33 additions & 30 deletions plugins/lyrics-genius/back.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
const {join} = require("path");
const {join} = require("path")

const {ipcMain} = require("electron");
const is = require("electron-is");
const {convert} = require("html-to-text");
const fetch = require("node-fetch");
const {ipcMain} = require("electron")
const is = require("electron-is")
const {convert} = require("html-to-text")
const fetch = require("node-fetch")

const {cleanupName} = require("../../providers/song-info");
const {injectCSS} = require("../utils");
const {cleanupName} = require("../../providers/song-info")
const {injectCSS} = require("../utils")

let lastLyrics = {}

module.exports = async (win) => {
injectCSS(win.webContents, join(__dirname, "style.css"));
injectCSS(win.webContents, join(__dirname, "style.css"))

ipcMain.on("search-genius-lyrics", async (event, extractedSongInfo) => {
const metadata = JSON.parse(extractedSongInfo);
event.returnValue = await fetchFromGenius(metadata);
event.returnValue = await fetchFromGenius(metadata)
});
};

const fetchFromGenius = async (metadata) => {
const queryString = `${cleanupName(
metadata.title
)} by ${cleanupName(metadata.artist)}`;
)} by ${cleanupName(metadata.artist)}`

if (lastLyrics.queryString === queryString && lastLyrics.lyrics) return lastLyrics.lyrics

let response = await fetch(
`https://genius.com/api/search/multi?per_page=5&q=${encodeURI(queryString)}`
);
if (!response.ok) {
return null;
}
)
if (!response.ok) return null

const info = await response.json();
let url = "";
const info = await response.json()
let url = ''
try {
url = info.response.sections.filter((section) => section.type === "song")[0]
.hits[0].result.url;
const title = metadata.title.toLowerCase()
const artist = metadata.artist.toLowerCase()
const sections = info.response.sections
const songs = sections.find(section => section.type === 'song')
const hit = songs.hits.find(song =>
(song.result.artist_names.toLowerCase().includes(artist) &&
song.result.full_title.toLowerCase().includes(title)) ||
(song.result.full_title.toLowerCase().includes(artist) &&
song.result.full_title.toLowerCase().includes(title))
) ?? songs.hits[0]
if (hit) url = hit.result.url
} catch {
return null;
return null
}

if (is.dev()) {
console.log("Fetching lyrics from Genius:", url);
}
if (is.dev()) console.log("Fetching lyrics from Genius:", url)

response = await fetch(url);
if (!response.ok) {
return null;
}
response = await fetch(url)
if (!response.ok) return null

const html = await response.text();
const html = await response.text()
const lyrics = convert(html, {
baseElements: {
selectors: ['[class^="Lyrics__Container"]', ".lyrics"],
Expand All @@ -68,12 +71,12 @@ const fetchFromGenius = async (metadata) => {
walk(elem.children, builder);
},
},
});
})

lastLyrics.lyrics = lyrics
lastLyrics.queryString = queryString

return lyrics;
return lyrics
};

module.exports.fetchFromGenius = fetchFromGenius;
module.exports.fetchFromGenius = fetchFromGenius

0 comments on commit f5684b4

Please sign in to comment.