-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from whitep4nth3r/remove-twitch-ef
Make home page static again
- Loading branch information
Showing
12 changed files
with
371 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
const accessTokenFetchUrl = `https://id.twitch.tv/oauth2/token?client_id=${process.env.TWITCH_CLIENT_ID}&client_secret=${process.env.TWITCH_CLIENT_SECRET}&grant_type=client_credentials&scope=user_read`; | ||
const twitchId = "469006291"; | ||
|
||
let tokenInMemory = null; | ||
|
||
const TwitchApi = { | ||
getAccessToken: async function () { | ||
try { | ||
const response = await fetch(accessTokenFetchUrl, { | ||
method: "POST", | ||
headers: { accept: "application/vnd.twitchtv.v5+json" }, | ||
}); | ||
const token = await response.json(); | ||
tokenInMemory = token; | ||
|
||
return token; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}, | ||
getfetchOptions: function (tokenResponse) { | ||
return { | ||
headers: { | ||
"Client-Id": process.env.TWITCH_CLIENT_ID, | ||
Authorization: `Bearer ${tokenResponse.access_token}`, | ||
}, | ||
}; | ||
}, | ||
getStreams: async function () { | ||
const tokenResponse = | ||
tokenInMemory !== null ? tokenInMemory : await TwitchApi.getAccessToken(); | ||
if (tokenResponse.access_token) { | ||
const streamsResponse = await fetch( | ||
`https://api.twitch.tv/helix/streams?user_id=${twitchId}`, | ||
TwitchApi.getfetchOptions(tokenResponse), | ||
); | ||
|
||
if (streamsResponse.status !== 200) { | ||
return null; | ||
} | ||
|
||
const streams = await streamsResponse.json(); | ||
return streams; | ||
} | ||
}, | ||
getVods: async function () { | ||
const tokenResponse = | ||
tokenInMemory !== null ? tokenInMemory : await TwitchApi.getAccessToken(); | ||
if (tokenResponse.access_token) { | ||
const vodsResponse = await fetch( | ||
`https://api.twitch.tv/helix/videos?user_id=${twitchId}&type=archive&first=1`, | ||
TwitchApi.getfetchOptions(tokenResponse), | ||
); | ||
|
||
if (vodsResponse.status !== 200) { | ||
return null; | ||
} | ||
|
||
const vods = await vodsResponse.json(); | ||
return vods; | ||
} | ||
}, | ||
}; | ||
|
||
module.exports = async function () { | ||
const imageSizeOffline = "998x556"; | ||
|
||
const streams = await TwitchApi.getStreams(); | ||
|
||
if (streams !== null && streams.data.length === 1) { | ||
return { | ||
isLive: true, | ||
}; | ||
} else { | ||
const vods = await TwitchApi.getVods(); | ||
|
||
if (vods !== null) { | ||
const latestVod = vods.data[0]; | ||
|
||
const today = new Date(); | ||
const createdOn = new Date(latestVod.created_at); | ||
const msInDay = 24 * 60 * 60 * 1000; | ||
|
||
createdOn.setHours(0, 0, 0, 0); | ||
today.setHours(0, 0, 0, 0); | ||
|
||
const diff = (+today - +createdOn) / msInDay; | ||
let subtitle; | ||
|
||
if (diff === 0) { | ||
subtitle = "Earlier today"; | ||
} else if (diff === 1) { | ||
subtitle = "Yesterday"; | ||
} else { | ||
subtitle = `${diff} days ago`; | ||
} | ||
|
||
const thumb_url = !latestVod.thumbnail_url.includes("processing") | ||
? latestVod.thumbnail_url.replace( | ||
"%{width}x%{height}", | ||
imageSizeOffline, | ||
) | ||
: "/img/stream_thumb_fallback.jpg"; | ||
|
||
return { | ||
isLive: false, | ||
vodData: { | ||
thumbnail: { | ||
url: thumb_url, | ||
height: "1080", | ||
width: "1920", | ||
}, | ||
title: latestVod.title, | ||
subtitle: subtitle, | ||
link: latestVod.url, | ||
}, | ||
}; | ||
} else { | ||
return { | ||
isLive: false, | ||
vodData: { | ||
thumbnail: { | ||
url: "/img/stream_thumb_fallback.jpg", | ||
height: "1080", | ||
width: "1920", | ||
}, | ||
title: "Watch me fix my website LIVE: debug behind the scenes", | ||
subtitle: "Last week", | ||
link: "https://whitep4nth3r.com", | ||
}, | ||
}; | ||
} | ||
} | ||
}; |
Oops, something went wrong.