From 38203b745ae1903ba0804ed5532b78d255bea635 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 5 Aug 2018 11:14:39 -0400 Subject: Added timeline polling Because the streaming API will sunset on August 16th, it is essential to implement timeline polling so that the library can still be used to access tweets. This commit breaks the current API even for clients still using the streaming API because the OAuth connection data was pulled from twitter::client into twitter::auth. Other than that, almost everything works the same, and if a client wants to update their libtwitter++ within the next week and a half, they are free to. --- src/timeline.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/timeline.cpp (limited to 'src/timeline.cpp') diff --git a/src/timeline.cpp b/src/timeline.cpp new file mode 100644 index 0000000..fd75a69 --- /dev/null +++ b/src/timeline.cpp @@ -0,0 +1,82 @@ +#include "timeline.h" +#include +#include +#include "codes.h" +#include "util.h" +#include "request.h" + +namespace twitter { + + timeline::timeline( + const auth& tauth, + std::string url) : + auth_(tauth), + url_(std::move(url)) + { + } + + std::list timeline::poll() + { + tweet_id maxId; + std::list result; + + for (int i = 0; i < 5; i++) + { + std::ostringstream urlstr; + urlstr << url_; + + std::list arguments; + + if (i > 0) + { + arguments.push_back("max_id=" + std::to_string(maxId)); + } + + if (hasSince_) + { + arguments.push_back("since_id=" + std::to_string(sinceId_)); + } + + if (!arguments.empty()) + { + urlstr << "?"; + urlstr << implode(std::begin(arguments), std::end(arguments), "&"); + } + + std::string theUrl = urlstr.str(); + std::string response = get(auth_, theUrl).perform(); + + try + { + nlohmann::json rjs = nlohmann::json::parse(response); + + if (rjs.empty()) + { + break; + } + + for (auto& single : rjs) + { + result.emplace_back(single.dump()); + } + } catch (const std::invalid_argument& error) + { + std::throw_with_nested(invalid_response(response)); + } catch (const std::domain_error& error) + { + std::throw_with_nested(invalid_response(response)); + } + + maxId = result.back().getID() - 1; + } + + if (!result.empty()) + { + sinceId_ = result.front().getID(); + hasSince_ = true; + } + + return result; + } + +}; -- cgit 1.4.1