diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/client.cpp | 16 | ||||
-rw-r--r-- | src/client.h | 6 | ||||
-rw-r--r-- | src/configuration.cpp | 109 | ||||
-rw-r--r-- | src/configuration.h | 52 | ||||
-rw-r--r-- | src/twitter.h | 1 |
6 files changed, 185 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 29a0a75..a0a1b0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -10,7 +10,7 @@ include_directories(vendor/liboauthcpp/include) | |||
10 | add_subdirectory(vendor/curlcpp) | 10 | add_subdirectory(vendor/curlcpp) |
11 | include_directories(${CURLCPP_SOURCE_DIR}/include) | 11 | include_directories(${CURLCPP_SOURCE_DIR}/include) |
12 | 12 | ||
13 | add_library(twitter++ src/client.cpp src/auth.cpp src/tweet.cpp src/codes.cpp src/notification.cpp src/direct_message.cpp src/list.cpp src/user.cpp) | 13 | add_library(twitter++ src/client.cpp src/auth.cpp src/tweet.cpp src/codes.cpp src/notification.cpp src/direct_message.cpp src/list.cpp src/user.cpp src/configuration.cpp) |
14 | set_property(TARGET twitter++ PROPERTY CXX_STANDARD 11) | 14 | set_property(TARGET twitter++ PROPERTY CXX_STANDARD 11) |
15 | set_property(TARGET twitter++ PROPERTY CXX_STANDARD_REQUIRED ON) | 15 | set_property(TARGET twitter++ PROPERTY CXX_STANDARD_REQUIRED ON) |
16 | target_link_libraries(twitter++ oauthcpp curlcpp curl pthread) | 16 | target_link_libraries(twitter++ oauthcpp curlcpp curl pthread) |
diff --git a/src/client.cpp b/src/client.cpp index b7d1624..cf2b5c4 100644 --- a/src/client.cpp +++ b/src/client.cpp | |||
@@ -321,6 +321,22 @@ namespace twitter { | |||
321 | return _current_user; | 321 | return _current_user; |
322 | } | 322 | } |
323 | 323 | ||
324 | configuration client::getConfiguration() | ||
325 | { | ||
326 | if (!_configuration || (difftime(time(NULL), _last_configuration_update) > 60*60*24)) | ||
327 | { | ||
328 | long response_code; | ||
329 | std::string response_data; | ||
330 | if (performGet("https://api.twitter.com/1.1/help/configuration.json", response_code, response_data)) | ||
331 | { | ||
332 | _configuration = configuration(response_data); | ||
333 | _last_configuration_update = time(NULL); | ||
334 | } | ||
335 | } | ||
336 | |||
337 | return _configuration; | ||
338 | } | ||
339 | |||
324 | response client::getFriends(std::set<user_id>& _ret) | 340 | response client::getFriends(std::set<user_id>& _ret) |
325 | { | 341 | { |
326 | if (!_current_user) | 342 | if (!_current_user) |
diff --git a/src/client.h b/src/client.h index c1c6344..f96022d 100644 --- a/src/client.h +++ b/src/client.h | |||
@@ -11,6 +11,7 @@ | |||
11 | #include <set> | 11 | #include <set> |
12 | #include <ctime> | 12 | #include <ctime> |
13 | #include <chrono> | 13 | #include <chrono> |
14 | #include "configuration.h" | ||
14 | 15 | ||
15 | namespace OAuth { | 16 | namespace OAuth { |
16 | class Consumer; | 17 | class Consumer; |
@@ -81,6 +82,8 @@ namespace twitter { | |||
81 | 82 | ||
82 | const user& getUser() const; | 83 | const user& getUser() const; |
83 | 84 | ||
85 | configuration getConfiguration(); | ||
86 | |||
84 | // NOTE: stream setting function calls will fail silently when stream is running | 87 | // NOTE: stream setting function calls will fail silently when stream is running |
85 | void setUserStreamNotifyCallback(stream::notify_callback callback); | 88 | void setUserStreamNotifyCallback(stream::notify_callback callback); |
86 | void setUserStreamReceiveAllReplies(bool _arg); | 89 | void setUserStreamReceiveAllReplies(bool _arg); |
@@ -99,6 +102,9 @@ namespace twitter { | |||
99 | user _current_user; | 102 | user _current_user; |
100 | stream _user_stream{*this}; | 103 | stream _user_stream{*this}; |
101 | 104 | ||
105 | configuration _configuration; | ||
106 | time_t _last_configuration_update; | ||
107 | |||
102 | bool performGet(std::string url, long& response_code, std::string& result); | 108 | bool performGet(std::string url, long& response_code, std::string& result); |
103 | bool performPost(std::string url, std::string dataStr, long& response_code, std::string& result); | 109 | bool performPost(std::string url, std::string dataStr, long& response_code, std::string& result); |
104 | bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, std::string& result); | 110 | bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, std::string& result); |
diff --git a/src/configuration.cpp b/src/configuration.cpp new file mode 100644 index 0000000..63464ed --- /dev/null +++ b/src/configuration.cpp | |||
@@ -0,0 +1,109 @@ | |||
1 | #include "configuration.h" | ||
2 | #include <json.hpp> | ||
3 | #include <cassert> | ||
4 | |||
5 | using nlohmann::json; | ||
6 | |||
7 | namespace twitter { | ||
8 | |||
9 | configuration::configuration() | ||
10 | { | ||
11 | _valid = false; | ||
12 | } | ||
13 | |||
14 | configuration::configuration(std::string data) | ||
15 | { | ||
16 | _valid = true; | ||
17 | |||
18 | auto _data = json::parse(data); | ||
19 | |||
20 | _characters_reserved_per_media = _data.at("characters_reserved_per_media"); | ||
21 | _dm_text_character_limit = _data.at("dm_text_character_limit"); | ||
22 | _max_media_per_upload = _data.at("max_media_per_upload"); | ||
23 | _photo_size_limit = _data.at("photo_size_limit"); | ||
24 | _short_url_length = _data.at("short_url_length"); | ||
25 | _short_https_url_length = _data.at("short_url_length_https"); | ||
26 | |||
27 | for (json::iterator sizedata = _data.at("photo_sizes").begin(); sizedata != _data.at("photo_sizes").end(); ++sizedata) | ||
28 | { | ||
29 | photosize size; | ||
30 | size.height = sizedata.value().at("h"); | ||
31 | size.width = sizedata.value().at("w"); | ||
32 | if (sizedata.value().at("resize") == "fit") | ||
33 | { | ||
34 | size.resize = resizetype::fit; | ||
35 | } else { | ||
36 | size.resize = resizetype::crop; | ||
37 | } | ||
38 | |||
39 | _photo_sizes[sizedata.key()] = size; | ||
40 | } | ||
41 | |||
42 | for (auto path : _data.at("non_username_paths")) | ||
43 | { | ||
44 | _non_username_paths.insert(path.get<std::string>()); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | size_t configuration::getCharactersReservedPerMedia() const | ||
49 | { | ||
50 | assert(_valid); | ||
51 | |||
52 | return _characters_reserved_per_media; | ||
53 | } | ||
54 | |||
55 | size_t configuration::getDirectMessageCharacterLimit() const | ||
56 | { | ||
57 | assert(_valid); | ||
58 | |||
59 | return _dm_text_character_limit; | ||
60 | } | ||
61 | |||
62 | size_t configuration::getMaxMediaPerUpload() const | ||
63 | { | ||
64 | assert(_valid); | ||
65 | |||
66 | return _max_media_per_upload; | ||
67 | } | ||
68 | |||
69 | size_t configuration::getPhotoSizeLimit() const | ||
70 | { | ||
71 | assert(_valid); | ||
72 | |||
73 | return _photo_size_limit; | ||
74 | } | ||
75 | |||
76 | std::map<std::string, configuration::photosize> configuration::getPhotoSizes() const | ||
77 | { | ||
78 | assert(_valid); | ||
79 | |||
80 | return _photo_sizes; | ||
81 | } | ||
82 | |||
83 | size_t configuration::getShortUrlLength() const | ||
84 | { | ||
85 | assert(_valid); | ||
86 | |||
87 | return _short_url_length; | ||
88 | } | ||
89 | |||
90 | size_t configuration::getShortHttpsUrlLength() const | ||
91 | { | ||
92 | assert(_valid); | ||
93 | |||
94 | return _short_https_url_length; | ||
95 | } | ||
96 | |||
97 | std::set<std::string> configuration::getNonUsernamePaths() const | ||
98 | { | ||
99 | assert(_valid); | ||
100 | |||
101 | return _non_username_paths; | ||
102 | } | ||
103 | |||
104 | configuration::operator bool() const | ||
105 | { | ||
106 | return _valid; | ||
107 | } | ||
108 | |||
109 | }; | ||
diff --git a/src/configuration.h b/src/configuration.h new file mode 100644 index 0000000..a15be3d --- /dev/null +++ b/src/configuration.h | |||
@@ -0,0 +1,52 @@ | |||
1 | #ifndef CONFIGURATION_H_A7164D18 | ||
2 | #define CONFIGURATION_H_A7164D18 | ||
3 | |||
4 | #include <map> | ||
5 | #include <string> | ||
6 | #include <set> | ||
7 | |||
8 | namespace twitter { | ||
9 | |||
10 | class configuration { | ||
11 | public: | ||
12 | enum class resizetype { | ||
13 | fit, | ||
14 | crop | ||
15 | }; | ||
16 | |||
17 | struct photosize { | ||
18 | size_t height; | ||
19 | size_t width; | ||
20 | resizetype resize; | ||
21 | }; | ||
22 | |||
23 | configuration(); | ||
24 | configuration(std::string data); | ||
25 | |||
26 | size_t getCharactersReservedPerMedia() const; | ||
27 | size_t getDirectMessageCharacterLimit() const; | ||
28 | size_t getMaxMediaPerUpload() const; | ||
29 | size_t getPhotoSizeLimit() const; | ||
30 | std::map<std::string, photosize> getPhotoSizes() const; | ||
31 | size_t getShortUrlLength() const; | ||
32 | size_t getShortHttpsUrlLength() const; | ||
33 | std::set<std::string> getNonUsernamePaths() const; | ||
34 | |||
35 | operator bool() const; | ||
36 | |||
37 | private: | ||
38 | bool _valid = false; | ||
39 | |||
40 | size_t _characters_reserved_per_media; | ||
41 | size_t _dm_text_character_limit; | ||
42 | size_t _max_media_per_upload; | ||
43 | size_t _photo_size_limit; | ||
44 | std::map<std::string, photosize> _photo_sizes; | ||
45 | size_t _short_url_length; | ||
46 | size_t _short_https_url_length; | ||
47 | std::set<std::string> _non_username_paths; | ||
48 | }; | ||
49 | |||
50 | }; | ||
51 | |||
52 | #endif /* end of include guard: CONFIGURATION_H_A7164D18 */ | ||
diff --git a/src/twitter.h b/src/twitter.h index d0b469e..90925df 100644 --- a/src/twitter.h +++ b/src/twitter.h | |||
@@ -16,5 +16,6 @@ namespace twitter { | |||
16 | #include "notification.h" | 16 | #include "notification.h" |
17 | #include "list.h" | 17 | #include "list.h" |
18 | #include "direct_message.h" | 18 | #include "direct_message.h" |
19 | #include "configuration.h" | ||
19 | 20 | ||
20 | #endif /* end of include guard: TWITTER_H_AC7A7666 */ | 21 | #endif /* end of include guard: TWITTER_H_AC7A7666 */ |