Refactor Genius logic into separate util

This commit is contained in:
TC
2022-02-13 22:19:16 +01:00
parent 6cad6871bf
commit ea35da52c3

View File

@ -16,25 +16,25 @@ module.exports = async (win) => {
metadata.title metadata.title
)}`; )}`;
event.returnValue = await fetchFromGenius(queryString);
});
};
const fetchFromGenius = async (queryString) => {
let response = await fetch( let response = await fetch(
`https://genius.com/api/search/multi?per_page=5&q=${encodeURI( `https://genius.com/api/search/multi?per_page=5&q=${encodeURI(queryString)}`
queryString
)}`
); );
if (!response.ok) { if (!response.ok) {
event.returnValue = null; return null;
return;
} }
const info = await response.json(); const info = await response.json();
let url = ""; let url = "";
try { try {
url = info.response.sections.filter( url = info.response.sections.filter((section) => section.type === "song")[0]
(section) => section.type === "song" .hits[0].result.url;
)[0].hits[0].result.url;
} catch { } catch {
event.returnValue = null; return null;
return;
} }
if (is.dev()) { if (is.dev()) {
@ -43,10 +43,8 @@ module.exports = async (win) => {
response = await fetch(url); response = await fetch(url);
if (!response.ok) { if (!response.ok) {
event.returnValue = null; return null;
return;
} }
event.returnValue = await response.text(); return await response.text();
});
}; };