diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-08-05 15:58:14 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-08-05 15:58:14 -0400 |
commit | b7bb942cadfe3d657895af1557b78acc2559947e (patch) | |
tree | 66ec390ba1b30c4d3012c3ec3526f9489c41d8f9 /src/tweet.cpp | |
parent | 204181c5654cee745b6b1ba98675609e784f0ee1 (diff) | |
download | libtwittercpp-b7bb942cadfe3d657895af1557b78acc2559947e.tar.gz libtwittercpp-b7bb942cadfe3d657895af1557b78acc2559947e.tar.bz2 libtwittercpp-b7bb942cadfe3d657895af1557b78acc2559947e.zip |
Tweets store their created time
C/C++ time handling is awful, pass it on
Diffstat (limited to 'src/tweet.cpp')
-rw-r--r-- | src/tweet.cpp | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/src/tweet.cpp b/src/tweet.cpp index 1c869f9..1003647 100644 --- a/src/tweet.cpp +++ b/src/tweet.cpp | |||
@@ -1,11 +1,12 @@ | |||
1 | #include "tweet.h" | 1 | #include "tweet.h" |
2 | #include <json.hpp> | 2 | #include <json.hpp> |
3 | #include <sstream> | ||
3 | #include "util.h" | 4 | #include "util.h" |
4 | #include "codes.h" | 5 | #include "codes.h" |
5 | #include "client.h" | 6 | #include "client.h" |
6 | 7 | ||
7 | namespace twitter { | 8 | namespace twitter { |
8 | 9 | ||
9 | tweet::tweet(std::string data) try | 10 | tweet::tweet(std::string data) try |
10 | : _valid(true) | 11 | : _valid(true) |
11 | { | 12 | { |
@@ -13,14 +14,20 @@ namespace twitter { | |||
13 | _id = json["id"].get<tweet_id>(); | 14 | _id = json["id"].get<tweet_id>(); |
14 | _text = json["text"].get<std::string>(); | 15 | _text = json["text"].get<std::string>(); |
15 | _author = std::make_unique<user>(json["user"].dump()); | 16 | _author = std::make_unique<user>(json["user"].dump()); |
16 | 17 | ||
18 | std::tm ctt = { 0 }; | ||
19 | std::stringstream createdAtStream; | ||
20 | createdAtStream << json["created_at"].get<std::string>(); | ||
21 | createdAtStream >> std::get_time(&ctt, "%a %b %d %H:%M:%S +0000 %Y"); | ||
22 | _created_at = twitter::timegm(&ctt); | ||
23 | |||
17 | if (!json["retweeted_status"].is_null()) | 24 | if (!json["retweeted_status"].is_null()) |
18 | { | 25 | { |
19 | _is_retweet = true; | 26 | _is_retweet = true; |
20 | 27 | ||
21 | _retweeted_status = std::make_unique<tweet>(json["retweeted_status"].dump()); | 28 | _retweeted_status = std::make_unique<tweet>(json["retweeted_status"].dump()); |
22 | } | 29 | } |
23 | 30 | ||
24 | if (!json["entities"].is_null()) | 31 | if (!json["entities"].is_null()) |
25 | { | 32 | { |
26 | auto entities = json["entities"]; | 33 | auto entities = json["entities"]; |
@@ -39,7 +46,7 @@ namespace twitter { | |||
39 | { | 46 | { |
40 | std::throw_with_nested(malformed_object("tweet", data)); | 47 | std::throw_with_nested(malformed_object("tweet", data)); |
41 | } | 48 | } |
42 | 49 | ||
43 | tweet::tweet(const tweet& other) | 50 | tweet::tweet(const tweet& other) |
44 | { | 51 | { |
45 | _valid = other._valid; | 52 | _valid = other._valid; |
@@ -47,27 +54,27 @@ namespace twitter { | |||
47 | _text = other._text; | 54 | _text = other._text; |
48 | _author = std::make_unique<user>(*other._author); | 55 | _author = std::make_unique<user>(*other._author); |
49 | _is_retweet = other._is_retweet; | 56 | _is_retweet = other._is_retweet; |
50 | 57 | ||
51 | if (_is_retweet) | 58 | if (_is_retweet) |
52 | { | 59 | { |
53 | _retweeted_status = std::make_unique<tweet>(*other._retweeted_status); | 60 | _retweeted_status = std::make_unique<tweet>(*other._retweeted_status); |
54 | } | 61 | } |
55 | 62 | ||
56 | _mentions = other._mentions; | 63 | _mentions = other._mentions; |
57 | } | 64 | } |
58 | 65 | ||
59 | tweet::tweet(tweet&& other) : tweet() | 66 | tweet::tweet(tweet&& other) : tweet() |
60 | { | 67 | { |
61 | swap(*this, other); | 68 | swap(*this, other); |
62 | } | 69 | } |
63 | 70 | ||
64 | tweet& tweet::operator=(tweet other) | 71 | tweet& tweet::operator=(tweet other) |
65 | { | 72 | { |
66 | swap(*this, other); | 73 | swap(*this, other); |
67 | 74 | ||
68 | return *this; | 75 | return *this; |
69 | } | 76 | } |
70 | 77 | ||
71 | void swap(tweet& first, tweet& second) | 78 | void swap(tweet& first, tweet& second) |
72 | { | 79 | { |
73 | std::swap(first._valid, second._valid); | 80 | std::swap(first._valid, second._valid); |
@@ -78,12 +85,12 @@ namespace twitter { | |||
78 | std::swap(first._retweeted_status, second._retweeted_status); | 85 | std::swap(first._retweeted_status, second._retweeted_status); |
79 | std::swap(first._mentions, second._mentions); | 86 | std::swap(first._mentions, second._mentions); |
80 | } | 87 | } |
81 | 88 | ||
82 | std::string tweet::generateReplyPrefill(const user& me) const | 89 | std::string tweet::generateReplyPrefill(const user& me) const |
83 | { | 90 | { |
84 | std::ostringstream output; | 91 | std::ostringstream output; |
85 | output << "@" << _author->getScreenName() << " "; | 92 | output << "@" << _author->getScreenName() << " "; |
86 | 93 | ||
87 | for (auto mention : _mentions) | 94 | for (auto mention : _mentions) |
88 | { | 95 | { |
89 | if ((mention.first != _author->getID()) && (mention.first != me.getID())) | 96 | if ((mention.first != _author->getID()) && (mention.first != me.getID())) |
@@ -91,21 +98,21 @@ namespace twitter { | |||
91 | output << "@" << mention.second << " "; | 98 | output << "@" << mention.second << " "; |
92 | } | 99 | } |
93 | } | 100 | } |
94 | 101 | ||
95 | return output.str(); | 102 | return output.str(); |
96 | } | 103 | } |
97 | 104 | ||
98 | std::string tweet::getURL() const | 105 | std::string tweet::getURL() const |
99 | { | 106 | { |
100 | assert(_valid); | 107 | assert(_valid); |
101 | 108 | ||
102 | std::ostringstream urlstr; | 109 | std::ostringstream urlstr; |
103 | urlstr << "https://twitter.com/"; | 110 | urlstr << "https://twitter.com/"; |
104 | urlstr << _author->getScreenName(); | 111 | urlstr << _author->getScreenName(); |
105 | urlstr << "/statuses/"; | 112 | urlstr << "/statuses/"; |
106 | urlstr << _id; | 113 | urlstr << _id; |
107 | 114 | ||
108 | return urlstr.str(); | 115 | return urlstr.str(); |
109 | } | 116 | } |
110 | 117 | ||
111 | }; | 118 | }; |