diff options
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 | }; | ||
