Skip to content

Commit ea35da5

Browse files
committed
Refactor Genius logic into separate util
1 parent 6cad687 commit ea35da5

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

plugins/lyrics-genius/back.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,35 @@ module.exports = async (win) => {
1616
metadata.title
1717
)}`;
1818

19-
let response = await fetch(
20-
`https://genius.com/api/search/multi?per_page=5&q=${encodeURI(
21-
queryString
22-
)}`
23-
);
24-
if (!response.ok) {
25-
event.returnValue = null;
26-
return;
27-
}
28-
29-
const info = await response.json();
30-
let url = "";
31-
try {
32-
url = info.response.sections.filter(
33-
(section) => section.type === "song"
34-
)[0].hits[0].result.url;
35-
} catch {
36-
event.returnValue = null;
37-
return;
38-
}
39-
40-
if (is.dev()) {
41-
console.log("Fetching lyrics from Genius:", url);
42-
}
43-
44-
response = await fetch(url);
45-
if (!response.ok) {
46-
event.returnValue = null;
47-
return;
48-
}
49-
50-
event.returnValue = await response.text();
19+
event.returnValue = await fetchFromGenius(queryString);
5120
});
5221
};
22+
23+
const fetchFromGenius = async (queryString) => {
24+
let response = await fetch(
25+
`https://genius.com/api/search/multi?per_page=5&q=${encodeURI(queryString)}`
26+
);
27+
if (!response.ok) {
28+
return null;
29+
}
30+
31+
const info = await response.json();
32+
let url = "";
33+
try {
34+
url = info.response.sections.filter((section) => section.type === "song")[0]
35+
.hits[0].result.url;
36+
} catch {
37+
return null;
38+
}
39+
40+
if (is.dev()) {
41+
console.log("Fetching lyrics from Genius:", url);
42+
}
43+
44+
response = await fetch(url);
45+
if (!response.ok) {
46+
return null;
47+
}
48+
49+
return await response.text();
50+
};

0 commit comments

Comments
 (0)