D
D
Dmitry2015-04-23 13:00:34
JavaScript
Dmitry, 2015-04-23 13:00:34

Bot for Instagram on Node.js?

Good afternoon!
I want to say right away that I am not strong in programming.
But the question is this.
I came across such a library for Node.js, which allows you to work with the instagram API.
And I wanted to create a bot that would select photos by a random tag once an hour and like them.
It turned out this

var Instagram = require('instagram-node-lib');
var arrayToken = ['token_1','token_2','token_3','token_n'];
var arrayTag = ['tag_1','tag_2','tag_3','tag_n'];

var photoId = [];

function instaBot () {
  for (var i = 0; i < arrayToken.length; i++) { 
    photoId = [];
    Instagram.set('access_token', arrayToken[i])
    for (var j = 0; j < 2; j++) { 
      var randomTag = arrayTag[Math.floor(Math.random() * arrayTag.length)];
      Instagram.tags.recent({
        name: randomTag,
        complete: function(data){
          for (var k=0; k<data.length; k++){
            var bit = data[k]
            photoId.push(bit['id'])
      }   
      for (var s=0; s<30; s++){
       //doSetTimeout(s)
        Instagram.media.like({ media_id: photoId[s] })
      }
    }
   })
  }
 }
}

The function works, but not quite correctly. The second cycle, where a random tag is selected and a photo search is performed on it, is performed twice at once, and only then the photo is liked.
I would like to ask how to make this function run every hour and like the photo occurs with a slight delay.
I know that there are such functions as setTimeout and setInterval, but nothing really worked.
This is just what came out.
function doSetTimeout(s) {
  setTimeout(function() {
      Instagram.media.like({ media_id: photoId[s] })
  }, s * 5000);
}

And then you call the doSetTimeout(s) function.
I hope I have explained the essence of the issue more or less clearly and I will be grateful for any help and advice in resolving this issue.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2015-05-05
@firlix

Using Node.js tools, it was not possible to solve my problem, but I managed to do it in python. To work, you only need the keys themselves (with the ability to like the photo "scope=likes") and connect the python-instagram library . With 5 keys, it worked for me for 2 days (like a photo every 24 seconds), after which I was disabled the ability to like a photo for more than a day.
Here is the code itself. I'm not a programmer, so it turned out something like this. Maybe someone will come in handy.

import random
import time
from instagram.client import InstagramAPI
from instagram.bind import InstagramAPIError

arrayToken = ['YOUR_TOKEN_1',
              'YOUR_TOKEN_2',
              'YOUR_TOKEN_3',
              'YOUR_TOKEN_4',
              'YOUR_TOKEN_5']

arrayTag = ['spb','vcocam','vcorussia','love','TFLers', 'tweegram',
            'photooftheday', '20likes', 'amazing', 'smile', 'follow4follow',
            'like4like', 'look', 'instalike', 'igers', 'picoftheday', 'food',
            'instadaily', 'instafollow', 'followme', 'girl', 'iphoneonly',
            'instagood', 'bestoftheday', 'instacool', 'instago', 'all_shots',
            'follow', 'webstagram', 'colorful', 'style', 'swag']

timeDelay = 3600 / (len(arrayToken) * 30)

while True:
    for i in arrayToken:
        random_tag = random.choice(arrayTag)
        access_token = i
        client_secret =""
        api = InstagramAPI(access_token=access_token,client_secret=client_secret)
        recent_media, next_ = api.tag_recent_media(count=30, tag_name=random_tag)
        photos = []
        print random_tag
        for media in recent_media:
            try:
                time.sleep(timeDelay)
                print api.like_media(media.id)
            except InstagramAPIError as e:
                if (e.status_code == 400):
                    print "You can not like this media"

A
Artem, 2015-05-01
@4tm

Hello. I think you can use async functions to solve your looping problem, like these .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question