public void InsertDashItem(DashItemScrap newItem) { try { SqlCommand cmd = new SqlCommand("dbo.InsertDashItemScrap", _conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@DashboardID", SqlDbType.Int).Value = newItem.DashboardID; cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = newItem.Title; cmd.Parameters.Add("@StartDateTime", SqlDbType.DateTime).Value = newItem.StartDateTime; cmd.Parameters.Add("@EndDateTime", SqlDbType.DateTime).Value = newItem.EndDateTime; cmd.Parameters.Add("@SortOrder", SqlDbType.Int).Value = newItem.SortOrder; cmd.Parameters.Add("@SourceURL", SqlDbType.VarChar).Value = newItem.SourceURL; cmd.Parameters.Add("@LogonUser", SqlDbType.VarChar).Value = newItem.LogonUser; cmd.Parameters.Add("@LogonPwd", SqlDbType.VarChar).Value = newItem.LogonPwd; cmd.Parameters.Add("@ClickThruUrl", SqlDbType.VarChar).Value = newItem.ClickThruURL; cmd.Parameters.Add("@CssSelector", SqlDbType.VarChar).Value = newItem.CssSelector; cmd.Parameters.Add("@ImageURI", SqlDbType.VarChar).Value = newItem.ImageURI; _conn.Open(); cmd.ExecuteNonQuery(); _conn.Close(); } catch (Exception e) { Console.WriteLine(e.Data); } }
// creates intial conversion of image public string Convert(DashItemScrap newItem) { string url = newItem.SourceURL; string userName = newItem.LogonUser; string password = newItem.LogonPwd; string cssSelector = newItem.CssSelector; string location = "wwwroot/images/ScrappedImages/" + newItem.Title; Random rnd = new Random(); while (System.IO.File.Exists(location + ".jpg")) // dont want to overwrite any pictures, so we keep adding random numbers until we get an unused name { location += rnd.Next(0, 10).ToString(); } location += ".jpg"; try { url = AddAuthentication(url, userName, password); ChromeOptions op = new ChromeOptions(); op.AddArguments("headless", "window-size=3000,3000"); // what specific window size should be used? By locator = By.CssSelector(cssSelector); IWebDriver driver = new ChromeDriver( //Todo switch location of Selenium Driver "..\\IntranetApplication\\bin\\Debug\\netcoreapp2.0", op); driver.Navigate().GoToUrl(url); Thread.Sleep(TimeSpan.FromSeconds(5)); IWebElement element = driver.FindElement(locator); Screenshot sc = ((ITakesScreenshot)driver).GetScreenshot(); var img = (Image.FromStream(new MemoryStream(sc.AsByteArray)) as Bitmap); img.Clone(new Rectangle(element.Location, element.Size), img.PixelFormat) .Save(location, System.Drawing.Imaging.ImageFormat.Jpeg); location = location.Replace("wwwroot", ""); return(location); } catch (Exception ex) { return("ERROR"); } }
public string TestImgScrap(DashItemScrap info) { string absoluteLocation = "C:\\Users\\nullman\\source\\repos\\OnlyReactIntranet\\IntranetApplication\\ClientApp\\public\\images\\Scrapped\\"; string uri = "/images/Scrapped/"; // the uri that absolute location points to try { string name = ScrappingEngine.ScrapImage(info.LogonUser, info.LogonPwd, info.CssSelector, info.SourceURL, absoluteLocation); // returns uri of scrapped image var response = uri + name; return(response); // returns the src that the img needs } catch (Exception e) { return(e.Message); } }
public async Task <IActionResult> ImageHtmlScrapping(DashItemScrap newItem) { if (!ModelState.IsValid) { return(View(newItem)); } DbAccessor accessor = new DbAccessor(); if (newItem.DashboardItemID == 0) // creating new item { TransformEngine yeah = new TransformEngine(); newItem.ImageURI = yeah.Convert(newItem); // this will take awhile TODO: make background process do this accessor.InsertDashItem(newItem); } else // editing pre-existing item { accessor.UpdateDashItem((DashboardItem)newItem); } return(Redirect("/AdminTools/DashManager")); }
public async Task <IActionResult> ImageHtmlScrapping(string DashboardId) //Input forms and Edit forms are same************, //we know which action is being taken depending on what arguments are provided { string id = TempData["EditID"]?.ToString(); // has value if we are editing if (DashboardId == null & id == null) // error { return(Redirect("/AdminTools/DashManager")); } if (id == null) // we are creating a new item { int Id = Int32.Parse(DashboardId); // todo use tryparse to check if we can actually return(View(new DashItemScrap { DashboardID = Id })); // creating new } // we are editing a item DbAccessor accessor = new DbAccessor(); DashItemScrap ItemToEdit = (DashItemScrap)accessor.GetDashItem(id); // get item to fill model with return(View(ItemToEdit)); // editing pre-exisiting }
public string TestScrapping([FromBody] DashItemScrap info) // info only had 4 needed properties { return(imageEngine.TestImgScrap(info)); // returns uri of location of uploaded image }