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