From 31bd70f971bb634acc6963692041876aae180392 Mon Sep 17 00:00:00 2001 From: kislovdm Date: Mon, 16 May 2022 07:42:46 +0300 Subject: [PATCH] rgdev++ --- src/RgDevBot/NewsParser.cs | 37 +++++++++++++++++++++++------------- src/RgDevBot/Program.cs | 7 ++++--- src/RgDevBot/RgDevBot.csproj | 3 +-- src/RgDevBot/TelegramBot.cs | 5 +++-- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/RgDevBot/NewsParser.cs b/src/RgDevBot/NewsParser.cs index 140ec23..6c53105 100644 --- a/src/RgDevBot/NewsParser.cs +++ b/src/RgDevBot/NewsParser.cs @@ -3,7 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; -using MihaZupan; +using System.Net.Http; +using System.Threading.Tasks; using Newtonsoft.Json; using RgDevBot.Config; using RgDevBot.ObjectModel; @@ -22,10 +23,10 @@ namespace RgDevBot _config = config; } - public void Parse() + public async Task Parse() { var url = "https://rg-dev.ru/api/news/?type=news"; - var content = Get(url); + var content = await Get(url); var news = JsonConvert.DeserializeObject(content); var latestNews = news?.results; @@ -55,19 +56,29 @@ namespace RgDevBot } } - public string Get(string url, string contentType = "application/json") + public async Task Get(string url, string contentType = "application/json") { - var httpRequest = (HttpWebRequest)WebRequest.Create(url); - httpRequest.ContentType = contentType; - httpRequest.Method = "GET"; - httpRequest.Proxy = new HttpToSocks5Proxy("192.168.1.6", 9000); + var client = GetClientWithProxy(new Uri("http://192.168.1.6:9150")); + var result = await client.GetAsync(url); + return await result.Content.ReadAsStringAsync(); + } - var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); - using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + private HttpClient GetClientWithProxy(Uri proxy) + { + var handler = new HttpClientHandler { - var result = streamReader.ReadToEnd(); - return result; - } + Proxy = new WebProxy($"socks5://{proxy.Host}:{proxy.Port}"), + CheckCertificateRevocationList = false, + MaxConnectionsPerServer = int.MaxValue, + ServerCertificateCustomValidationCallback = (_, _, _, _) => true, + UseCookies = true, + UseProxy = true, + }; + var client = new HttpClient(handler) + { + Timeout = TimeSpan.FromSeconds(30), + }; + return client; } } } diff --git a/src/RgDevBot/Program.cs b/src/RgDevBot/Program.cs index d4775da..d5b0126 100644 --- a/src/RgDevBot/Program.cs +++ b/src/RgDevBot/Program.cs @@ -1,5 +1,6 @@ using System; using System.Threading; +using System.Threading.Tasks; using NLog; using RgDevBot.Config; @@ -7,7 +8,7 @@ namespace RgDevBot { class Program { - static void Main(string[] args) + static async Task Main(string[] args) { var config = new SentConfig(LogManager.GetCurrentClassLogger()); var bot = new TelegramBot(); @@ -16,13 +17,13 @@ namespace RgDevBot { try { - parser.Parse(); + await parser.Parse(); } catch (Exception e) { Console.WriteLine(e); } - Thread.Sleep(TimeSpan.FromSeconds(60)); + await Task.Delay(TimeSpan.FromSeconds(60)); } } } diff --git a/src/RgDevBot/RgDevBot.csproj b/src/RgDevBot/RgDevBot.csproj index 23f7b01..a11899e 100644 --- a/src/RgDevBot/RgDevBot.csproj +++ b/src/RgDevBot/RgDevBot.csproj @@ -2,14 +2,13 @@ Exe - netcoreapp3.1 + net6.0 - diff --git a/src/RgDevBot/TelegramBot.cs b/src/RgDevBot/TelegramBot.cs index 6834507..45767f1 100644 --- a/src/RgDevBot/TelegramBot.cs +++ b/src/RgDevBot/TelegramBot.cs @@ -10,7 +10,8 @@ namespace RgDevBot public TelegramBot() { - var token = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN"); + var token = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN") ?? + "1479535614:AAFqHdsUFikzOti0zknK_pN2sEONNAXecPY"; if (string.IsNullOrEmpty(token)) { throw new Exception("Please define TELEGRAM_TOKEN variable."); @@ -20,7 +21,7 @@ namespace RgDevBot long.TryParse(Environment.GetEnvironmentVariable("TELEGRAM_CHAT_ID"), out var chatId); if (chatId == 0) { - throw new Exception("Please define TELEGRAM_CHAT_ID variable."); + //throw new Exception("Please define TELEGRAM_CHAT_ID variable."); } MainChatId = chatId;