File size: 2,955 Bytes
5682c91
 
 
 
 
 
 
 
 
 
88690bd
 
5682c91
88690bd
5682c91
 
 
 
88690bd
 
5682c91
 
 
 
 
 
88690bd
5682c91
88690bd
 
 
5682c91
88690bd
5682c91
 
88690bd
5682c91
 
88690bd
5682c91
 
88690bd
5682c91
 
 
88690bd
5682c91
 
 
88690bd
 
5682c91
 
 
88690bd
5682c91
 
88690bd
 
 
 
 
 
 
5682c91
88690bd
5682c91
88690bd
 
5682c91
 
 
 
88690bd
5682c91
 
 
88690bd
 
 
5682c91
 
88690bd
 
5682c91
 
 
88690bd
5682c91
 
 
 
 
 
823a0a9
5682c91
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
const express = require('express');
const axios = require('axios');
const NodeCache = require('node-cache');
const crypto = require('crypto');
const path = require('path');

const app = express();
const cache = new NodeCache();
let idCounter = 1;

// Serve static files (HTML, CSS, JS)
app.use(express.static(path.join(__dirname)));

// Main UI
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});

// Spotify Downloader UI
// Generate unique short key
function generateKey() {
  const id = idCounter++;
  const rand = crypto.randomBytes(3).toString('hex');
  return `${id}-${rand}`;
}

// Scrape via RapidAPI using provided API key
async function aio(url) {
  if (!url || !url.startsWith('https://')) throw new Error('Invalid URL');
  const apiKey = '1dda0d29d3mshc5f2aacec619c44p16f219jsn99a62a516f98';
  const { data } = await axios.post(
    'https://auto-download-all-in-one.p.rapidapi.com/v1/social/autolink',
    { url },
    {
      headers: {
        'content-type': 'application/json; charset=utf-8',
        'accept-encoding': 'gzip',
        'cache-control': 'no-cache',
        'referer': 'https://auto-download-all-in-one.p.rapidapi.com/',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
        'x-rapidapi-host': 'auto-download-all-in-one.p.rapidapi.com',
        'x-rapidapi-key': apiKey
      }
    }
  );
  return data;
}

/**
 * Endpoint to scrape & shorten media URLs
 * GET /proxy-url?url=<source_url>&ttlSeconds=<seconds>
 */
app.get('/proxy-url', async (req, res) => {
  const { url, ttlSeconds } = req.query;
  if (!url) return res.status(400).json({ error: 'Missing url parameter' });

  try {
    const result = await aio(url);
    if (result.data && Array.isArray(result.data.medias)) {
      result.data.medias = result.data.medias.map(media => {
        const key = generateKey();
        cache.set(key, media.url, ttlSeconds ? Number(ttlSeconds) : 0);
        return { ...media, url: `${req.protocol}://${req.get('host')}/proxy-url/d/${key}` };
      });
    }
    res.json(result);
  } catch (err) {
    console.error('Error in proxy-url:', err.message);
    res.status(500).json({ error: err.message });
  }
});

/**
 * Download proxy endpoint
 * GET /proxy-url/d/:key
 */
app.get('/proxy-url/d/:key', async (req, res) => {
  const key = req.params.key;
  const target = cache.get(key);
  if (!target) return res.status(404).send('Link expired or invalid');

  try {
    const response = await axios.get(target, { responseType: 'stream' });
    Object.entries(response.headers).forEach(([name, value]) => res.setHeader(name, value));
    response.data.pipe(res);
  } catch (err) {
    console.error('Download error:', err.message);
    res.status(502).send('Download failed');
  }
});

// Health check
app.get('/health', (req, res) => res.send('OK'));

const PORT = process.env.PORT || 7860;
app.listen(PORT, () => console.log(`AIO Downloader running on port ${PORT}`));