Problem with get.search

javascript

#1

I am making a twitter bot that download all the tweet with an specific hastag but my function dosent work very well, it download all the same twitter…
Ps i have used id_max with the lower twitter id…
this is all my code…

/* GET THE 50 TREND HASTAG */

function getTrends (done) {
  twit.get('trends/place', {
    id: 23424853  // italy
  },
  function (err, data, response) {
    done(data[0].trends)
  }
)
}

/* DOWNLOAD 100 TWIT WITH THE # PASSED TO THE FUNCTION */

function findTweets (trend, idmin, volume, done) {
  var query = {
    q: trend,
    count: 2,           // number of tweets
    max_id: idmin       // Fetch tweets "younger" than the id
  }
  twit.get('search/tweets', query, function (err, data, response) {
    for (var i = 0; i < data.statuses.length; i++) {
      console.log(cont, data.statuses[i].id, data.statuses[i].text)
      console.log('\n')
      cont++
      if (idmin === 0 || idmin > data.statuses[i].id) {
        idmin = data.statuses[i].id
      }
    }
    console.log('IDMIN', idmin)
    if (volume != null && cont <= volume) {
      findTweets(trend, idmin, volume, done)
    } else {
      checkresources(function (resources) {
        // 1) ho finito di controllare le risorse
        // 2) le risorse sono "resources"
        if (resources > 100) {
          findTweets(trend, idmin, volume, done)
        }
      })
    }
    if (typeof done === 'function') done(data.statuses)
  })
}

/* CHECK THE RESOURCES AVAIBLE */

function checkresources (callback) {
  twit.get('application/rate_limit_status', {
    resources: 'search'},
    function (err, data, response) {
      console.log(data.resources)
      // resources = data.resources.search['/search/tweets'].remaining
      if (typeof callback === 'function')
      callback(data.resources.search['/search/tweets'].remaining)
    }
  )
}

/* BOT CONSTRUCTOR */

var Bot = function (nomefile, done) {
  this.nomefile = nomefile
  fs.readFile(nomefile, (err, data) => {
    this.filecontent = data.toString('utf-8')         // aggiungo contenuto del file composto da caratteri a filecontent
    this.markovchain = new MarkovChain(err ? '' : this.filecontent)   // inline if
    if (typeof done === 'function') done(this)
  })
}

Bot.prototype.add = function (tweet) {
  this.markovchain.parse(tweet)
  var cnt = this.filecontent ? (this.filecontent + '\n') : ''
  if (!this.isWritingFile) {
    this.isWritingFile = true
    fs.writeFile(this.nomefile, cnt + tweet, () => this.isWritingFile = false)
  }
}

Bot.prototype.start = function () {
  if (this.started) return
  this.started = true
  getTrends(trends => {
    findTweets(trends[0].name, 0, trends[0].tweet_volume, tweets => {
      console.log('done')
      tweets.forEach(t => this.add(t.text))
    })
  })
}

module.exports = Bot // Esporta Bot

#2

What is the problem you are having with the Search API?


#3

after some tweet dowload all the same twitter… i dont know why :frowning:


#4

this is the code on github https://github.com/emanuelecesari/mrBot
i dont know why !!is all the day i try to fix it but i cant…
i think is a problem of asincron function call but i am not so good about this subject…
PLEASE SOMEONE HELP ME


#5

Are you saying that you are trying to use the search API but it always returns the same results and when you try to paginate to older results, you still get the same tweets?


#6

yes i dont know why