Skip to content

Instantly share code, notes, and snippets.

@kalinchernev
Last active September 6, 2021 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalinchernev/f645b1ed86f0fa8a7c514af728cc8ae5 to your computer and use it in GitHub Desktop.
Save kalinchernev/f645b1ed86f0fa8a7c514af728cc8ae5 to your computer and use it in GitHub Desktop.
get and upload data to instagram
import Instagram from 'instagram-web-api';
(async () => {
const username = process.env.IG_USERNAME;
const password = process.env.IG_PASSWORD;
const client = new Instagram({ username, password });
await client.login();
const photos = await client.getPhotosByUsername({ username, first: 50 });
photos.user.edge_owner_to_timeline_media.edges.map(async edge => {
const { id: mediaId } = edge.node;
const result = await client.deleteMedia({ mediaId });
console.log(result);
})
})()
Helpers
- https://instaloader.github.io/
- https://github.com/dilame/instagram-private-api
- https://www.npmjs.com/package/instagram-web-api
import { IgApiClient } from 'instagram-private-api';
import { run } from './utils.mjs';
const ig = new IgApiClient();
const username = process.env.IG_USERNAME;
const password = process.env.IG_PASSWORD;
const user = process.env.IG_USER;
ig.state.generateDevice(username);
ig.account.login(username, password).then(auth => {
console.log(`Logged in as ${auth.username}`);
run({ client: ig, user });
});
import Instagram from 'instagram-web-api'
import { run } from './utils.mjs';
const username = process.env.IG_USERNAME;
const password = process.env.IG_PASSWORD;
const user = process.env.IG_USER;
const client = new Instagram({ username, password });
client.login().then(() => {
run({ client, user });
});
import path from 'path';
import { existsSync, readdirSync, readFileSync } from 'fs'
export function run({ client, user }) {
if (!client) {
return console.error('no client');
}
const files = readdirSync(`./${user}`);
files.map(file => {
const ext = path.extname(file);
const fileName = file.replace(ext, '');
if (ext === '.jpg') {
const txtFile = `./${user}/${fileName}.txt`;
console.log(`Working with ${fileName}`);
const hasText = existsSync(txtFile);
console.log('Has text:', hasText);
const caption = hasText ? readFileSync(txtFile).toString() : '';
// swap with correct method from client
client.uploadPhoto({
photo: `./${user}/${file}`,
caption,
}).then(result => {
console.log(`${file} uploaded: `, result.status);
}).catch(e => {
console.error(e.message);
});
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment