mmcs-quotes-bridge/posts.cpp

52 lines
1.7 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.sourceType == 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) {
int oldPostLength = post.text.length() + 1;
std::string signature = "\nРепостнуто автоматически из ";
if (post.sourceType == SRC_VK) {
auto &wallId = cfg->vkSources[post.source].id;
std::string shortname = std::holds_alternative<std::string>(wallId) ? std::get<std::string>(wallId) : cfg->vkSources[post.source].link;
if (!shortname.empty())
signature += "vk.com/" + shortname;
else
signature += "VK";
post.text += signature;
} else {
std::string shortname = cfg->tgSources[post.source].link;
if (!shortname.empty())
signature += "t.me/" + shortname;
else
signature += "Telegram";
post.text += signature;
}
return std::string_view(post.text.c_str() + oldPostLength, signature.length());
}