这是我的twitter机器人的相关代码。它应该发布2条推文,每天一次。有些事情使它不稳定地发布。有时它会发送两次推文,有时是3次。发布功能中的switch语句只需在要发布的一组预定义状态之间进行选择。当机器人多次发布时,它有不同的状态,所以我知道,由于随机数正在发生变化,所以发布函数会被多次调用。机器人托管在heroku上,如果有帮助的话。有谁知道它多次发布的原因?
var Twitter = require('twitter');
var config = require('./config');
var client = new Twitter(config);
const oneDay = 1000*60*60*24
var timeOfLastTweets = 0
// This is supposed to ensure that it only posts once per day.
start()
setInterval(start, oneDay)
function start() {
var params = {
screen_name: 'someScreenName',
include_rts: false,
count: 100
}
client.get('statuses/user_timeline', params, function(error, tweets, response) {
if (!error) {
var words = []
for (var i = 0; i < tweets.length; i++) {
var time = new Date(tweets[i].created_at)
if (isPast24(time))
// Splits words and removes punctuation in tweet and merges that array to the words array.
words = words.concat(tweets[i].text.replace(/[.,\/!\"$%\^&\*;:{}=_`~()]/g,"").split(' '))
}
for (var i = 0; i < words.length; i++) { words[i] = words[i].toLowerCase() }
tweetAnalysis(words)
}
});
}
// This function analyzes the words of tweets... the part relevant to the question is the last if statement.
function tweetAnalysis(words) {
var wordCounts = {}
var longestWord = ""
for (var i = 0; i < words.length; i++) {
if (words[i].includes('http') || words[i].length <= 4 || words[i].includes('@') || words[i].includes('#'))
continue
if (wordCounts[words[i]])
wordCounts[words[i]] += 1
else
wordCounts[words[i]] = 1
if (words[i].length > longestWord.length)
longestWord = words[i]
}
var timesUsed = 1
var mostUsed = ""
for (key in wordCounts) {
// If word has same frequency AND it's longer than the current mostUsed AND it's not also the longest word ^
if (wordCounts[key] === timesUsed && key.length > mostUsed.length && key !== longestWord)
mostUsed = key
else if (wordCounts[key] > timesUsed) {
timesUsed++
mostUsed = key
}
}
// Since it was posting multiple times, I made this so that the posting functions can't even be called until one day has passed, yet it still posts multiple times.
var now = new Date()
if (now >= timeOfLastTweets + oneDay) {
timeOfLastTweets = now.getTime()
//console.log(wordCounts)
postMostUsed(mostUsed)
postLongest(longestWord)
}
}
// First posting function
function postMostUsed(word) {
var status = ""
switch (rnd(3)) { /* status gets a value */ }
// Somehow each of these gets called twice.
client.post('statuses/update', {status: status}, function(error, tweet, response) {
if(error) throw error;
});
// console.log("Tweeting: " + status)
}
// Second posting function
function postLongest(word) {
var status = ""
switch (rnd(3)) { /* status gets a value */ }
// Somehow each of these gets called twice.
client.post('statuses/update', {status: status}, function(error, tweet, response) {
if(error) throw error;
});
//console.log("Tweeting: " + status)
}
// Determines if the tweet was made in the past 24 hours.
function isPast24(date) {
var ago24 = new Date()
ago24 = ago24.setTime(ago24.getTime()-oneDay)
var now = new Date()
if (ago24 < date && date < now)
return true
else
return false
}
function rnd(ceiling) {
return Math.floor(Math.random()*ceiling)
}