private static void DownloadVideos(DownloadedActivities alreadyDownloadedActivities, DirectoryStructureManager directoryStructureManager, ChromeDriver driver, List <string> activitiesToDownloadUrl) { var webClient = new WebClient(); foreach (var activityToDownloadUrl in activitiesToDownloadUrl) { driver.Navigate().GoToUrl(activityToDownloadUrl); var videoContainer = default(IWebElement); var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(20)); videoContainer = wait.Until(driver => driver.FindElement(By.ClassName("video-container"))); var name = videoContainer.FindElement(By.CssSelector("[itemprop=name]")).GetAttribute("content"); if (name.Contains("Ride", System.StringComparison.InvariantCultureIgnoreCase) || name.Contains("hike", System.StringComparison.InvariantCultureIgnoreCase)) { continue; } var videoUrl = videoContainer.FindElement(By.CssSelector("[itemprop=contentURL]")).GetAttribute("content"); string date = null; var titles = driver.FindElementByClassName("titles").FindElements(By.CssSelector("p")); foreach (var item in titles) { if (item.Text.Contains("•")) { date = Regex.Match(item.Text, "•.+•").Value.Trim('•'); } } if (date == null) { throw new Exception(); } var activity = new DownloadedActivity { Name = name, Date = date }; if (alreadyDownloadedActivities.Names.Contains(activity)) { continue; } webClient.DownloadFile(videoUrl, directoryStructureManager.GetFilePath(activity)); var mapDiv = driver.FindElementById("map"); SaveScreenShotWithElement(driver, mapDiv, directoryStructureManager, activity); alreadyDownloadedActivities.Names.Add(activity); var json = JsonConvert.SerializeObject(alreadyDownloadedActivities, Formatting.Indented); File.WriteAllText(DownloadedActivitiesFile, json); } ; }
private static void SaveScreenShotWithElement(ChromeDriver driver, IWebElement elem, DirectoryStructureManager directoryStructureManager, DownloadedActivity activity) { driver.ExecuteScript("arguments[0].scrollIntoView(true);", elem); var myScreenShot = ((ITakesScreenshot)driver).GetScreenshot(); var screen = new Bitmap(new MemoryStream(myScreenShot.AsByteArray)); using var elemScreenshot = screen.Clone(new Rectangle(new Point(elem.Location.X, 0), elem.Size), screen.PixelFormat); screen.Dispose(); elemScreenshot.Save(directoryStructureManager.GetImagePath(activity)); }
private static string GetPath(DownloadedActivity activity, string fileExtension) { var date = DateTime.Parse(activity.Date); var nameWithoutRelive = Regex.Match(activity.Name, "'.+'").Value.Trim('\''); var directoryPath = Path.Combine("Relive", date.Year.ToString()); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } var filePath = Path.Combine("Relive", date.Year.ToString(), $"{nameWithoutRelive}.{fileExtension}"); while (File.Exists(filePath)) { var filePathWithoutExtension = Path.GetFileNameWithoutExtension(filePath); filePath = Path.Combine("Relive", date.Year.ToString(), $"{filePathWithoutExtension}(1).{fileExtension}"); } return(filePath); }
public string GetImagePath(DownloadedActivity activity) { return(GetPath(activity, "jpeg")); }
public string GetFilePath(DownloadedActivity activity) { return(GetPath(activity, "mp4")); }