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