2017-03-09 57 views
0

我試圖修復bug和我嘰嘰喳喳的機器人移動,基本上不存在與文件夾的所有文件名的數組,然後選擇一個隨機的職位,但有時再次發佈相同的圖像,我該如何解決它?選擇一個隨機文件,並與node.js的

這裏是代碼

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) 

希望有人能幫助我,謝謝。

回答

0

代碼似乎在開始時產生隨機文件(var filename = (random_file());),但不在運行upload_random_image()

所以,文件是隨機選擇的一個和upload_random_imagesetInterval

解調用多次:

移動下面行中的方法upload_random_image

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

糟糕裏面!沒有注意到!現在完美地工作 –