<commit_before>var config = require('./config'),
    Twit = require('twit'),
    MongoClient = require('mongodb').MongoClient;

var T = new Twit(config.oauth_creds);

var tweet = function() {
  MongoClient.connect(config.db_uri, function (err, db) {
    if (err) throw err;

    var collection = db.collection('quotes');
    collection.count(function (err, count) {
      var rnd = Math.floor(Math.random() * count);
      db.collection('quotes').find({}).limit(1).skip(rnd).toArray(function (err, results) {
        var quote = results[0],
            msg = quote.msg + '\n' + quote.src;
        T.post('statuses/update', {status: msg}, function (err, reply) {
          if (err) throw err;
          console.log(new Date);
          console.log(msg);
        });
        db.close();
      });
    });
  });
};

setInterval(tweet, config.tweet_interval);<commit_msg>Apply mongoose and promise pattern.<commit_after>var config = require('./config'),
    Twit = require('twit'),
    mongoose = require('mongoose');

var T = new Twit(config.oauth_creds),
    quotes = mongoose.model('quotes', {msg: String, src: String});

var tweet = function () {
  var promise = quotes.count().exec();
  promise.then(function (cnt) {
    var n = Math.floor(Math.random() * cnt);
    return quotes.findOne({}).skip(n).exec();
  }).then(function (quote) {
    var msg = quote.msg + '\n' + quote.src;
    T.post('statuses/update', {status: msg}, function (err, reply) {
      if (err) console.dir(err);
      console.log('--->>>');
      console.log(new Date);
      console.log(msg);
    });
  }).end();
};

mongoose.connect('mongodb://tweetbot:<EMAIL>:10098/ntalbs-mongodb');
setInterval(tweet, 5000);
{'fraction_non_alphanumeric': 0.18705463182897863, 'fraction_numerical': 0.006532066508313539, 'mean_word_length': 3.1707920792079207, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '167659', 'n_tokens_mistral': 566, 'n_tokens_neox': 560, 'n_words': 116}