about summary refs log tree commit diff stats
path: root/ebooks.cpp
blob: a3323518f2dcadc89e5c138137ae991ce10d3f71 (plain) (blame)
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
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { c
#include <cstdio>
#include <list>
#include <map>
#include "kgramstats.h"
#include <ctime>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <twitcurl.h>
#include <unistd.h>
#include <yaml-cpp/yaml.h>
#include "freevars.h"

using namespace::std;

int main(int argc, char** args)
{
	srand(time(NULL));
	
	YAML::Node config = YAML::LoadFile("config.yml");
    int delay = config["delay"].as<int>();

	ifstream infile(config["corpus"].as<std::string>().c_str());
	string corpus;
	string line;
	while (getline(infile, line))
	{
		corpus += " " + line;
	}

    cout << "Preprocessing corpus..." << endl;
	kgramstats* stats = new kgramstats(corpus, 5);
    
    cout << "Preprocessing freevars..." << endl;
    freevars* vars = new freevars();
    vars->addVar("name", "names.txt");
    vars->addVar("noun", "nouns.txt");

    cout << "Generating..." << endl;
	for (;;)
	{
		vector<string> doc = stats->randomSentence(rand() % 25 + 5);
		string hi;
		for (vector<string>::iterator it = doc.begin(); it != doc.end(); ++it)
		{
			hi += vars->parse(*it) + " ";
		}
	
		hi = hi.substr(0,140);

	    twitCurl twitterObj;
	    std::string tmpStr, tmpStr2;
	    std::string replyMsg;
	    char tmpBuf[1024];
		std::string username(config["username"].as<std::string>());
		std::string password(config["password"].as<std::string>());

	    /* Set twitter username and password */
	    twitterObj.setTwitterUsername(username);
	    twitterObj.setTwitterPassword(password);
	
	    /* OAuth flow begins */
	    /* Step 0: Set OAuth related params. These are got by registering your app at twitter.com */
	    twitterObj.getOAuth().setConsumerKey( config["consumer_key"].as<std::string>() );
	    twitterObj.getOAuth().setConsumerSecret( config["consumer_secret"].as<std::string>() );

	    /* Step 1: Check if we alredy have OAuth access token from a previous run */
	    std::string myOAuthAccessTokenKey("");
	    std::string myOAuthAccessTokenSecret("");
	    std::ifstream oAuthTokenKeyIn;
	    std::ifstream oAuthTokenSecretIn;

	    oAuthTokenKeyIn.open( "twitterClient_token_key.txt" );
	    oAuthTokenSecretIn.open( "twitterClient_token_secret.txt" );

	    memset( tmpBuf, 0, 1024 );
	    oAuthTokenKeyIn >> tmpBuf;
	    myOAuthAccessTokenKey = tmpBuf;

	    memset( tmpBuf, 0, 1024 );
	    oAuthTokenSecretIn >> tmpBuf;
	    myOAuthAccessTokenSecret = tmpBuf;

	    oAuthTokenKeyIn.close();
	    oAuthTokenSecretIn.close();

	    if( myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size() )
	    {
	        /* If we already have these keys, then no need to go through auth again */
	        printf( "\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str() );

	        twitterObj.getOAuth().setOAuthTokenKey( myOAuthAccessTokenKey );
	        twitterObj.getOAuth().setOAuthTokenSecret( myOAuthAccessTokenSecret );
	    }
	    else
	    {
	        /* Step 2: Get request token key and secret */
	        std::string authUrl;
	        twitterObj.oAuthRequestToken( authUrl );

	        /* Step 3: Get PIN  */
	        memset( tmpBuf, 0, 1024 );
	        printf( "\nDo you want to visit twitter.com for PIN (0 for no; 1 for yes): " );
	        gets( tmpBuf );
	        tmpStr = tmpBuf;
	        if( std::string::npos != tmpStr.find( "1" ) )
	        {
	            /* Ask user to visit twitter.com auth page and get PIN */
	            memset( tmpBuf, 0, 1024 );
	            printf( "\nPlease visit this link in web browser and authorize this application:\n%s", authUrl.c_str() );
	            printf( "\nEnter the PIN provided by twitter: " );
	            gets( tmpBuf );
	            tmpStr = tmpBuf;
	            twitterObj.getOAuth().setOAuthPin( tmpStr );
	        }
	        else
	        {
	            /* Else, pass auth url to twitCurl and get it via twitCurl PIN handling */
	            twitterObj.oAuthHandlePIN( authUrl );
	        }

	        /* Step 4: Exchange request token with access token */
	        twitterObj.oAuthAccessToken();

	        /* Step 5: Now, save this access token key and secret for future use without PIN */
	        twitterObj.getOAuth().getOAuthTokenKey( myOAuthAccessTokenKey );
	        twitterObj.getOAuth().getOAuthTokenSecret( myOAuthAccessTokenSecret );

	        /* Step 6: Save these keys in a file or wherever */
	        std::ofstream oAuthTokenKeyOut;
	        std::ofstream oAuthTokenSecretOut;

	        oAuthTokenKeyOut.open( "twitterClient_token_key.txt" );
	        oAuthTokenSecretOut.open( "twitterClient_token_secret.txt" );

	        oAuthTokenKeyOut.clear();
	        oAuthTokenSecretOut.clear();

	        oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str();
	        oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str();

	        oAuthTokenKeyOut.close();
	        oAuthTokenSecretOut.close();
	    }
	    /* OAuth flow ends */

	    /* Account credentials verification */
	    if( twitterObj.accountVerifyCredGet() )
	    {
	        twitterObj.getLastWebResponse( replyMsg );
	        printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet web response:\n%s\n", replyMsg.c_str() );
	    }
	    else
	    {
	        twitterObj.getLastCurlError( replyMsg );
	        printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet error:\n%s\n", replyMsg.c_str() );
	    }
	
	    /* Post a new status message */
	    replyMsg = "";
	    if( twitterObj.statusUpdate( hi ) )
	    {
	        twitterObj.getLastWebResponse( replyMsg );
	        printf( "\ntwitterClient:: twitCurl::statusUpdate web response:\n%s\n", replyMsg.c_str() );
	    }
	    else
	    {
	        twitterObj.getLastCurlError( replyMsg );
	        printf( "\ntwitterClient:: twitCurl::statusUpdate error:\n%s\n", replyMsg.c_str() );
	    }

		sleep(delay);
	}
	
	return 0;
}