From b86507ccd7879e1896f3afa6fd1d134c16737476 Mon Sep 17 00:00:00 2001 From: Monster1025 Date: Tue, 3 Nov 2020 19:16:19 +0300 Subject: [PATCH] Init --- src/Covid-Record.sln | 25 +++++++ src/Covid-Record/Covid-Record.csproj | 13 ++++ src/Covid-Record/CovidAvailability.cs | 94 +++++++++++++++++++++++++++ src/Covid-Record/EMaisAnswer.cs | 64 ++++++++++++++++++ src/Covid-Record/Program.cs | 33 ++++++++++ src/Covid-Record/TelegramBot.cs | 32 +++++++++ 6 files changed, 261 insertions(+) create mode 100644 src/Covid-Record.sln create mode 100644 src/Covid-Record/Covid-Record.csproj create mode 100644 src/Covid-Record/CovidAvailability.cs create mode 100644 src/Covid-Record/EMaisAnswer.cs create mode 100644 src/Covid-Record/Program.cs create mode 100644 src/Covid-Record/TelegramBot.cs diff --git a/src/Covid-Record.sln b/src/Covid-Record.sln new file mode 100644 index 0000000..5d06b92 --- /dev/null +++ b/src/Covid-Record.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30320.27 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Covid-Record", "Covid-Record\Covid-Record.csproj", "{C6056CE1-CEE0-4EC4-9717-E61B8A3D9DD6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C6056CE1-CEE0-4EC4-9717-E61B8A3D9DD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6056CE1-CEE0-4EC4-9717-E61B8A3D9DD6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6056CE1-CEE0-4EC4-9717-E61B8A3D9DD6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6056CE1-CEE0-4EC4-9717-E61B8A3D9DD6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {80533E98-F094-49A1-9816-B33937498F12} + EndGlobalSection +EndGlobal diff --git a/src/Covid-Record/Covid-Record.csproj b/src/Covid-Record/Covid-Record.csproj new file mode 100644 index 0000000..c5c8d91 --- /dev/null +++ b/src/Covid-Record/Covid-Record.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.1 + Covid_Record + + + + + + + diff --git a/src/Covid-Record/CovidAvailability.cs b/src/Covid-Record/CovidAvailability.cs new file mode 100644 index 0000000..c400154 --- /dev/null +++ b/src/Covid-Record/CovidAvailability.cs @@ -0,0 +1,94 @@ +using System; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using Newtonsoft.Json; + +namespace Covid_Record +{ + public class CovidAvailability + { + private readonly TelegramBot _telegram; + + public CovidAvailability(TelegramBot telegram) + { + _telegram = telegram; + } + + public string GetSession(string phone, string password) + { + var content = "{\"auth\":{\"login\":\"" + phone + "\",\"password\":\"" + password +"\",\"guid\":\"" + Guid.NewGuid() + "\"}}"; + var sessionResponse = Post("https://emp.mos.ru/v1.1/auth/virtualLogin?token=887033d0649e62a84f80433e823526a1", content); + dynamic data = JsonConvert.DeserializeObject(sessionResponse); + var sessionId = (data.session_id.Value as string); + return sessionId; + } + + public bool IsAvailable(string session, int? patient) + { + //2010 - covid + //2004 - Пост - для теста + var specialityCode = "2010"; + var patientId = patient.Value; + var json = + "{ \"patient_id\":\"" + patientId + "\",\"speciality_code\":\"" + specialityCode + "\",\"auth\":{ \"session_id\":\"" + session + "\"} }"; + + var response = Post("https://emp.mos.ru/v1.1/doctor/getDoctors?token=887033d0649e62a84f80433e823526a1", + json); + + var responseJson = JsonConvert.DeserializeObject(response); + + Console.WriteLine($"[{DateTime.Now}] {responseJson.errorCode}: {responseJson.errorMessage}"); + + if (responseJson.errorCode == 0) + { + var sb = new StringBuilder(); + + foreach (var clinic in responseJson.result.clinics) + { + sb.AppendLine($"Клиника: {clinic.address}"); + foreach (var doctorsInfo in clinic.doctors_info) + { + sb.AppendLine($"- {doctorsInfo.availability_date}: {doctorsInfo.last_name} {doctorsInfo.first_name} {doctorsInfo.second_name}"); + } + } + + Console.WriteLine(sb.ToString()); + _telegram.SendMessage(sb.ToString()); + } + + return true; + } + + public string Post(string url, string content, string contentType = "application/json") + { + var httpRequest = (HttpWebRequest)WebRequest.Create(url); + httpRequest.ContentType = contentType; + httpRequest.Method = "POST"; + httpRequest.CookieContainer = new CookieContainer(); + httpRequest.Headers.Add("User-Agent", "okhttp/4.2.2"); + //httpRequest.CookieContainer.Add(new Cookie { Name = "WILDAUTHNEW_V3", Value = session, Domain = "wildberries.ru" }); + + using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream())) + { + streamWriter.Write(content); + } + + var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + var result = streamReader.ReadToEnd(); + return result; + } + } + + public int? GetPatient(string session) + { + var content = "{\"auth\":{\"session_id\":\"" + session + "\"}}"; + var patientResponse = Post("https://emp.mos.ru/v1.1/patient/get?token=887033d0649e62a84f80433e823526a1", content); + var data = JsonConvert.DeserializeObject(patientResponse); + return data?.result.First().id; + } + } +} diff --git a/src/Covid-Record/EMaisAnswer.cs b/src/Covid-Record/EMaisAnswer.cs new file mode 100644 index 0000000..8ad527e --- /dev/null +++ b/src/Covid-Record/EMaisAnswer.cs @@ -0,0 +1,64 @@ +using System.Collections.Generic; + +namespace Covid_Record +{ + public class EMaisAnswer + { + public int errorCode { get; set; } + public Result result { get; set; } + public string errorMessage { get; set; } + public double execTime { get; set; } + public string request_id { get; set; } + public string session_id { get; set; } + } + + // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); + public class DoctorsInfo + { + public string availableResourceId { get; set; } + public string complexResourceId { get; set; } + public string first_name { get; set; } + public string second_name { get; set; } + public string last_name { get; set; } + public string availability_date { get; set; } + public string room { get; set; } + public string clinic { get; set; } + public string code { get; set; } + } + + public class Clinic + { + public string clinic { get; set; } + public string address { get; set; } + public List doctors_info { get; set; } + } + + public class Result + { + public List clinics { get; set; } + } + + + // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); + public class Patient + { + public string oms_number { get; set; } + public string birthdate { get; set; } + public int id { get; set; } + public string lastname { get; set; } + public string firstname { get; set; } + public string middlename { get; set; } + } + + public class PatientResponse + { + public int errorCode { get; set; } + public List result { get; set; } + public string errorMessage { get; set; } + public double execTime { get; set; } + public string request_id { get; set; } + public string session_id { get; set; } + } + + +} \ No newline at end of file diff --git a/src/Covid-Record/Program.cs b/src/Covid-Record/Program.cs new file mode 100644 index 0000000..6afe67a --- /dev/null +++ b/src/Covid-Record/Program.cs @@ -0,0 +1,33 @@ +using System; +using System.Threading; + +namespace Covid_Record +{ + class Program + { + static void Main(string[] args) + { + var telegram = new TelegramBot(); + var arr = new CovidAvailability(telegram); + + var phone = "79160000000"; + var password = "testpwd"; + + var session = arr.GetSession(phone, password); + + var patient = arr.GetPatient(session); + if (patient == null) + { + Console.WriteLine("Не могу получить ID пациента."); + Console.ReadLine(); + return; + } + + while (true) + { + arr.IsAvailable(session, patient); + Thread.Sleep(TimeSpan.FromSeconds(4)); + } + } + } +} diff --git a/src/Covid-Record/TelegramBot.cs b/src/Covid-Record/TelegramBot.cs new file mode 100644 index 0000000..049c1bf --- /dev/null +++ b/src/Covid-Record/TelegramBot.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Telegram.Bot; + +namespace Covid_Record +{ + public class TelegramBot + { + private TelegramBotClient _telegram; + public string Token => "1479535614:AAFqHdsUFikzOti0zknK_pN2sEONNAXecPY"; + public long MainChatId; + + public TelegramBot(long chatId = -415825856) + { + MainChatId = chatId; + _telegram = new TelegramBotClient(Token); + _telegram.OnMessage += Bot_OnMessage; + _telegram.StartReceiving(); + } + + public void Bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e) + { + Console.WriteLine($"[{e.Message.Chat.Id}, user: {e.Message.From.Id}] @{e.Message.From?.Username}: {e.Message.Text}"); + } + + public void SendMessage(string message, long? chatId = null) + { + _telegram.SendTextMessageAsync(chatId ?? MainChatId, message).GetAwaiter().GetResult(); + } + } +}