summaryrefslogblamecommitdiffstats
path: root/server.js
blob: 5c5a85d71bda99e6722a02d2b027d9d669cbd4e0 (plain) (tree)
























































                                                                                 
const express = require('express');
const { exec } = require('child_process');
const path = require('path');
const cors = require('cors');
const app = express();
const port = 5000;

app.use(express.json());
app.use(cors());

app.post('/download', (req, res) => {
  const profiles = req.body.profiles;
  const baseDir = path.join(__dirname, 'downloads');
  const dateStr = new Date().toISOString().split('T')[0];
  const dateDir = path.join(baseDir, dateStr);

  const script = `
    import instaloader
    import os

    L = instaloader.Instaloader()
    PROFILES = ${JSON.stringify(profiles)}
    BASE_DIR = "${dateDir.replace(/\\/g, '/')}"

    os.makedirs(BASE_DIR, exist_ok=True)

    def download_posts(profile):
        profile_dir = os.path.join(BASE_DIR, profile)
        os.makedirs(profile_dir, exist_ok=True)
        L.dirname_pattern = profile_dir
        posts = instaloader.Profile.from_username(L.context, profile).get_posts()
        for post in posts:
            L.download_post(post, target=profile)

    for profile in PROFILES:
        download_posts(profile)
  `;

  const scriptPath = path.join(__dirname, 'download_script.py');
  require('fs').writeFileSync(scriptPath, script);

  exec(`python3 ${scriptPath}`, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error: ${error.message}`);
      return res.status(500).json({ error: error.message });
    }
    if (stderr) {
      console.error(`Stderr: ${stderr}`);
      return res.status(500).json({ error: stderr });
    }
    res.json({ message: 'Download complete', output: stdout });
  });
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});