diff options
| -rw-r--r-- | .gitmodules | 6 | ||||
| -rw-r--r-- | CMakeLists.txt | 11 | ||||
| -rw-r--r-- | toldya.cpp | 181 | ||||
| m--------- | vendor/libtwittercpp | 0 | ||||
| m--------- | vendor/yaml-cpp | 0 |
5 files changed, 198 insertions, 0 deletions
| diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8544219 --- /dev/null +++ b/.gitmodules | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [submodule "vendor/libtwittercpp"] | ||
| 2 | path = vendor/libtwittercpp | ||
| 3 | url = https://github.com/hatkirby/libtwittercpp | ||
| 4 | [submodule "vendor/yaml-cpp"] | ||
| 5 | path = vendor/yaml-cpp | ||
| 6 | url = https://github.com/jbeder/yaml-cpp | ||
| diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..df9ccfe --- /dev/null +++ b/CMakeLists.txt | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | cmake_minimum_required (VERSION 3.1) | ||
| 2 | project (toldya) | ||
| 3 | |||
| 4 | add_subdirectory(vendor/libtwittercpp) | ||
| 5 | add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) | ||
| 6 | |||
| 7 | include_directories(vendor/libtwittercpp/src vendor/yaml-cpp/include) | ||
| 8 | add_executable(toldya toldya.cpp) | ||
| 9 | set_property(TARGET toldya PROPERTY CXX_STANDARD 11) | ||
| 10 | set_property(TARGET toldya PROPERTY CXX_STANDARD_REQUIRED ON) | ||
| 11 | target_link_libraries(toldya yaml-cpp twitter++) | ||
| diff --git a/toldya.cpp b/toldya.cpp new file mode 100644 index 0000000..96d5f35 --- /dev/null +++ b/toldya.cpp | |||
| @@ -0,0 +1,181 @@ | |||
| 1 | #include <twitter.h> | ||
| 2 | #include <yaml-cpp/yaml.h> | ||
| 3 | #include <mutex> | ||
| 4 | #include <thread> | ||
| 5 | #include <ctime> | ||
| 6 | #include <chrono> | ||
| 7 | #include <iostream> | ||
| 8 | |||
| 9 | int main(int argc, char** argv) | ||
| 10 | { | ||
| 11 | YAML::Node config = YAML::LoadFile("config.yml"); | ||
| 12 | |||
| 13 | twitter::auth auth; | ||
| 14 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | ||
| 15 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); | ||
| 16 | auth.setAccessKey(config["access_key"].as<std::string>()); | ||
| 17 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | ||
| 18 | |||
| 19 | std::vector<std::string> captions { | ||
| 20 | "It begins.", | ||
| 21 | "Yikes.", | ||
| 22 | "Frightening.", | ||
| 23 | "This is how it starts..." | ||
| 24 | }; | ||
| 25 | |||
| 26 | std::vector<twitter::tweet> potential; | ||
| 27 | std::mutex potential_mutex; | ||
| 28 | |||
| 29 | twitter::client client(auth); | ||
| 30 | std::set<twitter::user_id> streamed_friends; | ||
| 31 | client.setUserStreamNotifyCallback([&] (twitter::notification n) { | ||
| 32 | if (n.getType() == twitter::notification::type::friends) | ||
| 33 | { | ||
| 34 | streamed_friends = n.getFriends(); | ||
| 35 | } else if (n.getType() == twitter::notification::type::follow) | ||
| 36 | { | ||
| 37 | streamed_friends.insert(n.getUser().getID()); | ||
| 38 | } else if (n.getType() == twitter::notification::type::unfollow) | ||
| 39 | { | ||
| 40 | streamed_friends.erase(n.getUser().getID()); | ||
| 41 | } else if (n.getType() == twitter::notification::type::tweet) | ||
| 42 | { | ||
| 43 | if ( | ||
| 44 | (streamed_friends.count(n.getTweet().getAuthor().getID()) == 1) // Only monitor people you are following | ||
| 45 | && (!n.getTweet().isRetweet()) // Ignore retweets | ||
| 46 | && (n.getTweet().getText().front() != '@') // Ignore messages | ||
| 47 | ) | ||
| 48 | { | ||
| 49 | std::lock_guard<std::mutex> potential_guard(potential_mutex); | ||
| 50 | std::cout << n.getTweet().getText() << std::endl; | ||
| 51 | |||
| 52 | potential.push_back(n.getTweet()); | ||
| 53 | } | ||
| 54 | } else if (n.getType() == twitter::notification::type::followed) | ||
| 55 | { | ||
| 56 | twitter::response resp = client.follow(n.getUser()); | ||
| 57 | if (resp != twitter::response::ok) | ||
| 58 | { | ||
| 59 | std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << resp << std::endl; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | }); | ||
| 63 | |||
| 64 | std::this_thread::sleep_for(std::chrono::minutes(1)); | ||
| 65 | |||
| 66 | std::cout << "Starting streaming" << std::endl; | ||
| 67 | client.startUserStream(); | ||
| 68 | |||
| 69 | for (;;) | ||
| 70 | { | ||
| 71 | // Wait until 9am | ||
| 72 | auto midtime = time(NULL); | ||
| 73 | auto midtm = localtime(&midtime); | ||
| 74 | midtm->tm_hour = 0; | ||
| 75 | midtm->tm_min = 0; | ||
| 76 | midtm->tm_sec = 0; | ||
| 77 | auto to_until = std::chrono::system_clock::from_time_t(std::mktime(midtm)); | ||
| 78 | auto to_wait = std::chrono::duration_cast<std::chrono::seconds>((to_until + std::chrono::hours(24 + 9)) - std::chrono::system_clock::now()); | ||
| 79 | int waitlen = to_wait.count(); | ||
| 80 | if (waitlen == 0) | ||
| 81 | { | ||
| 82 | continue; | ||
| 83 | } else if (waitlen == 1) | ||
| 84 | { | ||
| 85 | std::cout << "Sleeping for 1 second..." << std::endl; | ||
| 86 | } else if (waitlen < 60) | ||
| 87 | { | ||
| 88 | std::cout << "Sleeping for " << waitlen << " seconds..." << std::endl; | ||
| 89 | } else if (waitlen == 60) | ||
| 90 | { | ||
| 91 | std::cout << "Sleeping for 1 minute..." << std::endl; | ||
| 92 | } else if (waitlen < 60*60) | ||
| 93 | { | ||
| 94 | std::cout << "Sleeping for " << (waitlen/60) << " minutes..." << std::endl; | ||
| 95 | } else if (waitlen == 60*60) | ||
| 96 | { | ||
| 97 | std::cout << "Sleeping for 1 hour..." << std::endl; | ||
| 98 | } else if (waitlen < 60*60*24) | ||
| 99 | { | ||
| 100 | std::cout << "Sleeping for " << (waitlen/60/60) << " hours..." << std::endl; | ||
| 101 | } else if (waitlen == 60*60*24) | ||
| 102 | { | ||
| 103 | std::cout << "Sleeping for 1 day..." << std::endl; | ||
| 104 | } else { | ||
| 105 | std::cout << "Sleeping for " << (waitlen/60/60/24) << " days..." << std::endl; | ||
| 106 | } | ||
| 107 | |||
| 108 | std::this_thread::sleep_for(to_wait); | ||
| 109 | |||
| 110 | // Unfollow people who have unfollowed us | ||
| 111 | std::set<twitter::user_id> friends; | ||
| 112 | std::set<twitter::user_id> followers; | ||
| 113 | twitter::response resp = client.getFriends(friends); | ||
| 114 | if (resp == twitter::response::ok) | ||
| 115 | { | ||
| 116 | resp = client.getFollowers(followers); | ||
| 117 | if (resp == twitter::response::ok) | ||
| 118 | { | ||
| 119 | std::list<twitter::user_id> old_friends, new_followers; | ||
| 120 | std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends)); | ||
| 121 | std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers)); | ||
| 122 | |||
| 123 | std::set<twitter::user_id> old_friends_set; | ||
| 124 | for (auto f : old_friends) | ||
| 125 | { | ||
| 126 | old_friends_set.insert(f); | ||
| 127 | |||
| 128 | resp = client.unfollow(f); | ||
| 129 | if (resp != twitter::response::ok) | ||
| 130 | { | ||
| 131 | std::cout << "Twitter error while unfollowing" << std::endl; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | for (auto f : new_followers) | ||
| 136 | { | ||
| 137 | resp = client.follow(f); | ||
| 138 | if (resp != twitter::response::ok) | ||
| 139 | { | ||
| 140 | std::cout << "Twitter error while following" << std::endl; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | std::lock_guard<std::mutex> potential_guard(potential_mutex); | ||
| 145 | std::vector<twitter::tweet> to_keep; | ||
| 146 | for (auto pt : potential) | ||
| 147 | { | ||
| 148 | if (old_friends_set.count(pt.getAuthor()) == 0) | ||
| 149 | { | ||
| 150 | to_keep.push_back(pt); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | potential = to_keep; | ||
| 155 | } else { | ||
| 156 | std::cout << "Twitter error while getting followers: " << resp << std::endl; | ||
| 157 | } | ||
| 158 | } else { | ||
| 159 | std::cout << "Twitter error while getting friends: " << resp << std::endl; | ||
| 160 | } | ||
| 161 | |||
| 162 | // Tweet! | ||
| 163 | if (!potential.empty()) | ||
| 164 | { | ||
| 165 | auto to_quote = potential[rand() % potential.size()]; | ||
| 166 | potential.clear(); | ||
| 167 | |||
| 168 | std::string caption = captions[rand() % captions.size()]; | ||
| 169 | std::string doc = caption + " " + to_quote.getURL(); | ||
| 170 | |||
| 171 | twitter::tweet sent; | ||
| 172 | twitter::response resp = client.updateStatus(doc, sent); | ||
| 173 | if (resp != twitter::response::ok) | ||
| 174 | { | ||
| 175 | std::cout << "Error tweeting: " << resp << std::endl; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | client.stopUserStream(); | ||
| 181 | } | ||
| diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 | |||
| Subproject c3cc76301d64320791f7097709ffcd36d720626 | |||
| diff --git a/vendor/yaml-cpp b/vendor/yaml-cpp new file mode 160000 | |||
| Subproject 728e26e42645d4d70ca65522990f915f47b47a5 | |||
