Advertisement
Xtreme_Killer

Search class

Jun 17th, 2021 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ytsr = require('youtube-sr').default
  2. const ytdl = require('ytdl-core-discord')
  3. const prism_media = require('prism-media')
  4.  
  5. const FFMPEG_OPUS_ARGUMENTS = [
  6.     '-analyzeduration',
  7.     '0',
  8.     '-loglevel',
  9.     '0',
  10.     '-acodec',
  11.     'libopus',
  12.     '-f',
  13.     'opus',
  14.     '-ar',
  15.     '48000',
  16.     '-ac',
  17.     '2',
  18. ];
  19.  
  20. class Search {
  21.     /**
  22.      *
  23.      * @param {string} search
  24.      * @returns Stream to be played in djs voice
  25.      */
  26.     async splay(search){
  27.         this.search = search
  28.         this.result = await ytsr.search(this.search, { limit: 1 }).then(x => x)
  29.         this.info = await ytdl.getInfo(this.result[0].url)
  30.         this.format_url = filter(this.info.formats)[0].url
  31.         return [ffmpeg_reconnect(this.format_url), this.result]
  32.     }
  33.     /**
  34.      *
  35.      * @param {string} url
  36.      * @returns
  37.      */
  38.     async uplay(url){
  39.         this.url = url
  40.         this.info = await ytdl.getInfo(this.url)
  41.         this.format_url = filter(this.info.formats)[0].url
  42.         return [ffmpeg_reconnect(this.format_url), {name : this.info.videoDetails.title, url : this.info.videoDetails.video_url, duration : time_convert(this.info.videoDetails.lengthSeconds)}]
  43.     }
  44.     /**
  45.      *
  46.      * @param {string} search
  47.      * @param {number} limit
  48.      * @returns List of songs
  49.      */
  50.     async search(search, limit){
  51.         this.limit = limit
  52.         this.search = search
  53.         return await ytsr.search(this.search, { limit: this.limit }).then(x => x)
  54.     }
  55.     /**
  56.      *
  57.      * @param {string} search
  58.      * @returns stream of 1st to be played
  59.      */
  60.     async playlist_play(search){
  61.         this.search = search
  62.         this.result = await ytsr.getPlaylist(this.search).then(x => x)
  63.         this.info = await ytdl.getInfo(this.result.videos[0].url)
  64.         this.format_url = filter(this.info.formats)[0].url
  65.         return [ffmpeg_reconnect(this.format_url), this.result]
  66.     }
  67.     /**
  68.      *
  69.      * @param {string} search
  70.      * @returns playlist videos
  71.      */
  72.     async playlist_list_full(search){
  73.         this.search = search
  74.         return await ytsr.getPlaylist(search).then(x => x.fetch()).then(x => x)
  75.     }
  76. }
  77. /**
  78.  *
  79.  * @param {string} search_term
  80.  * @returns search song and then play topmost
  81.  */
  82. exports.splay = async (search_term) => {
  83.     var cl = new Search
  84.     return await cl.splay(search_term)
  85. }
  86. /**
  87.  *
  88.  * @param {string} url
  89.  * @returns url playback
  90.  */
  91. exports.uplay = async (url) => {
  92.     var cl = new Search
  93.     return await cl.uplay(url)
  94. }
  95. /**
  96.  *
  97.  * @param {string} term
  98.  * @param {number} limit
  99.  * @returns list of all songs
  100.  */
  101. exports.search = async (term, limit) => {
  102.     var cl = new Search
  103.     return await cl.search(term, limit)
  104. }
  105. /**
  106.  *
  107.  * @param {string} url
  108.  * @returns [stream, list]
  109.  */
  110. exports.playlist_play = async (url) => {
  111.     var cl = new Search
  112.     return await cl.playlist_play(url)
  113. }
  114. /**
  115.  *
  116.  * @param {string} url
  117.  * @returns all songs in playlist
  118.  */
  119. exports.playlist_list_full = async (url) => {
  120.     var cl = new Search
  121.     return await cl.playlist_list_full(url)
  122. }
  123.  
  124. function filter(format) {
  125.     var results = []
  126.     format.forEach((ele) => {
  127.         if(ele.codecs === 'opus' && ele.container === 'webm' && ele.audioSampleRate === '48000' && ele.audioQuality === 'AUDIO_QUALITY_MEDIUM'){
  128.             results.push(ele)
  129.         }
  130.     })
  131.     return results
  132. }
  133.  
  134. function ffmpeg_reconnect(url){
  135.     return new prism_media.FFmpeg({
  136.         args : ['-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5','-i', url, ...FFMPEG_OPUS_ARGUMENTS ]
  137.     })
  138. }
  139.  
  140. function time_convert(seconds){
  141.     let mins = Math.floor(seconds / 60)
  142.     let sec = seconds - (mins * 60)
  143.     if(mins < 10) mins = `${mins}`
  144.     if(sec < 10) sec = `0${sec}`
  145.     return `${mins}:${sec}`
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement