2018-01-22 03:01:32 +01:00
|
|
|
/* This file is part of soupbot.
|
|
|
|
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
2018-01-22 05:04:40 +01:00
|
|
|
#include <random>
|
|
|
|
#include <chrono>
|
2018-04-09 18:24:29 +02:00
|
|
|
#include <mastodon-cpp/mastodon-cpp.hpp>
|
2018-01-22 03:01:32 +01:00
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include "version.hpp"
|
|
|
|
|
|
|
|
namespace pt = boost::property_tree;
|
2019-04-21 04:35:21 +02:00
|
|
|
using namespace Mastodon;
|
|
|
|
|
2018-01-22 03:01:32 +01:00
|
|
|
using std::cout;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
const string filepath = string(getenv("HOME")) + "/.config/soupbot/";
|
|
|
|
|
|
|
|
void read_config(pt::ptree &config, string &instance, string &access_token)
|
|
|
|
{
|
|
|
|
bool config_changed = false;
|
|
|
|
|
|
|
|
// Read config file, get access token
|
|
|
|
try {
|
|
|
|
pt::read_json(filepath + "config.json", config);
|
|
|
|
instance = config.get("instance", "");
|
|
|
|
access_token = config.get("access_token", "");
|
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
|
|
|
// most likely no config file found
|
|
|
|
cout << "No config file found. Building new one.\n";
|
|
|
|
const boost::filesystem::path path(filepath);
|
|
|
|
boost::filesystem::create_directory(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.empty())
|
|
|
|
{
|
|
|
|
cout << "Instance: ";
|
|
|
|
std::cin >> instance;
|
|
|
|
config.put("instance", instance);
|
|
|
|
config_changed = true;
|
|
|
|
}
|
|
|
|
if (access_token.empty())
|
|
|
|
{
|
|
|
|
cout << "Access token: ";
|
|
|
|
std::cin >> access_token;
|
|
|
|
config.put("access_token", access_token);
|
|
|
|
config_changed = true;
|
|
|
|
}
|
|
|
|
if (config_changed)
|
|
|
|
{
|
|
|
|
pt::write_json(filepath + "config.json", config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 04:10:21 +01:00
|
|
|
void populate_vector(const pt::ptree &ingredients, const string &node, std::vector<string> &vector)
|
|
|
|
{
|
|
|
|
for (const pt::ptree::value_type &value : ingredients.get_child(node))
|
|
|
|
{
|
|
|
|
vector.push_back(value.second.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 05:04:40 +01:00
|
|
|
string get_ingredient(std::vector<string> &vector)
|
|
|
|
{
|
|
|
|
if (vector.size() == 0)
|
|
|
|
return "nothing";
|
|
|
|
|
|
|
|
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
|
|
|
|
std::default_random_engine generator(seed);
|
|
|
|
std::uniform_int_distribution<std::uint16_t> distribution(0, vector.size() - 1);
|
|
|
|
const std::uint16_t i = distribution(generator);
|
|
|
|
const string ingredient = vector.at(i);
|
|
|
|
|
|
|
|
vector.erase(vector.begin() + i);
|
|
|
|
return ingredient;
|
|
|
|
}
|
|
|
|
|
2019-04-21 04:35:21 +02:00
|
|
|
int main()
|
2018-01-22 03:01:32 +01:00
|
|
|
{
|
|
|
|
pt::ptree config;
|
|
|
|
string instance = "";
|
|
|
|
string access_token = "";
|
|
|
|
pt::ptree ingredients;
|
2018-01-22 04:10:21 +01:00
|
|
|
std::vector<string> vegetables;
|
|
|
|
std::vector<string> spices;
|
|
|
|
std::vector<string> fruits;
|
|
|
|
std::vector<string> misc;
|
|
|
|
std::vector<string> highprio;
|
|
|
|
|
2018-01-23 20:40:14 +01:00
|
|
|
read_config(config, instance, access_token);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pt::read_json(filepath + "ingredients.json", ingredients);
|
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
|
|
|
// most likely file not found
|
|
|
|
std::cerr << "ERROR: " << filepath << "ingredients.json not found or not readable.\n";
|
2018-02-28 22:47:29 +01:00
|
|
|
return 1;
|
2018-01-23 20:40:14 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 04:10:21 +01:00
|
|
|
populate_vector(ingredients, "vegetables", vegetables);
|
|
|
|
populate_vector(ingredients, "spices", spices);
|
|
|
|
populate_vector(ingredients, "fruits", fruits);
|
|
|
|
populate_vector(ingredients, "misc", misc);
|
|
|
|
populate_vector(ingredients, "highprio", highprio);
|
|
|
|
|
|
|
|
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
|
|
|
|
std::default_random_engine generator(seed);
|
|
|
|
std::uniform_int_distribution<std::uint8_t> distribution(0, 3);
|
|
|
|
auto dice = std::bind(distribution, generator);
|
|
|
|
|
2018-06-20 17:22:15 +02:00
|
|
|
string toot = "Todays soup is made of:\n\n";
|
2018-01-22 04:10:21 +01:00
|
|
|
|
2018-01-22 05:04:40 +01:00
|
|
|
toot += "1 part " + get_ingredient(vegetables);
|
2018-01-22 06:25:56 +01:00
|
|
|
if (dice() > 0)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n1 part " + get_ingredient(vegetables);
|
2018-01-22 04:10:21 +01:00
|
|
|
if (dice() > 0)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n1 part " + get_ingredient(vegetables);
|
2018-01-22 04:10:21 +01:00
|
|
|
if (dice() > 1)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n1 part " + get_ingredient(vegetables);
|
2018-01-22 06:25:56 +01:00
|
|
|
if (dice() > 2)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n1 part " + get_ingredient(vegetables);
|
2018-01-22 04:10:21 +01:00
|
|
|
|
|
|
|
if (dice() > 1)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \na few " + get_ingredient(fruits);
|
2018-01-22 04:10:21 +01:00
|
|
|
|
|
|
|
if (dice() > 1)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \na good handful " + get_ingredient(misc);
|
2018-01-22 04:10:21 +01:00
|
|
|
if (dice() > 1)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \na good handful " + get_ingredient(misc);
|
2018-01-22 04:10:21 +01:00
|
|
|
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n" + get_ingredient(spices);
|
2018-01-22 04:10:21 +01:00
|
|
|
if (dice() > 0)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n" + get_ingredient(spices);
|
2018-01-22 05:04:40 +01:00
|
|
|
if (dice() > 1)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n" + get_ingredient(spices);
|
2018-01-22 04:10:21 +01:00
|
|
|
if (dice() > 1)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n" + get_ingredient(spices);
|
2018-01-22 05:04:40 +01:00
|
|
|
if (dice() > 2)
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \n" + get_ingredient(spices);
|
2018-01-22 04:10:21 +01:00
|
|
|
|
2018-02-28 22:44:44 +01:00
|
|
|
toot += ", \nan ample amount of " + get_ingredient(highprio);
|
2018-01-22 04:10:21 +01:00
|
|
|
if (dice() > 1)
|
2018-01-22 05:04:40 +01:00
|
|
|
toot += " and " + get_ingredient(highprio);
|
2018-01-22 04:10:21 +01:00
|
|
|
|
2018-03-22 16:37:44 +01:00
|
|
|
toot += ", \nand plenty oil. Salt to taste. \n\nHappy cooking! 🍲 \n\n#bot";
|
2018-01-22 04:10:21 +01:00
|
|
|
|
2019-04-21 04:35:21 +02:00
|
|
|
return_call ret;
|
|
|
|
API masto(instance, access_token);
|
2018-01-22 05:04:40 +01:00
|
|
|
masto.set_useragent("soupbot/" + (string)global::version);
|
|
|
|
|
2019-04-21 04:35:21 +02:00
|
|
|
parameters params =
|
2018-01-22 05:04:40 +01:00
|
|
|
{
|
|
|
|
{ "status", { toot } },
|
|
|
|
{ "visibility", { "public" } }
|
|
|
|
};
|
2019-04-21 04:35:21 +02:00
|
|
|
ret = masto.post(API::v1::statuses, params);
|
2018-01-22 04:10:21 +01:00
|
|
|
|
2019-04-21 04:35:21 +02:00
|
|
|
if (ret)
|
2018-01-22 05:04:40 +01:00
|
|
|
{
|
2019-04-21 04:35:21 +02:00
|
|
|
cout << ret.answer << '\n';
|
2018-01-22 05:04:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-21 04:35:21 +02:00
|
|
|
std::cerr << "Error code: " << ret.error_code << '\n';
|
|
|
|
return ret.error_code;
|
2018-01-22 05:04:40 +01:00
|
|
|
}
|
2018-01-22 04:10:21 +01:00
|
|
|
|
2018-01-22 03:01:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|