Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // source https://4kdownload.to/enXd/
- const yt = {
- MAX_TRIES: 24, //times
- CHECK_PROGRESS_DELAY: 5000, // ms
- fetchJson: async function (url, requestInit) {
- try {
- const res = await fetch(url, requestInit)
- if (!res.ok) throw Error(`${res.status}`)
- const data = await res.json()
- return { ok: true, data }
- } catch (e) {
- return { ok: false, data: e.message }
- }
- },
- headers: {
- "accept-encoding": "gzip, deflate, br, zstd",
- // "Referer": "https://4kdownload.to/"
- },
- downloadMp3: async function (youtubeUrl) {
- // init
- const i = await this.init(youtubeUrl)
- if (!i.ok) throw Error(`init function fail ${i.data}`)
- // progress check
- const pc = await this.processCheck(i)
- if (!pc.ok) throw Error(`init function fail ${i.data}`)
- return pc.data
- },
- init: async function (youtubeUrl) {
- console.log('init')
- const url = new URL('https://p.savenow.to/ajax/download.php')
- url.search = new URLSearchParams({
- 'copyright': '0',
- 'format': 'mp3', // modifi this value if u want other format, read web api docs
- 'url': youtubeUrl
- })
- return await this.fetchJson(url, { headers: this.headers })
- },
- processCheck: async function (initObj) {
- console.log('progress check')
- if (!initObj.data.success) throw Error(`return value from init is not success`)
- const url = initObj.data.progress_url
- let res = await this.fetchJson(url, { headers: this.headers })
- if (!res.ok) throw Error(`${res.status}`)
- if (res.data.success) return res
- for (let i = 0; i <= this.MAX_TRIES; i++) {
- console.log(`try ${i + 1}/${this.MAX_TRIES}`)
- res = await this.fetchJson(url, { headers: this.headers })
- if (!res.ok) throw Error(`${res.status}`)
- if (res.data.success) return res
- console.log(`${parseInt(res.data.progress) / 10}% (${res.data.text || '(unknown)'})`)
- await new Promise(r => setTimeout(r, this.CHECK_PROGRESS_DELAY))
- }
- }
- }
- await yt.downloadMp3("https://www.youtube.com/watch?v=7iNbnineUCI&list=RDBy7ctqcWxyM&index=22")
- .then(console.log)
- .catch(console.log)
- /* output
- {
- success: 1,
- progress: 1000,
- download_url: 'https://leo3.savenow.to/pacific/?F5OkmTGnaDtxS6yq0KzmF9z',
- text: 'Finished',
- message: 'If you want your application to use our API contact us: [email protected] or visit https://video-download-api.com/',
- alternative_download_urls: [
- {
- type: 'nip.io',
- url: 'https://81-17-16-210.nip.io/pacific/?F5OkmTGnaDtxS6yq0KzmF9z',
- has_ssl: true
- },
- {
- type: 'sslip.io',
- url: 'https://81-17-16-210.sslip.io/pacific/?F5OkmTGnaDtxS6yq0KzmF9z',
- has_ssl: true
- },
- {
- type: 'traefik.me',
- url: 'http://81-17-16-210.traefik.me/pacific/?F5OkmTGnaDtxS6yq0KzmF9z',
- has_ssl: false
- }
- ]
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment