Nexchan commited on
Commit
91d2cae
1 Parent(s): 21243ac

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +344 -0
index.js ADDED
@@ -0,0 +1,344 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios');
2
+ const cheerio = require('cheerio');
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 tempDir = path.join(os.tmpdir(), "temp");
11
+ app.use('/temp', express.static(tempDir));
12
+ app.use(express.json());
13
+
14
+ app.get("/", (req, res) => {
15
+ res.type("json");
16
+ const keluaran = {
17
+ success: true,
18
+ author: "Nex",
19
+ data: {
20
+ igdl: "/igdl"
21
+ },
22
+ };
23
+ res.send(keluaran);
24
+ });
25
+
26
+ async function downloadImage(url) {
27
+ try {
28
+ const response = await axios.get(url, {
29
+ responseType: 'arraybuffer'
30
+ });
31
+ const randomCode = Math.random().toString(36).substring(7);
32
+ const imagePath = `downloaded_image_${randomCode}.png`;
33
+ fs.writeFileSync(path.join(tempDir, "/" + imagePath), Buffer.from(response.data, 'binary'));
34
+ return imagePath;
35
+ } catch (error) {
36
+ console.error('Error downloading image:', error.message);
37
+ throw error;
38
+ }
39
+ }
40
+
41
+ async function downloadVideo(url) {
42
+ try {
43
+ const response = await axios.get(url, {
44
+ responseType: 'arraybuffer'
45
+ });
46
+ const randomCode = Math.random().toString(36).substring(7);
47
+ const videoPath = `downloaded_video_${randomCode}.mp4`;
48
+ fs.writeFileSync(path.join(tempDir, "/" + videoPath), Buffer.from(response.data, 'binary'));
49
+ return videoPath;
50
+ } catch (error) {
51
+ console.error('Error downloading video:', error.message);
52
+ throw error;
53
+ }
54
+ }
55
+
56
+ const generateRandomIP = () => {
57
+ const octet = () => Math.floor(Math.random() * 256);
58
+ return `${octet()}.${octet()}.${octet()}.${octet()}`;
59
+ };
60
+
61
+ const getMimeTypeFromUrl = async (url) => {
62
+ try {
63
+ const headers = {
64
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
65
+ 'Accept': '*/*',
66
+ '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;]',
67
+ 'Referer': 'https://igdownloader.app/en',
68
+ 'X-Forwarded-For': generateRandomIP()
69
+ };
70
+
71
+ const response = await axios.head(url, {
72
+ headers
73
+ });
74
+ const contentTypeHeader = response.headers['content-type'];
75
+ const [mediaType] = contentTypeHeader.split('/');
76
+ return mediaType;
77
+ } catch (error) {
78
+ console.error('Error while fetching media type:', error.message);
79
+ return undefined;
80
+ }
81
+ };
82
+
83
+ const checkMediaType = (url) => {
84
+ const supportedImageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'heic'];
85
+ const supportedVideoExtensions = ['mp4'];
86
+ const fileExtension = url.toLowerCase();
87
+ if (supportedImageExtensions.includes(fileExtension)) {
88
+ return 'image';
89
+ } else if (supportedVideoExtensions.includes(fileExtension)) {
90
+ return 'video';
91
+ } else {
92
+ return 'unknown';
93
+ }
94
+ };
95
+
96
+
97
+ /*
98
+ IGDL IGDL IGDL IGDL IGDL IGDL
99
+ IGDL IGDL IGDL IGDL IGDL IGDL
100
+ */
101
+
102
+ async function igdl1(url) {
103
+ try {
104
+ const apiEndpoint = 'https://v3.igdownloader.app/api/ajaxSearch';
105
+ const requestOptions = {
106
+ headers: {
107
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
108
+ 'Accept': '*/*',
109
+ '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;]',
110
+ 'Referer': 'https://igdownloader.app/en',
111
+ 'X-Forwarded-For': generateRandomIP()
112
+ },
113
+ };
114
+ const postData = `recaptchaToken=&q=${encodeURIComponent(url)}&t=media&lang=en`;
115
+ const response = await axios.post(apiEndpoint, postData, requestOptions);
116
+ const $ = cheerio.load(response.data.data);
117
+ const downloadLinks = $('div.download-items__btn > a');
118
+ const hrefArray = {
119
+ title: null,
120
+ urls: []
121
+ }
122
+ await Promise.all(downloadLinks.map(async (index, element) => {
123
+ const href = $(element).attr('href');
124
+ const media_type = await getMimeTypeFromUrl(href);
125
+ let type;
126
+ if (typeof media_type === 'undefined') {
127
+ type = "video";
128
+ } else {
129
+ type = media_type;
130
+ }
131
+ hrefArray.urls.push({
132
+ url: href,
133
+ type: type
134
+ });
135
+ }));
136
+ return hrefArray;
137
+ } catch (error) {
138
+ console.error('Instagram Downloader 1 - Error:', error.message);
139
+ return null;
140
+ }
141
+ }
142
+
143
+ async function igdl2(url) {
144
+ const apiEndpoint = 'https://snapinst.com/api/convert';
145
+
146
+ const requestData = {
147
+ url,
148
+ ts: Date.now(),
149
+ _ts: Date.now() - 106169240745,
150
+ _tsc: 0,
151
+ _s: 'b4d95f81de50ed9cace0103923a25dd2f57b2a76c142d82ac78a963f1274a1e1',
152
+ };
153
+
154
+ const headers = {
155
+ 'Accept': 'application/json, text/plain, */*',
156
+ 'Content-Type': 'application/json',
157
+ 'X-XSRF-TOKEN': 'eyJpdiI6InZ3aU9SVG41enJWOUljS3hIUnpsd3c9PSIsInZhbHVlIjoiakFHQjcrVjNMQm1wZ2xrcmF6NGdOSjdTUFp0ZTNFNXpCZ0tcL3VaNmFaN09TSVl2QzFZVndTVmxLUFo2QVVuYnpcL2JDdVwvc29sdHB6XC9jSzY2aURYMDdzcmd4TWVHUjZzWHpHZXEySXJMb0UwN3dqbUFDRlZFTXFxU2E4U2hOUzg5IiwibWFjIjoiNGQyYjBjYWNhNGQwMWUwZWE4YWM1MzdkMWJlYmRkYzBkOTMyZDZjNWVhNjE3MzAxOWMwNTM2OTJiOTM0ZjMwNyJ9',
158
+ 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36',
159
+ 'Referer': 'https://snapinst.com/',
160
+ };
161
+
162
+ try {
163
+ const response = await axios.post(apiEndpoint, requestData, {
164
+ headers
165
+ });
166
+ const resultArray = [];
167
+
168
+ const transformedData = {
169
+ title: response.data[0].meta.title,
170
+ urls: response.data.map(item => item.url.map(urlInfo => ({
171
+ url: urlInfo.url,
172
+ type: checkMediaType(urlInfo.type)
173
+ })))
174
+ };
175
+
176
+ return transformedData;
177
+ } catch (error) {
178
+ console.error('Instagram Downloader 2 - Error:', error.message);
179
+ return null;
180
+ }
181
+ }
182
+
183
+
184
+ async function igdl3(url) {
185
+ const apiEndpoint = 'https://co.wuk.sh/api/json';
186
+
187
+ const requestData = {
188
+ url,
189
+ aFormat: 'mp3',
190
+ filenamePattern: 'classic',
191
+ dubLang: false,
192
+ vQuality: '720',
193
+ };
194
+
195
+ const headers = {
196
+ 'Accept': 'application/json',
197
+ 'Content-Type': 'application/json',
198
+ 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36',
199
+ 'Referer': 'https://cobalt.tools/',
200
+ };
201
+
202
+ try {
203
+ const response = await axios.post(apiEndpoint, requestData, {
204
+ headers
205
+ });
206
+ var result = response.data;
207
+ let array_res = {
208
+ title: null,
209
+ urls: []
210
+ }
211
+ if (result.status === "redirect") {
212
+ let media_type = await getMimeTypeFromUrl(result.url)
213
+ array_res.urls.push({
214
+ url: result.url,
215
+ type: media_type
216
+ })
217
+ } else if (result.status === "picker") {
218
+ for (let i = 0; i < result.picker.length; i++) {
219
+ let media_type = await getMimeTypeFromUrl(result.picker[i].url)
220
+ array_res.urls.push({
221
+ url: result.picker[i].url,
222
+ type: media_type
223
+ })
224
+ }
225
+ }
226
+ return array_res;
227
+ } catch (error) {
228
+ console.error('Instagram Downloader 3 - Error:', error.message);
229
+ return null;
230
+ }
231
+ };
232
+
233
+ async function igdl4(url) {
234
+ try {
235
+ const apiEndpoint = 'https://v3.saveig.app/api/ajaxSearch';
236
+ const requestOptions = {
237
+ headers: {
238
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
239
+ 'Accept': '*/*',
240
+ '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;]',
241
+ 'Referer': 'https://saveig.app/en',
242
+ },
243
+ };
244
+ const postData = `recaptchaToken=&q=${encodeURIComponent(url)}&t=media&lang=en`;
245
+ const response = await axios.post(apiEndpoint, postData, requestOptions);
246
+ const $ = cheerio.load(response.data.data);
247
+ const downloadLinks = $('div.download-items__btn > a');
248
+ const hrefArray = {
249
+ title: null,
250
+ urls: []
251
+ }
252
+ await Promise.all(downloadLinks.map(async (index, element) => {
253
+ const href = $(element).attr('href');
254
+ const media_type = await getMimeTypeFromUrl(href);
255
+ let type;
256
+ if (typeof media_type === 'undefined') {
257
+ type = "video";
258
+ } else {
259
+ type = media_type;
260
+ }
261
+ hrefArray.urls.push({
262
+ url: href,
263
+ type: type
264
+ });
265
+ }));
266
+ return hrefArray;
267
+ } catch (error) {
268
+ console.error('Instagram Downloader 4 - Error:', error.message);
269
+ return null;
270
+ }
271
+ }
272
+
273
+ const getInstagramDownloadLinks = async (url) => {
274
+ let result = await igdl1(url);
275
+ if (!result) {
276
+ result = await igdl2(url);
277
+ }
278
+ if (!result) {
279
+ result = await igdl3(url);
280
+ }
281
+ if (!result) {
282
+ result = await igdl4(url);
283
+ }
284
+ if (!result) {
285
+ result = {
286
+ message: "all server error"
287
+ };
288
+ }
289
+ return result;
290
+ };
291
+
292
+ app.get('/igdl', async (req, res) => {
293
+ try {
294
+ const {
295
+ url
296
+ } = req.query;
297
+ if (!url) {
298
+ return res.status(400).json({
299
+ error: 'Parameter url is required'
300
+ });
301
+ }
302
+ if (!/https?:\/\/(www\.)?instagram\.com\/(p|reel|tv)/.test(url)) {
303
+ return res.status(400).json({
304
+ error: "Example: https://www.instagram.com/p/Cz1fTwMJFpx/?igsh=MXRrY2g4eWNucGoyZg=="
305
+ });
306
+ }
307
+ let result = await getInstagramDownloadLinks(url);
308
+ let result_upload = {
309
+ title: result?.title || 'untitled',
310
+ media: []
311
+ }
312
+
313
+
314
+ for (let item of result.urls) {
315
+ if (item.type === "image") {
316
+ let unduh = await downloadImage(item.url)
317
+ result_upload.media.push({
318
+ type: item.type,
319
+ path: unduh,
320
+ url_path: `http://${process.env.SPACE_HOST}/temp/${path.basename(unduh)}`
321
+ })
322
+ } else if (item.type === "video") {
323
+ let unduh = await downloadVideo(item.url)
324
+ result_upload.media.push({
325
+ type: item.type,
326
+ path: unduh,
327
+ url_path: `http://${process.env.SPACE_HOST}/temp/${path.basename(unduh)}`
328
+ })
329
+ }
330
+ }
331
+
332
+ res.json(result_upload);
333
+ } catch (error) {
334
+ console.error('Error processing request:', error);
335
+ res.status(500).json({
336
+ error: 'Failed to process request\n' + error
337
+ });
338
+ }
339
+ });
340
+
341
+ const hostname = `http://${process.env.SPACE_HOST}`; // Ganti dengan host yang sesuai
342
+ app.listen(PORT, () => {
343
+ console.log(`Server is running on http://${hostname}:${PORT}`);
344
+ });