Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fs from 'fs'
- import https from 'https'
- const USERNAME = 'XXX'
- const PASSWORD = 'YYY'
- const userId = `ZZZ`
- const Client = require('instagram-private-api').V1;
- const device = new Client.Device('someuser');
- const storage = new Client.CookieFileStorage(__dirname + '/my.json');
- const createDir = (dir) => {
- try {
- fs.mkdirSync(dir)
- } catch (err) {
- }
- }
- const download = ({ url, imageFile }) => new Promise((resolve, reject) => {
- try {
- const file = fs.createWriteStream(imageFile)
- https.get(url, (response) => {
- response.pipe(file);
- file.on('finish', function() {
- file.close()
- resolve()
- })
- }).on('error', (err) => {
- fs.unlink(imageFile)
- reject(err)
- })
- } catch (err) {
- console.log(err)
- reject(err)
- }
- })
- const getNext = async (feed, session) => {
- const media = await feed.get()
- for (const doc of media) {
- try {
- const {
- takenAt,
- pk,
- id,
- deviceTimestamp,
- mediaType,
- code,
- clientCacheKey,
- filterType,
- imageVersions2,
- originalWidth,
- originalHeight,
- location,
- lat,
- lng,
- use,
- caption,
- captionIsEdited,
- likeCount,
- hasLiked,
- topLikers,
- commentLikesEnabled,
- commentThreadingEnabled,
- hasMoreComments,
- maxNumVisiblePreviewComments,
- previewComments,
- commentCount,
- photoOfYou,
- canViewerSave,
- organicTrackingToken,
- webLink,
- carouselMedia,
- images,
- } = doc._params
- try {
- delete location['location']
- } catch (err) {
- }
- const dir = `${ __dirname }/backup/${ id }`
- createDir(dir)
- const jsonFile = `${ dir }/params.json`
- const commentFile = `${ dir }/comment.json`
- const commentsFeed = new Client.Feed.MediaComments(session, id, 10)
- const comments = await commentsFeed.get()
- // console.log(`COMMENTS`, comments)
- // doc.comments.map(comment => comment._params.text)
- const commentsData = comments.map(comment => ({
- createdAt: comment._params.createdAt,
- text: comment._params.text,
- userId: comment._params.userId,
- }))
- fs.writeFileSync(commentFile, JSON.stringify(commentsData))
- console.log(`TRY`, id)
- fs.writeFileSync(jsonFile, JSON.stringify({
- takenAt,
- pk,
- id,
- deviceTimestamp,
- mediaType,
- code,
- clientCacheKey,
- filterType,
- imageVersions2,
- originalWidth,
- originalHeight,
- location,
- lat,
- lng,
- use,
- caption,
- captionIsEdited,
- likeCount,
- hasLiked,
- topLikers,
- commentLikesEnabled,
- commentThreadingEnabled,
- hasMoreComments,
- maxNumVisiblePreviewComments,
- previewComments,
- commentCount,
- photoOfYou,
- canViewerSave,
- organicTrackingToken,
- webLink,
- // carouselMedia,
- }))
- const [{ url }] = images
- console.log(`IMAGE`, url)
- const imageFile = `${ dir }/image1.jpg`
- download({ url, imageFile })
- console.log(`SAVED`)
- } catch (err) {
- console.log(`ERROR`, err)
- }
- }
- }
- const main = async () => {
- const session = await Client.Session.create(device, storage, USERNAME, PASSWORD)
- const account = await Client.Account.searchForUser(session, userId)
- const feed = new Client.Feed.UserMedia(session, account.id)
- while (true) {
- await getNext(feed, session)
- }
- }
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement