diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-08-05 11:14:39 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-08-05 11:14:39 -0400 |
commit | 38203b745ae1903ba0804ed5532b78d255bea635 (patch) | |
tree | 52f57ce85e65361c534c461abc82fb1ed9768d49 /src/timeline.cpp | |
parent | 7c44fd17bb6be54a2ea4b60761e91053ca988977 (diff) | |
download | libtwittercpp-38203b745ae1903ba0804ed5532b78d255bea635.tar.gz libtwittercpp-38203b745ae1903ba0804ed5532b78d255bea635.tar.bz2 libtwittercpp-38203b745ae1903ba0804ed5532b78d255bea635.zip |
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.
Diffstat (limited to 'src/timeline.cpp')
-rw-r--r-- | src/timeline.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
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 @@ | |||
1 | #include "timeline.h" | ||
2 | #include <sstream> | ||
3 | #include <json.hpp> | ||
4 | #include "codes.h" | ||
5 | #include "util.h" | ||
6 | #include "request.h" | ||
7 | |||
8 | namespace twitter { | ||
9 | |||
10 | timeline::timeline( | ||
11 | const auth& tauth, | ||
12 | std::string url) : | ||
13 | auth_(tauth), | ||
14 | url_(std::move(url)) | ||
15 | { | ||
16 | } | ||
17 | |||
18 | std::list<tweet> timeline::poll() | ||
19 | { | ||
20 | tweet_id maxId; | ||
21 | std::list<tweet> result; | ||
22 | |||
23 | for (int i = 0; i < 5; i++) | ||
24 | { | ||
25 | std::ostringstream urlstr; | ||
26 | urlstr << url_; | ||
27 | |||
28 | std::list<std::string> arguments; | ||
29 | |||
30 | if (i > 0) | ||
31 | { | ||
32 | arguments.push_back("max_id=" + std::to_string(maxId)); | ||
33 | } | ||
34 | |||
35 | if (hasSince_) | ||
36 | { | ||
37 | arguments.push_back("since_id=" + std::to_string(sinceId_)); | ||
38 | } | ||
39 | |||
40 | if (!arguments.empty()) | ||
41 | { | ||
42 | urlstr << "?"; | ||
43 | urlstr << implode(std::begin(arguments), std::end(arguments), "&"); | ||
44 | } | ||
45 | |||
46 | std::string theUrl = urlstr.str(); | ||
47 | std::string response = get(auth_, theUrl).perform(); | ||
48 | |||
49 | try | ||
50 | { | ||
51 | nlohmann::json rjs = nlohmann::json::parse(response); | ||
52 | |||
53 | if (rjs.empty()) | ||
54 | { | ||
55 | break; | ||
56 | } | ||
57 | |||
58 | for (auto& single : rjs) | ||
59 | { | ||
60 | result.emplace_back(single.dump()); | ||
61 | } | ||
62 | } catch (const std::invalid_argument& error) | ||
63 | { | ||
64 | std::throw_with_nested(invalid_response(response)); | ||
65 | } catch (const std::domain_error& error) | ||
66 | { | ||
67 | std::throw_with_nested(invalid_response(response)); | ||
68 | } | ||
69 | |||
70 | maxId = result.back().getID() - 1; | ||
71 | } | ||
72 | |||
73 | if (!result.empty()) | ||
74 | { | ||
75 | sinceId_ = result.front().getID(); | ||
76 | hasSince_ = true; | ||
77 | } | ||
78 | |||
79 | return result; | ||
80 | } | ||
81 | |||
82 | }; | ||