This repository was archived by the owner on Mar 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
198 lines (170 loc) · 7.48 KB
/
app.js
File metadata and controls
198 lines (170 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const cors = require('cors');
const express = require('express');
const request = require('request');
const ytdl = require('ytdl-core');
const contentDisposition = require('content-disposition');
var app = express();
var stolenApiKey = "AIzaSyCIM4EzNqi1in22f4Z3Ru3iYvLaY8tc3bo";
app.use(cors());
app.get('/audio/:videoID', (req, res) => {
info(req.params.videoID, (err, info) => {
if (err) res.sendStatus(404);
else
for (var formats of info['formats'])
if (formats['container'] == "m4a") res.redirect(formats['url']);
});
});
app.get('/video/:videoID', (req, res) => {
info(req.params.videoID, (err, info) => {
if (err) res.sendStatus(404);
else res.redirect(info['formats'][0]['url']);
});
});
app.get('/shortinfo/:videoID', (req, res) => {
info(req.params.videoID, (err, info) => {
if (err) res.sendStatus(404);
else {
var craftedInfo = {};
craftedInfo['title'] = info['player_response']['videoDetails']['title'];
craftedInfo['author'] = info['player_response']['videoDetails']['author'].replace(" - Topic", "");
request.head("https://i.ytimg.com/vi/" + info['player_response']['videoDetails']['videoId'] + "/maxresdefault.jpg", (err, resp) => {
if (err || resp.statusCode == 404) craftedInfo['thumburl'] = "https://i.ytimg.com/vi/" + info['player_response']['videoDetails']['videoId'] + "/hqdefault.jpg";
else craftedInfo['thumburl'] = "https://i.ytimg.com/vi/" + info['player_response']['videoDetails']['videoId'] + "/maxresdefault.jpg";
res.json(craftedInfo);
});
}
});
});
app.get('/info/:videoID', (req, res) => {
info(req.params.videoID, (err, info) => {
if (err) res.sendStatus(404);
else res.json(info['player_response']['videoDetails']);
});
});
app.get('/formats/:videoID', (req, res) => {
info(req.params.videoID, (err, info) => {
if (err) res.sendStatus(404);
else res.json(info['formats']);
});
});
app.get('/query/:query', (req, res) => {
search(req.params.query, (err, results) => {
if (err) res.sendStatus(404);
else res.json(results);
});
});
app.get('/playlist/:listID', (req, res) => {
parsePlaylist(req.params.listID, (err, results) => {
if (err) res.sendStatus(404);
else res.json(results);
});
});
app.get('/playlistFull/:listID', (req, res) => {
parsePlaylist(req.params.listID, (err, results) => {
if (err) res.sendStatus(404);
else {
var playlistFull = [];
for (var id of results) {
info(id, (err, info) => {
if (err) res.sendStatus(404);
else {
var craftedInfo = {};
craftedInfo['title'] = info['player_response']['videoDetails']['title'];
craftedInfo['author'] = info['player_response']['videoDetails']['author'].replace(" - Topic", "");
request.head("https://i.ytimg.com/vi/" + info['player_response']['videoDetails']['videoId'] + "/maxresdefault.jpg", (err, resp) => {
if (err || resp.statusCode == 404) craftedInfo['thumburl'] = "https://i.ytimg.com/vi/" + info['player_response']['videoDetails']['videoId'] + "/hqdefault.jpg";
else craftedInfo['thumburl'] = "https://i.ytimg.com/vi/" + info['player_response']['videoDetails']['videoId'] + "/maxresdefault.jpg";
playlistFull.push(craftedInfo);
});
}
});
}
res.json(playlistFull);
}
});
});
app.get('/downloadAudio/:videoID', (req, res) => {
info(req.params.videoID, (err, info) => {
if (err) res.sendStatus(404);
else {
for (var formats of info['formats'])
if (formats['container'] == "m4a") {
var filename;
if (info['player_response']['videoDetails']['title'].toLowerCase().includes(info['player_response']['videoDetails']['author'].replace(" - Topic", "").toLowerCase())) filename = info['player_response']['videoDetails']['title']
else filename = info['player_response']['videoDetails']['title'] + " - " + info['player_response']['videoDetails']['author'].replace(" - Topic", "");
res.header('Content-Disposition', 'attachment; filename="' + filename.replace(/[^\x00-\x7F]/g, "") + '.m4a"');
request.head(formats['url'], (error, response, body) => {
if (error) return;
if (response.headers['accept-ranges'] == "bytes") {
var len = response.headers['content-length'];
var options = {
url: formats['url'],
headers: {
'Range': 'bytes=0-' + len
}
};
request.get(options).pipe(res);
}
})
break;
}
}
});
});
app.listen(3000);
function info(videoID, callback) {
if (ytdl.validateID(videoID)) {
ytdl.getInfo(videoID, {}, (err, info) => {
if (err) callback("not valid url");
callback(undefined, info);
});
} else callback("not valid url");
}
function parsePlaylist(listid, callback) {
var queryLink = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId=" + listid + "&key=" + stolenApiKey;
var options = {
url: queryLink,
json: true
};
request(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
var videoList = [];
for (var video of body['items']) videoList.push(video['contentDetails']['videoId']);
callback(undefined, videoList);
} else callback("error");
});
}
function search(query, callback) {
var queryLink = "https://www.googleapis.com/youtube/v3/search?part=id,snippet&maxResults=15&q=" + encodeURIComponent(query + " topic") + "&key=" + stolenApiKey;
var options = {
url: queryLink,
json: true
};
request(options, (error, response, body) => {
if (!error && response.statusCode === 200) beautyIterator(body['items']).then(vids => callback(undefined, vids));
else callback("error");
});
}
async function beautyIterator(videoList) {
var vids = []
for (var item of videoList)
if (item['id']['kind'] == "youtube#video") {
var beauty = await beautifyResult(item);
vids.push(beauty);
}
return vids;
}
async function beautifyResult(video) {
var videoItem = {};
videoItem['id'] = video['id']['videoId'];
videoItem['author'] = video['snippet']['channelTitle'].replace(" - Topic", "");
videoItem['title'] = video['snippet']['title'];
var promise = new Promise((resolve, reject) => {
request.head("https://i.ytimg.com/vi/" + videoItem['id'] + "/maxresdefault.jpg", (err, res) => {
if (err || res.statusCode == 404) resolve("https://i.ytimg.com/vi/" + videoItem['id'] + "/hqdefault.jpg");
else resolve("https://i.ytimg.com/vi/" + videoItem['id'] + "/maxresdefault.jpg");
});
});
videoItem['thumburl'] = await promise;
return videoItem;
}