diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-05-20 15:43:07 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-05-20 15:43:07 -0400 |
commit | 7abdb05a93bf845bc48d820d1ec16a0adb583f28 (patch) | |
tree | 7fb6a05c88291456b3e6475e3a50bfe909f429ec /snitch.cpp | |
download | snitch-7abdb05a93bf845bc48d820d1ec16a0adb583f28.tar.gz snitch-7abdb05a93bf845bc48d820d1ec16a0adb583f28.tar.bz2 snitch-7abdb05a93bf845bc48d820d1ec16a0adb583f28.zip |
Initial commit
Diffstat (limited to 'snitch.cpp')
-rw-r--r-- | snitch.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
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 | |||
7 | int 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 | } | ||