【问题标题】:Select a random file and move with node.js选择一个随机文件并使用 node.js 移动
【发布时间】:2017-07-29 23:07:33
【问题描述】:

我正在尝试用我的 twitter 机器人修复一个错误,基本上,有一个包含文件夹所有文件名的数组,然后随机选择一个并发布它,但有时会再次发布相同的图像,我该如何修复吗?

这里是代码

var fs = require('fs'),
    path = require('path'),
    Twit = require('twit'),
    set = require(path.join(__dirname, 'set.js'));
    //array of files
    files_memes = require(path.join(__dirname, 'files.js'))

var currentdate = new Date();
var upl = "Subido: "
          + currentdate.getHours() + ":"
          + currentdate.getMinutes()+ " hs.";

var setMin = 10;
var T = new Twit(set);

function random_file(){
  var allFiles = (files_memes)//array
  return allFiles[Math.floor(Math.random() * allFiles.length)];
}

var filename = (random_file());
var imgPATH = path.join(__dirname, '/memestorage/queue/' + filename);


//image selection and upload
function upload_random_image(){
  console.log('Opening file...');
  var image_path = imgPATH,
      b64content = fs.readFileSync(image_path, { encoding: 'base64' });

  console.log('Uploading file...');
  T.post('media/upload', { media_data: b64content }, function (err, data, response) {
    if (err){
      console.log('ERROR');
      console.log(err);
    }
    else{
      console.log('File loaded!');

      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('Tweeted!');
            console.log(upl);
            console.log('Next tweet in ' + setMin + ' min.');
          }
        }
      );
    }
  });
}

//timer

setTimeout(
      upload_random_image,
        0
    );
setInterval(
      upload_random_image,
      1000*10
    );

我试过了

...
var filename = (random_file());
var pfile = ("posted"+random_file());
var imgPATH = path.join(__dirname, '/memestorage/queue/' + filename);
var postedFile = path.join(__dirname, '/memestorage/posted/' + pfile);
fs.rename(imgPATH, postedFile, function(err) {
  if ( err ) console.log('ERROR: ' + err);
});

//image selection and upload
function upload_random_image(){
  console.log('Opening file...');
  var image_path = imgPATH,
      b64content = fs.readFileSync(imgPATH, { encoding: 'base64' });
...

但是一遍又一遍地发布相同的图像,或者有时会给出这个错误信息:

fs.js:640
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open 'D:\memesbot\memestorage\queue\645 (2).jpg'
    at Error (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at Timeout.upload_random_image [as _onTimeout] (D:\memesbot\memes.js:29:23)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)

希望有人能帮助我,谢谢。

【问题讨论】:

    标签: javascript node.js twitter bots


    【解决方案1】:

    代码似乎在开始时生成随机文件 (var filename = (random_file());),但不是在运行时生成 upload_random_image()

    因此,随机选择一个文件,并使用setInterval多次调用upload_random_image

    解决方案:

    在方法upload_random_image中移到下面的行

    var filename = (random_file());
    var imgPATH = path.join(__dirname, '/memestorage/queue/' + filename);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2011-07-29
      • 2020-12-13
      • 1970-01-01
      相关资源
      最近更新 更多