diff options
| -rw-r--r-- | ebooks.cpp | 131 |
1 files changed, 13 insertions, 118 deletions
| diff --git a/ebooks.cpp b/ebooks.cpp index a24bd8d..e38ebab 100644 --- a/ebooks.cpp +++ b/ebooks.cpp | |||
| @@ -34,6 +34,12 @@ int main(int argc, char** args) | |||
| 34 | freevars* vars = new freevars(); | 34 | freevars* vars = new freevars(); |
| 35 | vars->addVar("name", "names.txt"); | 35 | vars->addVar("name", "names.txt"); |
| 36 | vars->addVar("noun", "nouns.txt"); | 36 | vars->addVar("noun", "nouns.txt"); |
| 37 | |||
| 38 | twitCurl twitter; | ||
| 39 | twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); | ||
| 40 | twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); | ||
| 41 | twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>()); | ||
| 42 | twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>()); | ||
| 37 | 43 | ||
| 38 | std::cout << "Generating..." << std::endl; | 44 | std::cout << "Generating..." << std::endl; |
| 39 | for (;;) | 45 | for (;;) |
| @@ -52,126 +58,15 @@ int main(int argc, char** args) | |||
| 52 | { | 58 | { |
| 53 | hi = hi.substr(0, lastperiod+1); | 59 | hi = hi.substr(0, lastperiod+1); |
| 54 | } | 60 | } |
| 55 | 61 | ||
| 56 | twitCurl twitterObj; | ||
| 57 | std::string tmpStr, tmpStr2; | ||
| 58 | std::string replyMsg; | 62 | std::string replyMsg; |
| 59 | char tmpBuf[1024]; | 63 | if (twitter.statusUpdate(hi)) |
| 60 | std::string username(config["username"].as<std::string>()); | ||
| 61 | std::string password(config["password"].as<std::string>()); | ||
| 62 | |||
| 63 | /* Set twitter username and password */ | ||
| 64 | twitterObj.setTwitterUsername(username); | ||
| 65 | twitterObj.setTwitterPassword(password); | ||
| 66 | |||
| 67 | /* OAuth flow begins */ | ||
| 68 | /* Step 0: Set OAuth related params. These are got by registering your app at twitter.com */ | ||
| 69 | twitterObj.getOAuth().setConsumerKey( config["consumer_key"].as<std::string>() ); | ||
| 70 | twitterObj.getOAuth().setConsumerSecret( config["consumer_secret"].as<std::string>() ); | ||
| 71 | |||
| 72 | /* Step 1: Check if we alredy have OAuth access token from a previous run */ | ||
| 73 | std::string myOAuthAccessTokenKey(""); | ||
| 74 | std::string myOAuthAccessTokenSecret(""); | ||
| 75 | std::ifstream oAuthTokenKeyIn; | ||
| 76 | std::ifstream oAuthTokenSecretIn; | ||
| 77 | |||
| 78 | oAuthTokenKeyIn.open( "twitterClient_token_key.txt" ); | ||
| 79 | oAuthTokenSecretIn.open( "twitterClient_token_secret.txt" ); | ||
| 80 | |||
| 81 | memset( tmpBuf, 0, 1024 ); | ||
| 82 | oAuthTokenKeyIn >> tmpBuf; | ||
| 83 | myOAuthAccessTokenKey = tmpBuf; | ||
| 84 | |||
| 85 | memset( tmpBuf, 0, 1024 ); | ||
| 86 | oAuthTokenSecretIn >> tmpBuf; | ||
| 87 | myOAuthAccessTokenSecret = tmpBuf; | ||
| 88 | |||
| 89 | oAuthTokenKeyIn.close(); | ||
| 90 | oAuthTokenSecretIn.close(); | ||
| 91 | |||
| 92 | if( myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size() ) | ||
| 93 | { | ||
| 94 | /* If we already have these keys, then no need to go through auth again */ | ||
| 95 | printf( "\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str() ); | ||
| 96 | |||
| 97 | twitterObj.getOAuth().setOAuthTokenKey( myOAuthAccessTokenKey ); | ||
| 98 | twitterObj.getOAuth().setOAuthTokenSecret( myOAuthAccessTokenSecret ); | ||
| 99 | } | ||
| 100 | else | ||
| 101 | { | ||
| 102 | /* Step 2: Get request token key and secret */ | ||
| 103 | std::string authUrl; | ||
| 104 | twitterObj.oAuthRequestToken( authUrl ); | ||
| 105 | |||
| 106 | /* Step 3: Get PIN */ | ||
| 107 | memset( tmpBuf, 0, 1024 ); | ||
| 108 | printf( "\nDo you want to visit twitter.com for PIN (0 for no; 1 for yes): " ); | ||
| 109 | gets( tmpBuf ); | ||
| 110 | tmpStr = tmpBuf; | ||
| 111 | if( std::string::npos != tmpStr.find( "1" ) ) | ||
| 112 | { | ||
| 113 | /* Ask user to visit twitter.com auth page and get PIN */ | ||
| 114 | memset( tmpBuf, 0, 1024 ); | ||
| 115 | printf( "\nPlease visit this link in web browser and authorize this application:\n%s", authUrl.c_str() ); | ||
| 116 | printf( "\nEnter the PIN provided by twitter: " ); | ||
| 117 | gets( tmpBuf ); | ||
| 118 | tmpStr = tmpBuf; | ||
| 119 | twitterObj.getOAuth().setOAuthPin( tmpStr ); | ||
| 120 | } | ||
| 121 | else | ||
| 122 | { | ||
| 123 | /* Else, pass auth url to twitCurl and get it via twitCurl PIN handling */ | ||
| 124 | twitterObj.oAuthHandlePIN( authUrl ); | ||
| 125 | } | ||
| 126 | |||
| 127 | /* Step 4: Exchange request token with access token */ | ||
| 128 | twitterObj.oAuthAccessToken(); | ||
| 129 | |||
| 130 | /* Step 5: Now, save this access token key and secret for future use without PIN */ | ||
| 131 | twitterObj.getOAuth().getOAuthTokenKey( myOAuthAccessTokenKey ); | ||
| 132 | twitterObj.getOAuth().getOAuthTokenSecret( myOAuthAccessTokenSecret ); | ||
| 133 | |||
| 134 | /* Step 6: Save these keys in a file or wherever */ | ||
| 135 | std::ofstream oAuthTokenKeyOut; | ||
| 136 | std::ofstream oAuthTokenSecretOut; | ||
| 137 | |||
| 138 | oAuthTokenKeyOut.open( "twitterClient_token_key.txt" ); | ||
| 139 | oAuthTokenSecretOut.open( "twitterClient_token_secret.txt" ); | ||
| 140 | |||
| 141 | oAuthTokenKeyOut.clear(); | ||
| 142 | oAuthTokenSecretOut.clear(); | ||
| 143 | |||
| 144 | oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str(); | ||
| 145 | oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str(); | ||
| 146 | |||
| 147 | oAuthTokenKeyOut.close(); | ||
| 148 | oAuthTokenSecretOut.close(); | ||
| 149 | } | ||
| 150 | /* OAuth flow ends */ | ||
| 151 | |||
| 152 | /* Account credentials verification */ | ||
| 153 | if( twitterObj.accountVerifyCredGet() ) | ||
| 154 | { | ||
| 155 | twitterObj.getLastWebResponse( replyMsg ); | ||
| 156 | printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet web response:\n%s\n", replyMsg.c_str() ); | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { | ||
| 160 | twitterObj.getLastCurlError( replyMsg ); | ||
| 161 | printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet error:\n%s\n", replyMsg.c_str() ); | ||
| 162 | } | ||
| 163 | |||
| 164 | /* Post a new status message */ | ||
| 165 | replyMsg = ""; | ||
| 166 | if( twitterObj.statusUpdate( hi ) ) | ||
| 167 | { | ||
| 168 | twitterObj.getLastWebResponse( replyMsg ); | ||
| 169 | printf( "\ntwitterClient:: twitCurl::statusUpdate web response:\n%s\n", replyMsg.c_str() ); | ||
| 170 | } | ||
| 171 | else | ||
| 172 | { | 64 | { |
| 173 | twitterObj.getLastCurlError( replyMsg ); | 65 | twitter.getLastWebResponse(replyMsg); |
| 174 | printf( "\ntwitterClient:: twitCurl::statusUpdate error:\n%s\n", replyMsg.c_str() ); | 66 | std::cout << "Twitter message: " << replyMsg << std::endl; |
| 67 | } else { | ||
| 68 | twitter.getLastCurlError(replyMsg); | ||
| 69 | std::cout << "Curl error: " << replyMsg << std::endl; | ||
| 175 | } | 70 | } |
| 176 | 71 | ||
| 177 | sleep(rand() % delay); | 72 | sleep(rand() % delay); |
