Files
@ 3f29cad42ba7
Branch filter:
Location: libtransport.git/backends/twitter/libtwitcurl/.svn/text-base/twitcurl.h.svn-base
3f29cad42ba7
11.5 KiB
text/plain
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #ifndef _TWITCURL_H_
#define _TWITCURL_H_
#include <string>
#include <sstream>
#include <cstring>
#include "oauthlib.h"
#include "curl/curl.h"
namespace twitCurlTypes
{
typedef enum _eTwitCurlApiFormatType
{
eTwitCurlApiFormatXml = 0,
eTwitCurlApiFormatJson,
eTwitCurlApiFormatMax
} eTwitCurlApiFormatType;
typedef enum _eTwitCurlProtocolType
{
eTwitCurlProtocolHttp = 0,
eTwitCurlProtocolHttps,
eTwitCurlProtocolMax
} eTwitCurlProtocolType;
};
/* Default values used in twitcurl */
namespace twitCurlDefaults
{
/* Constants */
const int TWITCURL_DEFAULT_BUFFSIZE = 1024;
const std::string TWITCURL_COLON = ":";
const char TWITCURL_EOS = '\0';
const unsigned int MAX_TIMELINE_TWEET_COUNT = 200;
/* Miscellaneous data used to build twitter URLs*/
const std::string TWITCURL_STATUSSTRING = "status=";
const std::string TWITCURL_TEXTSTRING = "text=";
const std::string TWITCURL_QUERYSTRING = "query=";
const std::string TWITCURL_SEARCHQUERYSTRING = "q=";
const std::string TWITCURL_SCREENNAME = "screen_name=";
const std::string TWITCURL_USERID = "user_id=";
const std::string TWITCURL_EXTENSIONFORMATS[2] = { ".xml",
".json"
};
const std::string TWITCURL_PROTOCOLS[2] = { "http://",
"https://"
};
const std::string TWITCURL_TARGETSCREENNAME = "target_screen_name=";
const std::string TWITCURL_TARGETUSERID = "target_id=";
const std::string TWITCURL_SINCEID = "since_id=";
const std::string TWITCURL_TRIMUSER = "trim_user=true";
const std::string TWITCURL_INCRETWEETS = "include_rts=true";
const std::string TWITCURL_COUNT = "count=";
/* URL separators */
const std::string TWITCURL_URL_SEP_AMP = "&";
const std::string TWITCURL_URL_SEP_QUES = "?";
};
/* Default twitter URLs */
namespace twitterDefaults
{
/* Search URLs */
const std::string TWITCURL_SEARCH_URL = "search.twitter.com/search";
/* Status URLs */
const std::string TWITCURL_STATUSUPDATE_URL = "api.twitter.com/1/statuses/update";
const std::string TWITCURL_STATUSSHOW_URL = "api.twitter.com/1/statuses/show/";
const std::string TWITCURL_STATUDESTROY_URL = "api.twitter.com/1/statuses/destroy/";
/* Timeline URLs */
const std::string TWITCURL_HOME_TIMELINE_URL = "api.twitter.com/1/statuses/home_timeline";
const std::string TWITCURL_PUBLIC_TIMELINE_URL = "api.twitter.com/1/statuses/public_timeline";
const std::string TWITCURL_FEATURED_USERS_URL = "api.twitter.com/1/statuses/featured";
const std::string TWITCURL_FRIENDS_TIMELINE_URL = "api.twitter.com/1/statuses/friends_timeline";
const std::string TWITCURL_MENTIONS_URL = "api.twitter.com/1/statuses/mentions";
const std::string TWITCURL_USERTIMELINE_URL = "api.twitter.com/1/statuses/user_timeline";
/* Users URLs */
const std::string TWITCURL_SHOWUSERS_URL = "api.twitter.com/1/users/show";
const std::string TWITCURL_SHOWFRIENDS_URL = "api.twitter.com/1/statuses/friends";
const std::string TWITCURL_SHOWFOLLOWERS_URL = "api.twitter.com/1/statuses/followers";
/* Direct messages URLs */
const std::string TWITCURL_DIRECTMESSAGES_URL = "api.twitter.com/1/direct_messages";
const std::string TWITCURL_DIRECTMESSAGENEW_URL = "api.twitter.com/1/direct_messages/new";
const std::string TWITCURL_DIRECTMESSAGESSENT_URL = "api.twitter.com/1/direct_messages/sent";
const std::string TWITCURL_DIRECTMESSAGEDESTROY_URL = "api.twitter.com/1/direct_messages/destroy/";
/* Friendships URLs */
const std::string TWITCURL_FRIENDSHIPSCREATE_URL = "api.twitter.com/1/friendships/create";
const std::string TWITCURL_FRIENDSHIPSDESTROY_URL = "api.twitter.com/1/friendships/destroy";
const std::string TWITCURL_FRIENDSHIPSSHOW_URL = "api.twitter.com/1/friendships/show";
/* Social graphs URLs */
const std::string TWITCURL_FRIENDSIDS_URL = "api.twitter.com/1/friends/ids";
const std::string TWITCURL_FOLLOWERSIDS_URL = "api.twitter.com/1/followers/ids";
/* Account URLs */
const std::string TWITCURL_ACCOUNTRATELIMIT_URL = "api.twitter.com/1/account/rate_limit_status";
const std::string TWITCURL_ACCOUNTVERIFYCRED_URL = "api.twitter.com/1/account/verify_credentials";
/* Favorites URLs */
const std::string TWITCURL_FAVORITESGET_URL = "api.twitter.com/1/favorites";
const std::string TWITCURL_FAVORITECREATE_URL = "api.twitter.com/1/favorites/create/";
const std::string TWITCURL_FAVORITEDESTROY_URL = "api.twitter.com/1/favorites/destroy/";
/* Block URLs */
const std::string TWITCURL_BLOCKSCREATE_URL = "api.twitter.com/1/blocks/create/";
const std::string TWITCURL_BLOCKSDESTROY_URL = "api.twitter.com/1/blocks/destroy/";
/* Saved Search URLs */
const std::string TWITCURL_SAVEDSEARCHGET_URL = "api.twitter.com/1/saved_searches";
const std::string TWITCURL_SAVEDSEARCHSHOW_URL = "api.twitter.com/1/saved_searches/show/";
const std::string TWITCURL_SAVEDSEARCHCREATE_URL = "api.twitter.com/1/saved_searches/create";
const std::string TWITCURL_SAVEDSEARCHDESTROY_URL = "api.twitter.com/1/saved_searches/destroy/";
/* Trends URLs */
const std::string TWITCURL_TRENDS_URL = "api.twitter.com/1/trends";
const std::string TWITCURL_TRENDSDAILY_URL = "api.twitter.com/1/trends/daily";
const std::string TWITCURL_TRENDSCURRENT_URL = "api.twitter.com/1/trends/current";
const std::string TWITCURL_TRENDSWEEKLY_URL = "api.twitter.com/1/trends/weekly";
const std::string TWITCURL_TRENDSAVAILABLE_URL = "api.twitter.com/1/trends/available";
};
/* twitCurl class */
class twitCurl
{
public:
twitCurl();
~twitCurl();
/* Twitter OAuth authorization methods */
oAuth& getOAuth();
bool oAuthRequestToken( std::string& authorizeUrl /* out */ );
bool oAuthAccessToken();
bool oAuthHandlePIN( const std::string& authorizeUrl /* in */ );
/* Twitter login APIs, set once and forget */
std::string& getTwitterUsername();
std::string& getTwitterPassword();
void setTwitterUsername( std::string& userName /* in */ );
void setTwitterPassword( std::string& passWord /* in */ );
/* Twitter API type */
void setTwitterApiType( twitCurlTypes::eTwitCurlApiFormatType eType );
void setTwitterProcotolType( twitCurlTypes::eTwitCurlProtocolType eType );
/* Twitter search APIs */
bool search( std::string& searchQuery /* in */ );
/* Twitter status APIs */
bool statusUpdate( std::string& newStatus /* in */ );
bool statusShowById( std::string& statusId /* in */ );
bool statusDestroyById( std::string& statusId /* in */ );
/* Twitter timeline APIs */
bool timelinePublicGet();
bool timelineFriendsGet();
bool timelineUserGet( bool trimUser /* in */, bool includeRetweets /* in */, unsigned int tweetCount /* in */, std::string userInfo = "" /* in */, bool isUserId = false /* in */ );
bool featuredUsersGet();
bool mentionsGet( std::string sinceId = "" /* in */ );
/* Twitter user APIs */
bool userGet( std::string& userInfo /* in */, bool isUserId = false /* in */ );
bool friendsGet( std::string userInfo = "" /* in */, bool isUserId = false /* in */ );
bool followersGet( std::string userInfo = "" /* in */, bool isUserId = false /* in */ );
/* Twitter direct message APIs */
bool directMessageGet();
bool directMessageSend( std::string& userInfo /* in */, std::string& dMsg /* in */, bool isUserId = false /* in */ );
bool directMessageGetSent();
bool directMessageDestroyById( std::string& dMsgId /* in */ );
/* Twitter friendships APIs */
bool friendshipCreate( std::string& userInfo /* in */, bool isUserId = false /* in */ );
bool friendshipDestroy( std::string& userInfo /* in */, bool isUserId = false /* in */ );
bool friendshipShow( std::string& userInfo /* in */, bool isUserId = false /* in */ );
/* Twitter social graphs APIs */
bool friendsIdsGet( std::string& userInfo /* in */, bool isUserId = false /* in */ );
bool followersIdsGet( std::string& userInfo /* in */, bool isUserId = false /* in */ );
/* Twitter account APIs */
bool accountRateLimitGet();
bool accountVerifyCredGet();
/* Twitter favorites APIs */
bool favoriteGet();
bool favoriteCreate( std::string& statusId /* in */ );
bool favoriteDestroy( std::string& statusId /* in */ );
/* Twitter block APIs */
bool blockCreate( std::string& userInfo /* in */ );
bool blockDestroy( std::string& userInfo /* in */ );
/* Twitter search APIs */
bool savedSearchGet();
bool savedSearchCreate( std::string& query /* in */ );
bool savedSearchShow( std::string& searchId /* in */ );
bool savedSearchDestroy( std::string& searchId /* in */ );
/* Twitter trends APIs (JSON) */
bool trendsGet();
bool trendsDailyGet();
bool trendsWeeklyGet();
bool trendsCurrentGet();
bool trendsAvailableGet();
/* cURL APIs */
bool isCurlInit();
void getLastWebResponse( std::string& outWebResp /* out */ );
void getLastCurlError( std::string& outErrResp /* out */);
/* Internal cURL related methods */
int saveLastWebResponse( char*& data, size_t size );
/* cURL proxy APIs */
std::string& getProxyServerIp();
std::string& getProxyServerPort();
std::string& getProxyUserName();
std::string& getProxyPassword();
void setProxyServerIp( std::string& proxyServerIp /* in */ );
void setProxyServerPort( std::string& proxyServerPort /* in */ );
void setProxyUserName( std::string& proxyUserName /* in */ );
void setProxyPassword( std::string& proxyPassword /* in */ );
private:
/* cURL data */
CURL* m_curlHandle;
char m_errorBuffer[twitCurlDefaults::TWITCURL_DEFAULT_BUFFSIZE];
std::string m_callbackData;
/* cURL flags */
bool m_curlProxyParamsSet;
bool m_curlLoginParamsSet;
bool m_curlCallbackParamsSet;
/* cURL proxy data */
std::string m_proxyServerIp;
std::string m_proxyServerPort;
std::string m_proxyUserName;
std::string m_proxyPassword;
/* Twitter data */
std::string m_twitterUsername;
std::string m_twitterPassword;
/* Twitter API type */
twitCurlTypes::eTwitCurlApiFormatType m_eApiFormatType;
twitCurlTypes::eTwitCurlProtocolType m_eProtocolType;
/* OAuth data */
oAuth m_oAuth;
/* Private methods */
void clearCurlCallbackBuffers();
void prepareCurlProxy();
void prepareCurlCallback();
void prepareCurlUserPass();
void prepareStandardParams();
bool performGet( const std::string& getUrl );
bool performGet( const std::string& getUrl, const std::string& oAuthHttpHeader );
bool performDelete( const std::string& deleteUrl );
bool performPost( const std::string& postUrl, std::string dataStr = "" );
/* Internal cURL related methods */
static int curlCallback( char* data, size_t size, size_t nmemb, twitCurl* pTwitCurlObj );
};
/* Private functions */
void utilMakeCurlParams( std::string& outStr, std::string& inParam1, std::string& inParam2 );
void utilMakeUrlForUser( std::string& outUrl, const std::string& baseUrl, std::string& userInfo, bool isUserId );
#endif // _TWITCURL_H_
|