我正在尝试使用我的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:memesbotmemestoragequeue645 (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:memesbotmemes.js:29:23)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
希望有人可以帮助我,谢谢。
代码似乎是在开始时生成随机文件(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);