about summary refs log tree commit diff stats
path: root/main.cpp
diff options
context:
space:
mode:
authorFeffernoose <fefferburbia@gmail.com>2013-10-01 18:15:22 -0400
committerFeffernoose <fefferburbia@gmail.com>2013-10-01 18:15:22 -0400
commit8de3134bf2cd26ff81359df703e5fbc6280448d7 (patch)
tree325e99abac6b7e3316334af7961645e9381e6517 /main.cpp
parent2b1f8c3363ef667bc20f33bbb5a856a35f2591ba (diff)
downloadrawr-ebooks-8de3134bf2cd26ff81359df703e5fbc6280448d7.tar.gz
rawr-ebooks-8de3134bf2cd26ff81359df703e5fbc6280448d7.tar.bz2
rawr-ebooks-8de3134bf2cd26ff81359df703e5fbc6280448d7.zip
Wrote program
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp161
1 files changed, 161 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp index 8185573..8310bf4 100644 --- a/main.cpp +++ b/main.cpp
@@ -1,6 +1,167 @@
1#include <cstdio> 1#include <cstdio>
2#include <list>
3#include <map>
4#include "kgramstats.h"
5#include <ctime>
6#include <vector>
7#include <cstdlib>
8#include <fstream>
9#include <iostream>
10#include <twitcurl.h>
11#include <unistd.h>
12#include <yaml-cpp/yaml.h>
13
14using namespace::std;
2 15
3int main(int argc, char** args) 16int main(int argc, char** args)
4{ 17{
18 srand(time(NULL));
19
20 YAML::Node config = YAML::LoadFile("config.yml");
21
22 for (;;)
23 {
24 ifstream infile(config["corpus"].as<std::string>().c_str());
25 string corpus;
26 string line;
27 while (getline(infile, line))
28 {
29 corpus += " " + line;
30 }
31
32 kgramstats* stats = new kgramstats(corpus, 5);
33 vector<string> doc = stats->randomSentence(rand() % 25 + 5);
34 string hi;
35 for (vector<string>::iterator it = doc.begin(); it != doc.end(); ++it)
36 {
37 hi += *it + " ";
38 }
39
40 hi = hi.substr(0,140);
41
42 twitCurl twitterObj;
43 std::string tmpStr, tmpStr2;
44 std::string replyMsg;
45 char tmpBuf[1024];
46 std::string username(config["username"].as<std::string>());
47 std::string password(config["password"].as<std::string>());
48
49 /* Set twitter username and password */
50 twitterObj.setTwitterUsername(username);
51 twitterObj.setTwitterPassword(password);
52
53 /* OAuth flow begins */
54 /* Step 0: Set OAuth related params. These are got by registering your app at twitter.com */
55 twitterObj.getOAuth().setConsumerKey( config["consumer_key"].as<std::string>() );
56 twitterObj.getOAuth().setConsumerSecret( config["consumer_secret"].as<std::string>() );
57
58 /* Step 1: Check if we alredy have OAuth access token from a previous run */
59 std::string myOAuthAccessTokenKey("");
60 std::string myOAuthAccessTokenSecret("");
61 std::ifstream oAuthTokenKeyIn;
62 std::ifstream oAuthTokenSecretIn;
63
64 oAuthTokenKeyIn.open( "twitterClient_token_key.txt" );
65 oAuthTokenSecretIn.open( "twitterClient_token_secret.txt" );
66
67 memset( tmpBuf, 0, 1024 );
68 oAuthTokenKeyIn >> tmpBuf;
69 myOAuthAccessTokenKey = tmpBuf;
70
71 memset( tmpBuf, 0, 1024 );
72 oAuthTokenSecretIn >> tmpBuf;
73 myOAuthAccessTokenSecret = tmpBuf;
74
75 oAuthTokenKeyIn.close();
76 oAuthTokenSecretIn.close();
77
78 if( myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size() )
79 {
80 /* If we already have these keys, then no need to go through auth again */
81 printf( "\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str() );
82
83 twitterObj.getOAuth().setOAuthTokenKey( myOAuthAccessTokenKey );
84 twitterObj.getOAuth().setOAuthTokenSecret( myOAuthAccessTokenSecret );
85 }
86 else
87 {
88 /* Step 2: Get request token key and secret */
89 std::string authUrl;
90 twitterObj.oAuthRequestToken( authUrl );
91
92 /* Step 3: Get PIN */
93 memset( tmpBuf, 0, 1024 );
94 printf( "\nDo you want to visit twitter.com for PIN (0 for no; 1 for yes): " );
95 gets( tmpBuf );
96 tmpStr = tmpBuf;
97 if( std::string::npos != tmpStr.find( "1" ) )
98 {
99 /* Ask user to visit twitter.com auth page and get PIN */
100 memset( tmpBuf, 0, 1024 );
101 printf( "\nPlease visit this link in web browser and authorize this application:\n%s", authUrl.c_str() );
102 printf( "\nEnter the PIN provided by twitter: " );
103 gets( tmpBuf );
104 tmpStr = tmpBuf;
105 twitterObj.getOAuth().setOAuthPin( tmpStr );
106 }
107 else
108 {
109 /* Else, pass auth url to twitCurl and get it via twitCurl PIN handling */
110 twitterObj.oAuthHandlePIN( authUrl );
111 }
112
113 /* Step 4: Exchange request token with access token */
114 twitterObj.oAuthAccessToken();
115
116 /* Step 5: Now, save this access token key and secret for future use without PIN */
117 twitterObj.getOAuth().getOAuthTokenKey( myOAuthAccessTokenKey );
118 twitterObj.getOAuth().getOAuthTokenSecret( myOAuthAccessTokenSecret );
119
120 /* Step 6: Save these keys in a file or wherever */
121 std::ofstream oAuthTokenKeyOut;
122 std::ofstream oAuthTokenSecretOut;
123
124 oAuthTokenKeyOut.open( "twitterClient_token_key.txt" );
125 oAuthTokenSecretOut.open( "twitterClient_token_secret.txt" );
126
127 oAuthTokenKeyOut.clear();
128 oAuthTokenSecretOut.clear();
129
130 oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str();
131 oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str();
132
133 oAuthTokenKeyOut.close();
134 oAuthTokenSecretOut.close();
135 }
136 /* OAuth flow ends */
137
138 /* Account credentials verification */
139 if( twitterObj.accountVerifyCredGet() )
140 {
141 twitterObj.getLastWebResponse( replyMsg );
142 printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet web response:\n%s\n", replyMsg.c_str() );
143 }
144 else
145 {
146 twitterObj.getLastCurlError( replyMsg );
147 printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet error:\n%s\n", replyMsg.c_str() );
148 }
149
150 /* Post a new status message */
151 replyMsg = "";
152 if( twitterObj.statusUpdate( hi ) )
153 {
154 twitterObj.getLastWebResponse( replyMsg );
155 printf( "\ntwitterClient:: twitCurl::statusUpdate web response:\n%s\n", replyMsg.c_str() );
156 }
157 else
158 {
159 twitterObj.getLastCurlError( replyMsg );
160 printf( "\ntwitterClient:: twitCurl::statusUpdate error:\n%s\n", replyMsg.c_str() );
161 }
162
163 sleep(900);
164 }
165
5 return 0; 166 return 0;
6} \ No newline at end of file 167} \ No newline at end of file