Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -1,340 +1,185 @@
|
|
1 |
-
const
|
2 |
-
const
|
3 |
-
const express = require("express");
|
4 |
-
const path = require("path");
|
5 |
-
const os = require("os");
|
6 |
-
const fs = require("fs");
|
7 |
-
const PORT = process.env.PORT || 7860;
|
8 |
const app = express();
|
9 |
-
|
10 |
-
const
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
}
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
data: {
|
26 |
-
igdl: "/igdl"
|
27 |
-
},
|
28 |
-
};
|
29 |
-
res.send(keluaran);
|
30 |
-
});
|
31 |
-
|
32 |
-
async function downloadImage(url) {
|
33 |
-
try {
|
34 |
-
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
35 |
-
const randomCode = Math.random().toString(36).substring(7);
|
36 |
-
const imagePath = `downloaded_image_${randomCode}.png`;
|
37 |
-
fs.writeFileSync(path.join(tempDir, "/" + imagePath), Buffer.from(response.data, 'binary'));
|
38 |
-
return imagePath;
|
39 |
-
} catch (error) {
|
40 |
-
console.error('Error downloading image:', error.message);
|
41 |
-
throw error;
|
42 |
-
}
|
43 |
-
}
|
44 |
-
|
45 |
-
async function downloadVideo(url) {
|
46 |
-
try {
|
47 |
-
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
48 |
-
const randomCode = Math.random().toString(36).substring(7);
|
49 |
-
const videoPath = `downloaded_video_${randomCode}.mp4`;
|
50 |
-
fs.writeFileSync(path.join(tempDir, "/" + videoPath), Buffer.from(response.data, 'binary'));
|
51 |
-
return videoPath;
|
52 |
-
} catch (error) {
|
53 |
-
console.error('Error downloading video:', error.message);
|
54 |
-
throw error;
|
55 |
-
}
|
56 |
-
}
|
57 |
|
58 |
const generateRandomIP = () => {
|
59 |
const octet = () => Math.floor(Math.random() * 256);
|
60 |
return `${octet()}.${octet()}.${octet()}.${octet()}`;
|
61 |
};
|
62 |
|
63 |
-
|
64 |
try {
|
65 |
-
const
|
66 |
-
|
67 |
-
'Accept': '*/*',
|
68 |
-
'User-Agent': 'Mozilla/5.0 (Linux; Android 12; SM-S908B Build/SP1A.210812.016; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/99.0.4844.58 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/357.0.0.23.115;]',
|
69 |
-
'Referer': 'https://igdownloader.app/en',
|
70 |
-
'X-Forwarded-For': generateRandomIP()
|
71 |
-
};
|
72 |
|
73 |
-
const response = await axios.
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
});
|
76 |
-
|
77 |
-
|
78 |
-
return mediaType;
|
79 |
} catch (error) {
|
80 |
-
console.error(
|
81 |
-
return
|
82 |
}
|
83 |
-
}
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
}
|
96 |
-
}
|
97 |
-
|
98 |
-
|
99 |
-
/*
|
100 |
-
IGDL IGDL IGDL IGDL IGDL IGDL
|
101 |
-
IGDL IGDL IGDL IGDL IGDL IGDL
|
102 |
-
*/
|
103 |
|
104 |
-
async function
|
105 |
try {
|
106 |
-
const
|
107 |
-
|
|
|
|
|
|
|
|
|
108 |
headers: {
|
109 |
-
'Content-Type': 'application/
|
110 |
-
'
|
111 |
-
'User-Agent': 'Mozilla/5.0 (Linux; Android 12; SM-S908B Build/SP1A.210812.016; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/99.0.4844.58 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/357.0.0.23.115;]',
|
112 |
-
'Referer': 'https://igdownloader.app/en',
|
113 |
'X-Forwarded-For': generateRandomIP()
|
114 |
-
},
|
115 |
-
};
|
116 |
-
const postData = `recaptchaToken=&q=${encodeURIComponent(url)}&t=media&lang=en`;
|
117 |
-
const response = await axios.post(apiEndpoint, postData, requestOptions);
|
118 |
-
const $ = cheerio.load(response.data.data);
|
119 |
-
const downloadLinks = $('div.download-items__btn > a');
|
120 |
-
const hrefArray = {
|
121 |
-
title: null,
|
122 |
-
urls: []
|
123 |
-
}
|
124 |
-
await Promise.all(downloadLinks.map(async (index, element) => {
|
125 |
-
const href = $(element).attr('href');
|
126 |
-
const media_type = await getMimeTypeFromUrl(href);
|
127 |
-
let type;
|
128 |
-
if (typeof media_type === 'undefined') {
|
129 |
-
type = "video";
|
130 |
-
} else {
|
131 |
-
type = media_type;
|
132 |
}
|
133 |
-
|
134 |
-
|
135 |
-
type: type
|
136 |
-
});
|
137 |
-
}));
|
138 |
-
return hrefArray;
|
139 |
} catch (error) {
|
140 |
-
console.error(
|
141 |
-
return null;
|
142 |
}
|
143 |
}
|
144 |
|
145 |
-
async function
|
146 |
-
const
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
_ts: Date.now() - 106169240745,
|
152 |
-
_tsc: 0,
|
153 |
-
_s: 'b4d95f81de50ed9cace0103923a25dd2f57b2a76c142d82ac78a963f1274a1e1',
|
154 |
-
};
|
155 |
-
|
156 |
-
const headers = {
|
157 |
-
'Accept': 'application/json, text/plain, */*',
|
158 |
-
'Content-Type': 'application/json',
|
159 |
-
'X-XSRF-TOKEN': 'eyJpdiI6InZ3aU9SVG41enJWOUljS3hIUnpsd3c9PSIsInZhbHVlIjoiakFHQjcrVjNMQm1wZ2xrcmF6NGdOSjdTUFp0ZTNFNXpCZ0tcL3VaNmFaN09TSVl2QzFZVndTVmxLUFo2QVVuYnpcL2JDdVwvc29sdHB6XC9jSzY2aURYMDdzcmd4TWVHUjZzWHpHZXEySXJMb0UwN3dqbUFDRlZFTXFxU2E4U2hOUzg5IiwibWFjIjoiNGQyYjBjYWNhNGQwMWUwZWE4YWM1MzdkMWJlYmRkYzBkOTMyZDZjNWVhNjE3MzAxOWMwNTM2OTJiOTM0ZjMwNyJ9',
|
160 |
-
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36',
|
161 |
-
'Referer': 'https://snapinst.com/',
|
162 |
};
|
163 |
|
164 |
try {
|
165 |
-
const response = await axios.post(
|
166 |
-
headers
|
|
|
|
|
|
|
|
|
167 |
});
|
168 |
-
const resultArray = [];
|
169 |
-
|
170 |
-
const transformedData = {
|
171 |
-
title: response.data[0].meta.title,
|
172 |
-
urls: response.data.map(item => item.url.map(urlInfo => ({
|
173 |
-
url: urlInfo.url,
|
174 |
-
type: checkMediaType(urlInfo.type)
|
175 |
-
})))
|
176 |
-
};
|
177 |
|
178 |
-
return
|
179 |
} catch (error) {
|
180 |
-
console.error(
|
181 |
-
return null;
|
182 |
}
|
183 |
}
|
184 |
|
185 |
|
186 |
-
async function
|
187 |
-
|
188 |
|
189 |
-
|
190 |
-
|
191 |
-
aFormat: 'mp3',
|
192 |
-
filenamePattern: 'classic',
|
193 |
-
dubLang: false,
|
194 |
-
vQuality: '720',
|
195 |
-
};
|
196 |
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
};
|
203 |
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
});
|
208 |
-
var result = response.data;
|
209 |
-
let array_res = {
|
210 |
-
title: null,
|
211 |
-
urls: []
|
212 |
-
}
|
213 |
-
if (result.status === "redirect") {
|
214 |
-
let media_type = await getMimeTypeFromUrl(result.url)
|
215 |
-
array_res.urls.push({
|
216 |
-
url: result.url,
|
217 |
-
type: media_type
|
218 |
-
})
|
219 |
-
} else if (result.status === "picker") {
|
220 |
-
for (let i = 0; i < result.picker.length; i++) {
|
221 |
-
let media_type = await getMimeTypeFromUrl(result.picker[i].url)
|
222 |
-
array_res.urls.push({
|
223 |
-
url: result.picker[i].url,
|
224 |
-
type: media_type
|
225 |
-
})
|
226 |
-
}
|
227 |
-
}
|
228 |
-
return array_res;
|
229 |
-
} catch (error) {
|
230 |
-
console.error('Instagram Downloader 3 - Error:', error.message);
|
231 |
-
return null;
|
232 |
-
}
|
233 |
-
};
|
234 |
|
235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
try {
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
245 |
};
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
|
|
|
|
|
|
|
|
|
|
253 |
}
|
254 |
-
await Promise.all(downloadLinks.map(async (index, element) => {
|
255 |
-
const href = $(element).attr('href');
|
256 |
-
const media_type = await getMimeTypeFromUrl(href);
|
257 |
-
let type;
|
258 |
-
if (typeof media_type === 'undefined') {
|
259 |
-
type = "video";
|
260 |
-
} else {
|
261 |
-
type = media_type;
|
262 |
-
}
|
263 |
-
hrefArray.urls.push({
|
264 |
-
url: href,
|
265 |
-
type: type
|
266 |
-
});
|
267 |
-
}));
|
268 |
-
return hrefArray;
|
269 |
-
} catch (error) {
|
270 |
-
console.error('Instagram Downloader 4 - Error:', error.message);
|
271 |
-
return null;
|
272 |
}
|
273 |
-
}
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
result = await igdl3(url);
|
282 |
-
}
|
283 |
-
if (!result) {
|
284 |
-
result = await igdl4(url);
|
285 |
-
}
|
286 |
-
if (!result) {
|
287 |
-
result = {
|
288 |
-
message: "all server error"
|
289 |
-
};
|
290 |
-
}
|
291 |
-
return result;
|
292 |
-
};
|
293 |
|
294 |
-
app.get('/
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
return res.status(400).json({ error: 'Parameter url is required' });
|
299 |
-
}
|
300 |
-
if (!/https?:\/\/(www\.)?instagram\.com\/(p|reel|tv)/.test(url)) {
|
301 |
-
return res.status(400).json({ error: "Example: https://www.instagram.com/p/Cz1fTwMJFpx/?igsh=MXRrY2g4eWNucGoyZg==" });
|
302 |
-
}
|
303 |
-
let result = await getInstagramDownloadLinks(url);
|
304 |
-
let result_upload = {
|
305 |
-
title: result?.title || 'untitled',
|
306 |
-
media: []
|
307 |
-
}
|
308 |
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
let unduh = await downloadVideo(item.url);
|
315 |
-
result_upload.media.push({ type: item.type, path: unduh, url_path: `http://${process.env.SPACE_HOST}/temp/${path.basename(unduh)}` });
|
316 |
-
}
|
317 |
-
}
|
318 |
-
|
319 |
-
res.json(result_upload);
|
320 |
-
|
321 |
-
for (let item of result_upload.media) {
|
322 |
-
try {
|
323 |
-
await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000)); // 10 minutes
|
324 |
-
await fs.unlink(item.path);
|
325 |
-
console.log(`File ${item.path} deleted.`);
|
326 |
-
} catch (error) {
|
327 |
-
console.error(`Error deleting file ${item.path}:`, error);
|
328 |
-
}
|
329 |
-
}
|
330 |
-
} catch (error) {
|
331 |
-
console.error('Error processing request:', error);
|
332 |
-
res.status(500).json({ error: 'Failed to process request\n' + error});
|
333 |
}
|
|
|
|
|
|
|
334 |
});
|
335 |
|
336 |
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
});
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const bodyParser = require('body-parser');
|
|
|
|
|
|
|
|
|
|
|
3 |
const app = express();
|
4 |
+
const axios = require('axios');
|
5 |
+
const { DateTime } = require('luxon');
|
6 |
+
const { format } = require("util")
|
7 |
+
const port = 7860;
|
8 |
+
app.use(bodyParser.json());
|
9 |
+
|
10 |
+
const chatHistory = {};
|
11 |
+
|
12 |
+
const generateRandomString = (length) => {
|
13 |
+
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
14 |
+
let result = '';
|
15 |
+
for (let i = 0; i < length; i++) {
|
16 |
+
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
17 |
+
}
|
18 |
+
return result;
|
19 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
const generateRandomIP = () => {
|
22 |
const octet = () => Math.floor(Math.random() * 256);
|
23 |
return `${octet()}.${octet()}.${octet()}.${octet()}`;
|
24 |
};
|
25 |
|
26 |
+
async function createNewChat() {
|
27 |
try {
|
28 |
+
const randomUserId = generateRandomString(17);
|
29 |
+
const randomIP = generateRandomIP();
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
const response = await axios.post('https://chat.chatgptdemo.net/new_chat', {
|
32 |
+
user_id: randomUserId
|
33 |
+
}, {
|
34 |
+
headers: {
|
35 |
+
'Content-Type': 'application/json',
|
36 |
+
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
|
37 |
+
'X-Forwarded-For': randomIP
|
38 |
+
}
|
39 |
});
|
40 |
+
|
41 |
+
return response.data.id_;
|
|
|
42 |
} catch (error) {
|
43 |
+
console.error(error);
|
44 |
+
return false;
|
45 |
}
|
46 |
+
}
|
47 |
|
48 |
+
async function updateChatName(id, chatName) {
|
49 |
+
try {
|
50 |
+
const response = await axios.post('https://chat.chatgptdemo.net/update_chat_name', {
|
51 |
+
chat_id: id,
|
52 |
+
chat_name: chatName
|
53 |
+
}, {
|
54 |
+
headers: {
|
55 |
+
'Content-Type': 'application/json',
|
56 |
+
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
|
57 |
+
'X-Forwarded-For': generateRandomIP()
|
58 |
+
}
|
59 |
+
});
|
60 |
+
} catch (error) {
|
61 |
+
console.error(error);
|
62 |
}
|
63 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
async function sendMessage(question, id, retry) {
|
66 |
try {
|
67 |
+
const response = await axios.post('https://chat.chatgptdemo.net/chat_api_stream', {
|
68 |
+
question: question,
|
69 |
+
chat_id: id,
|
70 |
+
timestamp: Date.now(),
|
71 |
+
retry: retry
|
72 |
+
}, {
|
73 |
headers: {
|
74 |
+
'Content-Type': 'application/json',
|
75 |
+
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
|
|
|
|
|
76 |
'X-Forwarded-For': generateRandomIP()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
}
|
78 |
+
});
|
79 |
+
return response.data;
|
|
|
|
|
|
|
|
|
80 |
} catch (error) {
|
81 |
+
console.error(error);
|
|
|
82 |
}
|
83 |
}
|
84 |
|
85 |
+
async function updateMessages(id, message) {
|
86 |
+
const url = 'https://chat.chatgptdemo.net/update_messages';
|
87 |
+
const data = {
|
88 |
+
chat_id: id,
|
89 |
+
bot_response: message,
|
90 |
+
timestamp: Date.now(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
};
|
92 |
|
93 |
try {
|
94 |
+
const response = await axios.post(url, data, {
|
95 |
+
headers: {
|
96 |
+
'Content-Type': 'application/json',
|
97 |
+
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
|
98 |
+
'X-Forwarded-For': generateRandomIP()
|
99 |
+
}
|
100 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
return response.data;
|
103 |
} catch (error) {
|
104 |
+
console.error(error);
|
|
|
105 |
}
|
106 |
}
|
107 |
|
108 |
|
109 |
+
async function cleanInactiveUsers() {
|
110 |
+
const currentTime = DateTime.local();
|
111 |
|
112 |
+
Object.keys(chatHistory).forEach((userId) => {
|
113 |
+
const user = chatHistory[userId];
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 10 })) < currentTime) {
|
116 |
+
// User's last message was more than 10 minutes ago, remove the user from chatHistory
|
117 |
+
delete chatHistory[userId];
|
118 |
+
}
|
119 |
+
});
|
|
|
120 |
|
121 |
+
// Schedule the next execution after 1 minute
|
122 |
+
setTimeout(cleanInactiveUsers, 30000);
|
123 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
+
// Start the continuous execution of cleanInactiveUsers
|
126 |
+
cleanInactiveUsers();
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
app.get('/send', async (req, res) => {
|
132 |
try {
|
133 |
+
const { id, message } = req.query;
|
134 |
+
if(!id)return res.status(400).json({ success: false, response: "input param id & input id user"});
|
135 |
+
if(!message)return res.status(400).json({ success: false, response: "input param message & input a message from user"});
|
136 |
+
if (!chatHistory[id]) {
|
137 |
+
const newid = await createNewChat();
|
138 |
+
chatHistory[id] = {
|
139 |
+
lastChat: DateTime.local(),
|
140 |
+
chat_id: newid,
|
141 |
+
message: [],
|
142 |
};
|
143 |
+
}
|
144 |
+
const role = 'user';
|
145 |
+
chatHistory[id].message.push({ role, message });
|
146 |
+
chatHistory[id].lastChat = DateTime.local();
|
147 |
+
id_room = chatHistory[id].chat_id
|
148 |
+
const botResponse = await sendMessage(message, id_room, false);
|
149 |
+
const regex = /"content":"(.*?)"/g;
|
150 |
+
let matches;
|
151 |
+
let contents = '';
|
152 |
+
while ((matches = regex.exec(botResponse)) !== null) {
|
153 |
+
if (typeof matches[1] !== 'undefined') {
|
154 |
+
contents += matches[1];
|
155 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
}
|
157 |
+
chatHistory[id].message.push({ role: 'Assistant', message: contents });
|
158 |
+
chatHistory[id].lastChat = DateTime.local();
|
159 |
+
await updateMessages(id_room, contents);
|
160 |
+
res.status(200).json({ success: true, response: contents });
|
161 |
+
} catch (e) {
|
162 |
+
res.status(400).json({ success: false, response: format(e) });
|
163 |
+
}
|
164 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
app.get('/listuser', (req, res) => {
|
167 |
+
const userList = Object.keys(chatHistory);
|
168 |
+
res.status(200).json({ userList });
|
169 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
+
app.get('/get/message/:id', (req, res) => {
|
172 |
+
const id = req.params.id;
|
173 |
+
if(!id)return res.status(400).json({ success: false, response: "input id"});
|
174 |
+
if (!chatHistory[id]) {
|
175 |
+
return res.status(404).json({ error: 'User not found' });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
}
|
177 |
+
const userMessages = chatHistory[id].message;
|
178 |
+
const userRoom = chatHistory[id].chat_id;
|
179 |
+
res.status(200).json({ id, messages: userMessages });
|
180 |
});
|
181 |
|
182 |
|
183 |
+
app.listen(port, () => {
|
184 |
+
console.log(`Server is running on port ${port}`);
|
185 |
+
});
|
|