diff --git a/docker-compose.yaml b/docker-compose.yaml index 64dc93e..eb8e9b0 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,6 +4,8 @@ services: build: ./ container_name: rgdev restart: always + volumes: + - "./sent.json:/app/sent.json" environment: - TZ=Europe/Moscow logging: diff --git a/sent.json b/sent.json new file mode 100644 index 0000000..2d22911 --- /dev/null +++ b/sent.json @@ -0,0 +1,32 @@ +[ + 622, + 621, + 620, + 619, + 617, + 616, + 618, + 615, + 614, + 613, + 612, + 611, + 609, + 608, + 607, + 605, + 604, + 603, + 610, + 602, + 601, + 600, + 598, + 597, + 596, + 595, + 594, + 593, + 592, + 591 +] \ No newline at end of file diff --git a/src/RgDevBot/Config/AbstractConfig.cs b/src/RgDevBot/Config/AbstractConfig.cs new file mode 100644 index 0000000..6e52cdd --- /dev/null +++ b/src/RgDevBot/Config/AbstractConfig.cs @@ -0,0 +1,59 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using NLog; + +namespace RgDevBot.Config +{ + public abstract class AbstractConfig + { + protected readonly ILogger _logger; + public T ConfigValues { get; set; } + + private readonly object _lock = new object(); + private readonly string _fileName; + + protected AbstractConfig(ILogger logger, string fileName) + { + _logger = logger; + _fileName = fileName ?? "dbproducts.json"; + ConfigValues = Load() ?? Activator.CreateInstance(); + } + + protected T Load() + { + try + { + var js = File.ReadAllText(_fileName); + return JsonConvert.DeserializeObject(js); + } + catch (Exception e1) + { + _logger.Error(e1, "Ошибка загрузки файла."); + Save(); + return default; + } + } + + public virtual void Save() + { + try + { + lock (_lock) + { + + File.WriteAllText(_fileName, + JsonConvert.SerializeObject(ConfigValues, + Formatting.Indented, + new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore } + ) + ); + } + } + catch (Exception e) + { + _logger.Error(e, "Ошибка сохранения файла."); + } + } + } +} diff --git a/src/RgDevBot/Config/SentConfig.cs b/src/RgDevBot/Config/SentConfig.cs new file mode 100644 index 0000000..0bebe44 --- /dev/null +++ b/src/RgDevBot/Config/SentConfig.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; +using NLog; + +namespace RgDevBot.Config +{ + public class SentConfig: AbstractConfig> + { + public SentConfig(ILogger logger) : base(logger, "sent.json") + { + } + } +} diff --git a/src/RgDevBot/NewsParser.cs b/src/RgDevBot/NewsParser.cs index bdfcaea..519a821 100644 --- a/src/RgDevBot/NewsParser.cs +++ b/src/RgDevBot/NewsParser.cs @@ -6,6 +6,7 @@ using System.Net; using System.Text; using System.Text.Json.Serialization; using Newtonsoft.Json; +using RgDevBot.Config; using RgDevBot.ObjectModel; namespace RgDevBot @@ -13,11 +14,13 @@ namespace RgDevBot public class NewsParser { private readonly TelegramBot _bot; + private readonly SentConfig _config; private int _lastSentId = -1; - public NewsParser(TelegramBot bot) + public NewsParser(TelegramBot bot, SentConfig config) { _bot = bot; + _config = config; } public void Parse() @@ -26,20 +29,30 @@ namespace RgDevBot var content = Get(url); var news = JsonConvert.DeserializeObject(content); - var latestNews = news.results.OrderByDescending(f => f.id).FirstOrDefault(); - - if (latestNews != null && latestNews.id != _lastSentId) + var latestNews = news.results; + foreach (var post in latestNews) { - var text = $"{latestNews.title}:\r\nhttps://rg-dev.ru/press/news/all/{latestNews.id}/"; - Console.WriteLine(text); - _bot.SendMessage(text); + if (_config.ConfigValues.Contains(post.id)) + { + continue; + } - _lastSentId = latestNews.id; + try + { + var text = $"{post.title}:\r\nhttps://rg-dev.ru/press/news/all/{post.id}/"; + Console.WriteLine(text); + _bot.SendMessage(text); + + _config.ConfigValues.Add(post.id); + _config.Save(); + } + catch (Exception ex) + { + Console.WriteLine(ex); + } } } - - public string Get(string url, string contentType = "application/json") { var httpRequest = (HttpWebRequest)WebRequest.Create(url); diff --git a/src/RgDevBot/Program.cs b/src/RgDevBot/Program.cs index 2914303..6c01e85 100644 --- a/src/RgDevBot/Program.cs +++ b/src/RgDevBot/Program.cs @@ -1,5 +1,7 @@ using System; using System.Threading; +using NLog; +using RgDevBot.Config; namespace RgDevBot { @@ -7,8 +9,9 @@ namespace RgDevBot { static void Main(string[] args) { + var config = new SentConfig(LogManager.GetCurrentClassLogger()); var bot = new TelegramBot(); - var parser = new NewsParser(bot); + var parser = new NewsParser(bot, config); while (true) { parser.Parse(); diff --git a/src/RgDevBot/RgDevBot.csproj b/src/RgDevBot/RgDevBot.csproj index 43e675b..ee2fdca 100644 --- a/src/RgDevBot/RgDevBot.csproj +++ b/src/RgDevBot/RgDevBot.csproj @@ -7,6 +7,7 @@ +