about summary refs log tree commit diff stats
path: root/snitch.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'snitch.cpp')
-rw-r--r--snitch.cpp82
1 files changed, 50 insertions, 32 deletions
diff --git a/snitch.cpp b/snitch.cpp index 3fcb3d0..2357a7d 100644 --- a/snitch.cpp +++ b/snitch.cpp
@@ -23,30 +23,44 @@ int main(int argc, char** argv)
23 img_file.close(); 23 img_file.close();
24 24
25 twitter::client client(auth); 25 twitter::client client(auth);
26 std::set<twitter::user_id> streamed_friends;
26 client.setUserStreamNotifyCallback([&] (twitter::notification n) { 27 client.setUserStreamNotifyCallback([&] (twitter::notification n) {
27 if (n.getType() == twitter::notification::type::tweet) 28 if (n.getType() == twitter::notification::type::friends)
28 { 29 {
29 std::string orig = n.getTweet().getText(); 30 streamed_friends = n.getFriends();
30 std::string canonical; 31 } else if (n.getType() == twitter::notification::type::follow)
31 std::transform(std::begin(orig), std::end(orig), std::back_inserter(canonical), [] (char ch) { 32 {
32 return std::tolower(ch); 33 streamed_friends.insert(n.getUser().getID());
33 }); 34 } else if (n.getType() == twitter::notification::type::unfollow)
34 35 {
35 if (canonical.find("calling the cops") != std::string::npos) 36 streamed_friends.erase(n.getUser().getID());
37 } else if (n.getType() == twitter::notification::type::tweet)
38 {
39 // Only monitor people you are following
40 if (streamed_friends.count(n.getTweet().getAuthor().getID()) == 1)
36 { 41 {
37 std::cout << "Calling the cops on @" << n.getTweet().getAuthor().getScreenName() << std::endl; 42 std::string orig = n.getTweet().getText();
38 43 std::string canonical;
39 long media_id; 44 std::transform(std::begin(orig), std::end(orig), std::back_inserter(canonical), [] (char ch) {
40 twitter::response resp = client.uploadMedia("image/jpeg", (const char*) img_buf, img_len, media_id); 45 return std::tolower(ch);
41 if (resp != twitter::response::ok) 46 });
47
48 if (canonical.find("calling the cops") != std::string::npos)
42 { 49 {
43 std::cout << "Twitter error while uploading image: " << resp << std::endl; 50 std::cout << "Calling the cops on @" << n.getTweet().getAuthor().getScreenName() << std::endl;
44 } else { 51
45 twitter::tweet tw; 52 long media_id;
46 resp = client.updateStatus("@" + n.getTweet().getAuthor().getScreenName(), tw, n.getTweet(), {media_id}); 53 twitter::response resp = client.uploadMedia("image/jpeg", (const char*) img_buf, img_len, media_id);
47 if (resp != twitter::response::ok) 54 if (resp != twitter::response::ok)
48 { 55 {
49 std::cout << "Twitter error while tweeting: " << resp << std::endl; 56 std::cout << "Twitter error while uploading image: " << resp << std::endl;
57 } else {
58 twitter::tweet tw;
59 resp = client.updateStatus("@" + n.getTweet().getAuthor().getScreenName(), tw, n.getTweet(), {media_id});
60 if (resp != twitter::response::ok)
61 {
62 std::cout << "Twitter error while tweeting: " << resp << std::endl;
63 }
50 } 64 }
51 } 65 }
52 } 66 }
@@ -66,7 +80,7 @@ int main(int argc, char** argv)
66 client.startUserStream(); 80 client.startUserStream();
67 for (;;) 81 for (;;)
68 { 82 {
69 std::this_thread::sleep_for(std::chrono::hours(4)); 83 std::this_thread::sleep_for(std::chrono::minutes(1));
70 84
71 std::set<twitter::user_id> friends; 85 std::set<twitter::user_id> friends;
72 std::set<twitter::user_id> followers; 86 std::set<twitter::user_id> followers;
@@ -76,22 +90,24 @@ int main(int argc, char** argv)
76 resp = client.getFollowers(followers); 90 resp = client.getFollowers(followers);
77 if (resp == twitter::response::ok) 91 if (resp == twitter::response::ok)
78 { 92 {
79 auto frit = std::begin(friends); 93 std::list<twitter::user_id> old_friends, new_followers;
80 auto foit = std::begin(followers); 94 std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends));
95 std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers));
96 for (auto f : old_friends)
97 {
98 resp = client.unfollow(f);
99 if (resp != twitter::response::ok)
100 {
101 std::cout << "Twitter error while unfollowing" << std::endl;
102 }
103 }
81 104
82 while (frit != std::end(friends)) 105 for (auto f : new_followers)
83 { 106 {
84 auto match = std::mismatch(frit, std::end(friends), foit); 107 resp = client.follow(f);
85 if (match.first != std::end(friends)) 108 if (resp != twitter::response::ok)
86 { 109 {
87 resp = client.unfollow(*match.first); 110 std::cout << "Twitter error while following" << std::endl;
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 } 111 }
96 } 112 }
97 } else { 113 } else {
@@ -100,6 +116,8 @@ int main(int argc, char** argv)
100 } else { 116 } else {
101 std::cout << "Twitter error while getting friends: " << resp << std::endl; 117 std::cout << "Twitter error while getting friends: " << resp << std::endl;
102 } 118 }
119
120 std::this_thread::sleep_for(std::chrono::hours(4));
103 } 121 }
104 122
105 client.stopUserStream(); 123 client.stopUserStream();