Esempio n. 1
0
        private Dictionary <string, string[]> GetSectionsAndTitles()
        {
            const string sectionsXpath = @"//div[@data-purpose='curriculum-section-container']/div";
            var          sections      = _helper.FindAllByXPath(sectionsXpath).ToArray();

            if (sections.Length == 0)
            {
                return(null);
            }
            var dic = new Dictionary <string, string[]>();

            foreach (var section in sections)
            {
                var sectionHeadingElement = section.FindElement(
                    By.XPath(@"./div[@data-purpose='section-heading']"));
                var titleElement = sectionHeadingElement.FindElement(
                    By.XPath(@"./div[@data-purpose='section-label']/span/span/span"));
                var sectionTitle = titleElement.GetAttribute("innerText");
                // is this section expanded?
                var expanded = sectionHeadingElement.GetAttribute("aria-expanded") ?? "false";
                if (!bool.Parse(expanded))
                {
                    _helper.DelayedClick(sectionHeadingElement);
                }

                var listItems   = section.FindElements(By.XPath("./ul/li"));
                var videoTitles = new List <string>();
                foreach (var listItem in listItems)
                {
                    var videoTitleElement =
                        listItem.FindElements(
                            By.XPath(".//div[starts-with(@class, 'curriculum-item-link--title')]"))
                        .FirstOrDefault();
                    if (videoTitleElement == null)
                    {
                        continue;
                    }
                    var videoTitle = videoTitleElement.GetAttribute("innerText");
                    videoTitles.Add(videoTitle);
                }

                dic.Add(sectionTitle, videoTitles.ToArray());
            }

            return(dic);
        }
        public void Process()
        {
            try
            {
                var url = _helper.PromptUser("Url: ");
                if (string.IsNullOrWhiteSpace(url))
                {
                    return;
                }
                Console.Write("Enter Folder Name: ");
                var folder = Console.ReadLine() ?? string.Empty;
                folder = Path.Combine(Environment.CurrentDirectory, folder);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                _helper.Navigate(url);
                _helper.FindAndClickByXPath("//a[@href='/login']");
                var email = _helper.FindById("login-input-email");
                _helper.SendKeys(email, "*****@*****.**");
                var pwd = _helper.FindById("login-input-password");
                _helper.SendKeys(pwd, "roXyaub1");
                _helper.FindAndClickByXPath("//div[@id='login-form']//button[@type='submit']");
                _helper.FindAndClickById("menu-toggle");
                const string linksXpath = "//li[@class='sub-nav ng-scope']/a";
                var          links      = _helper.FindAllByXPath(linksXpath).Select(c => c.GetAttribute("href")).ToArray();
                var          webClient  = new WebClient();
                var          lockObject = new object();
                webClient.DownloadProgressChanged += (obj, evt) =>
                {
                    lock (lockObject)
                    {
                        Console.SetCursorPosition(0, 1);
                        var pct = Math.Round(evt.BytesReceived / (double)evt.TotalBytesToReceive, 2) *
                                  100;
                        Console.Write($"{pct}% done...".PadRight(10));
                    }
                };
                var i = 0;
                foreach (var link in links)
                {
                    if (string.IsNullOrWhiteSpace(link))
                    {
                        continue;
                    }
                    i++;
                    try
                    {
                        _webDriver.Navigate().GoToUrl(link);
                        Thread.Sleep(1500);
                        var title = _helper.FindByXPath("//h2[starts-with(@class,'title')]")
                                    .GetAttribute("innerText");
                        while (string.IsNullOrWhiteSpace(title))
                        {
                            Thread.Sleep(2000);
                            title = _helper.FindByXPath("//h2[starts-with(@class,'title')]")
                                    .GetAttribute("innerText");
                        }

                        var video    = _helper.FindByXPath("//video[@id='video-content_html5_api']");
                        var videoUrl = video.GetAttribute("src");
                        var filename = $"{i.ToString().PadLeft(3, '0')} - {title}.mp4";
                        filename = _helper.RemoveIllegalFilenameChars(filename);
                        Console.Clear();
                        Console.WriteLine($"Downloading {filename}...");
                        filename = $"{folder}\\{filename}";
                        if (File.Exists(filename))
                        {
                            continue;
                        }
                        webClient.DownloadFileTaskAsync(videoUrl, filename).ConfigureAwait(true).GetAwaiter().GetResult();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                System.Diagnostics.Debugger.Break();
            }
        }