Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

add getVideoInformation

Christian Fei651dc6b841f1
diff --git a/lib/youtube.js b/lib/youtube.js
index 2293224..2f7d63f 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -134,3 +134,32 @@ function parseRelativeTime(relativeTime) {
console.error('Invalid relative time format', relativeTime);
return null
}
+
+export async function getVideoInformation(id) {
+ if (id.startsWith('https://')) id = id.replace(/https:\/\/www\.youtube\.com\/watch\?v=/, '')
+ try {
+ const response = await fetch(`https://www.youtube.com/watch?v=${id}`, {
+ headers: { 'Accept-Language': 'en' }
+ })
+ const text = await response.text()
+ const match = text.match(/var ytInitialData = (.+?);<\/script>/)
+ if (!match || !match[1]) return null
+ const json = JSON.parse(match[1].trim())
+ console.log(JSON.stringify(json))
+ return {
+ channelName: json.playerOverlays.playerOverlayRenderer.autoplay.playerOverlayAutoplayRenderer.byline.runs[0].navigationEndpoint.browseEndpoint.canonicalBaseUrl.replace('/@',''),
+ title: json.playerOverlays.playerOverlayRenderer.autoplay.playerOverlayAutoplayRenderer.videoTitle.simpleText,
+ url: `https://www.youtube.com/watch?v=${id}`,
+ thumbnail: `https://img.youtube.com/vi/${id}/mqdefault.jpg`,
+ description: ' ',
+ id,
+ publishedTime: json.contents.twoColumnWatchNextResults.results.results.contents[0].videoPrimaryInfoRenderer.relativeDateText.simpleText,
+ publishedAt: parseRelativeTime(json.contents.twoColumnWatchNextResults.results.results.contents[0].videoPrimaryInfoRenderer.relativeDateText.simpleText),
+ viewCount: ' ', // data.viewCountText?.simpleText,
+ duration: ' ', // data.lengthText?.simpleText,
+ }
+ } catch (error) {
+ console.error('Error fetching video information:', error.message)
+ return null
+ }
+}
\ No newline at end of file