选择一个随机文件并使用node.js移动

时间:2017-03-09 05:17:10

标签: javascript node.js twitter bots

我试图用我的twitter机器人修复一个bug,基本上,有一个包含文件夹所有文件名的数组,然后随机选择一个并发布它,但有时会再次发布相同的图像,怎么能我解决了吗?

这是代码

public class Top_Fragment extends Fragment implements View.OnClickListener {
    Button button;
    LinearLayout mLinearLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.top_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        button = (Button) getActivity().findViewById(R.id.button);
        mLinearLayout = (LinearLayout) getActivity().findViewById(R.id.layout);
    }

    @Override
    public void onClick(View v) {
        mLinearLayout.setBackgroundColor(Color.parseColor("#ffffbb33"));
    }
}

我已经尝试了

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' });
...

希望有人可以帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

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

因此,文件是随机选择的,upload_random_image多次调用setInterval

解决方案:

在方法upload_random_image

内移动以下行
var filename = (random_file());
var imgPATH = path.join(__dirname, '/memestorage/queue/' + filename);