about summary refs log tree commit diff stats
path: root/ebooks.cpp
blob: 8b567ed3e81beb840d99865e8e9db47bac72e65e (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#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 ";
  }
  
  // Replace old-style freevars while I can't be bothered to remake the corpus yet
  std::vector<std::string> fv_names;
  std::ifstream namefile("names.txt");
  if (namefile.is_open())
  {
    while (!namefile.eof())
    {
      std::string l;
      getline(namefile, l);
      if (l.back() == '\r')
      {
        l.pop_back();
      }
      
      fv_names.push_back(l);
    }
  }
  
  namefile.close();

  std::cout << "Preprocessing corpus..." << std::endl;
  rawr kgramstats;
  kgramstats.addCorpus(corpus);
  kgramstats.compile(4);
  kgramstats.setTransformCallback([&] (std::string canonical, std::string) {
    size_t pos = canonical.find("$name$");
    if (pos != std::string::npos)
    {
      canonical.replace(pos, 6, fv_names[rand() % fv_names.size()]);
    }
    
    return canonical;
  });
  
  std::mutex stats_mutex;
  
  client.setUserStreamNotifyCallback([&] (twitter::notification n) {
    if (n.getType() == twitter::notification::type::tweet)
    {
      if ((!n.getTweet().isRetweet()) && (n.getTweet().getAuthor() != client.getUser()))
      {
        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 = client.generateReplyPrefill(n.getTweet());
          {
            std::lock_guard<std::mutex> stats_lock(stats_mutex);
            doc += kgramstats.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 = kgramstats.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;
}