diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-04-12 18:40:52 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-04-12 18:40:52 -0400 |
commit | 891d57d200f55b91f80b6d3b4dd0c30479be6109 (patch) | |
tree | e87d1566ff941aa5003e1f937ccbb497fe1606a2 /src/client.cpp | |
download | libtwittercpp-891d57d200f55b91f80b6d3b4dd0c30479be6109.tar.gz libtwittercpp-891d57d200f55b91f80b6d3b4dd0c30479be6109.tar.bz2 libtwittercpp-891d57d200f55b91f80b6d3b4dd0c30479be6109.zip |
Added basic ability to tweet
Diffstat (limited to 'src/client.cpp')
-rw-r--r-- | src/client.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/client.cpp b/src/client.cpp new file mode 100644 index 0000000..b71ff70 --- /dev/null +++ b/src/client.cpp | |||
@@ -0,0 +1,124 @@ | |||
1 | #include "client.h" | ||
2 | #include <curl_easy.h> | ||
3 | #include <curl_header.h> | ||
4 | #include <sstream> | ||
5 | #include <set> | ||
6 | #include <algorithm> | ||
7 | #include <liboauthcpp/liboauthcpp.h> | ||
8 | |||
9 | namespace twitter { | ||
10 | |||
11 | client::client(const auth& _arg) | ||
12 | { | ||
13 | _oauth_consumer = new OAuth::Consumer(_arg.getConsumerKey(), _arg.getConsumerSecret()); | ||
14 | _oauth_token = new OAuth::Token(_arg.getAccessKey(), _arg.getAccessSecret()); | ||
15 | _oauth_client = new OAuth::Client(_oauth_consumer, _oauth_token); | ||
16 | } | ||
17 | |||
18 | client::~client() | ||
19 | { | ||
20 | delete _oauth_client; | ||
21 | delete _oauth_token; | ||
22 | delete _oauth_consumer; | ||
23 | } | ||
24 | |||
25 | response client::updateStatus(std::string msg, tweet& result) | ||
26 | { | ||
27 | std::ostringstream output; | ||
28 | curl::curl_ios<std::ostringstream> ios(output); | ||
29 | curl::curl_easy conn(ios); | ||
30 | |||
31 | std::stringstream datastrstream; | ||
32 | datastrstream << "status=" << OAuth::PercentEncode(msg); | ||
33 | |||
34 | std::string datastr = datastrstream.str(); | ||
35 | std::string url = "https://api.twitter.com/1.1/statuses/update.json"; | ||
36 | |||
37 | curl::curl_header headers; | ||
38 | std::string oauth_header = _oauth_client->getFormattedHttpHeader(OAuth::Http::Post, url, datastr); | ||
39 | if (!oauth_header.empty()) | ||
40 | { | ||
41 | headers.add(oauth_header); | ||
42 | } | ||
43 | |||
44 | try { | ||
45 | conn.add<CURLOPT_URL>(url.c_str()); | ||
46 | conn.add<CURLOPT_COPYPOSTFIELDS>(datastr.c_str()); | ||
47 | conn.add<CURLOPT_POST>(1); | ||
48 | conn.add<CURLOPT_HTTPHEADER>(headers.get()); | ||
49 | |||
50 | conn.perform(); | ||
51 | } catch (curl::curl_easy_exception error) | ||
52 | { | ||
53 | error.print_traceback(); | ||
54 | |||
55 | return response::curl_error; | ||
56 | } | ||
57 | |||
58 | long response_code = conn.get_info<CURLINFO_RESPONSE_CODE>().get(); | ||
59 | json response_data = json::parse(output.str()); | ||
60 | if (response_code == 200) | ||
61 | { | ||
62 | result = tweet(response_data); | ||
63 | return response::ok; | ||
64 | } | ||
65 | |||
66 | std::set<int> error_codes; | ||
67 | if (response_data.find("errors") != response_data.end()) | ||
68 | { | ||
69 | std::transform(std::begin(response_data["errors"]), std::end(response_data["errors"]), std::inserter(error_codes, std::begin(error_codes)), [] (const json& error) { | ||
70 | return error["code"].get<int>(); | ||
71 | }); | ||
72 | } | ||
73 | |||
74 | if (error_codes.count(32) == 1 || error_codes.count(135) == 1 || error_codes.count(215) == 1) | ||
75 | { | ||
76 | return response::bad_auth; | ||
77 | } else if (error_codes.count(64) == 1) | ||
78 | { | ||
79 | return response::suspended; | ||
80 | } else if (error_codes.count(88) == 1 || error_codes.count(185) == 1) | ||
81 | { | ||
82 | return response::limited; | ||
83 | } else if (error_codes.count(89) == 1) | ||
84 | { | ||
85 | return response::bad_token; | ||
86 | } else if (error_codes.count(130) == 1) | ||
87 | { | ||
88 | return response::server_overloaded; | ||
89 | } else if (error_codes.count(131) == 1) | ||
90 | { | ||
91 | return response::server_error; | ||
92 | } else if (error_codes.count(186) == 1) | ||
93 | { | ||
94 | return response::bad_length; | ||
95 | } else if (error_codes.count(187) == 1) | ||
96 | { | ||
97 | return response::duplicate_status; | ||
98 | } else if (error_codes.count(226) == 1) | ||
99 | { | ||
100 | return response::suspected_spam; | ||
101 | } else if (error_codes.count(261) == 1) | ||
102 | { | ||
103 | return response::write_restricted; | ||
104 | } else if (response_code == 429) | ||
105 | { | ||
106 | return response::limited; | ||
107 | } else if (response_code == 500) | ||
108 | { | ||
109 | return response::server_error; | ||
110 | } else if (response_code == 502) | ||
111 | { | ||
112 | return response::server_unavailable; | ||
113 | } else if (response_code == 503) | ||
114 | { | ||
115 | return response::server_overloaded; | ||
116 | } else if (response_code == 504) | ||
117 | { | ||
118 | return response::server_timeout; | ||
119 | } else { | ||
120 | return response::unknown_error; | ||
121 | } | ||
122 | } | ||
123 | |||
124 | }; | ||