From 7abdb05a93bf845bc48d820d1ec16a0adb583f28 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 20 May 2016 15:43:07 -0400 Subject: Initial commit --- .gitmodules | 6 +++ CMakeLists.txt | 13 +++++++ image.jpg | Bin 0 -> 7054 bytes snitch.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ vendor/libtwittercpp | 1 + vendor/yaml-cpp | 1 + 6 files changed, 127 insertions(+) create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 image.jpg create mode 100644 snitch.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..fe4b575 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required (VERSION 3.1) +project (snitch) + +set(CMAKE_BUILD_TYPE Debug) + +add_subdirectory(vendor/libtwittercpp) +add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) + +include_directories(vendor/libtwittercpp/src vendor/libtwittercpp/vendor/json/src vendor/libtwittercpp/vendor/curlcpp/include vendor/yaml-cpp/include) +add_executable(snitch snitch.cpp) +set_property(TARGET snitch PROPERTY CXX_STANDARD 11) +set_property(TARGET snitch PROPERTY CXX_STANDARD_REQUIRED ON) +target_link_libraries(snitch yaml-cpp twitter++ curlcpp curl) diff --git a/image.jpg b/image.jpg new file mode 100644 index 0000000..293982e Binary files /dev/null and b/image.jpg differ diff --git a/snitch.cpp b/snitch.cpp new file mode 100644 index 0000000..3fcb3d0 --- /dev/null +++ b/snitch.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + 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::ifstream img_file("image.jpg"); + img_file.seekg(0, std::ios::end); + size_t img_len = img_file.tellg(); + char* img_buf = new char[img_len]; + img_file.seekg(0, std::ios::beg); + img_file.read(img_buf, img_len); + img_file.close(); + + twitter::client client(auth); + client.setUserStreamNotifyCallback([&] (twitter::notification n) { + if (n.getType() == twitter::notification::type::tweet) + { + std::string orig = n.getTweet().getText(); + std::string canonical; + std::transform(std::begin(orig), std::end(orig), std::back_inserter(canonical), [] (char ch) { + return std::tolower(ch); + }); + + if (canonical.find("calling the cops") != std::string::npos) + { + std::cout << "Calling the cops on @" << n.getTweet().getAuthor().getScreenName() << std::endl; + + long media_id; + twitter::response resp = client.uploadMedia("image/jpeg", (const char*) img_buf, img_len, media_id); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while uploading image: " << resp << std::endl; + } else { + twitter::tweet tw; + resp = client.updateStatus("@" + n.getTweet().getAuthor().getScreenName(), tw, n.getTweet(), {media_id}); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while tweeting: " << 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(4)); + + 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) + { + auto frit = std::begin(friends); + auto foit = std::begin(followers); + + while (frit != std::end(friends)) + { + auto match = std::mismatch(frit, std::end(friends), foit); + if (match.first != std::end(friends)) + { + resp = client.unfollow(*match.first); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while unfollowing" << std::endl; + } + + frit = match.first; + frit++; + } + } + } else { + std::cout << "Twitter error while getting followers: " << resp << std::endl; + } + } else { + std::cout << "Twitter error while getting friends: " << resp << std::endl; + } + } + + client.stopUserStream(); +} diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 index 0000000..77b6ba9 --- /dev/null +++ b/vendor/libtwittercpp @@ -0,0 +1 @@ +Subproject commit 77b6ba9a45714b0b91b9ad782877bce5ca92a258 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