about summary refs log tree commit diff stats
path: root/lunatic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lunatic.cpp')
-rw-r--r--lunatic.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/lunatic.cpp b/lunatic.cpp new file mode 100644 index 0000000..2438788 --- /dev/null +++ b/lunatic.cpp
@@ -0,0 +1,84 @@
1#include <yaml-cpp/yaml.h>
2#include <iostream>
3#include <random>
4#include <fstream>
5#include <twitter.h>
6#include <chrono>
7#include <thread>
8
9int main(int argc, char** argv)
10{
11 if (argc != 2)
12 {
13 std::cout << "usage: lunatic [configfile]" << std::endl;
14 return -1;
15 }
16
17 std::string configfile(argv[1]);
18 YAML::Node config = YAML::LoadFile(configfile);
19
20 twitter::auth auth;
21 auth.setConsumerKey(config["consumer_key"].as<std::string>());
22 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
23 auth.setAccessKey(config["access_key"].as<std::string>());
24 auth.setAccessSecret(config["access_secret"].as<std::string>());
25
26 twitter::client client(auth);
27
28 std::random_device randomDevice;
29 std::mt19937 rng(randomDevice());
30
31 for (;;)
32 {
33 std::cout << "Generating tweet" << std::endl;
34
35 // Reload achievements list every time in case it has been updated
36 std::vector<std::string> achievements;
37 std::ifstream datafile(config["achievements"].as<std::string>());
38 if (!datafile.is_open())
39 {
40 std::cout << "Could not find achievements file." << std::endl;
41 return 1;
42 }
43
44 std::string line;
45 while (getline(datafile, line))
46 {
47 if (line.back() == '\r')
48 {
49 line.pop_back();
50 }
51
52 achievements.push_back(line);
53 }
54
55 std::uniform_int_distribution<int> dist(0, achievements.size() - 1);
56 std::string achievement = achievements[dist(rng)];
57
58 std::string header;
59 if (std::bernoulli_distribution(1.0 / 50.0)(rng))
60 {
61 header = "YOU GOT A MULTI MOON!";
62 } else {
63 header = "YOU GOT A MOON!";
64 }
65
66 std::string action = header + "\n" + achievement;
67 action.resize(140);
68
69 try
70 {
71 client.updateStatus(action);
72 } catch (const twitter::twitter_error& e)
73 {
74 std::cout << "Twitter error: " << e.what() << std::endl;
75 }
76
77 std::cout << action << std::endl;
78 std::cout << "Waiting" << std::endl;
79
80 std::this_thread::sleep_for(std::chrono::hours(3));
81
82 std::cout << std::endl;
83 }
84}