From c2025cfdf07a56859c447a4eac7e1546f4a168ae Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Fri, 28 Jun 2024 12:31:31 +0530 Subject: arranged dir --- server/download_script.py | 25 ++++++++ server/server.js | 143 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 server/download_script.py create mode 100644 server/server.js (limited to 'server') diff --git a/server/download_script.py b/server/download_script.py new file mode 100644 index 0000000..d6e03bc --- /dev/null +++ b/server/download_script.py @@ -0,0 +1,25 @@ + +import instaloader +import os + +L = instaloader.Instaloader() +L.login('xtester2', 'teTeD7QojvhOdvEGa50u6FFJo') +PROFILES = ["aao.remote.ho.jaye"] +BASE_DIR = "/home/genos/imp/work/project/insta/server/downloads" + +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: + 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) + \ No newline at end of file diff --git a/server/server.js b/server/server.js new file mode 100644 index 0000000..b795846 --- /dev/null +++ b/server/server.js @@ -0,0 +1,143 @@ +require('dotenv').config(); // Add this line at the top +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 bodyParser = require('body-parser'); +const app = express(); +const port = 5001; + +const adminPassword = process.env.ADMIN_PASSWORD; // Use environment variable + +app.use(express.json()); +app.use(cors()); +app.use(bodyParser.json()); + +const baseDir = path.join(__dirname, 'downloads'); + +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); + +function downloadContent() { + const influencers = JSON.parse(fs.readFileSync(path.join(__dirname, 'influencers.json'), 'utf8')).profiles; + + const script = ` +import instaloader +import os + +L = instaloader.Instaloader() +L.login('${escapedUsername}', '${escapedPassword}') +PROFILES = ${JSON.stringify(influencers)} +BASE_DIR = "${baseDir.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: + 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) + `; + + const scriptPath = path.join(__dirname, 'download_script.py'); + fs.writeFileSync(scriptPath, script); + + exec(`python3 ${scriptPath}`, (error, stdout, stderr) => { + if (error) { + console.error(`Error: ${error.message}`); + } + if (stderr) { + console.error(`Stderr: ${stderr}`); + } + 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(baseDir, (err, profiles) => { + if (err) { + return res.status(500).json({ error: err.message }); + } + const allowedExtensions = ['.mp4']; + const fileList = profiles.flatMap(profileDir => { + const profilePath = path.join(baseDir, 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'))); + +// Authentication middleware +const authenticate = (req, res, next) => { + const password = req.headers['x-admin-password']; + if (password === adminPassword) { + next(); + } else { + res.status(401).json({ error: 'Unauthorized' }); + } +}; + +// Endpoint to get influencers +app.get('/influencers', authenticate, (req, res) => { + const influencers = JSON.parse(fs.readFileSync(path.join(__dirname, 'influencers.json'), 'utf8')).profiles; + res.json(influencers); +}); + +// Endpoint to update influencers +app.post('/influencers', authenticate, (req, res) => { + const { profiles } = req.body; + fs.writeFileSync(path.join(__dirname, 'influencers.json'), JSON.stringify({ profiles }, null, 2)); + res.status(200).json({ message: 'Influencers updated successfully' }); +}); + +// Endpoint to export influencers JSON +app.get('/export-influencers', authenticate, (req, res) => { + const influencers = JSON.parse(fs.readFileSync(path.join(__dirname, 'influencers.json'), 'utf8')); + res.json(influencers); +}); + +// Endpoint to import influencers JSON +app.post('/import-influencers', authenticate, (req, res) => { + const { profiles } = req.body; + fs.writeFileSync(path.join(__dirname, 'influencers.json'), JSON.stringify({ profiles }, null, 2)); + res.status(200).json({ message: 'Influencers imported successfully' }); +}); + +app.listen(port, () => { + console.log(`Server running at http://localhost:${port}`); +}); -- cgit v1.2.3-59-g8ed1b