简体   繁体   中英

looking to make a twitter bot that will post images from a folder in order. using node.js

so essentially i've been following a guide online that is showing me how to create a twitter bot that will tweet out images from a folder on my pc. I have a large library of photos I want to tweet, however, this guide is only showing me a way to tweet them out in a random order. I want to be able to tweet the photos out in the order they are sorted in inside of the folder. Here is what I'm working with. I imagine the issue lies with the [Math.floor(Math.random() * images.length)]; line. I apoligize if this is a stupid question. I'm pretty new to all of this.

var Twit = require('twit')

var fs = require('fs'),
    path = require('path'),
    Twit = require('twit'),
    config = require(path.join(__dirname, 'config.js'));

var T = new Twit(config);

function random_from_array(images){
  return images[Math.floor(Math.random() * images.length)];
}

function upload_random_image(images){
  console.log('Opening an image...');
  var image_path = path.join(__dirname, '/images/' + random_from_array(images)),
      b64content = fs.readFileSync(image_path, { encoding: 'base64' });

  console.log('Uploading an image...');

  T.post('media/upload', { media_data: b64content }, function (err, data, response) {
    if (err){
      console.log('ERROR:');
      console.log(err);
    }
    else{
      console.log('Image uploaded!');
      console.log('Now tweeting it...');

      T.post('statuses/update', {
        media_ids: new Array(data.media_id_string)
      },
        function(err, data, response) {
          if (err){
            console.log('ERROR:');
            console.log(err);
          }
          else{
            console.log('Posted an image!');
          }
        }
      );
    }
  });
}

you want the bot to post the next image in the sequence instead of a random one, this requires a bit of restructuring to the function upload_random_image() I would copy paste that function, add a parameter, and rename the new function upload_all(images, n) it would look something like this:

function upload_all(images, n){
  console.log('Opening an image...');
  var image_path = path.join(__dirname, '/images/' + n),
      b64content = fs.readFileSync(image_path, { encoding: 'base64' });

  console.log('Uploading an image...');

  T.post('media/upload', { media_data: b64content }, function (err, data, response) {
    if (err){
      console.log('ERROR:');
      console.log(err);
    }
    else{
      console.log('Image uploaded!');
      console.log('Now tweeting it...');

      T.post('statuses/update', {
        media_ids: new Array(data.media_id_string)
      },
        function(err, data, response) {
          if (err){
            console.log('ERROR:');
            console.log(err);
          }
          else{
            console.log('Posted an image!');
          }
        }
      );
    }
  });
  if ( n < images.length()-1){
    return upload_all(images, n++);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM