-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (144 loc) · 4.23 KB
/
index.js
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
const ytdl = require("ytdl-core");
const ffmpeg = require('ffmpeg-static');
const cp = require('child_process');
const http = require("http")
const https = require("https")
let urlsObject = []
function randomUrl() {
let index = random(0,urlsObject.length - 1)
return urlsObject[index]
}
function random(min,max) {
let randomParem = Math.random()
let delta = max - min
return Math.round(randomParem * delta) + min
}
function randomVideo(res) {
let url = randomUrl()
console.log(typeof url,url,urlsObject);
if(url == undefined) {res.end("can't get video url"); return}
let CharacterEntities = {
" ":" ",
"<":"%lt;",
">":">",
"&":"&",
"\"":""",
"\'":"'",
"¢":"¢",
"£":"£",
"¥":"¥",
"€":"€",
"©":"©",
"®":"®",
}
console.clear();
Object.entries(CharacterEntities).forEach(entry => {
let [character,entitie] = entry
let regexp = new RegExp(`${entitie}`,"ig")
url = url.replace(regexp,character)
})
handelResponse(url,res)
}
let server = http.createServer((req,res) => {
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
let urls = []
functions = {
GET,POST,PATCH,DELETE
}
let url = new URL(`https://www.example.com${req.url}`)
let method = url.searchParams.get("method")
if(method == "GET" || method == null) {
functions.GET(res)
return
}
req.on("data",data => {
jsonObject = JSON.parse(data);
jsonObject.forEach((url,index) => {
if(url != null) {
urls.push(url)
}
if(index == jsonObject.length) {
let fun = functions[method]
if(fun) fun()
res.end()
}
})
})
function POST() {
urls.forEach(url => {
let exist = urlsObject.indexOf(url) != -1
if(!exist) {
urlsObject.push(url)
}
})
console.log("POST",urls,urlsObject)
}
function PATCH() {
urlsObject = urls
console.log("PATCH",urls,urlsObject)
}
function DELETE() {
urls.forEach(url => {
let index = urlsObject.indexOf(url)
if(index != -1) {
urlsObject.splice(index,1)
}
})
console.log("DELETE",urls,urlsObject)
}
function GET() {
randomVideo(res)
}
})
server.listen(process.env.PORT ?? 2600,undefined, _ => {
console.log(`it runed ${process.env.PORT ?? 2600}`);
})
let responseHandelers = {
"youtube.com": {
regex: /youtube\.com\/watch/,
func: youtubeVideo
}
}
function handelResponse(url,res) {
let protocol = http;
if((/^https:/ig).test(url)) protocol = https;
let handeler
Object.values(responseHandelers).every(handel => {
let match = handel.regex.test(url)
if(match) handeler = handel
return !match
})
if(handeler) {
handeler.func({url,res})
return
}
protocol.get(url,respond => {
respond.pipe(res)
})
}
async function youtubeVideo({url,res} = {}) {
let audioStream = ytdl(url,{quality:'highestaudio',filter:"audioonly"});
let videoStream = ytdl(url,{quality:'highestvideo'});
const ffmpegProcess = cp.spawn(ffmpeg, [
'-i', `pipe:3`,
'-i', `pipe:4`,
'-map','0:v',
'-map','1:a',
'-c:v', 'copy',
'-c:a', 'libmp3lame',
'-crf','27',
'-preset','veryfast',
'-movflags','frag_keyframe+empty_moov',
'-f','mp4',
'-loglevel','error',
'-'
], {
stdio: [
'pipe', 'pipe', 'pipe', 'pipe', 'pipe'
],
});
videoStream.pipe(ffmpegProcess.stdio[3]);
audioStream.pipe(ffmpegProcess.stdio[4]);
ffmpegProcess.stdio[1].pipe(res);
}