Beispiel #1
0
        public string SaveRightMovePdf(RightMoveModel rightMoveModel)
        {
            try
            {
                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(new FileStream(templatePath, FileMode.Open));

                if (!Directory.Exists(archiveFolder))
                {
                    Directory.CreateDirectory(archiveFolder);
                }

                if (loadedDocument.PageCount > 0)
                {
                    PdfLoadedPage pdfLoadedPage = loadedDocument.Pages[1] as PdfLoadedPage;

                    PdfTemplate pdfTemplate = new PdfTemplate(900, 600);

                    PdfFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 15);

                    PdfBrush brush = new PdfSolidBrush(SfDrawing.Color.Black);

                    byte[] imageBytes  = new WebClient().DownloadData(rightMoveModel.PropertyMainPicture);
                    Stream imageStream = new MemoryStream(imageBytes);

                    pdfTemplate.Graphics.DrawString($"Property Address: {rightMoveModel.PropertyAddress}", pdfFont, brush, 100, 30);
                    pdfTemplate.Graphics.DrawString($"Property Type: {rightMoveModel.PropertyType}", pdfFont, brush, 100, 50);
                    pdfTemplate.Graphics.DrawString($"PropertyPrice: {rightMoveModel.PropertyPrice} ", pdfFont, brush, 100, 70);
                    pdfTemplate.Graphics.DrawImage(PdfImage.FromStream(imageStream), new SfDrawing.PointF(100, 100), new SfDrawing.SizeF(400, 400));

                    pdfLoadedPage.Graphics.DrawPdfTemplate(pdfTemplate, SfDrawing.PointF.Empty);

                    string rawName = rightMoveModel
                                     .PropertyUrl
                                     .Replace("/", "")
                                     .Replace("-", "")
                                     .Replace(".", "")
                                     .Replace(":", "")
                                     .Replace("//", "");

                    string fileName = Regex.Match(rawName, @"(\d+(?:\.\d{1,2})?)").Value;

                    PdfDocument propertyHeatMapPdfDocument = htmlConverter.Convert(rightMoveModel.PropertyHeatHtmlString, string.Empty);
                    PdfDocument homeCoUKHtmlPdfDocument    = htmlConverter.Convert(rightMoveModel.HomeCoUKHtmlString, string.Empty);

                    string tempPropertyHeatMap = Path.Combine(tempFolder, $"propertyHeatMap{fileName}.pdf");

                    using (FileStream propertyHeatMapStream = new FileStream(tempPropertyHeatMap, FileMode.Create))
                    {
                        propertyHeatMapPdfDocument.Save(propertyHeatMapStream);
                        propertyHeatMapPdfDocument.Close(true);
                        propertyHeatMapPdfDocument.Dispose();

                        propertyHeatMapStream.Close();
                        propertyHeatMapStream.Dispose();
                    }

                    string tempHomeCoUK = Path.Combine(tempFolder, $"homeCoUK{fileName}.pdf");

                    using (FileStream homeCoUKHtmlPdfStream = new FileStream(tempHomeCoUK, FileMode.Create))
                    {
                        homeCoUKHtmlPdfDocument.Save(homeCoUKHtmlPdfStream);
                        homeCoUKHtmlPdfDocument.Close(true);
                        homeCoUKHtmlPdfDocument.Dispose();

                        homeCoUKHtmlPdfStream.Close();
                        homeCoUKHtmlPdfStream.Dispose();
                    }

                    using (FileStream propertyHeatMapReadStream = new FileStream(tempPropertyHeatMap, FileMode.Open))
                    {
                        PdfLoadedDocument tempPropertyHeatMapDocument = new PdfLoadedDocument(propertyHeatMapReadStream);
                        loadedDocument.ImportPage(tempPropertyHeatMapDocument, 0);

                        propertyHeatMapReadStream.Close();
                        propertyHeatMapReadStream.Dispose();
                    }

                    using (FileStream homeCoUKReadStream = new FileStream(tempHomeCoUK, FileMode.Open))
                    {
                        PdfLoadedDocument tempHomeCoUKDocument = new PdfLoadedDocument(homeCoUKReadStream);

                        loadedDocument.ImportPage(tempHomeCoUKDocument, 0);

                        homeCoUKReadStream.Close();
                        homeCoUKReadStream.Dispose();
                    }

                    string savePath = Path.Combine(archiveFolder, $"{fileName}.pdf");

                    using (FileStream saveStream = new FileStream(savePath, FileMode.Create))
                    {
                        loadedDocument.Save(saveStream);
                        loadedDocument.Close(true);
                        loadedDocument.Dispose();
                        saveStream.Close();
                        saveStream.Dispose();
                    }

                    return($"file successfully saved at: {savePath}");
                }
                else
                {
                    Console.WriteLine("Invalid PDF file");
                    return($"Invalid PDF file");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to save the file. {ex.Message}");

                return($"Unable to save the file");
            }
        }
        public void ScrapRightMove()
        {
            try
            {
                string rVal = string.Empty;

                driver  = new ChromeDriver(service);
                htmlWeb = new HtmlWeb();

                NavigationOutput($"Navigating to: {rightMoveUrl}");

                driver.Url = rightMoveUrl;

                driver.FindElement(By.Name("searchLocation"))
                .SendKeys(postalCode);
                driver.FindElement(By.Id("buy")).Click();

                NavigationOutput($"Navigating to: {driver.Url}");

                MainProgressBar.Value = 2;

                driver.FindElement(By.Id("submit")).Click();

                #region Get property list URLs

                ReadOnlyCollection <IWebElement> readOnlyCollection;

                readOnlyCollection = driver.FindElements(By.CssSelector(".propertyCard-moreInfoItem.is-carousel"));

                if (readOnlyCollection.Count <= 0)
                {
                    readOnlyCollection = driver.FindElements(By.CssSelector(".propertyCard-link"));
                }

                NavigationOutput($"Finding URLs on: {driver.Url}");

                MainProgressBar.Value = 10;

                foreach (var item in readOnlyCollection)
                {
                    propertiesLinks.Add(item.GetAttribute("href"));
                }

                ProgressBar.Value = 90;

                if (propertiesLinks.Count > 0)
                {
                    NavigationOutput($"Successfully found {propertiesLinks.Count} URLs on: {driver.Url}");
                    driver.Quit();

                    var progresscount = propertiesLinks.Count / 100;

                    MainProgressBar.Value = 15;

                    foreach (var _item in propertiesLinks)
                    {
                        ProgressBar.Value = 0;

                        if (!string.IsNullOrEmpty(_item))
                        {
                            NavigationOutput($"Fatching data from: {_item}");

                            ProgressBar.Value     += progresscount;
                            MainProgressBar.Value += progresscount / 2;

                            Text_Outputs.Content = $"Fatching data from: {_item}";

                            HtmlDocument htmlDocument = htmlWeb.Load(_item);

                            string propertyType = htmlDocument
                                                  .DocumentNode
                                                  .SelectNodes("//h1[@class='fs-22']")[0]
                                                  .InnerHtml;

                            string propertyAddress = htmlDocument
                                                     .DocumentNode
                                                     .SelectNodes("//meta[@itemprop='streetAddress']")[0]
                                                     .GetAttributeValue("content", string.Empty);

                            string propertyPriceHtml = htmlDocument
                                                       .DocumentNode
                                                       .SelectNodes("//p[@id='propertyHeaderPrice']")[0].InnerText;

                            string rawPrice = propertyPriceHtml
                                              .Replace("\r", "")
                                              .Replace("\n", "")
                                              .Replace("\t", "")
                                              .Replace(";", "")
                                              .Replace(",", "");
                            string propertyPrice = Regex.Match(rawPrice, @"(\d+(?:\.\d{1,2})?)").Value;

                            string propertyMainPicture = htmlDocument
                                                         .DocumentNode
                                                         .SelectNodes("//img[@class='js-gallery-main']")[0]
                                                         .GetAttributeValue("src", string.Empty);

                            string propertyHeatHtmlString = ScrapPropertyHeat();
                            string innerHtml = ScrapHomeCoUk();

                            RightMoveModel rightMoveModel = new RightMoveModel()
                            {
                                PropertyAddress        = propertyAddress,
                                PropertyMainPicture    = propertyMainPicture,
                                PropertyPrice          = propertyPrice,
                                PropertyType           = propertyType,
                                PropertyUrl            = _item,
                                PropertyHeatHtmlString = propertyHeatHtmlString,
                                postalCode             = postalCode,
                                HomeCoUKHtmlString     = innerHtml
                            };

                            Text_Outputs.Content = $"Saving file to pdf";
                            rVal = pdfHandler.SaveRightMovePdf(rightMoveModel);
                            Text_Outputs.Content = rVal;
                        }
                        else
                        {
                            NavigationOutput($"Invalid Url: {_item}");
                        }
                    }

                    MainProgressBar.Value = 100;
                    ProgressBar.Value     = 100;
                }
                else
                {
                    NavigationOutput($"No URLs found on: {driver.Url}");
                    driver.Quit();
                }
                #endregion
            }
            catch (Exception ex)
            {
                NavigationOutput(ex.Message);
                driver.Quit();
                return;
            }
        }