Advertisement
Guest User

Untitled

a guest
Aug 4th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.45 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows.Forms;
  4. using CefSharp;
  5. using CefSharp.OffScreen;
  6. using AngleSharp.Html.Dom;
  7. using AngleSharp.Html.Parser;
  8. using AngleSharp.Dom;
  9. using System.Collections.Generic;
  10. using System.Text;
  11.  
  12. namespace Arbitr
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         ChromiumWebBrowser browser;
  17.  
  18.         IHtmlDocument angle;
  19.         HtmlParser parser = new HtmlParser();
  20.  
  21.         string PlaceOfConsiderationSelector = "#chrono_list_content > div > div > div.b-chrono-cols.g-ec > div.r-col > h4 > span:nth-child(2) > span > a";
  22.         string CurrentStep = "#b-case-header > div";
  23.         string CauseSelector = "#b-container > div.b-noColumns-middle.g-fs-120 > dl > dt > span";
  24.         string JudjeNameSelector = "#gr_case_judges > table > tbody > tr > td > div > ul > li";
  25.         string JudjePlaceSelector = "#gr_case_judges > table > thead > tr > td > div > a";
  26.         string DefendantsSelector = "#gr_case_partps > table > tbody > tr > td.defendants > div > ul > li:nth-child(1) > span";
  27.         string ThirdPartiesSelector = "#gr_case_partps > table > tbody > tr > td.third > div > ul > li:nth-child(1) > span > a";
  28.         string OtherPersonsSelector = "#gr_case_partps > table > tbody > tr > td.others > div > ul > li:nth-child(1) > span > a";
  29.         string PlaintiffsSelector = "#gr_case_partps > table > tbody > tr > td.plaintiffs.first > div > ul > li:nth-child(1) > span";
  30.         string hrefForJudjePlace;
  31.         string hrefForPlace;
  32.  
  33.         public Form1()
  34.         {
  35.             InitializeComponent();
  36.             InitializeChromium();
  37.  
  38.             browser.FrameLoadEnd += WebBrowserFrameLoadEnded;
  39.         }
  40.  
  41.         private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
  42.         {
  43.             Update();
  44.         }
  45.  
  46.         public void InitializeChromium()
  47.         {
  48.             CefSettings settings = new CefSettings();
  49.             Cef.Initialize(settings);
  50.             browser = new ChromiumWebBrowser("https://kad.arbitr.ru/");
  51.             //this.Controls.Add(browser);
  52.             //browser.Dock = DockStyle.Fill;
  53.         }
  54.  
  55.         private void button1_Click(object sender, EventArgs e)
  56.         {
  57.             ClearTextInLabel();
  58.  
  59.             browser.Load($"https://kad.arbitr.ru/Card?number={textBox1.Text}");
  60.         }
  61.  
  62.         private new async void Update()
  63.         {
  64.             ClearTextInLabel();
  65.  
  66.             string html = await browser.GetSourceAsync();
  67.  
  68.             if (!string.IsNullOrEmpty(html))
  69.             {
  70.                 angle = parser.ParseDocument(html);
  71.                 if(angle.QuerySelector("#gr_case_partps > table > thead > tr > td.defendants > div") != null)
  72.                 {
  73.                     if (angle.QuerySelector("#gr_case_partps > table > thead > tr > td.defendants > div").TextContent.Trim() == "Ответчики")
  74.                     {
  75.                         Debug.WriteLine(angle.QuerySelector("#gr_case_partps > table > thead > tr > td.defendants > div")?.TextContent);
  76.  
  77.                         if (GetElementsBySelector(PlaintiffsSelector) != null)
  78.                         {
  79.                             Invoke((MethodInvoker)(() =>
  80.                             {
  81.                                 PlaintiffsTop.Text = "Истцы:";
  82.  
  83.                                 foreach (IElement element in GetElementsBySelector(PlaintiffsSelector))
  84.                                 {
  85.                                     Plaintiffs.Text += element.TextContent.Trim() + "\n\n";
  86.                                 }
  87.                             }));
  88.                         }
  89.  
  90.                         if (GetElementsBySelector(DefendantsSelector) != null)
  91.                         {
  92.                             Invoke((MethodInvoker)(() =>
  93.                             {
  94.                                 StringBuilder stringBuilder = new StringBuilder();
  95.  
  96.                                 foreach (IElement element in GetElementsBySelector(DefendantsSelector))
  97.                                 {
  98.                                     Defendants.Text += element.TextContent.Trim() + "\n\n";
  99.                                 }
  100.  
  101.                                 DefendantsTop.Text = "Ответчики:";
  102.                             }));
  103.                         }
  104.  
  105.                         if (GetElementsBySelector(ThirdPartiesSelector) != null)
  106.                         {
  107.                             Invoke((MethodInvoker)(() =>
  108.                             {
  109.                                 ThirdPartiestTop.Text = "Третьи лица:";
  110.  
  111.                                 foreach (IElement element in GetElementsBySelector(ThirdPartiesSelector))
  112.                                 {
  113.                                     ThirdParties.Text += element.TextContent.Trim();
  114.                                 }
  115.                             }));
  116.                         }
  117.  
  118.                         if (GetElementsBySelector(OtherPersonsSelector) != null)
  119.                         {
  120.                             Invoke((MethodInvoker)(() =>
  121.                             {
  122.                                 OtherPersonsTop.Text = "Иные лица:";
  123.  
  124.                                 foreach (IElement element in GetElementsBySelector(OtherPersonsSelector))
  125.                                 {
  126.                                     OtherPersons.Text += element.TextContent.Trim() + "\n\n";
  127.                                 }
  128.                             }));
  129.                         }
  130.  
  131.                         if (GetElementBySelector(CurrentStep) != null)
  132.                         {
  133.                             Invoke((MethodInvoker)(() =>
  134.                             {
  135.                                 ReviewerText.Text = GetElementBySelector(CurrentStep).TextContent.Trim();
  136.                                 hrefForPlace = GetElementBySelector(CurrentStep).GetAttribute("href");
  137.                             }));
  138.                         }
  139.                         if (GetElementBySelector(PlaceOfConsiderationSelector) != null)
  140.                         {
  141.                             Invoke((MethodInvoker)(() =>
  142.                             {
  143.                                 Place.Text = GetElementBySelector(PlaceOfConsiderationSelector).TextContent.Trim();
  144.                                 hrefForPlace = GetElementBySelector(PlaceOfConsiderationSelector).GetAttribute("href");
  145.                             }));
  146.                         }
  147.                         if (GetElementBySelector(JudjeNameSelector) != null)
  148.                         {
  149.                             Invoke((MethodInvoker)(() =>
  150.                             {
  151.                                 JudjeName.Text = GetElementBySelector(JudjeNameSelector).TextContent.Trim();
  152.                                 hrefForPlace = GetElementBySelector(JudjeNameSelector).GetAttribute("href");
  153.                             }));
  154.                         }
  155.                         if (GetElementBySelector(CauseSelector) != null)
  156.                         {
  157.                             Invoke((MethodInvoker)(() =>
  158.                             {
  159.                                 Cause.Text = GetElementBySelector(CauseSelector).TextContent.Trim();
  160.                             }));
  161.                         }
  162.                         if (GetElementBySelector(JudjePlaceSelector) != null)
  163.                         {
  164.                             Invoke((MethodInvoker)(() =>
  165.                             {
  166.                                 JudjePlace.Text = GetElementBySelector(JudjePlaceSelector).TextContent.Trim();
  167.                                 hrefForPlace = GetElementBySelector(JudjePlaceSelector).GetAttribute("href");
  168.                             }));
  169.                         }
  170.                     }
  171.                     else
  172.                     {
  173.                         Debug.WriteLine(angle.QuerySelector("#gr_case_partps > table > thead > tr > td.defendants > div")?.TextContent);
  174.  
  175.                         if (GetElementsBySelector(PlaintiffsSelector) != null)
  176.                         {
  177.                             Invoke((MethodInvoker)(() =>
  178.                             {
  179.                                 foreach (IElement element in GetElementsBySelector(PlaintiffsSelector))
  180.                                 {
  181.                                     Plaintiffs.Text += element.TextContent.Trim() + "\n\n";
  182.                                 }
  183.  
  184.                                 PlaintiffsTop.Text = "Кредиторы (заявители):";
  185.                             }));
  186.                         }
  187.  
  188.                         if (GetElementsBySelector(DefendantsSelector) != null)
  189.                         {
  190.                             Invoke((MethodInvoker)(() =>
  191.                             {
  192.                                 StringBuilder stringBuilder = new StringBuilder();
  193.  
  194.                                 foreach (IElement element in GetElementsBySelector(DefendantsSelector))
  195.                                 {
  196.                                     Defendants.Text += element.TextContent.Trim() + "\n\n";
  197.                                 }
  198.  
  199.                                 DefendantsTop.Text = "Должники:";
  200.                             }));
  201.                         }
  202.  
  203.                         if (GetElementsBySelector(ThirdPartiesSelector) != null)
  204.                         {
  205.                             Invoke((MethodInvoker)(() =>
  206.                             {
  207.                                 foreach (IElement element in GetElementsBySelector(ThirdPartiesSelector))
  208.                                 {
  209.                                     ThirdParties.Text += element.TextContent.Trim();
  210.                                 }
  211.  
  212.                                 ThirdPartiestTop.Text = "Третьи лица:";
  213.                             }));
  214.                         }
  215.  
  216.                         if (GetElementsBySelector(OtherPersonsSelector) != null)
  217.                         {
  218.                             Invoke((MethodInvoker)(() =>
  219.                             {
  220.                                 foreach (IElement element in GetElementsBySelector(OtherPersonsSelector))
  221.                                 {
  222.                                     OtherPersons.Text += element.TextContent.Trim() + "\n\n";
  223.                                 }
  224.  
  225.                                 OtherPersonsTop.Text = "Иные лица:";
  226.                             }));
  227.                         }
  228.  
  229.                         if (GetElementBySelector(CurrentStep) != null)
  230.                         {
  231.                             Invoke((MethodInvoker)(() =>
  232.                             {
  233.                                 ReviewerText.Text = GetElementBySelector(CurrentStep).TextContent.Trim();
  234.                                 hrefForPlace = GetElementBySelector(CurrentStep).GetAttribute("href");
  235.                             }));
  236.                         }
  237.                         if (GetElementBySelector(PlaceOfConsiderationSelector) != null)
  238.                         {
  239.                             Invoke((MethodInvoker)(() =>
  240.                             {
  241.                                 Place.Text = GetElementBySelector(PlaceOfConsiderationSelector).TextContent.Trim();
  242.                                 hrefForPlace = GetElementBySelector(PlaceOfConsiderationSelector).GetAttribute("href");
  243.                             }));
  244.                         }
  245.                         if (GetElementBySelector(JudjeNameSelector) != null)
  246.                         {
  247.                             Invoke((MethodInvoker)(() =>
  248.                             {
  249.                                 JudjeName.Text = GetElementBySelector(JudjeNameSelector).TextContent.Trim();
  250.                                 hrefForPlace = GetElementBySelector(JudjeNameSelector).GetAttribute("href");
  251.                             }));
  252.                         }
  253.                         if (GetElementBySelector(CauseSelector) != null)
  254.                         {
  255.                             Invoke((MethodInvoker)(() =>
  256.                             {
  257.                                 Cause.Text = GetElementBySelector(CauseSelector).TextContent.Trim();
  258.                             }));
  259.                         }
  260.                         if (GetElementBySelector(JudjePlaceSelector) != null)
  261.                         {
  262.                             Invoke((MethodInvoker)(() =>
  263.                             {
  264.                                 JudjePlace.Text = GetElementBySelector(JudjePlaceSelector).TextContent.Trim();
  265.                                 hrefForPlace = GetElementBySelector(JudjePlaceSelector).GetAttribute("href");
  266.                             }));
  267.                         }
  268.                     }
  269.  
  270.                 }
  271.             }
  272.         }
  273.  
  274.         private void ClearTextInLabel()
  275.         {
  276.             Invoke((MethodInvoker)(() =>
  277.             {
  278.                 ReviewerText.Text = string.Empty;
  279.                 OtherPersons.Text = string.Empty;
  280.                 ThirdParties.Text = string.Empty;
  281.                 Plaintiffs.Text = string.Empty;
  282.                 Defendants.Text = string.Empty;
  283.                 JudjePlace.Text = string.Empty;
  284.                 JudjeName.Text = string.Empty;
  285.                 Place.Text = string.Empty;
  286.             }));
  287.         }
  288.  
  289.         private List<IElement> GetElementsBySelector(string Selector)
  290.         {
  291.             IElement element = angle.QuerySelector(Selector);
  292.             List<IElement> elements = new List<IElement>();
  293.  
  294.             int i = 1;
  295.  
  296.             while(element != null)
  297.             {
  298.                 elements.Add(element);
  299.  
  300.                 i++;
  301.  
  302.                 Selector = Selector.Replace((i - 1).ToString(), i.ToString());      
  303.  
  304.                 element = angle.QuerySelector(Selector);
  305.                
  306.             }
  307.  
  308.             return elements;
  309.         }
  310.  
  311.         private IElement GetElementBySelector(string Selector)
  312.         {
  313.             IElement element = angle.QuerySelector(Selector);
  314.  
  315.             if(element != null)
  316.             {
  317.                 return element;
  318.             }
  319.  
  320.             return null;
  321.         }
  322.  
  323.         private void JudjePlace_Click(object sender, EventArgs e)
  324.         {
  325.             if (!string.IsNullOrEmpty(hrefForJudjePlace))
  326.             {
  327.                 Process.Start(hrefForJudjePlace);
  328.             }
  329.         }
  330.  
  331.         private void Place_Click(object sender, EventArgs e)
  332.         {
  333.             if (!string.IsNullOrEmpty(hrefForPlace))
  334.             {
  335.                 Process.Start(hrefForPlace);
  336.             }
  337.         }
  338.     }
  339. }
  340.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement