RikoBot-Sharp/Utils.cs

107 lines
3.4 KiB
C#
Raw Normal View History

2020-03-13 13:57:10 +01:00
using System.Linq;
2020-01-30 20:57:41 +01:00
using System.Text.RegularExpressions;
using System.Threading.Tasks;
2020-03-13 13:57:10 +01:00
using System.Web;
2020-01-30 20:57:41 +01:00
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace RSSBot {
public static class Utils {
2020-03-13 13:57:10 +01:00
private static readonly Regex RegexHtml = new Regex("<.*?>");
2020-11-05 19:34:45 +01:00
public static string StripHtml(string input)
{
2020-03-13 13:57:10 +01:00
return RegexHtml.Replace(input, string.Empty).Trim();
2020-01-30 20:57:41 +01:00
}
2020-12-20 23:43:17 +01:00
public static string EscapeHtml(string input)
{
input = input.Replace("<", "&lt;");
input = input.Replace(">", "&gt;");
return input;
}
2020-11-05 19:34:45 +01:00
private static string CleanRss(string input)
{
2020-01-30 20:57:41 +01:00
string[] replacements = {
2020-03-13 14:29:07 +01:00
"[←]",
2020-01-30 20:57:41 +01:00
"[…]",
2020-03-13 14:29:07 +01:00
"[...]",
2020-01-30 20:57:41 +01:00
"[bilder]",
"[boerse]",
"[mehr]",
"[video]",
"...[more]",
"[more]",
"[liveticker]",
"[livestream]",
"[multimedia]",
"[sportschau]",
"[phoenix]",
"[swr]",
"[ndr]",
"[mdr]",
"[rbb]",
"[wdr]",
"[hr]",
"[br]",
"Click for full.",
"Read more »",
"Read more",
"...Read More",
2020-05-03 18:58:55 +02:00
"...mehr lesen",
"mehr lesen",
2020-01-30 20:57:41 +01:00
"(more…)",
"View On WordPress",
"Continue reading →",
2020-03-13 14:29:07 +01:00
"» weiterlesen",
"Ein Kommentar.",
"Änderungen zeigen",
"(Feed generated with FetchRSS)",
2020-01-30 20:57:41 +01:00
"(RSS generated with FetchRss)",
"-- Delivered by Feed43 service",
"Meldung bei www.tagesschau.de lesen"
};
string[] regexReplacements = {
"Der Beitrag.*erschien zuerst auf .+.",
"The post.*appeared first on .+.",
2020-03-13 14:29:07 +01:00
"http://www.serienjunkies.de/.*.html",
"Nächstältere Version.*",
"Die Seite wurde neu angelegt.*",
"Weiterleitung nach.*erstellt.*"
2020-01-30 20:57:41 +01:00
};
2020-03-13 14:29:07 +01:00
input = input.Replace("\n", " ");
2020-01-30 20:57:41 +01:00
input = replacements.Aggregate(input, (current, replacement) => current.Replace(replacement, ""));
input = regexReplacements.Aggregate(input,
(current, replacement) => Regex.Replace(current, replacement, ""));
return input;
}
2020-11-05 19:34:45 +01:00
public static string ProcessContent(string input)
{
string content = StripHtml(HttpUtility.HtmlDecode(input));
2020-01-30 20:57:41 +01:00
content = CleanRss(content);
2020-11-05 19:34:45 +01:00
if (content.Length > 250) {
content = content.Substring(0, 250) + "...";
}
2020-01-30 20:57:41 +01:00
return content;
}
2020-11-05 19:34:45 +01:00
public static GroupCollection ReturnMatches(string text, string pattern)
{
2020-01-30 20:57:41 +01:00
return Regex.Match(text,
pattern,
RegexOptions.IgnoreCase
).Groups;
}
2020-11-05 19:34:45 +01:00
public static async Task<bool> IsBotAdmin(long chatId)
{
2020-01-30 20:57:41 +01:00
ChatMember chatMember = await Bot.BotClient.GetChatMemberAsync(chatId, Bot.BotClient.BotId);
return chatMember.Status.Equals(ChatMemberStatus.Administrator);
}
}
2020-03-13 14:29:07 +01:00
}