private static void AllUnhandledExceptions(object sender, UnhandledExceptionEventArgs e) { var ex = (Exception)e.ExceptionObject; Log.Error(ex, "Unknown error occured. Running app again"); Console.WriteLine(); TUI.ShowError("Unknown error occured - " + ex.Message); TUI.ShowError("Restarting..."); Extractor.ExtractionProgressChanged -= Extractor_ExtractionProgressChanged; RunApp(); }
static void Main(string[] args) { Intro(); Console.Title = "Lynda Courses Downloader"; AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; var config = new Config(); if (File.Exists("./Config.json")) { Console.WriteLine(TUI.startGlyph + "Found a Config file"); try { config = Config.FromJson(File.ReadAllText("./Config.json")); Console.WriteLine(TUI.continueGlyph + "Data in config file : "); Console.WriteLine(TUI.continueGlyph + "Browser : " + config.Browser); Console.WriteLine(TUI.continueGlyph + "Quality to download in : " + config.Quality); Console.WriteLine(TUI.continueGlyph + "Course Directory/Path : " + config.CourseDirectory); Console.WriteLine(TUI.continueGlyph + "Authentication Token : " + config.AuthenticationToken); if (TUI.UseConfig()) { RunWithConfig(config); } else { Console.WriteLine(TUI.continueGlyph + "The data you enter will be saved in a new Config file"); RunWithoutConfig(); } } catch (JsonSerializationException) { TUI.ShowError("Config file is corrupt"); Console.WriteLine(TUI.continueGlyph + "The data you enter will be saved in a new Config file"); RunWithoutConfig(); } } else { Console.WriteLine(TUI.startGlyph + "Config File not found"); Console.WriteLine(TUI.continueGlyph + "The data you enter will be saved in a new Config file"); RunWithoutConfig(); } Console.WriteLine(); Console.ReadLine(); }
private static void RunWithConfig(Config config) { string courseUrl = TUI.GetCourseUrl(); Log.Information("Logging in..."); Console.WriteLine(TUI.continueGlyph + "Logging in..."); try { Extractor.InitializeDriver(config.Browser); Extractor.Login(config.AuthenticationToken, courseUrl).Wait(); } catch (InvalidTokenException ex) { Log.Error(ex, "Failed to log in with course url : {0} and token of {1} characters", courseUrl, config.AuthenticationToken.Length); TUI.ShowError("The token or the course url you provided is invalid. Please make sure you entered the right token and course url"); RunWithoutConfig(); return; } catch (Exception e) when(e.InnerException is InvalidTokenException ex) { Log.Error(ex, "Failed to log in with course url : {0} and token of {1} characters", courseUrl, config.AuthenticationToken.Length); TUI.ShowError("The token or the course url you provided is invalid. Please make sure you entered the right token and course url"); RunWithoutConfig(); return; } catch (Exception ex) { Log.Error(ex, "Unknown Exception"); TUI.ShowError("Unknown Error occured - " + ex.Message); RunWithoutConfig(); return; } Log.Information("Logged in successfully"); Console.WriteLine(TUI.continueGlyph + "Logged in successfully"); Log.Information("Intializing Course Extractor"); Console.WriteLine(TUI.endGlyph + "Intializing Course Extractor"); Course course = ExtractCourse(config); if (course is null) { RunWithConfig(config); return; } Log.Information("Course Extracted. Downloading..."); Console.WriteLine(); CourseDownloader.DownloadCourse(course, config.CourseDirectory); }
public static void DownloadCourse(Course course, DirectoryInfo courseRootDirectory) { try { using (var pbarCourse = new ProgressBar(course.Chapters.ToList().Count, "Downloading Course : " + course.Name, optionsCourse)) { var courseDirectory = courseRootDirectory.CreateSubdirectory(ToSafeFileName(course.Name)); foreach (var chapter in course.Chapters) { var chapterDirectory = courseDirectory.CreateSubdirectory($"[{chapter.Id}] {ToSafeFileName(chapter.Name)}"); using (var pbarChapter = pbarCourse.Spawn(chapter.Videos.ToList().Count, $"Downloading Chapter {chapter.Id} : {chapter.Name}", optionsChapter)) { foreach (var video in chapter.Videos) { currentVideo = video.Name; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; using (var downloadClient = new WebClient()) using (pbarVideo = pbarChapter.Spawn(100, $"Downloading Video {video.Id} : {currentVideo}", optionsVideo)) { downloadClient.DownloadProgressChanged += DownloadClient_DownloadProgressChanged; downloadClient.DownloadFileCompleted += DownloadClient_DownloadFileCompleted; string captionName = $"[{ video.Id}] { ToSafeFileName(video.Name)}.srt"; string videoName = $"[{ video.Id}] { ToSafeFileName(video.Name)}.mp4"; File.WriteAllText($"{Path.Combine(chapterDirectory.FullName, ToSafeFileName(captionName))}", video.CaptionText); downloadClient.DownloadFileTaskAsync(new Uri(video.VideoDownloadUrl), Path.Combine(chapterDirectory.FullName, videoName)).Wait(); } pbarChapter.Tick(); } pbarChapter.Message = $"Chapter {chapter.Id} : {chapter.Name} chapter has been downloaded successfully"; } pbarCourse.Tick(); } pbarCourse.Message = $"{course.Name} course has been downloaded successfully"; } } catch (Exception ex) { TUI.ShowError("An error occured while downloading the course"); TUI.ShowError("Error details : " + ex.StackTrace); TUI.ShowError("Trying again..."); DownloadCourse(course, courseRootDirectory); } }
public static void DownloadCourse(Course course, DirectoryInfo courseRootDirectory) { try { using (var pbarCourse = new ProgressBar(course.Chapters.ToList().Count, "Downloading Course : " + course.Name, optionsCourse)) { var courseDirectory = courseRootDirectory.CreateSubdirectory(ToSafeFileName(course.Name)); foreach (var chapter in course.Chapters) { var chapterDirectory = courseDirectory.CreateSubdirectory($"[{chapter.Id}] {ToSafeFileName(chapter.Name)}"); using (var pbarChapter = pbarCourse.Spawn(chapter.Videos.ToList().Count, $"Downloading Chapter {chapter.Id} : {chapter.Name}", optionsChapter)) { foreach (var video in chapter.Videos) { currentVideo = video.Name; DownloadVideo(chapterDirectory, pbarChapter, video); pbarChapter.Tick(); } pbarChapter.Message = $"Chapter {chapter.Id} : {chapter.Name} chapter has been downloaded successfully"; } pbarCourse.Tick(); } pbarCourse.Message = $"{course.Name} course has been downloaded successfully"; } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Course downloaded successfully :)"); Console.ResetColor(); Log.Information("Course downloaded successfully"); } catch (Exception ex) { TUI.ShowError("An error occured while downloading the course"); TUI.ShowError("Error details : " + ex.StackTrace); TUI.ShowError("Trying again..."); DownloadCourse(course, courseRootDirectory); } }
private static void RunApp() { if (File.Exists("./Config.json")) { Console.WriteLine(TUI.startGlyph + "Found a Config file"); try { Config config = Config.FromJson(File.ReadAllText("./Config.json")); Console.WriteLine(TUI.continueGlyph + "Data in config file : "); Console.WriteLine(TUI.continueGlyph + "Browser : " + config.Browser); Console.WriteLine(TUI.continueGlyph + "Quality to download in : " + config.Quality); Console.WriteLine(TUI.continueGlyph + "Course Directory/Path : " + config.CourseDirectory); Console.WriteLine(TUI.continueGlyph + "Authentication Token : " + config.AuthenticationToken); if (TUI.UseConfig()) { RunWithConfig(config); } else { Console.WriteLine(TUI.continueGlyph + "The data you enter will be saved in a new Config file"); RunWithoutConfig(); } } catch (JsonSerializationException) { TUI.ShowError("Config file is corrupt"); Console.WriteLine(TUI.continueGlyph + "The data you enter will be saved in a new Config file"); RunWithoutConfig(); } } else { Console.WriteLine(TUI.startGlyph + "Config File not found"); Console.WriteLine(TUI.continueGlyph + "The data you enter will be saved in a new Config file"); RunWithoutConfig(); } }
private static Course ExtractCourse(Config config) { Course course = new Course(); Extractor.ExtractCourseStructure(out int videosCount); using (pbarExtractor = new ProgressBar(videosCount, "Extracting Course Links - This will take some time", optionPbarExtractor)) { Retry.Do( function: () => { Log.Information("Extracting..."); Extractor.ExtractionProgressChanged += Extractor_ExtractionProgressChanged; course = Extractor.ExtractCourse(config.Quality); }, exceptionMessage: "An error occured while extracting the course", actionOnError: () => { Extractor.CloseTabs(); var progress = pbarExtractor.AsProgress <float>(); progress?.Report(0); Extractor.ExtractionProgressChanged -= Extractor_ExtractionProgressChanged; }, actionOnFatal: () => { TUI.ShowError("Failed to extract course. You can find more info in the logs"); Log.Error("Unknown error occured. Running app again"); TUI.ShowError("Unknown error occured"); TUI.ShowError("Restarting..."); Extractor.KillDrivers(); Extractor.ExtractionProgressChanged -= Extractor_ExtractionProgressChanged; course = null; } ); } return(course); }
private static void RunWithConfig(Config config) { while (true) { try { string courseUrl = TUI.GetCourseUrl(); Console.WriteLine(TUI.continueGlyph + "Logging in..."); var extractor = new Extractor(); var initializationTask = extractor.InitializeDriver(config.Browser); initializationTask.Start(); try { extractor.Login(config.AuthenticationToken, courseUrl, initializationTask).Wait(); Console.WriteLine(TUI.continueGlyph + "Logged in successfully"); Console.WriteLine(TUI.endGlyph + "Intializing Course Extractor"); Course course = new Course(); int videosCount; extractor.ExtractCourseStructure(out videosCount); using (pbarExtractor = new ProgressBar(videosCount, "Extracting Course Links - This will take some time", optionPbarExtractor)) { extractor.ExtractionProgressChanged += Extractor_ExtractionProgressChanged; course = extractor.ExtractCourse(config.Quality); } CourseDownloader.DownloadCourse(course, config.CourseDirectory); return; } catch (InvalidTokenException) { TUI.ShowError("The token you supplied is invalid - Login Failed"); Console.WriteLine("Creating new config file"); RunWithoutConfig(); return; } catch (WebDriverException ex) { TUI.ShowError("An error occured in the driver : " + ex.Message); TUI.ShowError("Error details : " + ex.StackTrace); TUI.ShowError("Trying again..."); RunWithConfig(config); return; } catch (Exception ex) { TUI.ShowError("An error occured while extracting the course data : " + ex.Message); TUI.ShowError("Error details : " + ex.StackTrace); TUI.ShowError("Trying again..."); RunWithConfig(config); return; } } catch (WebDriverException ex) { TUI.ShowError("An error occured in the driver : " + ex.Message); TUI.ShowError("Error details : " + ex.StackTrace); TUI.ShowError("Trying again..."); RunWithConfig(config); return; } catch (Exception ex) { TUI.ShowError("An error occured : " + ex.Message); TUI.ShowError("Error details : " + ex.StackTrace); RunWithConfig(config); return; } } }
private static void RunWithoutConfig() { while (true) { try { var extractor = new Extractor(); Browser selectedBrowser = TUI.GetBrowser(); var initializationTask = extractor.InitializeDriver(selectedBrowser); initializationTask.Start(); string courseUrl = TUI.GetCourseUrl(); while (true) { try { string token = TUI.GetLoginToken(); var loginTask = extractor.Login(token, courseUrl, initializationTask); var courseRootDirectory = TUI.GetPath(); var selectedQuality = TUI.GetQuality(); Console.WriteLine(TUI.continueGlyph + "Logging in..."); loginTask.Wait(); Console.WriteLine(TUI.continueGlyph + "Logged in successfully"); Config config = new Config { AuthenticationToken = token, Browser = selectedBrowser, Quality = selectedQuality, CourseDirectory = courseRootDirectory }; File.WriteAllText("./Config.json", config.ToJson()); Console.WriteLine(TUI.continueGlyph + "Saved entries to config file"); Console.WriteLine(TUI.endGlyph + "Intializing Course Extractor..."); Course course = new Course(); extractor.ExtractCourseStructure(out int videosCount); using (pbarExtractor = new ProgressBar(videosCount, "Extracting Course Links - This will take some time", optionPbarExtractor)) { extractor.ExtractionProgressChanged += Extractor_ExtractionProgressChanged; course = extractor.ExtractCourse(selectedQuality); } CourseDownloader.DownloadCourse(course, courseRootDirectory); return; } catch (InvalidTokenException) { TUI.ShowError("The token you supplied is invalid - Login Failed"); } } } catch (WebDriverException ex) { TUI.ShowError("An error occured in the driver : " + ex.Message); TUI.ShowError("Error details : " + ex.StackTrace); TUI.ShowError("Trying again..."); RunWithoutConfig(); return; } catch (Exception ex) { TUI.ShowError("An error occured : " + ex.Message); TUI.ShowError("Error details : " + ex.StackTrace); RunWithoutConfig(); return; } } }
private static void RunWithoutConfig() { Browser selectedBrowser = TUI.GetBrowser(); Extractor.InitializeDriver(selectedBrowser); string courseUrl = TUI.GetCourseUrl(); string token = TUI.GetLoginToken(); var loginTask = Extractor.Login(token, courseUrl); var courseRootDirectory = TUI.GetPath(); var selectedQuality = TUI.GetQuality(); Log.Information("Logging in..."); Console.WriteLine(TUI.continueGlyph + "Logging in..."); try { loginTask.Wait(); } catch (InvalidTokenException ex) { Log.Error(ex, "Failed to log in with course url : {0} and token of {1} characters", courseUrl, token.Length); TUI.ShowError("The token or the course url you provided is invalid. Please make sure you entered the right token and course url"); RunWithoutConfig(); return; } catch (Exception e) when(e.InnerException is InvalidTokenException ex) { Log.Error(ex, "Failed to log in with course url : {0} and token of {1} characters", courseUrl, token.Length); TUI.ShowError("The token or the course url you provided is invalid. Please make sure you entered the right token and course url"); RunWithoutConfig(); return; } catch (Exception ex) { Log.Error(ex, "Unknown Exception"); TUI.ShowError("Unknown Error occured - " + ex.Message); RunWithoutConfig(); return; } Log.Information("Logged in successfully"); Console.WriteLine(TUI.continueGlyph + "Logged in successfully"); Config config = new Config { AuthenticationToken = token, Browser = selectedBrowser, Quality = selectedQuality, CourseDirectory = courseRootDirectory }; File.WriteAllText("./Config.json", config.ToJson()); Console.WriteLine(TUI.continueGlyph + "Saved entries to config file"); Log.Information("Saved entries to congig file. Intializing Course Extractor"); Console.WriteLine(TUI.endGlyph + "Intializing Course Extractor"); Course course = ExtractCourse(config); if (course is null) { RunWithoutConfig(); return; } Log.Information("Course Extracted. Downloading..."); Console.WriteLine(); CourseDownloader.DownloadCourse(course, courseRootDirectory); }