【发布时间】:2018-05-31 01:40:05
【问题描述】:
我正在使用 Firebase 和 Twitter API 创建一个 Twitter 机器人来取消关注非活动帐户。
这是 Nodejs 代码:
// Get the screen_names in `to_unfollow` table
firebase.database().ref("to_unfollow/" + settings.PERSON_TWITTER_HANDLE).on("value", function(snapshot) {
// Functional Loop
var i = 0;
function timedLoop() { // unFollows the user after every `x` seconds
/*=============================================>>>>>
= Thing to be done =
===============================================>>>>>*/
function snapshotToArray(snapshot) { // This function converts the Snapshot data into an array
var returnArr = [];
snapshot.forEach(function(childSnapshot) {
var item = childSnapshot.val();
item.key = childSnapshot.key;
returnArr.push(item);
});
return returnArr;
};
var screen_name_to_unfollow = snapshotToArray(snapshot)[i].key;
console.log(screen_name_to_unfollow);
/*= End of Thing to be done =*/
/*=============================================<<<<<*/
// Increase value of variable `i` by 1. (Increment)
i++;
// How many times to loop
if(i < 5000) {
setTimeout( timedLoop, 1000*20 ); // timedLoop();
}
}
timedLoop(); // Run the loop
});
在这里,我的循环运行良好。 screen_name_to_unfollow 变量每 20 秒登录一次控制台。
但是当我添加执行代码以取消关注人员时,循环工作......但不是有时间间隔。它只是不断取消关注人。
我的代码看起来是这样的:
// Get the screen_names in `to_unfollow` table
firebase.database().ref("to_unfollow/" + settings.PERSON_TWITTER_HANDLE).on("value", function(snapshot) {
// Functional Loop
var i = 0;
function timedLoop() { // unFollows the user after every `x` seconds
/*=============================================>>>>>
= Thing to be done =
===============================================>>>>>*/
function snapshotToArray(snapshot) { // This function converts the Snapshot data into an array
var returnArr = [];
snapshot.forEach(function(childSnapshot) {
var item = childSnapshot.val();
item.key = childSnapshot.key;
returnArr.push(item);
});
return returnArr;
};
var screen_name_to_unfollow = snapshotToArray(snapshot)[i].key;
console.log(screen_name_to_unfollow);
// UnFollow
T.post('friendships/destroy', { screen_name: screen_name_to_unfollow }, function (err, data, response) {
console.log('T.Post', new Date());
if(!err){
console.log(settings.PERSON_NICKNAME + " follower " + screen_name_to_unfollow + " unfollowed.");
// Create an `unfollowed` table and insert the screen_name there
firebase.database().ref("unfollowed").child(settings.PERSON_TWITTER_HANDLE).update({
[screen_name_to_unfollow]: {
connection: "unfollowed"
}
});
// Delete the screen_name from `to_unfollow` table
firebase.database().ref("to_unfollow/" + settings.PERSON_TWITTER_HANDLE).child(screen_name_to_unfollow).remove();
} else{
console.log(err);
}
});
/*= End of Thing to be done =*/
/*=============================================<<<<<*/
// Increase value of variable `i` by 1. (Increment)
i++;
// How many times to loop
if(i < 5000) {
setTimeout( timedLoop, 1000*20 ); // timedLoop();
}
}
timedLoop(); // Run the loop
});
在终端中,您可以看到间隔没有解决。
T.Post 2018-05-31T02:11:27.234Z
Wesbos follower AnnSaid unfollowed.
T.Post 2018-05-31T02:11:27.914Z
Wesbos follower AnnyShivang unfollowed.
T.Post 2018-05-31T02:11:28.865Z
Wesbos follower AntJanus unfollowed.
T.Post 2018-05-31T02:11:29.888Z
Wesbos follower AnthonyCatonPR unfollowed.
T.Post 2018-05-31T02:11:30.975Z
Wesbos follower AppleLaa unfollowed.
T.Post 2018-05-31T02:11:31.733Z
Wesbos follower AsyrafDuyshart unfollowed.
在这种情况下我该怎么做才能使循环像以前一样工作?我希望每 20 秒后取消关注少数不活动的 Twitter 帐户,因为我不想陷入速率限制.
提前致谢 :-)
【问题讨论】:
-
具体的问题是什么?
T.post会抛出任何错误吗? -
@MarcosCasagrande T.post 不会引发任何错误。但循环不适用于时间间隔..
-
timedLoop只运行一次,还是从不运行? -
按需要连续工作。但不是每 20 秒后。它只是一直不关注人们。
-
我不明白你的意思:它只是不断地取消关注这些人。你不希望它取消关注人,它会尝试取消关注同一个人两次,或者实际问题是什么?
标签: javascript node.js loops firebase twitter