From 9b5d284e8fa8af25b613ce362656df6db379f322 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 12 Jun 2016 20:09:45 -0400 Subject: Initial commit --- .gitmodules | 6 ++ CMakeLists.txt | 15 +++++ troublemaker.cpp | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++ vendor/libtwittercpp | 1 + vendor/yaml-cpp | 1 + 5 files changed, 199 insertions(+) create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 troublemaker.cpp create mode 160000 vendor/libtwittercpp create mode 160000 vendor/yaml-cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8544219 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "vendor/libtwittercpp"] + path = vendor/libtwittercpp + url = https://github.com/hatkirby/libtwittercpp +[submodule "vendor/yaml-cpp"] + path = vendor/yaml-cpp + url = https://github.com/jbeder/yaml-cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1dd323c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required (VERSION 3.1) +project (troublemaker) + +set(CMAKE_BUILD_TYPE Debug) + +add_subdirectory(vendor/libtwittercpp) +include_directories(vendor/libtwittercpp/src) + +add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) +include_directories(vendor/yaml-cpp/include) + +add_executable(troublemaker troublemaker.cpp) +set_property(TARGET troublemaker PROPERTY CXX_STANDARD 11) +set_property(TARGET troublemaker PROPERTY CXX_STANDARD_REQUIRED ON) +target_link_libraries(troublemaker twitter++ yaml-cpp) diff --git a/troublemaker.cpp b/troublemaker.cpp new file mode 100644 index 0000000..31381dc --- /dev/null +++ b/troublemaker.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct userstats { + int total = 0; + int days = 1; + int day1 = 0; + int day2 = 0; + int day3 = 0; + int day4 = 0; + int day5 = 0; + int day6 = 0; + int day7 = 0; +}; + +int main(int argc, char** argv) +{ + srand(time(NULL)); + rand(); rand(); rand(); rand(); + + YAML::Node config = YAML::LoadFile("config.yml"); + + twitter::auth auth; + auth.setConsumerKey(config["consumer_key"].as()); + auth.setConsumerSecret(config["consumer_secret"].as()); + auth.setAccessKey(config["access_key"].as()); + auth.setAccessSecret(config["access_secret"].as()); + + std::set friends; + std::mutex friends_mutex; + + std::map stats; + std::mutex stats_mutex; + + twitter::client client(auth); + client.setUserStreamNotifyCallback([&] (twitter::notification n) { + std::lock_guard friend_guard(friends_mutex); + + if (n.getType() == twitter::notification::type::friends) + { + friends = n.getFriends(); + } else if (n.getType() == twitter::notification::type::follow) + { + friends.insert(n.getUser().getID()); + } else if (n.getType() == twitter::notification::type::unfollow) + { + friends.erase(n.getUser().getID()); + } else if (n.getType() == twitter::notification::type::tweet) + { + if ( + (friends.count(n.getTweet().getAuthor().getID()) == 1) // Only monitor people you are following + && (!n.getTweet().isRetweet()) // Ignore retweets + && (n.getTweet().getText().front() != '@') // Ignore messages + ) + { + std::lock_guard stats_guard(stats_mutex); + + userstats& s = stats[n.getTweet().getAuthor().getID()]; + s.total++; + s.day1++; + + if (s.days >= 7) + { + int outof = s.total; + if (outof < 200) + { + outof = 200; + } + + if (rand() % outof == 0) + { + std::cout << "@" << n.getTweet().getAuthor().getScreenName() << "'s one of " << outof << "!" << std::endl; + + std::string doc = client.generateReplyPrefill(n.getTweet()) + "is this a subtweet?"; + twitter::tweet theTweet; + twitter::response resp = client.updateStatus(doc, theTweet, n.getTweet()); + if (resp != twitter::response::ok) + { + std::cout << "Error tweeting witty joke: " << resp << std::endl; + } + } + } + } + } else if (n.getType() == twitter::notification::type::followed) + { + twitter::response resp = client.follow(n.getUser()); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << resp << std::endl; + } + } + }); + + std::this_thread::sleep_for(std::chrono::minutes(1)); + + std::cout << "Starting streaming" << std::endl; + client.startUserStream(); + + for (;;) + { + std::this_thread::sleep_for(std::chrono::hours(24)); + + // Unfollow people who have unfollowed us + std::set friends; + std::set followers; + twitter::response resp = client.getFriends(friends); + if (resp == twitter::response::ok) + { + resp = client.getFollowers(followers); + if (resp == twitter::response::ok) + { + 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) + { + std::lock_guard friend_guard(friends_mutex); + friends.erase(f); + + resp = client.unfollow(f); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while unfollowing" << std::endl; + } + } + + for (auto f : new_followers) + { + resp = client.follow(f); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while following" << std::endl; + } + } + } else { + std::cout << "Twitter error while getting followers: " << resp << std::endl; + } + } else { + std::cout << "Twitter error while getting friends: " << resp << std::endl; + } + + // This is all just for stats rotation + { + std::lock_guard stats_guard(stats_mutex); + for (auto mapping : stats) + { + auto& s = mapping.second; + if (s.days < 7) + { + s.days++; + } + + s.day7 = s.day6; + s.day6 = s.day5; + s.day5 = s.day4; + s.day4 = s.day3; + s.day3 = s.day2; + s.day2 = s.day1; + s.day1 = 0; + + s.total = s.day2 + s.day3 + s.day4 + s.day5 + s.day6 + s.day7; + } + } + } +} diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 index 0000000..c3cc763 --- /dev/null +++ b/vendor/libtwittercpp @@ -0,0 +1 @@ +Subproject commit c3cc76301d64320791f7097709ffcd36d7206266 diff --git a/vendor/yaml-cpp b/vendor/yaml-cpp new file mode 160000 index 0000000..728e26e --- /dev/null +++ b/vendor/yaml-cpp @@ -0,0 +1 @@ +Subproject commit 728e26e42645d4d70ca65522990f915f47b47a50 -- cgit 1.4.1