diff options
| -rw-r--r-- | toldya.cpp | 111 | ||||
| m--------- | vendor/libtwittercpp | 0 |
2 files changed, 57 insertions, 54 deletions
| diff --git a/toldya.cpp b/toldya.cpp index 2df49eb..285b82a 100644 --- a/toldya.cpp +++ b/toldya.cpp | |||
| @@ -29,7 +29,10 @@ int main(int argc, char** argv) | |||
| 29 | 29 | ||
| 30 | twitter::client client(auth); | 30 | twitter::client client(auth); |
| 31 | std::set<twitter::user_id> streamed_friends; | 31 | std::set<twitter::user_id> streamed_friends; |
| 32 | client.setUserStreamNotifyCallback([&] (twitter::notification n) { | 32 | |
| 33 | std::cout << "Starting streaming" << std::endl; | ||
| 34 | |||
| 35 | twitter::stream user_stream(client, [&] (twitter::notification n) { | ||
| 33 | if (n.getType() == twitter::notification::type::friends) | 36 | if (n.getType() == twitter::notification::type::friends) |
| 34 | { | 37 | { |
| 35 | streamed_friends = n.getFriends(); | 38 | streamed_friends = n.getFriends(); |
| @@ -50,23 +53,22 @@ int main(int argc, char** argv) | |||
| 50 | std::lock_guard<std::mutex> potential_guard(potential_mutex); | 53 | std::lock_guard<std::mutex> potential_guard(potential_mutex); |
| 51 | std::cout << n.getTweet().getText() << std::endl; | 54 | std::cout << n.getTweet().getText() << std::endl; |
| 52 | 55 | ||
| 53 | potential.push_back(n.getTweet()); | 56 | potential.push_back(std::move(n.getTweet())); |
| 54 | } | 57 | } |
| 55 | } else if (n.getType() == twitter::notification::type::followed) | 58 | } else if (n.getType() == twitter::notification::type::followed) |
| 56 | { | 59 | { |
| 57 | twitter::response resp = client.follow(n.getUser()); | 60 | try |
| 58 | if (resp != twitter::response::ok) | 61 | { |
| 62 | client.follow(n.getUser()); | ||
| 63 | } catch (const twitter::twitter_error& error) | ||
| 59 | { | 64 | { |
| 60 | std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << resp << std::endl; | 65 | std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << error.what() << std::endl; |
| 61 | } | 66 | } |
| 62 | } | 67 | } |
| 63 | }); | 68 | }); |
| 64 | 69 | ||
| 65 | std::this_thread::sleep_for(std::chrono::minutes(1)); | 70 | std::this_thread::sleep_for(std::chrono::minutes(1)); |
| 66 | 71 | ||
| 67 | std::cout << "Starting streaming" << std::endl; | ||
| 68 | client.startUserStream(); | ||
| 69 | |||
| 70 | for (;;) | 72 | for (;;) |
| 71 | { | 73 | { |
| 72 | // Wait until 9am | 74 | // Wait until 9am |
| @@ -109,74 +111,75 @@ int main(int argc, char** argv) | |||
| 109 | std::this_thread::sleep_for(to_wait); | 111 | std::this_thread::sleep_for(to_wait); |
| 110 | 112 | ||
| 111 | // Unfollow people who have unfollowed us | 113 | // Unfollow people who have unfollowed us |
| 112 | std::set<twitter::user_id> friends; | 114 | try |
| 113 | std::set<twitter::user_id> followers; | ||
| 114 | twitter::response resp = client.getFriends(friends); | ||
| 115 | if (resp == twitter::response::ok) | ||
| 116 | { | 115 | { |
| 117 | resp = client.getFollowers(followers); | 116 | std::set<twitter::user_id> friends = client.getFriends(); |
| 118 | if (resp == twitter::response::ok) | 117 | std::set<twitter::user_id> followers = client.getFollowers(); |
| 118 | |||
| 119 | std::list<twitter::user_id> old_friends; | ||
| 120 | std::list<twitter::user_id> new_followers; | ||
| 121 | std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends)); | ||
| 122 | std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers)); | ||
| 123 | |||
| 124 | std::set<twitter::user_id> old_friends_set; | ||
| 125 | for (auto f : old_friends) | ||
| 119 | { | 126 | { |
| 120 | std::list<twitter::user_id> old_friends, new_followers; | 127 | old_friends_set.insert(f); |
| 121 | std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends)); | ||
| 122 | std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers)); | ||
| 123 | 128 | ||
| 124 | std::set<twitter::user_id> old_friends_set; | 129 | try |
| 125 | for (auto f : old_friends) | 130 | { |
| 131 | client.unfollow(f); | ||
| 132 | } catch (const twitter::twitter_error& error) | ||
| 126 | { | 133 | { |
| 127 | old_friends_set.insert(f); | 134 | std::cout << "Twitter error while unfollowing: " << error.what() << std::endl; |
| 128 | |||
| 129 | resp = client.unfollow(f); | ||
| 130 | if (resp != twitter::response::ok) | ||
| 131 | { | ||
| 132 | std::cout << "Twitter error while unfollowing" << std::endl; | ||
| 133 | } | ||
| 134 | } | 135 | } |
| 135 | 136 | } | |
| 136 | for (auto f : new_followers) | 137 | |
| 138 | for (auto f : new_followers) | ||
| 139 | { | ||
| 140 | try | ||
| 137 | { | 141 | { |
| 138 | resp = client.follow(f); | 142 | client.follow(f); |
| 139 | if (resp != twitter::response::ok) | 143 | } catch (const twitter::twitter_error& error) |
| 140 | { | 144 | { |
| 141 | std::cout << "Twitter error while following" << std::endl; | 145 | std::cout << "Twitter error while following: " << error.what() << std::endl; |
| 142 | } | ||
| 143 | } | 146 | } |
| 144 | 147 | } | |
| 145 | std::lock_guard<std::mutex> potential_guard(potential_mutex); | 148 | |
| 146 | std::vector<twitter::tweet> to_keep; | 149 | std::lock_guard<std::mutex> potential_guard(potential_mutex); |
| 147 | for (auto pt : potential) | 150 | std::vector<twitter::tweet> to_keep; |
| 151 | for (auto& pt : potential) | ||
| 152 | { | ||
| 153 | if (old_friends_set.count(pt.getAuthor().getID()) == 0) | ||
| 148 | { | 154 | { |
| 149 | if (old_friends_set.count(pt.getAuthor()) == 0) | 155 | to_keep.push_back(std::move(pt)); |
| 150 | { | ||
| 151 | to_keep.push_back(pt); | ||
| 152 | } | ||
| 153 | } | 156 | } |
| 154 | |||
| 155 | potential = to_keep; | ||
| 156 | } else { | ||
| 157 | std::cout << "Twitter error while getting followers: " << resp << std::endl; | ||
| 158 | } | 157 | } |
| 159 | } else { | 158 | |
| 160 | std::cout << "Twitter error while getting friends: " << resp << std::endl; | 159 | potential = std::move(to_keep); |
| 160 | } catch (const twitter::twitter_error& error) | ||
| 161 | { | ||
| 162 | std::cout << "Twitter error while getting friends/followers: " << error.what() << std::endl; | ||
| 161 | } | 163 | } |
| 162 | 164 | ||
| 163 | // Tweet! | 165 | // Tweet! |
| 164 | if (!potential.empty()) | 166 | if (!potential.empty()) |
| 165 | { | 167 | { |
| 166 | auto to_quote = potential[rand() % potential.size()]; | 168 | auto to_quote = std::move(potential[rand() % potential.size()]); |
| 167 | potential.clear(); | 169 | potential.clear(); |
| 168 | 170 | ||
| 169 | std::string caption = captions[rand() % captions.size()]; | 171 | std::string caption = captions[rand() % captions.size()]; |
| 170 | std::string doc = caption + " " + to_quote.getURL(); | 172 | std::string doc = caption + " " + to_quote.getURL(); |
| 171 | 173 | ||
| 172 | twitter::tweet sent; | 174 | try |
| 173 | twitter::response resp = client.updateStatus(doc, sent); | ||
| 174 | if (resp != twitter::response::ok) | ||
| 175 | { | 175 | { |
| 176 | std::cout << "Error tweeting: " << resp << std::endl; | 176 | client.updateStatus(doc); |
| 177 | |||
| 178 | std::cout << "Tweeted!" << std::endl; | ||
| 179 | } catch (const twitter::twitter_error& error) | ||
| 180 | { | ||
| 181 | std::cout << "Error tweeting: " << error.what() << std::endl; | ||
| 177 | } | 182 | } |
| 178 | } | 183 | } |
| 179 | } | 184 | } |
| 180 | |||
| 181 | client.stopUserStream(); | ||
| 182 | } | 185 | } |
| diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp | |||
| Subproject c3cc76301d64320791f7097709ffcd36d720626 | Subproject 7c44fd17bb6be54a2ea4b60761e91053ca98897 | ||
