wolfyflutter

[www.youtube.com] [post] [downloader] [official api]

Jun 22nd, 2025 (edited)
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   base   : www.youtube.com
  3.   fungsi : buat dapetin value youtube post
  4.   node   : v24.20
  5.   by     : wolep
  6.   note   : biasa, single async function, 1 param url post yt
  7.            return object, skrep support single post (text/image)
  8.            multiple image post, vote (text/image), youtube post
  9.            sebenarnya ada banyak ukuran image, cuma gw pick
  10.            yang ukuran paling besar. biar simple. gw suka yang
  11.            besar besar 😏. udh gw rapiin valuenya jadi lu dapet
  12.            json yang konsistent, kalaupun gak ada value nya udh
  13.            gw kasih null
  14.            sorry kalau ada kesalahan skrep :v... enjoyy
  15.   update : 23 Juli 2025, 02:22 WITA
  16. */
  17. const ytpost = async (ytpostUrl) => {
  18.     if (!ytpostUrl) throw Error(`mana url youtube post nya >:o`)
  19.  
  20.     const response = await fetch(ytpostUrl)
  21.     if (!response.ok) throw Error(`${response.status} ${response.statusText}\n${await response.text() || null}`)
  22.  
  23.     const html = await response.text()
  24.  
  25.     const match = html.match(/ytInitialData = (.+?);</)?.[1]
  26.     if (!match) throw Error(`tidak menemukan match pada youtube post pastiin link post youtube nya benar.`)
  27.  
  28.     const json = JSON.parse(match) // bisa langsung return json variabel nya
  29.     //return json
  30.  
  31.     // atau pakai serialize ku uwu
  32.     let postType = null
  33.     let images = null
  34.     let videoShareUrl = null
  35.  
  36.     const bpr = json?.contents?.twoColumnBrowseResultsRenderer?.tabs?.[0]?.tabRenderer?.content?.sectionListRenderer?.contents?.[0]?.itemSectionRenderer?.contents?.[0]?.backstagePostThreadRenderer?.post?.backstagePostRenderer
  37.     if (!bpr) throw Error (`hmm.. link youtube sih tapi keknya bukan post yang valid atau ada kesalahan`)
  38.     const votePost = bpr?.backstageAttachment?.pollRenderer?.choices
  39.     const multipleImagePost = bpr?.backstageAttachment?.postMultiImageRenderer?.images
  40.     const singleImagePost = bpr?.backstageAttachment?.backstageImageRenderer?.image?.thumbnails
  41.     const videoSharePost = bpr?.backstageAttachment?.videoRenderer?.videoId
  42.  
  43.  
  44.     if (votePost) {
  45.         let isVoteImage = false
  46.         images = votePost.map(v => {
  47.             const text = v.text.runs[0].text
  48.             let url = v.image?.thumbnails || null
  49.             if (url) {
  50.                 url = url.map(wolep => wolep.url).pop()
  51.                 isVoteImage = true
  52.             }
  53.             return { text, url }
  54.         })
  55.         isVoteImage ? postType = "voteImage" : postType = "voteText"
  56.     } else if (multipleImagePost) {
  57.         postType = "multipleImages"
  58.         const imagesArray = multipleImagePost.map(v => v.backstageImageRenderer.image.thumbnails)
  59.         images = imagesArray.map(v => {
  60.             const url = v.map(wolep => wolep.url).pop()
  61.             return { url, text: null }
  62.         })
  63.     } else if (singleImagePost) {
  64.         postType = "singleImage"
  65.         images = []
  66.         images.push({
  67.             url: singleImagePost.map(v => {
  68.                 return v.url
  69.             }).pop(),
  70.             text: null
  71.         })
  72.     } else if (videoSharePost) {
  73.         postType = "videoShare"
  74.         videoShareUrl = new URL(response.url).origin + videoSharePost
  75.     } else {
  76.         postType = "text"
  77.     }
  78.  
  79.     let tags = json?.microformat?.microformatDataRenderer?.tags || null
  80.     if (tags){
  81.         tags = tags.join(", ")
  82.     }
  83.     let result = {
  84.         author: bpr.authorText.runs[0].text,
  85.         authorUrl: new URL(response.url).origin + bpr.authorEndpoint.commandMetadata.webCommandMetadata.url,
  86.         publishTime: bpr.publishedTimeText.runs[0].text,
  87.         publishDate: json.microformat.microformatDataRenderer.publishDate,
  88.         tags,
  89.         text: bpr.contentText.runs.map(v => v.text).join(""),
  90.         like: bpr?.voteCount?.accessibility?.accessibilityData?.label || null,
  91.         images,
  92.         videoShareUrl,
  93.         postUrl: json.microformat.microformatDataRenderer.urlCanonical,
  94.         postType
  95.     }
  96.  
  97.     return result // tada :v
  98.  
  99. }
  100.  
  101. // cara pakai
  102. const url = "http://youtube.com/post/UgkxChSWx6pfUUCxxHeR3LsodyV6Aqwf4GOS?si=Ywu5j0qw08A50lue"
  103. ytpost(url)
  104.     .then(data => console.log(JSON.stringify(data, null, 2)))
  105.     .catch(console.log)
  106.  
  107. /* hasil
  108. {
  109.   "author": "the wolf",
  110.   "authorUrl": "https://www.youtube.com/@thewolf-ps1qz",
  111.   "publishTime": "10 jam yang lalu",
  112.   "publishDate": "2025-06-22T00:48:53.632504-07:00",
  113.   "tags": null,
  114.   "text": "voting teks",
  115.   "like": "1 suka",
  116.   "images": [
  117.     {
  118.       "text": "wolpi",
  119.       "url": "https://yt3.ggpht.com/F1TWx1qxb6MG2ybiB5z47-kgqrX59UYrScNFCoyc2I0bA5834kY66b5QrvjvFbxkRdT5U3kMHpzxjg=s1280-c-fcrop64=1,00000000ffffffff-nd-v1"
  120.     },
  121.     {
  122.       "text": "wolep",
  123.       "url": "https://yt3.ggpht.com/oVXiAkcgXxd9frkywNTWnruEHEjp-6Inznq1xzw8jlVaAXhxzXqSXjznDyYmNemdqMttwFe8iI7aeg=s1280-c-fcrop64=1,00000000ffffffff-nd-v1"
  124.     },
  125.     {
  126.       "text": "okta",
  127.       "url": "https://yt3.ggpht.com/BsQf5qn1StnkLLzv1KWw_IwV4X5RRYQYNj-D-lfP51_39leUWqeBWQS0gMDipV8jFFkOowRSdE47=s1280-c-fcrop64=1,00000000ffffffff-nd-v1"
  128.     }
  129.   ],
  130.   "videoShareUrl": null,
  131.   "postUrl": "https://www.youtube.com/post/UgkxChSWx6pfUUCxxHeR3LsodyV6Aqwf4GOS",
  132.   "postType": "voteImage"
  133. }
  134. */
Tags: youtube post
Add Comment
Please, Sign In to add comment