about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--.gitmodules6
-rw-r--r--CMakeLists.txt13
-rw-r--r--image.jpgbin0 -> 7054 bytes
-rw-r--r--snitch.cpp106
m---------vendor/libtwittercpp0
m---------vendor/yaml-cpp0
6 files changed, 125 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..fe4b575 --- /dev/null +++ b/CMakeLists.txt
@@ -0,0 +1,13 @@
1cmake_minimum_required (VERSION 3.1)
2project (snitch)
3
4set(CMAKE_BUILD_TYPE Debug)
5
6add_subdirectory(vendor/libtwittercpp)
7add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL)
8
9include_directories(vendor/libtwittercpp/src vendor/libtwittercpp/vendor/json/src vendor/libtwittercpp/vendor/curlcpp/include vendor/yaml-cpp/include)
10add_executable(snitch snitch.cpp)
11set_property(TARGET snitch PROPERTY CXX_STANDARD 11)
12set_property(TARGET snitch PROPERTY CXX_STANDARD_REQUIRED ON)
13target_link_libraries(snitch yaml-cpp twitter++ curlcpp curl)
diff --git a/image.jpg b/image.jpg new file mode 100644 index 0000000..293982e --- /dev/null +++ b/image.jpg
Binary files 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 @@
1#include <yaml-cpp/yaml.h>
2#include <twitter.h>
3#include <fstream>
4#include <thread>
5#include <chrono>
6
7int main(int argc, char** argv)
8{
9 YAML::Node config = YAML::LoadFile("config.yml");
10
11 twitter::auth auth;
12 auth.setConsumerKey(config["consumer_key"].as<std::string>());
13 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
14 auth.setAccessKey(config["access_key"].as<std::string>());
15 auth.setAccessSecret(config["access_secret"].as<std::string>());
16
17 std::ifstream img_file("image.jpg");
18 img_file.seekg(0, std::ios::end);
19 size_t img_len = img_file.tellg();
20 char* img_buf = new char[img_len];
21 img_file.seekg(0, std::ios::beg);
22 img_file.read(img_buf, img_len);
23 img_file.close();
24
25 twitter::client client(auth);
26 client.setUserStreamNotifyCallback([&] (twitter::notification n) {
27 if (n.getType() == twitter::notification::type::tweet)
28 {
29 std::string orig = n.getTweet().getText();
30 std::string canonical;
31 std::transform(std::begin(orig), std::end(orig), std::back_inserter(canonical), [] (char ch) {
32 return std::tolower(ch);
33 });
34
35 if (canonical.find("calling the cops") != std::string::npos)
36 {
37 std::cout << "Calling the cops on @" << n.getTweet().getAuthor().getScreenName() << std::endl;
38
39 long media_id;
40 twitter::response resp = client.uploadMedia("image/jpeg", (const char*) img_buf, img_len, media_id);
41 if (resp != twitter::response::ok)
42 {
43 std::cout << "Twitter error while uploading image: " << resp << std::endl;
44 } else {
45 twitter::tweet tw;
46 resp = client.updateStatus("@" + n.getTweet().getAuthor().getScreenName(), tw, n.getTweet(), {media_id});
47 if (resp != twitter::response::ok)
48 {
49 std::cout << "Twitter error while tweeting: " << resp << std::endl;
50 }
51 }
52 }
53 } else if (n.getType() == twitter::notification::type::followed)
54 {
55 twitter::response resp = client.follow(n.getUser());
56 if (resp != twitter::response::ok)
57 {
58 std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << resp << std::endl;
59 }
60 }
61 });
62
63 std::this_thread::sleep_for(std::chrono::minutes(1));
64
65 std::cout << "Starting streaming" << std::endl;
66 client.startUserStream();
67 for (;;)
68 {
69 std::this_thread::sleep_for(std::chrono::hours(4));
70
71 std::set<twitter::user_id> friends;
72 std::set<twitter::user_id> followers;
73 twitter::response resp = client.getFriends(friends);
74 if (resp == twitter::response::ok)
75 {
76 resp = client.getFollowers(followers);
77 if (resp == twitter::response::ok)
78 {
79 auto frit = std::begin(friends);
80 auto foit = std::begin(followers);
81
82 while (frit != std::end(friends))
83 {
84 auto match = std::mismatch(frit, std::end(friends), foit);
85 if (match.first != std::end(friends))
86 {
87 resp = client.unfollow(*match.first);
88 if (resp != twitter::response::ok)
89 {
90 std::cout << "Twitter error while unfollowing" << std::endl;
91 }
92
93 frit = match.first;
94 frit++;
95 }
96 }
97 } else {
98 std::cout << "Twitter error while getting followers: " << resp << std::endl;
99 }
100 } else {
101 std::cout << "Twitter error while getting friends: " << resp << std::endl;
102 }
103 }
104
105 client.stopUserStream();
106}
diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000
Subproject 77b6ba9a45714b0b91b9ad782877bce5ca92a25
diff --git a/vendor/yaml-cpp b/vendor/yaml-cpp new file mode 160000
Subproject 728e26e42645d4d70ca65522990f915f47b47a5