From 30b8286ed4c6f0bd7207f98d3a2c90bffe5c7691 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 1 Feb 2018 12:03:16 +0100 Subject: [PATCH] more filtering --- CMakeLists.txt | 2 +- README.md | 2 ++ src/rss2mastodon.cpp | 54 +++++++++++++++++++++++++++++++++----------- watchwords.json | 2 +- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d742795..9a9ab89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.7) include(GNUInstallDirs) project (rss2mastodon - VERSION 0.1.6 + VERSION 0.1.7 LANGUAGES CXX ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall") diff --git a/README.md b/README.md index 72b558e..efeaeba 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ **rss2mastodon** dumps RSS feeds into a mastodon account. +It is hacked together and generally only extended/fixed when it fails. +Use at your own risk. # Install diff --git a/src/rss2mastodon.cpp b/src/rss2mastodon.cpp index a79a9e5..6962877 100644 --- a/src/rss2mastodon.cpp +++ b/src/rss2mastodon.cpp @@ -37,6 +37,7 @@ using std::cout; using std::cerr; using std::string; +uint16_t max_size = 500; const string filepath = string(getenv("HOME")) + "/.config/rss2mastodon/"; void read_config(pt::ptree &config, const string &profile, string &instance, string &access_token, string &feedurl) @@ -53,7 +54,7 @@ void read_config(pt::ptree &config, const string &profile, string &instance, str catch (std::exception &e) { // most likely no config file found - cout << "No config file found. Building new one.\n"; + cout << "Config file not readable. Building new one.\n"; const boost::filesystem::path path(filepath); boost::filesystem::create_directory(filepath); } @@ -141,13 +142,35 @@ std::vector parse_website(const string &profile, const string &xml) string desc = v.second.get_child("description").data(); string str = title + "\n\n" + desc; + // Some feeds contain encoded xhtml-tags >:| + std::regex relt("<"); + std::regex regt(">"); + std::regex reparagraph("

"); + std::regex recdata1(""); + std::regex restrip("<[^>]*>"); + std::regex reindyfuckup("\\/\\* Style Definitions \\*\\/[.[:space:]]*$"); + + str = std::regex_replace(str, relt, "<"); + str = std::regex_replace(str, regt, ">"); + str = std::regex_replace(str, reparagraph, "\n\n"); + str = std::regex_replace(str, recdata1, ""); + str = std::regex_replace(str, recdata2, ""); + str = std::regex_replace(str, restrip, ""); + str = std::regex_replace(str, reindyfuckup, ""); + for (const string &hashtag : watchwords) { - std::regex rehashtag("[[:space:][:punct:]](" + hashtag + ")[[:space:][:punct:]]", + std::regex rehashtag("([[:space:][:punct:]^])(" + hashtag + ")([[:space:][:punct:]$])", std::regex_constants::icase); - str = std::regex_replace(str, rehashtag, "#$1", + str = std::regex_replace(str, rehashtag, "$1#$2$3", std::regex_constants::format_first_only); } + if ((str.size() + link.size()) > (max_size - 15)) + { + str.resize((max_size - link.size() - 15)); + str += " […]"; + } str += "\n\n" + link + "\n\n#bot"; ret.push_back(str); } @@ -161,15 +184,21 @@ int main(int argc, char *argv[]) { if (argc < 2) { - cerr << "usage: " << argv[0] << " \n"; + cerr << "usage: " << argv[0] << " [max size]\n"; return 32; } + if (argc == 3) + { + max_size == std::stoi(argv[2]); + } + pt::ptree config; string instance = ""; string access_token = ""; string feedurl = ""; const string profile = argv[1]; + std::uint16_t ret; read_config(config, profile, instance, access_token, feedurl); std::size_t pos = 0; @@ -180,19 +209,18 @@ int main(int argc, char *argv[]) string answer; string last_entry = config.get(profile + ".last_entry", ""); std::vector entries; - http_get(hostname, path, answer); + ret = http_get(hostname, path, answer); + if (ret != 0) + { + return ret; + } entries = parse_website(profile, answer); if (last_entry.empty()) { last_entry = entries.at(1); - config.put(profile + ".last_entry", last_entry); } - else - { - config.put(profile + ".last_entry", entries.front()); - } - pt::write_json(filepath + "config-" + profile + ".json", config); + config.put(profile + ".last_entry", entries.front()); bool new_content = false; for (auto rit = entries.rbegin(); rit != entries.rend(); ++rit) @@ -208,7 +236,6 @@ int main(int argc, char *argv[]) } string answer; - std::uint16_t ret; Mastodon::API masto(instance, access_token); API::parametermap parameters = @@ -220,11 +247,12 @@ int main(int argc, char *argv[]) if (ret == 0) { - //cout << answer << '\n'; + pt::write_json(filepath + "config-" + profile + ".json", config); } else { std::cerr << "Error code: " << ret << '\n'; + std::cerr << answer << '\n'; return ret; } diff --git a/watchwords.json b/watchwords.json index b6a0ed5..7741314 100644 --- a/watchwords.json +++ b/watchwords.json @@ -42,7 +42,7 @@ "Botswana", "CentralAfricanRepublic", "Canada", - "Cocos(Keeling)Islands", + "CocosIslands", "Switzerland", "Chile", "China",