This commit is contained in:
kislovdm
2022-05-16 07:42:46 +03:00
parent f960a86cad
commit 31bd70f971
4 changed files with 32 additions and 20 deletions
+24 -13
View File
@@ -3,7 +3,8 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using MihaZupan; using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using RgDevBot.Config; using RgDevBot.Config;
using RgDevBot.ObjectModel; using RgDevBot.ObjectModel;
@@ -22,10 +23,10 @@ namespace RgDevBot
_config = config; _config = config;
} }
public void Parse() public async Task Parse()
{ {
var url = "https://rg-dev.ru/api/news/?type=news"; var url = "https://rg-dev.ru/api/news/?type=news";
var content = Get(url); var content = await Get(url);
var news = JsonConvert.DeserializeObject<NewsListResponse>(content); var news = JsonConvert.DeserializeObject<NewsListResponse>(content);
var latestNews = news?.results; var latestNews = news?.results;
@@ -55,19 +56,29 @@ namespace RgDevBot
} }
} }
public string Get(string url, string contentType = "application/json") public async Task<string> Get(string url, string contentType = "application/json")
{ {
var httpRequest = (HttpWebRequest)WebRequest.Create(url); var client = GetClientWithProxy(new Uri("http://192.168.1.6:9150"));
httpRequest.ContentType = contentType; var result = await client.GetAsync(url);
httpRequest.Method = "GET"; return await result.Content.ReadAsStringAsync();
httpRequest.Proxy = new HttpToSocks5Proxy("192.168.1.6", 9000); }
var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); private HttpClient GetClientWithProxy(Uri proxy)
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{ {
var result = streamReader.ReadToEnd(); var handler = new HttpClientHandler
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;
} }
} }
} }
+4 -3
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using NLog; using NLog;
using RgDevBot.Config; using RgDevBot.Config;
@@ -7,7 +8,7 @@ namespace RgDevBot
{ {
class Program class Program
{ {
static void Main(string[] args) static async Task Main(string[] args)
{ {
var config = new SentConfig(LogManager.GetCurrentClassLogger()); var config = new SentConfig(LogManager.GetCurrentClassLogger());
var bot = new TelegramBot(); var bot = new TelegramBot();
@@ -16,13 +17,13 @@ namespace RgDevBot
{ {
try try
{ {
parser.Parse(); await parser.Parse();
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); Console.WriteLine(e);
} }
Thread.Sleep(TimeSpan.FromSeconds(60)); await Task.Delay(TimeSpan.FromSeconds(60));
} }
} }
} }
+1 -2
View File
@@ -2,14 +2,13 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net6.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<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="NLog" Version="4.7.7" />
<PackageReference Include="Telegram.Bot" Version="10.4.0" /> <PackageReference Include="Telegram.Bot" Version="10.4.0" />
<PackageReference Include="HttpToSocks5Proxy" Version="1.4.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+3 -2
View File
@@ -10,7 +10,8 @@ namespace RgDevBot
public TelegramBot() public TelegramBot()
{ {
var token = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN"); var token = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN") ??
"1479535614:AAFqHdsUFikzOti0zknK_pN2sEONNAXecPY";
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
throw new Exception("Please define TELEGRAM_TOKEN variable."); throw new Exception("Please define TELEGRAM_TOKEN variable.");
@@ -20,7 +21,7 @@ namespace RgDevBot
long.TryParse(Environment.GetEnvironmentVariable("TELEGRAM_CHAT_ID"), out var chatId); long.TryParse(Environment.GetEnvironmentVariable("TELEGRAM_CHAT_ID"), out var chatId);
if (chatId == 0) if (chatId == 0)
{ {
throw new Exception("Please define TELEGRAM_CHAT_ID variable."); //throw new Exception("Please define TELEGRAM_CHAT_ID variable.");
} }
MainChatId = chatId; MainChatId = chatId;