about summary refs log tree commit diff stats
path: root/nancy.cpp
blob: 00c68c54624b6bd5d812053a31a24a20fa0399b2 (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
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <twitcurl.h>
#include <unistd.h>
#include <verbly.h>

std::string capitalize(std::string input)
{
  std::string result;
  bool capnext = true;
  
  for (auto ch : input)
  {
    if (capnext)
    {
      result += toupper(ch);
      capnext = false;
    } else {
      result += ch;
    }
    
    if ((ch == ' ') || (ch == '-'))
    {
      capnext = true;
    }
  }
  
  return result;
}

int main(int argc, char** argv)
{
  srand(time(NULL));
  
  YAML::Node config = YAML::LoadFile("config.yml");
  
  // Forms
  std::vector<std::string> forms;
  std::ifstream formfile("forms.txt");
  if (formfile.is_open())
  {
    while (!formfile.eof())
    {
      std::string l;
      getline(formfile, l);
      if (l.back() == '\r')
      {
        l.pop_back();
      }
      
      forms.push_back(l);
    }
    
    formfile.close();
  }
  
  if (forms.size() == 0)
  {
    std::cout << "No forms found... check forms.txt." << std::endl;
    
    return 2;
  }
  
  // verbly
  verbly::data database("data.sqlite3");
  
  // Twitter  
  twitCurl twitter;
  twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>());
  twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>());
  twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>());
  twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>());
  
  for (;;)
  {
    std::cout << "Generating tweet" << std::endl;
    
    std::string form = forms[rand() % forms.size()];
    
    // Adjectives
    int i;
    while ((i = form.find("{adj}")) != std::string::npos)
    {
      verbly::adjective adj = database.adjectives().random(true).limit(1).run().front();
      form.replace(i, 5, capitalize(adj.base_form()));
    }
    
    // Nouns
    while ((i = form.find("{noun}")) != std::string::npos)
    {
      std::string nf;
      for (;;)
      {
        verbly::noun n = database.nouns().is_not_proper(true).random(true).limit(1).run().front();
        if (n.singular_form().find("genus") == std::string::npos)
        {
          nf = n.singular_form();
          break;
        }
      }
      
      form.replace(i, 6, capitalize(nf));
    }
    
    if (form.size() > 140)
    {
      continue;
    }
        
    std::string replyMsg;
    if (twitter.statusUpdate(form))
    {
      twitter.getLastWebResponse(replyMsg);
      std::cout << "Twitter message: " << replyMsg << std::endl;
    } else {
      twitter.getLastCurlError(replyMsg);
      std::cout << "Curl error: " << replyMsg << std::endl;
    }
    
    std::cout << "Waiting" << std::endl;
    sleep(60 * 60 * 3);
  }
}