diff options
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 97 |
1 files changed, 72 insertions, 25 deletions
@@ -2,56 +2,103 @@ const express = require('express'); const { exec } = require('child_process'); const path = require('path'); const cors = require('cors'); +const fs = require('fs'); +const cron = require('node-cron'); const app = express(); -const port = 5000; +const port = 5001; 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); +function escapePythonString(str) { + return str.replace(/\\/g, '\\\\') + .replace(/'/g, "\\'") + .replace(/\n/g, '\\n') + .replace(/\t/g, '\\t'); +} + +// Read credentials from JSON file +const credentials = JSON.parse(fs.readFileSync(path.join(__dirname, 'credentials.json'), 'utf8')); +const escapedUsername = escapePythonString(credentials.username); +const escapedPassword = escapePythonString(credentials.password); + +const baseDir = path.join(__dirname, 'downloads'); +const dateStr = new Date().toISOString().split('T')[0]; +const dateDir = path.join(baseDir, dateStr); + +function downloadContent() { + const influencers = JSON.parse(fs.readFileSync(path.join(__dirname, 'influencers.json'), 'utf8')).profiles; const script = ` - import instaloader - import os +import instaloader +import os - L = instaloader.Instaloader() - PROFILES = ${JSON.stringify(profiles)} - BASE_DIR = "${dateDir.replace(/\\/g, '/')}" +L = instaloader.Instaloader() +L.login('${escapedUsername}', '${escapedPassword}') +PROFILES = ${JSON.stringify(influencers)} +BASE_DIR = "${dateDir.replace(/\\/g, '/')}" - os.makedirs(BASE_DIR, exist_ok=True) +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: +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: + if post.typename == 'GraphVideo': # Only download videos + L.download_post(post, target=profile) + elif post.typename == 'GraphImage': # Only download images L.download_post(post, target=profile) - for profile in PROFILES: - download_posts(profile) - `; +for profile in PROFILES: + download_posts(profile) +`; const scriptPath = path.join(__dirname, 'download_script.py'); - require('fs').writeFileSync(scriptPath, script); + 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 }); + console.log('Download complete'); + }); +} + +// Schedule the download task to run every hour +cron.schedule('0 * * * *', downloadContent); + +// Run the download task immediately on server start +downloadContent(); + +// Endpoint to list downloaded files +app.get('/downloads', (req, res) => { + fs.readdir(dateDir, (err, profiles) => { + if (err) { + return res.status(500).json({ error: err.message }); + } + const allowedExtensions = ['.mp4', '.jpg', '.jpeg']; + const fileList = profiles.flatMap(profileDir => { + const profilePath = path.join(dateDir, profileDir); + return fs.readdirSync(profilePath) + .filter(file => allowedExtensions.includes(path.extname(file).toLowerCase())) + .map(file => { + const filePath = path.join(profileDir, file); + console.log('Serving file:', filePath); // Log the file path + return filePath; + }); + }); + res.json(fileList); }); }); +// Serve downloaded files statically +app.use('/static', express.static(path.join(__dirname, 'downloads'))); + app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); }); |