Last active
September 6, 2021 13:20
-
-
Save kalinchernev/f645b1ed86f0fa8a7c514af728cc8ae5 to your computer and use it in GitHub Desktop.
get and upload data to instagram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Helpers | |
- https://instaloader.github.io/ | |
- https://github.com/dilame/instagram-private-api | |
- https://www.npmjs.com/package/instagram-web-api | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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