private async Task <ChapterObject> LoadChapterObjectAsync(MangaObject MangaObject, ChapterObject ChapterObject, CancellationToken ct, IProgress <Int32> progress) { try { await TaskConcurrencySemaphore.WaitAsync(ct); ct.ThrowIfCancellationRequested(); // Store valid ISiteExtension IEnumerable <ISiteExtension> ValidSiteExtensions = ValidExtensions(CORE.SiteExtensions, CORE.UserConfiguration.EnabledExtensions).Cast <ISiteExtension>(); // Re-Order the Chapter's LocationObjects to the EnabledExtensions order. IEnumerable <LocationObject> OrderedChapterObjectLocations = from EnExt in CORE.UserConfiguration.EnabledExtensions where ChapterObject.Locations.Exists(LocObj => EnExt.EqualsLocationObject(LocObj)) select ChapterObject.Locations.FirstOrDefault(LocObj => EnExt.EqualsLocationObject(LocObj)); foreach (LocationObject LocationObject in OrderedChapterObjectLocations) { ct.ThrowIfCancellationRequested(); ISiteExtension SiteExtension = ValidSiteExtensions.FirstOrDefault(_ => LocationObjectExtension(_, LocationObject)); if (Equals(SiteExtension, null)) { continue; // Continue with the foreach loop } ct.ThrowIfCancellationRequested(); using (WebDownloader WebDownloader = new WebDownloader(SiteExtension.Cookies)) { WebDownloader.Encoding = System.Text.Encoding.UTF8; WebDownloader.Referer = SiteExtension.ExtensionDescriptionAttribute.RefererHeader; DownloadProgressChangedEventHandler ProgressEventHandler = (s, e) => { if (!Equals(progress, null)) { progress.Report((Int32)Math.Round((Double)e.ProgressPercentage * 0.9)); } ct.ThrowIfCancellationRequested(); }; WebDownloader.DownloadProgressChanged += ProgressEventHandler; String ChapterWebContent = await WebDownloader.DownloadStringTaskAsync(LocationObject.Url).Retry(DOWNLOAD_TIMEOUT); ChapterObject DownloadedChapterObject = SiteExtension.ParseChapterObject(ChapterWebContent); WebDownloader.DownloadProgressChanged -= ProgressEventHandler; ct.ThrowIfCancellationRequested(); if (!Equals(DownloadedChapterObject, null)) { ChapterObject.Merge(DownloadedChapterObject); ChapterObject.Pages = DownloadedChapterObject.Pages; break; // Break free of the foreach loop } } } if (!Equals(progress, null)) { progress.Report(100); } ct.ThrowIfCancellationRequested(); return(ChapterObject); } finally { TaskConcurrencySemaphore.Release(); } }
/// <summary> /// Filter Extensions /// </summary> /// <param name="Extensions">IEnumerable<IExtension> to filter</param> /// <param name="EnabledExtensions">IEnumerable<EnabledExtensionObject> to use for filter</param> /// <returns></returns> public IEnumerable <IExtension> ValidExtensions(IEnumerable <IExtension> Extensions, IEnumerable <EnabledExtensionObject> EnabledExtensions, SupportedObjects Supported = SupportedObjects.All) => Extensions.Where(Extension => { if (!Extension.ExtensionDescriptionAttribute.SupportedObjects.HasFlag(Supported)) { return(false); } if (Extension.ExtensionDescriptionAttribute.RequiresAuthentication) { if (!Extension.IsAuthenticated) { return(false); } } Int32 Count = EnabledExtensions.Where(EnExt => EnExt.Enabled).Count(EnExt => EnExt.EqualsIExtension(Extension)); if (Equals(Count, 0)) { return(false); } return(true); });