Rianofc commited on
Commit
5c48d9a
·
verified ·
1 Parent(s): 6d2ec30

Update ytcdn.js

Browse files
Files changed (1) hide show
  1. ytcdn.js +193 -81
ytcdn.js CHANGED
@@ -1,93 +1,205 @@
1
- const axios = require('axios');
2
-
3
- async function ytmp3(url) {
4
- const headers = {
5
- "accept": "*/*",
6
- "accept-language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
7
- "sec-ch-ua": '"Not A(Brand";v="8", "Chromium";v="132"',
8
- "sec-ch-ua-mobile": "?1",
9
- "sec-ch-ua-platform": '"Android"',
10
- "sec-fetch-dest": "empty",
11
- "sec-fetch-mode": "cors",
12
- "sec-fetch-site": "cross-site",
13
- "Referer": "https://id.ytmp3.mobi/",
14
- "Referrer-Policy": "strict-origin-when-cross-origin"
15
- };
16
-
17
  try {
18
- const initial = await axios.get(`https://d.ymcdn.org/api/v1/init?p=y&23=1llum1n471&_=${Math.random()}`, { headers });
19
- const init = initial.data;
20
-
21
- const id = url.match(/(?:youtu\.be\/|youtube\.com\/(?:.*v=|.*\/|.*embed\/))([^&?/]+)/)?.[1];
22
- if (!id) throw new Error("Invalid YouTube URL");
23
-
24
- const mp3Url = init.convertURL + `&v=${id}&f=mp3&_=${Math.random()}`;
25
- const mp3Response = await axios.get(mp3Url, { headers });
26
-
27
- let info = {};
28
- let attempts = 0;
29
- while (attempts < 3) {
30
- const progressResponse = await axios.get(mp3Response.data.progressURL, { headers });
31
- info = progressResponse.data;
32
- if (info.progress === 3) break;
33
- attempts++;
34
  }
35
-
36
- if (info.progress !== 3) throw new Error("MP3 conversion did not complete");
37
-
38
- return {
39
- title: info.title,
40
- url: mp3Response.data.downloadURL
41
- };
 
 
 
 
 
 
 
 
 
 
42
  } catch (error) {
43
- console.error("Error in ytmp3:", error);
44
- throw error;
45
- }
 
46
  }
47
 
48
- async function ytmp4(url) {
49
- const headers = {
50
- "accept": "*/*",
51
- "accept-language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
52
- "sec-ch-ua": '"Not A(Brand";v="8", "Chromium";v="132"',
53
- "sec-ch-ua-mobile": "?1",
54
- "sec-ch-ua-platform": '"Android"',
55
- "sec-fetch-dest": "empty",
56
- "sec-fetch-mode": "cors",
57
- "sec-fetch-site": "cross-site",
58
- "Referer": "https://id.ytmp3.mobi/",
59
- "Referrer-Policy": "strict-origin-when-cross-origin"
60
- };
61
-
62
  try {
63
- const initial = await axios.get(`https://d.ymcdn.org/api/v1/init?p=y&23=1llum1n471&_=${Math.random()}`, { headers });
64
- const init = initial.data;
65
-
66
- const id = url.match(/(?:youtu\.be\/|youtube\.com\/(?:.*v=|.*\/|.*embed\/))([^&?/]+)/)?.[1];
67
- if (!id) throw new Error("Invalid YouTube URL");
68
-
69
- const mp4Url = init.convertURL + `&v=${id}&f=mp4&_=${Math.random()}`;
70
- const mp4Response = await axios.get(mp4Url, { headers });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- let info = {};
73
- let attempts = 0;
74
- while (attempts < 3) {
75
- const progressResponse = await axios.get(mp4Response.data.progressURL, { headers });
76
- info = progressResponse.data;
77
- if (info.progress === 3) break;
78
- attempts++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  }
 
 
 
80
 
81
- if (info.progress !== 3) throw new Error("MP4 conversion did not complete");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
- return {
84
- title: info.title,
85
- url: mp4Response.data.downloadURL
86
- };
87
- } catch (error) {
88
- console.error("Error in ytmp4:", error);
89
- throw error;
90
- }
 
 
 
 
 
 
91
  }
92
 
93
- module.exports = { ytmp3, ytmp4 }
 
 
 
 
 
 
 
1
+ const yt = require("@distube/ytdl-core")
2
+ const yts = require("yt-search")
3
+ const fs = require('fs');
4
+ const agent = yt.createAgent(JSON.parse(fs.readFileSync("./cookies/cookies-yt.json")));
5
+ async function ytDonlodMp3(url) {
6
+ return new Promise((resolve, reject) => {
 
 
 
 
 
 
 
 
 
 
7
  try {
8
+ const id = yt.getVideoID(url)
9
+ const yutub = yt.getInfo(`https://www.youtube.com/watch?v=${id}`, { agent})
10
+ .then((data) => {
11
+ let pormat = data.formats
12
+ let audio = []
13
+ for (let i = 0; i < pormat.length; i++) {
14
+ if (pormat[i].mimeType == 'audio/webm; codecs=\"opus\"') {
15
+ let aud = pormat[i]
16
+ audio.push(aud.url)
17
+ }
 
 
 
 
 
 
18
  }
19
+ const title = data.player_response.microformat.playerMicroformatRenderer.title.simpleText
20
+ const thumb = data.player_response.microformat.playerMicroformatRenderer.thumbnail.thumbnails[0].url
21
+ const channel = data.player_response.microformat.playerMicroformatRenderer.ownerChannelName
22
+ const views = data.player_response.microformat.playerMicroformatRenderer.viewCount
23
+ const published = data.player_response.microformat.playerMicroformatRenderer.publishDate
24
+
25
+ const result = {
26
+ title: title,
27
+ thumb: thumb,
28
+ channel: channel,
29
+ published: published,
30
+ views: views,
31
+ url: audio[1]
32
+ }
33
+ return(result)
34
+ })
35
+ resolve(yutub)
36
  } catch (error) {
37
+ reject(error);
38
+ }
39
+ console.log(error)
40
+ })
41
  }
42
 
43
+ async function ytDonlodMp4(url) {
44
+ return new Promise((resolve, reject) => {
 
 
 
 
 
 
 
 
 
 
 
 
45
  try {
46
+ const id = yt.getVideoID(url)
47
+ const yutub = yt.getInfo(`https://www.youtube.com/watch?v=${id}`, { agent })
48
+ .then((data) => {
49
+ let pormat = data.formats
50
+ let video = []
51
+ for (let i = 0; i < pormat.length; i++) {
52
+ if (pormat[i].container == 'mp4' && pormat[i].hasVideo == true && pormat[i].hasAudio == true) {
53
+ let vid = pormat[i]
54
+ video.push(vid.url)
55
+ }
56
+ }
57
+ const title = data.player_response.microformat.playerMicroformatRenderer.title.simpleText
58
+ const thumb = data.player_response.microformat.playerMicroformatRenderer.thumbnail.thumbnails[0].url
59
+ const channel = data.player_response.microformat.playerMicroformatRenderer.ownerChannelName
60
+ const views = data.player_response.microformat.playerMicroformatRenderer.viewCount
61
+ const published = data.player_response.microformat.playerMicroformatRenderer.publishDate
62
+
63
+ const result = {
64
+ title: title,
65
+ thumb: thumb,
66
+ channel: channel,
67
+ published: published,
68
+ views: views,
69
+ url: video[0]
70
+ }
71
+ return(result)
72
+ })
73
+ resolve(yutub)
74
+ } catch (error) {
75
+ reject(error);
76
+ }
77
+ console.log(error)
78
+ })
79
+ }
80
 
81
+ async function ytPlayMp3(query) {
82
+ return new Promise((resolve, reject) => {
83
+ try {
84
+ const search = yts(query)
85
+ .then((data) => {
86
+ const url = []
87
+ const pormat = data.all
88
+ for (let i = 0; i < pormat.length; i++) {
89
+ if (pormat[i].type == 'video') {
90
+ let dapet = pormat[i]
91
+ url.push(dapet.url)
92
+ }
93
+ }
94
+ const id = yt.getVideoID(url[0])
95
+ const yutub = yt.getInfo(`https://www.youtube.com/watch?v=${id}`, { agent })
96
+ .then((data) => {
97
+ let pormat = data.formats
98
+ let audio = []
99
+ let video = []
100
+ for (let i = 0; i < pormat.length; i++) {
101
+ if (pormat[i].mimeType == 'audio/webm; codecs=\"opus\"') {
102
+ let aud = pormat[i]
103
+ audio.push(aud.url)
104
+ }
105
+ }
106
+ const title = data.player_response.microformat.playerMicroformatRenderer.title.simpleText
107
+ const thumb = data.player_response.microformat.playerMicroformatRenderer.thumbnail.thumbnails[0].url
108
+ const channel = data.player_response.microformat.playerMicroformatRenderer.ownerChannelName
109
+ const views = data.player_response.microformat.playerMicroformatRenderer.viewCount
110
+ const published = data.player_response.microformat.playerMicroformatRenderer.publishDate
111
+ const result = {
112
+ status: true,
113
+ code: 200,
114
+ creator: 'Reyganz',
115
+ title: title,
116
+ thumb: thumb,
117
+ channel: channel,
118
+ published: published,
119
+ views: views,
120
+ url: audio[0]
121
+ }
122
+ return(result)
123
+ })
124
+ return(yutub)
125
+ })
126
+ resolve(search)
127
+ } catch (error) {
128
+ reject(error)
129
  }
130
+ console.log(error)
131
+ })
132
+ }
133
 
134
+ async function ytPlayMp4(query) {
135
+ return new Promise((resolve, reject) => {
136
+ try {
137
+ const search = yts(query)
138
+ .then((data) => {
139
+ const url = []
140
+ const pormat = data.all
141
+ for (let i = 0; i < pormat.length; i++) {
142
+ if (pormat[i].type == 'video') {
143
+ let dapet = pormat[i]
144
+ url.push(dapet.url)
145
+ }
146
+ }
147
+ const id = yt.getVideoID(url[0])
148
+ const yutub = yt.getInfo(`https://www.youtube.com/watch?v=${id}`, { agent })
149
+ .then((data) => {
150
+ let pormat = data.formats
151
+ let video = []
152
+ for (let i = 0; i < pormat.length; i++) {
153
+ if (pormat[i].container == 'mp4' && pormat[i].hasVideo == true && pormat[i].hasAudio == true) {
154
+ let vid = pormat[i]
155
+ video.push(vid.url)
156
+ }
157
+ }
158
+ const title = data.player_response.microformat.playerMicroformatRenderer.title.simpleText
159
+ const thumb = data.player_response.microformat.playerMicroformatRenderer.thumbnail.thumbnails[0].url
160
+ const channel = data.player_response.microformat.playerMicroformatRenderer.ownerChannelName
161
+ const views = data.player_response.microformat.playerMicroformatRenderer.viewCount
162
+ const published = data.player_response.microformat.playerMicroformatRenderer.publishDate
163
+ const result = {
164
+ title: title,
165
+ thumb: thumb,
166
+ channel: channel,
167
+ published: published,
168
+ views: views,
169
+ url: video[0]
170
+ }
171
+ return(result)
172
+ })
173
+ return(yutub)
174
+ })
175
+ resolve(search)
176
+ } catch (error) {
177
+ reject(error)
178
+ }
179
+ console.log(error)
180
+ })
181
+ }
182
 
183
+ async function ytSearch(query) {
184
+ return new Promise((resolve, reject) => {
185
+ try {
186
+ const cari = yts(query)
187
+ .then((data) => {
188
+ res = data.all
189
+ return res
190
+ })
191
+ resolve(cari)
192
+ } catch (error) {
193
+ reject(error)
194
+ }
195
+ console.log(error)
196
+ })
197
  }
198
 
199
+ module.exports = {
200
+ ytDonlodMp3,
201
+ ytDonlodMp4,
202
+ ytPlayMp3,
203
+ ytPlayMp4,
204
+ ytSearch
205
+ };