about summary refs log tree commit diff stats
path: root/ebooks.cpp
blob: aa690c2d0de4de6ec450d7970ba8ade785f7eecd (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
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
#include <cstdio>
#include <list>
#include <map>
#include "kgramstats.h"
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <twitter.h>
#include <yaml-cpp/yaml.h>
#include <thread>
#include <chrono>

int main(int argc, char** args)
{
  srand(time(NULL));
  rand(); rand(); rand(); rand();
	
  YAML::Node config = YAML::LoadFile("config.yml");
  int delay = config["delay"].as<int>();
  
  twitter::auth auth;
  auth.setConsumerKey(config["consumer_key"].as<std::string>());
  auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
  auth.setAccessKey(config["access_key"].as<std::string>());
  auth.setAccessSecret(config["access_secret"].as<std::string>());
  
  twitter::client client(auth);

  std::ifstream infile(config["corpus"].as<std::string>().c_str());
  std::string corpus;
  std::string line;
  while (getline(infile, line))
  {
    if (line.back() == '\r')
    {
      line.pop_back();
    }
    
    corpus += line + "\n ";
  }

  std::cout << "Preprocessing corpus..." << std::endl;
  kgramstats* stats = new kgramstats(corpus, 4);
  std::mutex stats_mutex;
  
  client.setUserStreamNotifyCallback([&] (twitter::notification n) {
    if (n.getType() == twitter::notification::type::tweet)
    {
      if (!n.getTweet().isRetweet())
      {
        std::string original = n.getTweet().getText();
        std::string canonical;
        std::transform(std::begin(original), std::end(original), std::back_inserter(canonical), [] (char ch) {
          return std::tolower(ch);
        });
      
        if (canonical.find("@rawr_ebooks") != std::string::npos)
        {
          std::string doc = "@" + n.getTweet().getAuthor().getScreenName() + " ";
          {
            std::lock_guard<std::mutex> stats_lock(stats_mutex);
            doc += stats->randomSentence(140 - doc.length());
            doc.resize(140);
          }
        
          twitter::tweet tw;
          twitter::response resp = client.updateStatus(doc, tw, n.getTweet());
          if (resp != twitter::response::ok)
          {
            std::cout << "Twitter error while tweeting: " << resp << std::endl;
          }
        }
      }
    }
  });
  
  client.startUserStream();
  std::this_thread::sleep_for(std::chrono::minutes(1));

  std::cout << "Generating..." << std::endl;
  for (;;)
  {
    std::string doc;
    {
      std::lock_guard<std::mutex> stats_lock(stats_mutex);
      doc = stats->randomSentence(140);
    }
    doc.resize(140);
    
    twitter::tweet tw;
    twitter::response resp = client.updateStatus(doc, tw);
    if (resp != twitter::response::ok)
    {
      std::cout << "Twitter error while tweeting: " << resp << std::endl;
    }
    
    int waitlen = rand() % delay;
    if (waitlen == 0)
    {
      continue;
    } else if (waitlen == 1)
    {
      std::cout << "Sleeping for 1 second..." << std::endl;
    } else if (waitlen < 60)
    {
      std::cout << "Sleeping for " << waitlen << " seconds..." << std::endl;
    } else if (waitlen == 60)
    {
      std::cout << "Sleeping for 1 minute..." << std::endl;
    } else if (waitlen < 60*60)
    {
      std::cout << "Sleeping for " << (waitlen/60) << " minutes..." << std::endl;
    } else if (waitlen == 60*60)
    {
      std::cout << "Sleeping for 1 hour..." << std::endl;
    } else if (waitlen < 60*60*24)
    {
      std::cout << "Sleeping for " << (waitlen/60/60) << " hours..." << std::endl;
    } else if (waitlen == 60*60*24)
    {
      std::cout << "Sleeping for 1 day..." << std::endl;
    } else {
      std::cout << "Sleeping for " << (waitlen/60/60/24) << " days..." << std::endl;
    }

    std::this_thread::sleep_for(std::chrono::seconds(waitlen));
  }
	
  return 0;
}