WILDAN_IZZUDIN

[JS] YOUTUBE SCRAPER (DOWNLOAD)

Oct 25th, 2021 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let axios = require('axios')
  2.  
  3. function ytr(url) {
  4.     let regEx = /^.*(youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)([^#\&\?]*).*/
  5.     let id = url.match(regEx)
  6.     return id[2]
  7. }
  8.  
  9. function round(value, precision) {
  10.     var multiplier = Math.pow(10, precision || 0)
  11.     return Math.round(value * multiplier) / multiplier
  12. }
  13.  
  14. var _1MB = 1024 * 1024
  15. var _1GB = 1024 * _1MB
  16. var _1TB = 1024 * _1GB
  17.  
  18. function formatSize(size) {
  19.     if (size < 1024) {
  20.         return size + ' B'
  21.     } else if (size < _1MB) {
  22.         return round(size/1024, 1) + ' KB'
  23.     } else if (size < _1GB) {
  24.         return round(size/_1MB, 1) + ' MB'
  25.     } else if (size < _1TB) {
  26.         return round(size/_1GB, 1) + ' GB'
  27.     } else {
  28.         return round(size/_1TB, 1) + ' TB'
  29.     } return ''
  30. }
  31.  
  32. function extract(id) {
  33.     return new Promise(async(resolve, reject) => {
  34.         let json = await axios.get('https://api.btclod.com/v1/youtube/get-download-link/?file_id=' + id)
  35.         resolve(json.data)
  36.     })
  37. }
  38.  
  39. function analyze(url) {
  40.     return new Promise(async(resolve, reject) => {
  41.         let json = await axios.get('https://api.btclod.com/v1/youtube/extract-infos/?detail=' + ytr(url) + '&video=1')
  42.         if (json.data.code != 200) return resolve({ creator: '@neoxrs – Wildan Izzudin', status: false })
  43.         let video = [], audio = []
  44.         for (let i=0; i<json.data.data.videos.length; i++) {
  45.             video.push({
  46.                 id: json.data.data.videos[i].id,
  47.                 type: json.data.data.videos[i].extension,
  48.                 quality: json.data.data.videos[i].format_note,
  49.                 bytes: json.data.data.videos[i].file_size,
  50.                 size: json.data.data.videos[i].file_size != '~' ? formatSize(json.data.data.videos[i].file_size) : '~'
  51.             })
  52.         }
  53.         for (let i=0; i<json.data.data.audios.length; i++) {
  54.             audio.push({
  55.                 id: json.data.data.audios[i].id,
  56.                 type: json.data.data.audios[i].extension,
  57.                 quality: json.data.data.audios[i].format_note,
  58.                 bytes: json.data.data.audios[i].file_size,
  59.                 size: json.data.data.audios[i].file_size != '~' ? formatSize(json.data.data.audios[i].file_size) : '~'
  60.             })
  61.         } resolve({ creator: '@neoxrs – Wildan Izzudin', status: true, data: [ ...video, ...audio ] })
  62.     })
  63. }
  64.  
  65. // USAGE FUNCTION
  66. // analyze('https://youtu.be/j5-yKhDd64s').then(res => console.log(res))
  67. // extract('ajUteUtoRGQ2NHM6dmlkZW86Mzk4KzE0MDo3MjBwOm1wNA==').then(res => console.log(res))
Add Comment
Please, Sign In to add comment