Advertisement
Guest User

Untitled

a guest
Dec 1st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fs                   from 'fs'
  2. import https                from 'https'
  3.  
  4. const USERNAME = 'XXX'
  5. const PASSWORD = 'YYY'
  6.  
  7. const userId = `ZZZ`
  8.  
  9. const Client = require('instagram-private-api').V1;
  10. const device = new Client.Device('someuser');
  11. const storage = new Client.CookieFileStorage(__dirname + '/my.json');
  12.  
  13. const createDir = (dir) => {
  14.   try {
  15.     fs.mkdirSync(dir)
  16.   } catch (err) {
  17.  
  18.   }
  19. }
  20.  
  21. const download = ({ url, imageFile }) => new Promise((resolve, reject) => {
  22.   try {
  23.     const file = fs.createWriteStream(imageFile)
  24.     https.get(url, (response) => {
  25.       response.pipe(file);
  26.       file.on('finish', function() {
  27.         file.close()
  28.         resolve()
  29.       })
  30.     }).on('error', (err) => {
  31.       fs.unlink(imageFile)
  32.       reject(err)
  33.     })
  34.   } catch (err) {
  35.     console.log(err)
  36.     reject(err)
  37.   }
  38. })
  39.  
  40. const getNext = async (feed, session) => {
  41.   const media = await feed.get()
  42.  
  43.   for (const doc of media) {
  44.     try {
  45.       const {
  46.         takenAt,
  47.         pk,
  48.         id,
  49.         deviceTimestamp,
  50.         mediaType,
  51.         code,
  52.         clientCacheKey,
  53.         filterType,
  54.         imageVersions2,
  55.         originalWidth,
  56.         originalHeight,
  57.         location,
  58.         lat,
  59.         lng,
  60.         use,
  61.         caption,
  62.         captionIsEdited,
  63.         likeCount,
  64.         hasLiked,
  65.         topLikers,
  66.         commentLikesEnabled,
  67.         commentThreadingEnabled,
  68.         hasMoreComments,
  69.         maxNumVisiblePreviewComments,
  70.         previewComments,
  71.         commentCount,
  72.         photoOfYou,
  73.         canViewerSave,
  74.         organicTrackingToken,
  75.         webLink,
  76.         carouselMedia,
  77.         images,
  78.       } = doc._params
  79.  
  80.       try {
  81.         delete location['location']
  82.       } catch (err) {
  83.       }
  84.  
  85.       const dir = `${ __dirname }/backup/${ id }`
  86.       createDir(dir)
  87.       const jsonFile = `${ dir }/params.json`
  88.       const commentFile = `${ dir }/comment.json`
  89.  
  90.       const commentsFeed = new Client.Feed.MediaComments(session, id, 10)
  91.       const comments = await commentsFeed.get()
  92.  
  93.       // console.log(`COMMENTS`, comments)
  94.       // doc.comments.map(comment => comment._params.text)
  95.  
  96.       const commentsData = comments.map(comment => ({
  97.         createdAt: comment._params.createdAt,
  98.         text: comment._params.text,
  99.         userId: comment._params.userId,
  100.       }))
  101.       fs.writeFileSync(commentFile, JSON.stringify(commentsData))
  102.  
  103.       console.log(`TRY`, id)
  104.  
  105.       fs.writeFileSync(jsonFile, JSON.stringify({
  106.         takenAt,
  107.         pk,
  108.         id,
  109.         deviceTimestamp,
  110.         mediaType,
  111.         code,
  112.         clientCacheKey,
  113.         filterType,
  114.         imageVersions2,
  115.         originalWidth,
  116.         originalHeight,
  117.         location,
  118.         lat,
  119.         lng,
  120.         use,
  121.         caption,
  122.         captionIsEdited,
  123.         likeCount,
  124.         hasLiked,
  125.         topLikers,
  126.         commentLikesEnabled,
  127.         commentThreadingEnabled,
  128.         hasMoreComments,
  129.         maxNumVisiblePreviewComments,
  130.         previewComments,
  131.         commentCount,
  132.         photoOfYou,
  133.         canViewerSave,
  134.         organicTrackingToken,
  135.         webLink,
  136.         // carouselMedia,
  137.       }))
  138.  
  139.       const [{ url }] = images
  140.  
  141.       console.log(`IMAGE`, url)
  142.  
  143.       const imageFile = `${ dir }/image1.jpg`
  144.  
  145.       download({ url, imageFile })
  146.  
  147.       console.log(`SAVED`)
  148.     } catch (err) {
  149.       console.log(`ERROR`, err)
  150.     }
  151.   }
  152. }
  153.  
  154. const main = async () => {
  155.   const session = await Client.Session.create(device, storage, USERNAME, PASSWORD)
  156.   const account = await Client.Account.searchForUser(session, userId)
  157.   const feed = new Client.Feed.UserMedia(session, account.id)
  158.  
  159.  while (true) {
  160.     await getNext(feed, session)
  161.  }
  162. }
  163.  
  164. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement