mastorss/src/mastorss.cpp

119 lines
3.1 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-04-14 13:57:03 +02:00
#include <jsoncpp/json/json.h>
#include <mastodon-cpp/mastodon-cpp.hpp>
2018-08-25 14:29:13 +02:00
#include <mastodon-cpp/easy/all.hpp>
2018-02-10 12:35:06 +01:00
#include "version.hpp"
#include "mastorss.hpp"
2018-01-26 02:33:58 +01:00
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-04-14 13:57:03 +02:00
Json::Value config;
2018-03-15 13:20:26 +01:00
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;
2018-08-25 14:29:13 +02:00
std::vector<Mastodon::Easy::Status> 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)
{
2018-04-29 17:14:48 +02:00
std::cerr << "Error code: " << ret << '\n';
std::cerr << answer << '\n';
2018-02-01 12:03:16 +01:00
return ret;
}
2018-03-15 13:20:26 +01:00
entries = parse_website(answer);
2018-01-26 02:33:58 +01:00
2018-04-14 14:10:14 +02:00
string last_entry = config[profile]["last_entry"].asString();
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-08-25 14:29:13 +02:00
last_entry = entries.at(1).content();
2018-01-26 20:59:01 +01:00
}
2018-08-25 14:29:13 +02:00
config[profile]["last_entry"] = entries.front().content();
2018-01-26 02:33:58 +01:00
bool new_content = false;
for (auto rit = entries.rbegin(); rit != entries.rend(); ++rit)
{
2018-08-25 14:29:13 +02:00
if (!new_content && (*rit).content().compare(last_entry) == 0)
2018-01-26 02:33:58 +01:00
{
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;
2018-08-25 14:29:13 +02:00
Mastodon::Easy masto(instance, access_token);
2018-01-26 03:35:52 +01:00
2018-08-25 14:29:13 +02:00
masto.send_post(*rit);
2018-01-26 03:35:52 +01:00
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-04-14 13:57:03 +02:00
write_config();
2018-02-18 14:27:01 +01:00
2018-01-26 02:33:58 +01:00
return 0;
}