예제 #1
0
        async void Algorithm(object obj)
        {
            BigWriter bigWriter = new BigWriter(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text));

            try
            {
                stop_btn.Invoke(new Action(() => stop_btn.Enabled   = true));
                parse_btn.Invoke(new Action(() => parse_btn.Enabled = false));
                var cts = (CancellationToken)obj;
                await _bf.Algorithm(cts, textBox3.Text, textBox1.Text, textBox2.Text, bigWriter, checkBox1.Checked);

                parse_btn.Invoke(new Action(() => parse_btn.Enabled = true));
                stop_btn.Invoke(new Action(() => stop_btn.Enabled   = false));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                bigWriter.Finish();
                Stop_btn_Click(null, null);
            }
        }
예제 #2
0
        public async Task <string> Algorithm(CancellationToken cts, string location, string start, string finish, BigWriter writer, bool hotels)
        {
            //var writer = new BigWriter();
            var formatter = new URLFormatter(start, finish, location);
            var allProps  = new List <Property>();
            //Initiate
            var url = formatter.URL;

            await LoadPageAsync(url);

            var mainScript = "";

            using (StreamReader r = new StreamReader("RoomTypes.js"))
            {
                mainScript = await r.ReadToEndAsync();
            }
            using (StreamReader r = new StreamReader("ChooseProp.js"))
            {
                await EvaluateScript($"var hotels = {hotels.ToString().ToLower()};var apart = {(!hotels).ToString().ToLower()};" + await r.ReadToEndAsync());
            }
            using (StreamReader r = new StreamReader("AvailableOnly.js"))
            {
                await EvaluateScript(await r.ReadToEndAsync());
            }
            url = browser.Address;
            await LoadPageAsync(url);

            var    addition     = "";
            string exceps       = "";
            int    excepCounter = 0;
            int    lastk        = 0;

            for (int k = 0; k < 99999 + 1; k++)
            {
                var hotelnum = Convert.ToInt16(await EvaluateScript("document.getElementsByClassName('hotel_name_link url').length;"));;
                while (hotelnum == 0)
                {
                    hotelnum = Convert.ToInt16(await EvaluateScript("document.getElementsByClassName('hotel_name_link url').length;"));
                    Thread.Sleep(1000);
                }
                //Add all urls of hotel on the current page
                var hotelURLs = new List <string>();
                for (int s = 0; s < hotelnum; s++)
                {
                    hotelURLs.Add(formatter.BaseURL + await EvaluateScript($"document.getElementsByClassName('hotel_name_link url')[{s}].getAttribute('href');"));
                }
                for (int i = 0; i < hotelnum; i++)
                {
                    if (cts.IsCancellationRequested)
                    {
                        throw new OperationCanceledException("Парсинг остановлен");
                    }
                    //LoadPage of hotel
                    await LoadPageAsync(hotelURLs[i]);

                    //MainScript is working
                    var json = await EvaluateScript(mainScript);

                    try
                    {
                        if (json.Equals("f**k"))
                        {
                            throw new InvalidOperationException("You were f****d");
                        }
                        var property = JsonConvert.DeserializeObject <Property>(json);
                        foreach (var item in allProps)
                        {
                            if (item.Name.Equals(property.Name))
                            {
                                throw new InvalidDataException(i.ToString());
                            }
                        }
                        allProps.Add(property);
                        writer.Write(property);
                    }
                    catch (InvalidDataException)
                    {
                        excepCounter++;
                    }
                    catch (InvalidOperationException e)
                    {
                        exceps += e.Message + '\t' + hotelURLs[i] + '\n';
                        excepCounter++;
                    }
                    catch (Exception e)
                    {
                        exceps += e.Message + '\t' + hotelURLs[i] + '\n';
                    }
                }
                if (addition.Equals(""))
                {
                    await LoadPageAsync(url);
                }
                else
                {
                    await LoadPageAsync(formatter.BaseURL + addition);
                }
                lastk    = k;
                addition = await EvaluateScript($"var counter = 0;while(counter != 100)" +
                                                "{if(document.getElementsByClassName('bui-pagination__link sr_pagination_link')[++counter].hasChildNodes()){if(parseInt(document.getElementsByClassName('bui-pagination__link sr_pagination_link')[counter].childNodes[2].innerText) == " +
                                                $"{k + 2})" + "{break;}}}document.getElementsByClassName('bui-pagination__link sr_pagination_link')[counter].getAttribute('href');");

                if (addition.Equals("f**k"))
                {
                    break;
                }
                await LoadPageAsync(formatter.BaseURL + addition);
            }
            using (StreamWriter sw = new StreamWriter("exceptions.txt"))
            {
                await sw.WriteAsync(exceps);

                await sw.WriteAsync($"\nexcepCounter:{excepCounter}");

                await sw.WriteAsync($"\nlastk:{lastk}");
            }
            return(null);
        }