mastorss/src/mastorss.cpp

125 lines
3.3 KiB
C++
Raw Normal View History

2018-02-10 12:35:06 +01:00
/* This file is part of mastorss.
2018-01-26 02:33:58 +01:00
* 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>
2018-02-18 14:27:01 +01:00
#include <cstdlib> // getenv()
2018-01-26 02:33:58 +01:00
#include <cstdint>
#include <thread>
#include <chrono>
2018-01-26 02:33:58 +01:00
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/filesystem.hpp>
#include <mastodon-cpp.hpp>
2018-02-10 12:35:06 +01:00
#include "version.hpp"
#include "mastorss.hpp"
2018-01-26 02:33:58 +01:00
namespace pt = boost::property_tree;
using Mastodon::API;
using std::cout;
using std::cerr;
2018-02-10 12:35:06 +01:00
using std::cin;
2018-01-26 02:33:58 +01:00
using std::string;
2018-02-18 14:27:01 +01:00
// Initialize global variables
2018-02-06 13:53:34 +01:00
std::uint16_t max_size = 500;
2018-02-10 12:35:06 +01:00
const string filepath = string(getenv("HOME")) + "/.config/mastorss/";
2018-03-15 13:20:26 +01:00
pt::ptree config;
std::string profile;
2018-01-26 02:33:58 +01:00
int main(int argc, char *argv[])
{
if (argc < 2)
{
2018-02-01 12:03:16 +01:00
cerr << "usage: " << argv[0] << " <profile> [max size]\n";
return 10;
2018-01-26 02:33:58 +01:00
}
2018-02-01 12:03:16 +01:00
if (argc == 3)
{
2018-02-01 12:03:43 +01:00
max_size = std::stoi(argv[2]);
2018-02-01 12:03:16 +01:00
}
2018-01-26 02:33:58 +01:00
string instance = "";
string access_token = "";
2018-01-26 03:35:52 +01:00
string feedurl = "";
2018-03-15 13:20:26 +01:00
profile = argv[1];
2018-02-01 12:03:16 +01:00
std::uint16_t ret;
2018-01-26 02:33:58 +01:00
string answer;
std::vector<string> entries;
2018-03-15 13:20:26 +01:00
read_config(instance, access_token, feedurl);
curlpp_init();
2018-02-10 12:35:06 +01:00
ret = http_get(feedurl, answer, "mastorss/" + (string)global::version);
2018-02-01 12:03:16 +01:00
if (ret != 0)
{
return ret;
}
2018-03-15 13:20:26 +01:00
entries = parse_website(answer);
2018-01-26 02:33:58 +01:00
2018-03-15 13:20:26 +01:00
string last_entry = config.get(profile + ".last_entry", "");
2018-01-26 20:59:01 +01:00
if (last_entry.empty())
{
2018-03-15 13:20:26 +01:00
// If no last_entry is stored in the config file,
// make last_entry the second-newest entry.
2018-01-26 20:59:01 +01:00
last_entry = entries.at(1);
}
2018-02-01 12:03:16 +01:00
config.put(profile + ".last_entry", entries.front());
2018-01-26 02:33:58 +01:00
bool new_content = false;
for (auto rit = entries.rbegin(); rit != entries.rend(); ++rit)
{
if (!new_content && (*rit).compare(last_entry) == 0)
{
2018-03-15 13:20:26 +01:00
// If the last entry is found in entries,
// start tooting in the next loop.
2018-01-26 02:33:58 +01:00
new_content = true;
continue;
}
else if (!new_content)
{
continue;
}
2018-01-26 03:35:52 +01:00
string answer;
Mastodon::API masto(instance, access_token);
API::parametermap parameters =
{
{ "status", { *rit } },
{ "visibility", { "public" } }
};
ret = masto.post(API::v1::statuses, parameters, answer);
2018-02-18 14:27:01 +01:00
if (ret != 0)
2018-01-26 03:35:52 +01:00
{
std::cerr << "Error code: " << ret << '\n';
2018-02-01 12:03:16 +01:00
std::cerr << answer << '\n';
2018-01-26 03:35:52 +01:00
return ret;
}
std::this_thread::sleep_for(std::chrono::seconds(2));
2018-01-26 02:33:58 +01:00
}
2018-03-15 13:20:26 +01:00
// Write the new last_entry only if no error happened.
2018-02-18 14:27:01 +01:00
pt::write_json(filepath + "config-" + profile + ".json", config);
2018-01-26 02:33:58 +01:00
return 0;
}