diff options
Diffstat (limited to 'snitch.cpp')
-rw-r--r-- | snitch.cpp | 89 |
1 files changed, 36 insertions, 53 deletions
diff --git a/snitch.cpp b/snitch.cpp index 88d9d3b..700a0a9 100644 --- a/snitch.cpp +++ b/snitch.cpp | |||
@@ -29,10 +29,14 @@ int main(int argc, char** argv) | |||
29 | "calling the police" | 29 | "calling the police" |
30 | }; | 30 | }; |
31 | 31 | ||
32 | // Initialize the client | ||
32 | twitter::client client(auth); | 33 | twitter::client client(auth); |
34 | std::this_thread::sleep_for(std::chrono::minutes(1)); | ||
35 | |||
36 | // Start streaming | ||
37 | std::cout << "Starting streaming" << std::endl; | ||
33 | std::set<twitter::user_id> streamed_friends; | 38 | std::set<twitter::user_id> streamed_friends; |
34 | client.setUserStreamReceiveAllReplies(true); | 39 | twitter::stream userStream(client, [&] (const twitter::notification& n) { |
35 | client.setUserStreamNotifyCallback([&] (twitter::notification n) { | ||
36 | if (n.getType() == twitter::notification::type::friends) | 40 | if (n.getType() == twitter::notification::type::friends) |
37 | { | 41 | { |
38 | streamed_friends = n.getFriends(); | 42 | streamed_friends = n.getFriends(); |
@@ -59,18 +63,13 @@ int main(int argc, char** argv) | |||
59 | { | 63 | { |
60 | std::cout << "Calling the cops on @" << n.getTweet().getAuthor().getScreenName() << std::endl; | 64 | std::cout << "Calling the cops on @" << n.getTweet().getAuthor().getScreenName() << std::endl; |
61 | 65 | ||
62 | long media_id; | 66 | try |
63 | twitter::response resp = client.uploadMedia("image/jpeg", (const char*) img_buf, img_len, media_id); | ||
64 | if (resp != twitter::response::ok) | ||
65 | { | 67 | { |
66 | std::cout << "Twitter error while uploading image: " << resp << std::endl; | 68 | long media_id = client.uploadMedia("image/jpeg", (const char*) img_buf, img_len); |
67 | } else { | 69 | client.replyToTweet(n.getTweet().generateReplyPrefill(), n.getTweet().getID(), {media_id}); |
68 | twitter::tweet tw; | 70 | } catch (const twitter::twitter_error& e) |
69 | resp = client.updateStatus(client.generateReplyPrefill(n.getTweet()), tw, n.getTweet(), {media_id}); | 71 | { |
70 | if (resp != twitter::response::ok) | 72 | std::cout << "Twitter error: " << e.what() << std::endl; |
71 | { | ||
72 | std::cout << "Twitter error while tweeting: " << resp << std::endl; | ||
73 | } | ||
74 | } | 73 | } |
75 | 74 | ||
76 | break; | 75 | break; |
@@ -79,59 +78,43 @@ int main(int argc, char** argv) | |||
79 | } | 78 | } |
80 | } else if (n.getType() == twitter::notification::type::followed) | 79 | } else if (n.getType() == twitter::notification::type::followed) |
81 | { | 80 | { |
82 | twitter::response resp = client.follow(n.getUser()); | 81 | try |
83 | if (resp != twitter::response::ok) | 82 | { |
83 | n.getUser().follow(); | ||
84 | } catch (const twitter::twitter_error& e) | ||
84 | { | 85 | { |
85 | std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << resp << std::endl; | 86 | std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << e.what() << std::endl; |
86 | } | 87 | } |
87 | } | 88 | } |
88 | }); | 89 | }, true, true); |
89 | 90 | ||
90 | std::this_thread::sleep_for(std::chrono::minutes(1)); | ||
91 | |||
92 | std::cout << "Starting streaming" << std::endl; | ||
93 | client.startUserStream(); | ||
94 | for (;;) | 91 | for (;;) |
95 | { | 92 | { |
96 | std::this_thread::sleep_for(std::chrono::minutes(1)); | 93 | std::this_thread::sleep_for(std::chrono::minutes(1)); |
97 | 94 | ||
98 | std::set<twitter::user_id> friends; | 95 | try |
99 | std::set<twitter::user_id> followers; | ||
100 | twitter::response resp = client.getFriends(friends); | ||
101 | if (resp == twitter::response::ok) | ||
102 | { | 96 | { |
103 | resp = client.getFollowers(followers); | 97 | std::set<twitter::user_id> friends = client.getUser().getFriends(); |
104 | if (resp == twitter::response::ok) | 98 | std::set<twitter::user_id> followers = client.getUser().getFollowers(); |
99 | |||
100 | std::list<twitter::user_id> old_friends, new_followers; | ||
101 | std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends)); | ||
102 | std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers)); | ||
103 | |||
104 | for (auto f : old_friends) | ||
105 | { | 105 | { |
106 | std::list<twitter::user_id> old_friends, new_followers; | 106 | client.unfollow(f); |
107 | std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends)); | 107 | } |
108 | std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers)); | 108 | |
109 | for (auto f : old_friends) | 109 | for (auto f : new_followers) |
110 | { | 110 | { |
111 | resp = client.unfollow(f); | 111 | client.follow(f); |
112 | if (resp != twitter::response::ok) | ||
113 | { | ||
114 | std::cout << "Twitter error while unfollowing" << std::endl; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | for (auto f : new_followers) | ||
119 | { | ||
120 | resp = client.follow(f); | ||
121 | if (resp != twitter::response::ok) | ||
122 | { | ||
123 | std::cout << "Twitter error while following" << std::endl; | ||
124 | } | ||
125 | } | ||
126 | } else { | ||
127 | std::cout << "Twitter error while getting followers: " << resp << std::endl; | ||
128 | } | 112 | } |
129 | } else { | 113 | } catch (const twitter::twitter_error& e) |
130 | std::cout << "Twitter error while getting friends: " << resp << std::endl; | 114 | { |
115 | std::cout << "Twitter error: " << e.what() << std::endl; | ||
131 | } | 116 | } |
132 | 117 | ||
133 | std::this_thread::sleep_for(std::chrono::hours(4)); | 118 | std::this_thread::sleep_for(std::chrono::hours(4)); |
134 | } | 119 | } |
135 | |||
136 | client.stopUserStream(); | ||
137 | } | 120 | } |