Use StringBuilder

This commit is contained in:
Andreas Bielawski 2020-03-07 01:11:58 +01:00
parent af1ae0883c
commit 30cdf087cc
No known key found for this signature in database
GPG Key ID: D2073645DC2C3DE5

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Web; using System.Web;
using CodeHollow.FeedReader; using CodeHollow.FeedReader;
@ -34,17 +35,16 @@ namespace RSSBot {
} }
public static async void Subscribe(Message message, GroupCollection args) { public static async void Subscribe(Message message, GroupCollection args) {
string url = args[1].Value; var url = args[1].Value;
long chatId = message.Chat.Id; var chatId = message.Chat.Id;
RssBotFeed feed = new RssBotFeed(url); var feed = new RssBotFeed(url);
await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing);
if (args.Count > 2) { if (args.Count > 2) {
string chatName = args[2].Value; var chatName = args[2].Value;
if (!chatName.StartsWith("@")) { if (!chatName.StartsWith("@")) chatName = $"@{chatName}";
chatName = $"@{chatName}";
}
Chat chatInfo; Chat chatInfo;
try { try {
chatInfo = await Bot.BotClient.GetChatAsync(chatName); chatInfo = await Bot.BotClient.GetChatAsync(chatName);
@ -75,11 +75,10 @@ namespace RSSBot {
// Check if we already have the feed // Check if we already have the feed
RssBotFeed existingFeed = Bot.RssBotFeeds RssBotFeed existingFeed = Bot.RssBotFeeds
.FirstOrDefault(x => x.Url.ToLower().Equals(feed.Url.ToLower())); .FirstOrDefault(x => x.Url.ToLower().Equals(feed.Url.ToLower()));
if (existingFeed == null) { if (existingFeed == null)
Bot.RssBotFeeds.Add(feed); Bot.RssBotFeeds.Add(feed);
} else { else
feed = existingFeed; feed = existingFeed;
}
// Check if chat already subscribed // Check if chat already subscribed
if (feed.Subs.Contains(chatId)) { if (feed.Subs.Contains(chatId)) {
@ -92,16 +91,15 @@ namespace RSSBot {
} }
public static async void Unsubscribe(Message message, GroupCollection args) { public static async void Unsubscribe(Message message, GroupCollection args) {
string url = args[1].Value; var url = args[1].Value;
long chatId = message.Chat.Id; var chatId = message.Chat.Id;
RssBotFeed feed = Bot.RssBotFeeds RssBotFeed feed = Bot.RssBotFeeds
.FirstOrDefault(x => x.Url.ToLower().Equals(url.ToLower())); .FirstOrDefault(x => x.Url.ToLower().Equals(url.ToLower()));
if (args.Count > 2) { if (args.Count > 2) {
string chatName = args[2].Value; var chatName = args[2].Value;
if (!chatName.StartsWith("@")) { if (!chatName.StartsWith("@")) chatName = $"@{chatName}";
chatName = $"@{chatName}";
}
Chat chatInfo; Chat chatInfo;
try { try {
chatInfo = await Bot.BotClient.GetChatAsync(chatName); chatInfo = await Bot.BotClient.GetChatAsync(chatName);
@ -125,24 +123,21 @@ namespace RSSBot {
} }
feed.Cleanup(chatId); feed.Cleanup(chatId);
if (feed.Subs.Count == 0) { if (feed.Subs.Count == 0) Bot.RssBotFeeds.Remove(feed);
Bot.RssBotFeeds.Remove(feed);
}
await Bot.BotClient.SendTextMessageAsync(message.Chat, "✅ Feed deabonniert!"); await Bot.BotClient.SendTextMessageAsync(message.Chat, "✅ Feed deabonniert!");
Bot.Save(); Bot.Save();
} }
public static async void Show(Message message, GroupCollection args) { public static async void Show(Message message, GroupCollection args) {
long chatId = message.Chat.Id; var chatId = message.Chat.Id;
string chatTitle = message.Chat.Type.Equals(ChatType.Private) ? message.Chat.FirstName : message.Chat.Title; var chatTitle = message.Chat.Type.Equals(ChatType.Private) ? message.Chat.FirstName : message.Chat.Title;
await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing); await Bot.BotClient.SendChatActionAsync(message.Chat, ChatAction.Typing);
if (args.Count > 1) { if (args.Count > 1) {
string chatName = args[1].Value; var chatName = args[1].Value;
if (!chatName.StartsWith("@")) { if (!chatName.StartsWith("@")) chatName = $"@{chatName}";
chatName = $"@{chatName}";
}
Chat chatInfo; Chat chatInfo;
try { try {
@ -161,25 +156,23 @@ namespace RSSBot {
return; return;
} }
} }
string text;
List<RssBotFeed> feeds = Bot.RssBotFeeds.Where(x => x.Subs.Contains(chatId)).ToList(); var feeds = Bot.RssBotFeeds.Where(x => x.Subs.Contains(chatId)).ToList();
var text = new StringBuilder();
if (feeds.Count < 1) { if (feeds.Count < 1) {
text = "❌ Keine Feeds abonniert."; text.Append("❌ Keine Feeds abonniert.");
} else { } else {
text = $"<strong>{HttpUtility.HtmlEncode(chatTitle)}</strong> hat abonniert:\n"; text.Append($"<strong>{HttpUtility.HtmlEncode(chatTitle)}</strong> hat abonniert:\n");
for (int i = 0; i < feeds.Count; i++) { for (var i = 0; i < feeds.Count; i++) text.Append($"<strong>{i + 1}</strong>) {feeds[i].Url}\n");
text += $"<strong>{i + 1}</strong>) {feeds[i].Url}\n";
}
} }
await Bot.BotClient.SendTextMessageAsync(message.Chat, text, ParseMode.Html, true);
await Bot.BotClient.SendTextMessageAsync(message.Chat, text.ToString(), ParseMode.Html, true);
} }
public static async void Sync() { public static async void Sync() {
Logger.Info(("================================")); Logger.Info("================================");
bool hadEntries = false; var hadEntries = false;
foreach (RssBotFeed feed in Bot.RssBotFeeds.ToList()) { foreach (RssBotFeed feed in Bot.RssBotFeeds.ToList()) {
Logger.Info(feed.Url); Logger.Info(feed.Url);
try { try {
@ -200,13 +193,11 @@ namespace RSSBot {
: $"{feed.NewEntries.Count} neue Beiträge"); : $"{feed.NewEntries.Count} neue Beiträge");
foreach (FeedItem entry in feed.NewEntries) { foreach (FeedItem entry in feed.NewEntries) {
string postTitle = "Kein Titel"; var postTitle = "Kein Titel";
if (!string.IsNullOrWhiteSpace(entry.Title)) { if (!string.IsNullOrWhiteSpace(entry.Title)) postTitle = Utils.StripHtml(entry.Title);
postTitle = Utils.StripHtml(entry.Title);
}
string postLink = feed.MainLink; var postLink = feed.MainLink;
string linkName = postLink; var linkName = postLink;
if (!string.IsNullOrWhiteSpace(entry.Link)) { if (!string.IsNullOrWhiteSpace(entry.Link)) {
postLink = entry.Link; postLink = entry.Link;
// FeedProxy URLs // FeedProxy URLs
@ -216,51 +207,44 @@ namespace RSSBot {
} }
// Remove "www." // Remove "www."
int index = linkName.IndexOf("www.", StringComparison.Ordinal); var index = linkName.IndexOf("www.", StringComparison.Ordinal);
if (index > 0) { if (index > 0) linkName = linkName.Remove(index, 4);
linkName = linkName.Remove(index, 4);
}
string content = ""; var content = "";
if (!string.IsNullOrWhiteSpace(entry.Content)) { if (!string.IsNullOrWhiteSpace(entry.Content))
content = Utils.ProcessContent(entry.Content); // magic processing missing content = Utils.ProcessContent(entry.Content); // magic processing missing
} else if (!string.IsNullOrWhiteSpace(entry.Description)) { else if (!string.IsNullOrWhiteSpace(entry.Description))
content = Utils.ProcessContent(entry.Description); content = Utils.ProcessContent(entry.Description);
}
string text = $"<b>{postTitle}</b>\n<i>{feed.Title}</i>\n{content}"; var text = $"<b>{postTitle}</b>\n<i>{feed.Title}</i>\n{content}";
text += $"\n<a href=\"{postLink}\">Weiterlesen auf {linkName}</a>"; text += $"\n<a href=\"{postLink}\">Weiterlesen auf {linkName}</a>";
// Send // Send
foreach (long chatId in feed.Subs.ToList()) { foreach (var chatId in feed.Subs.ToList())
try { try {
await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true); await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true);
} catch (ApiRequestException e) { } catch (ApiRequestException e) {
if (e.ErrorCode.Equals(403)) { if (e.ErrorCode.Equals(403)) {
Logger.Warn(e.Message); Logger.Warn(e.Message);
feed.Cleanup(chatId); feed.Cleanup(chatId);
if (feed.Subs.Count == 0) { // was last subscriber if (feed.Subs.Count == 0) // was last subscriber
Bot.RssBotFeeds.Remove(feed); Bot.RssBotFeeds.Remove(feed);
}
} else { } else {
Logger.Error($"{e.ErrorCode}: {e.Message}"); Logger.Error($"{e.ErrorCode}: {e.Message}");
} }
} }
}
} }
} }
Logger.Info("Nächster Check in 60 Sekunden"); Logger.Info("Nächster Check in 60 Sekunden");
if (hadEntries) { if (hadEntries) Bot.Save();
Bot.Save();
}
Bot.JobQueue.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(-1)); Bot.JobQueue.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(-1));
} }
public static async void ShowAvailableFeeds(Message message, GroupCollection args) { public static async void ShowAvailableFeeds(Message message, GroupCollection args) {
string url = args[1].Value; var url = args[1].Value;
IEnumerable<HtmlFeedLink> feeds; IEnumerable<HtmlFeedLink> feeds;
try { try {
feeds = await FeedReader.GetFeedUrlsFromUrlAsync(url); feeds = await FeedReader.GetFeedUrlsFromUrlAsync(url);
@ -269,14 +253,15 @@ namespace RSSBot {
return; return;
} }
List<HtmlFeedLink> htmlFeedLinks = feeds.ToList(); var htmlFeedLinks = feeds.ToList();
if (htmlFeedLinks.Count == 0) { if (htmlFeedLinks.Count == 0) {
await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Keine Feeds gefunden."); await Bot.BotClient.SendTextMessageAsync(message.Chat, "❌ Keine Feeds gefunden.");
return; return;
} }
string text = htmlFeedLinks.Aggregate("Feeds gefunden:\n", var text = htmlFeedLinks.Aggregate("Feeds gefunden:\n",
(current, feedLink) => current + $"* <a href=\"{feedLink.Url}\">{Utils.StripHtml(feedLink.Title)}</a>\n"); (current, feedLink) =>
current + $"* <a href=\"{feedLink.Url}\">{Utils.StripHtml(feedLink.Title)}</a>\n");
await Bot.BotClient.SendTextMessageAsync(message.Chat, text, ParseMode.Html, true); await Bot.BotClient.SendTextMessageAsync(message.Chat, text, ParseMode.Html, true);
} }