From 1fd1aa09e34e63f4463503b012c9cd246a41b765 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 20 May 2016 16:41:32 -0400 Subject: Added functionality to follow new followers not sent by the stream --- snitch.cpp | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'snitch.cpp') diff --git a/snitch.cpp b/snitch.cpp index cd5153b..2357a7d 100644 --- a/snitch.cpp +++ b/snitch.cpp @@ -30,10 +30,10 @@ int main(int argc, char** argv) streamed_friends = n.getFriends(); } else if (n.getType() == twitter::notification::type::follow) { - streamed_friends.insert(n.getUserID()); + streamed_friends.insert(n.getUser().getID()); } else if (n.getType() == twitter::notification::type::unfollow) { - streamed_friends.erase(n.getUserID()); + streamed_friends.erase(n.getUser().getID()); } else if (n.getType() == twitter::notification::type::tweet) { // Only monitor people you are following @@ -80,7 +80,7 @@ int main(int argc, char** argv) client.startUserStream(); for (;;) { - std::this_thread::sleep_for(std::chrono::hours(4)); + std::this_thread::sleep_for(std::chrono::minutes(1)); std::set friends; std::set followers; @@ -90,22 +90,24 @@ int main(int argc, char** argv) resp = client.getFollowers(followers); if (resp == twitter::response::ok) { - auto frit = std::begin(friends); - auto foit = std::begin(followers); + std::list old_friends, new_followers; + std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends)); + std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers)); + for (auto f : old_friends) + { + resp = client.unfollow(f); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while unfollowing" << std::endl; + } + } - while (frit != std::end(friends)) + for (auto f : new_followers) { - auto match = std::mismatch(frit, std::end(friends), foit); - if (match.first != std::end(friends)) + resp = client.follow(f); + if (resp != twitter::response::ok) { - resp = client.unfollow(*match.first); - if (resp != twitter::response::ok) - { - std::cout << "Twitter error while unfollowing" << std::endl; - } - - frit = match.first; - frit++; + std::cout << "Twitter error while following" << std::endl; } } } else { @@ -114,6 +116,8 @@ int main(int argc, char** argv) } else { std::cout << "Twitter error while getting friends: " << resp << std::endl; } + + std::this_thread::sleep_for(std::chrono::hours(4)); } client.stopUserStream(); -- cgit 1.4.1