Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- base : www.youtube.com
- fungsi : buat dapetin value youtube post
- node : v24.20
- by : wolep
- note : biasa, single async function, 1 param url post yt
- return object, skrep support single post (text/image)
- multiple image post, vote (text/image), youtube post
- sebenarnya ada banyak ukuran image, cuma gw pick
- yang ukuran paling besar. biar simple. gw suka yang
- besar besar 😏. udh gw rapiin valuenya jadi lu dapet
- json yang konsistent, kalaupun gak ada value nya udh
- gw kasih null
- sorry kalau ada kesalahan skrep :v... enjoyy
- update : 23 Juli 2025, 02:22 WITA
- */
- const ytpost = async (ytpostUrl) => {
- if (!ytpostUrl) throw Error(`mana url youtube post nya >:o`)
- const response = await fetch(ytpostUrl)
- if (!response.ok) throw Error(`${response.status} ${response.statusText}\n${await response.text() || null}`)
- const html = await response.text()
- const match = html.match(/ytInitialData = (.+?);</)?.[1]
- if (!match) throw Error(`tidak menemukan match pada youtube post pastiin link post youtube nya benar.`)
- const json = JSON.parse(match) // bisa langsung return json variabel nya
- //return json
- // atau pakai serialize ku uwu
- let postType = null
- let images = null
- let videoShareUrl = null
- const bpr = json?.contents?.twoColumnBrowseResultsRenderer?.tabs?.[0]?.tabRenderer?.content?.sectionListRenderer?.contents?.[0]?.itemSectionRenderer?.contents?.[0]?.backstagePostThreadRenderer?.post?.backstagePostRenderer
- if (!bpr) throw Error (`hmm.. link youtube sih tapi keknya bukan post yang valid atau ada kesalahan`)
- const votePost = bpr?.backstageAttachment?.pollRenderer?.choices
- const multipleImagePost = bpr?.backstageAttachment?.postMultiImageRenderer?.images
- const singleImagePost = bpr?.backstageAttachment?.backstageImageRenderer?.image?.thumbnails
- const videoSharePost = bpr?.backstageAttachment?.videoRenderer?.videoId
- if (votePost) {
- let isVoteImage = false
- images = votePost.map(v => {
- const text = v.text.runs[0].text
- let url = v.image?.thumbnails || null
- if (url) {
- url = url.map(wolep => wolep.url).pop()
- isVoteImage = true
- }
- return { text, url }
- })
- isVoteImage ? postType = "voteImage" : postType = "voteText"
- } else if (multipleImagePost) {
- postType = "multipleImages"
- const imagesArray = multipleImagePost.map(v => v.backstageImageRenderer.image.thumbnails)
- images = imagesArray.map(v => {
- const url = v.map(wolep => wolep.url).pop()
- return { url, text: null }
- })
- } else if (singleImagePost) {
- postType = "singleImage"
- images = []
- images.push({
- url: singleImagePost.map(v => {
- return v.url
- }).pop(),
- text: null
- })
- } else if (videoSharePost) {
- postType = "videoShare"
- videoShareUrl = new URL(response.url).origin + videoSharePost
- } else {
- postType = "text"
- }
- let tags = json?.microformat?.microformatDataRenderer?.tags || null
- if (tags){
- tags = tags.join(", ")
- }
- let result = {
- author: bpr.authorText.runs[0].text,
- authorUrl: new URL(response.url).origin + bpr.authorEndpoint.commandMetadata.webCommandMetadata.url,
- publishTime: bpr.publishedTimeText.runs[0].text,
- publishDate: json.microformat.microformatDataRenderer.publishDate,
- tags,
- text: bpr.contentText.runs.map(v => v.text).join(""),
- like: bpr?.voteCount?.accessibility?.accessibilityData?.label || null,
- images,
- videoShareUrl,
- postUrl: json.microformat.microformatDataRenderer.urlCanonical,
- postType
- }
- return result // tada :v
- }
- // cara pakai
- const url = "http://youtube.com/post/UgkxChSWx6pfUUCxxHeR3LsodyV6Aqwf4GOS?si=Ywu5j0qw08A50lue"
- ytpost(url)
- .then(data => console.log(JSON.stringify(data, null, 2)))
- .catch(console.log)
- /* hasil
- {
- "author": "the wolf",
- "authorUrl": "https://www.youtube.com/@thewolf-ps1qz",
- "publishTime": "10 jam yang lalu",
- "publishDate": "2025-06-22T00:48:53.632504-07:00",
- "tags": null,
- "text": "voting teks",
- "like": "1 suka",
- "images": [
- {
- "text": "wolpi",
- "url": "https://yt3.ggpht.com/F1TWx1qxb6MG2ybiB5z47-kgqrX59UYrScNFCoyc2I0bA5834kY66b5QrvjvFbxkRdT5U3kMHpzxjg=s1280-c-fcrop64=1,00000000ffffffff-nd-v1"
- },
- {
- "text": "wolep",
- "url": "https://yt3.ggpht.com/oVXiAkcgXxd9frkywNTWnruEHEjp-6Inznq1xzw8jlVaAXhxzXqSXjznDyYmNemdqMttwFe8iI7aeg=s1280-c-fcrop64=1,00000000ffffffff-nd-v1"
- },
- {
- "text": "okta",
- "url": "https://yt3.ggpht.com/BsQf5qn1StnkLLzv1KWw_IwV4X5RRYQYNj-D-lfP51_39leUWqeBWQS0gMDipV8jFFkOowRSdE47=s1280-c-fcrop64=1,00000000ffffffff-nd-v1"
- }
- ],
- "videoShareUrl": null,
- "postUrl": "https://www.youtube.com/post/UgkxChSWx6pfUUCxxHeR3LsodyV6Aqwf4GOS",
- "postType": "voteImage"
- }
- */
Add Comment
Please, Sign In to add comment