Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexInABox committed Nov 29, 2023
1 parent e03014c commit 04493b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions modules/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ async function getLatestYTMusicThumbnail(history) {
"https://i.ytimg.com/vi/" + videoId + "/3.jpg",
"https://i.ytimg.com/vi/" + videoId + "/0.jpg"
]

await intialFetchOfDefaultThumbnail();
for (let i = 0; i < thumbnailURLs.length; i++) {
try {
if (await isNotDefault(thumbnailURLs[i])) {
Expand All @@ -204,16 +206,23 @@ async function getLatestYTMusicThumbnail(history) {
))
}

function isNotDefault(url) { //get the uint8Array buffer and compare it to the default buffer string ("lorem ipsum")
return new Promise(async (res) => {
var defaultThumbnailUnit8Array;

function intialFetchOfDefaultThumbnail() {
return new Promise(async (finish) => {
const defaultResponse = await fetch("https://i.ytimg.com/vi/vi/0/maxresdefault.jpg"); //TODO: only fetch once (or once per iteration)
const defaultBuffer = await defaultResponse.arrayBuffer();
const defaultUint8Array = new Uint8Array(defaultBuffer);
defaultThumbnailUnit8Array = new Uint8Array(defaultBuffer);
finish();
})
}

function isNotDefault(url) { //get the uint8Array buffer and compare it to the default buffer string ("lorem ipsum")
return new Promise(async (res) => {
const response = await fetch(url);
const buffer = await response.arrayBuffer();
const uint8Array = new Uint8Array(buffer);
res(uint8Array.toString() != defaultUint8Array.toString())
res(uint8Array.toString() != defaultThumbnailUnit8Array.toString())
})
}
//End of YTMusic API functions
Expand Down
10 changes: 5 additions & 5 deletions presence.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
},
"youtube": {
"video": {
"title": "The Fousey Situation Can’t Get Worse…",
"channel": "oompaville",
"url": "https://www.youtube.com/watch?v=GdL4et8gEqM",
"thumbnail": "https://i.ytimg.com/vi/GdL4et8gEqM/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCwAS5W2ILTTLNW2nonhIC-SJSkJg",
"lastUpdate": 1693257608298
"title": "The End of Evangelion - Shinji discovers Unit 02's corpse (a.k.a Shinji screams)",
"channel": "Devpop",
"url": "https://www.youtube.com/watch?v=dSuYWdn61_o",
"thumbnail": "https://i.ytimg.com/vi/dSuYWdn61_o/hqdefault.jpg?sqp=-oaymwE2CNACELwBSFXyq4qpAygIARUAAIhCGAFwAcABBvABAfgB_gmAAtAFigIMCAAQARhyIEEoPTAP&rs=AOn4CLBxyuFfUeN4gYJlb3JKgd5FCBxDXg",
"lastUpdate": 1701259191992
},
"music": {
"title": "All Alone (Official Video)",
Expand Down

0 comments on commit 04493b5

Please sign in to comment.