This commit is contained in:
2021-02-05 19:28:02 +03:00
parent 8c9e69720b
commit 0000837c40
7 changed files with 133 additions and 11 deletions
+2
View File
@@ -4,6 +4,8 @@ services:
build: ./ build: ./
container_name: rgdev container_name: rgdev
restart: always restart: always
volumes:
- "./sent.json:/app/sent.json"
environment: environment:
- TZ=Europe/Moscow - TZ=Europe/Moscow
logging: logging:
+32
View File
@@ -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
]
+59
View File
@@ -0,0 +1,59 @@
using System;
using System.IO;
using Newtonsoft.Json;
using NLog;
namespace RgDevBot.Config
{
public abstract class AbstractConfig<T>
{
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<T>();
}
protected T Load()
{
try
{
var js = File.ReadAllText(_fileName);
return JsonConvert.DeserializeObject<T>(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, "Ошибка сохранения файла.");
}
}
}
}
+12
View File
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using NLog;
namespace RgDevBot.Config
{
public class SentConfig: AbstractConfig<List<int>>
{
public SentConfig(ILogger logger) : base(logger, "sent.json")
{
}
}
}
+23 -10
View File
@@ -6,6 +6,7 @@ using System.Net;
using System.Text; using System.Text;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
using RgDevBot.Config;
using RgDevBot.ObjectModel; using RgDevBot.ObjectModel;
namespace RgDevBot namespace RgDevBot
@@ -13,11 +14,13 @@ namespace RgDevBot
public class NewsParser public class NewsParser
{ {
private readonly TelegramBot _bot; private readonly TelegramBot _bot;
private readonly SentConfig _config;
private int _lastSentId = -1; private int _lastSentId = -1;
public NewsParser(TelegramBot bot) public NewsParser(TelegramBot bot, SentConfig config)
{ {
_bot = bot; _bot = bot;
_config = config;
} }
public void Parse() public void Parse()
@@ -26,20 +29,30 @@ namespace RgDevBot
var content = Get(url); var content = Get(url);
var news = JsonConvert.DeserializeObject<NewsListResponse>(content); var news = JsonConvert.DeserializeObject<NewsListResponse>(content);
var latestNews = news.results.OrderByDescending(f => f.id).FirstOrDefault(); var latestNews = news.results;
foreach (var post in latestNews)
if (latestNews != null && latestNews.id != _lastSentId)
{ {
var text = $"{latestNews.title}:\r\nhttps://rg-dev.ru/press/news/all/{latestNews.id}/"; if (_config.ConfigValues.Contains(post.id))
Console.WriteLine(text); {
_bot.SendMessage(text); 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") public string Get(string url, string contentType = "application/json")
{ {
var httpRequest = (HttpWebRequest)WebRequest.Create(url); var httpRequest = (HttpWebRequest)WebRequest.Create(url);
+4 -1
View File
@@ -1,5 +1,7 @@
using System; using System;
using System.Threading; using System.Threading;
using NLog;
using RgDevBot.Config;
namespace RgDevBot namespace RgDevBot
{ {
@@ -7,8 +9,9 @@ namespace RgDevBot
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
var config = new SentConfig(LogManager.GetCurrentClassLogger());
var bot = new TelegramBot(); var bot = new TelegramBot();
var parser = new NewsParser(bot); var parser = new NewsParser(bot, config);
while (true) while (true)
{ {
parser.Parse(); parser.Parse();
+1
View File
@@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.7.7" />
<PackageReference Include="Telegram.Bot" Version="10.4.0" /> <PackageReference Include="Telegram.Bot" Version="10.4.0" />
</ItemGroup> </ItemGroup>