mmcs-quotes-bridge/posts.cpp

62 lines
2.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "posts.h"
#include "config.h"
#include <regex>
#include <string>
#include <string_view>
#include <variant>
using namespace posts;
static std::regex vkRegex("«\\s*#[A-Za-z0-9\\-_.А-Яа-яЁё]+@mmcs_quotes\\s*»");
static std::regex tgRegex1("#цитат(а|ы)");
static std::regex tgRegex2("- ");
bool posts::filter_and_transform(AbstractPost &post) {
if (post.source == SRC_VK) {
bool hasHashtag = std::regex_search(post.text, vkRegex);
if (!hasHashtag) return false;
return true;
} else {
if (std::regex_search(post.text, tgRegex1)) return true;
int lastLinebreak = post.text.find_last_of('\n');
std::string lastLine = post.text.substr(lastLinebreak == -1 ? 0 : lastLinebreak, post.text.size());
if (std::regex_search(lastLine, tgRegex2)) {
post.text = post.text.substr(0, lastLinebreak);
return true;
}
return false;
}
}
std::string_view posts::add_signature(AbstractPost &post, config::AppConfig *cfg) {
if (post.source == SRC_VK) {
return add_vk_signature(post.text, cfg);
} else {
return add_tg_signature(post.text, cfg);
}
}
std::string_view posts::add_vk_signature(std::string &text, config::AppConfig *cfg) {
std::string shortname = std::holds_alternative<std::string>(cfg->vkSource) ? std::get<std::string>(cfg->vkSource) : cfg->vkSourceLink;
std::string signature = "\nРепостнуто автоматически из ";
if (!shortname.empty())
signature += "vk.com/" + shortname;
else
signature += "VK";
int oldPostLength = text.length() + 1;
text += signature;
return std::string_view(text.c_str() + oldPostLength, signature.length());
}
std::string_view posts::add_tg_signature(std::string &text, config::AppConfig *cfg) {
std::string shortname = cfg->tgSourceLink;
std::string signature = "\nРепостнуто автоматически из ";
if (!shortname.empty())
signature += "t.me/" + shortname;
else
signature += "Telegram";
int oldPostLength = text.length() + 1;
text += signature;
return std::string_view(text.c_str() + oldPostLength, signature.length());
}