about summary refs log tree commit diff stats
path: root/blessed.cpp
blob: 69cc5f3d2c28fa190dbf139b7f5d26d453bbc267 (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
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <verbly.h>
#include <twitter.h>
#include <chrono>
#include <random>
#include <thread>

int main(int argc, char** argv)
{
  std::random_device random_device;
  std::mt19937 random_engine{random_device()};
  
  YAML::Node config = YAML::LoadFile("config.yml");
    
  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);
  
  verbly::data database {"data.sqlite3"};
  
  std::vector<std::string> emojis;
  {
    std::ifstream emojifile("emojis.txt");
    if (!emojifile.is_open())
    {
      std::cout << "Could not find emoji." << std::endl;
      return -1;
    }

    std::string line;
    while (getline(emojifile, line))
    {
      if (line.back() == '\r')
      {
        line.pop_back();
      }
    
      emojis.push_back(line);
    }
  }
  
  std::uniform_int_distribution<int> emojidist(0, emojis.size()-1);
  std::bernoulli_distribution continuedist(1.0/2.0);
  
  for (;;)
  {
    std::cout << "Generating tweet..." << std::endl;
    
    std::string exclamation;
    while (exclamation.empty())
    {
      verbly::verb v = database.verbs().random().limit(1).has_pronunciation().run().front();
      auto rhyms = database.nouns().rhymes_with(v).random().limit(1).is_not_proper().run();
      if (!rhyms.empty())
      {
        auto n = rhyms.front();
        if (n.base_form() != v.base_form())
        {
          exclamation = "god " + v.base_form() + " this " + n.base_form();
        }
      }
    }
    
    std::string emojibef;
    std::string emojiaft;
    int emn = 0;
    do
    {
      emn++;
      std::string em = emojis[emojidist(random_engine)];
      emojibef += em;
      emojiaft = em + emojiaft;
    } while (continuedist(random_engine));
    
    std::string result;
    if (emn > 3)
    {
      result = emojibef + "\n" + exclamation + "\n" + emojiaft;
    } else {
      result = emojibef + " " + exclamation + " " + emojiaft;
    }
    
    result.resize(140);
    std::cout << result << std::endl;
    
    try
    {
      client.updateStatus(result);
      
      std::cout << "Tweeted!" << std::endl;
    } catch (const twitter::twitter_error& e)
    {
      std::cout << "Twitter error: " << e.what() << std::endl;
    }
    
    std::cout << "Waiting..." << std::endl;
    
    std::this_thread::sleep_for(std::chrono::hours(1));
  }
}