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.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<NewsListResponse>(content);
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);
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 result = streamReader.ReadToEnd();
return result;
}
var handler = new HttpClientHandler
{
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.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));
}
}
}
+1 -2
View File
@@ -2,14 +2,13 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<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="HttpToSocks5Proxy" Version="1.4.0" />
</ItemGroup>
</Project>
+3 -2
View File
@@ -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;