コード例 #1
0
        /// <summary>
        /// Download the content of the IExtension for the MangaObject
        /// </summary>
        /// <param name="Extension">The IExtension to use for Cookies and Referer</param>
        /// <param name="MangaObject">The MangaObject used</param>
        /// <returns></returns>
        private async Task <ExtensionContentResult> LoadExtensionMangaContent(IExtension Extension, MangaObject MangaObject)
        {
            // Locate the LocationObject for the requested IExtension
            List <LocationObject> MangaInfoLocationObjects = new List <LocationObject>();

            MangaInfoLocationObjects.AddRange(MangaObject.Locations);
            MangaInfoLocationObjects.AddRange(MangaObject.DatabaseLocations);
            LocationObject LocationObject = MangaInfoLocationObjects.FirstOrDefault(_ =>
            {
                if (!Equals(Extension.ExtensionDescriptionAttribute.Name, _.ExtensionName))
                {
                    return(false);
                }
                if (!Equals(_.ExtensionLanguage, null)) // Only check language if location has one.
                {
                    if (!Equals(Extension.ExtensionDescriptionAttribute.Language, _.ExtensionLanguage))
                    {
                        return(false);
                    }
                }
                return(_.Enabled);
            });

            if (Equals(LocationObject, null)) // If there is not a match return null
            {
                return(null);
            }
            using (WebDownloader WebDownloader = new WebDownloader(Extension.Cookies))
            {
                WebDownloader.Encoding = System.Text.Encoding.UTF8;
                WebDownloader.Referer  = Extension.ExtensionDescriptionAttribute.RefererHeader;
                try
                {
                    String Content = await WebDownloader.DownloadStringTaskAsync(LocationObject.Url).Retry(DOWNLOAD_TIMEOUT);

                    return(new ExtensionContentResult()
                    {
                        Extension = Extension,
                        Location = LocationObject,
                        Content = Content
                    });
                }
                catch (Exception ex)
                {
                    String Name     = Extension.ExtensionDescriptionAttribute.Name,
                           Language = Extension.ExtensionDescriptionAttribute.Language;
                    if (!Equals(CORE.Logger, null))
                    {
                        CORE.Logger.Warn(String.Format("Unable to load content from {0}-{1} for {2}.", Name, Language, MangaObject.Name), ex);
                    }
                    return(null);
                }
            }
        }
コード例 #2
0
 private async Task <String> ProcessSearchRequest(SearchRequestObject RequestObject)
 {
     using (WebDownloader WebDownloader = new WebDownloader())
     {
         WebDownloader.Encoding = System.Text.Encoding.UTF8;
         WebDownloader.Referer  = RequestObject.Referer;
         switch (RequestObject.Method)
         {
         case SearchMethod.GET:
             return(await WebDownloader.DownloadStringTaskAsync(RequestObject.Url));
         }
     }
     return(null);
 }
コード例 #3
0
        private async Task <Stream> LoadImageAsync(String Url, String Referer, CookieCollection Cookies, CancellationToken ct, IProgress <Int32> progress)
        {
            try
            {
                await ImageTaskConcurrencySemaphore.WaitAsync(ct);

                ct.ThrowIfCancellationRequested();

                using (WebDownloader WebDownloader = new WebDownloader(Cookies))
                {
                    WebDownloader.Referer = Referer;
                    DownloadProgressChangedEventHandler ProgressEventHandler = (s, e) =>
                    {
                        if (!Equals(progress, null))
                        {
                            progress.Report(e.ProgressPercentage);
                        }
                        ct.ThrowIfCancellationRequested();
                    };
                    WebDownloader.DownloadProgressChanged += ProgressEventHandler;
                    Stream ImageStream = new MemoryStream();
                    using (Stream WebStream = await WebDownloader.OpenReadTaskAsync(Url))
                    { await WebStream.CopyToAsync(ImageStream); }
                    WebDownloader.DownloadProgressChanged -= ProgressEventHandler;
                    ImageStream.Seek(0, SeekOrigin.Begin);

                    if (ct.IsCancellationRequested)
                    {
                        ImageStream.Dispose();
                    }
                    ct.ThrowIfCancellationRequested();

                    if (!Equals(progress, null))
                    {
                        progress.Report(100);
                    }
                    return(ImageStream);
                }
            }
            finally { ImageTaskConcurrencySemaphore.Release(); }
        }
コード例 #4
0
        private async Task <PageObject> LoadPageObjectAsync(MangaObject MangaObject, ChapterObject ChapterObject, PageObject PageObject, CancellationToken ct, IProgress <Int32> progress)
        {
            try
            {
                await TaskConcurrencySemaphore.WaitAsync(ct);

                ct.ThrowIfCancellationRequested();

                ISiteExtension SiteExtension = CORE.SiteExtensions.First(_SiteExtension => PageObject.Url.Contains(_SiteExtension.ExtensionDescriptionAttribute.URLFormat));
                using (WebDownloader WebDownloader = new WebDownloader(SiteExtension.Cookies))
                {
                    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 PageWebContent = await WebDownloader.DownloadStringTaskAsync(PageObject.Url).Retry(DOWNLOAD_TIMEOUT);

                    PageObject = SiteExtension.ParsePageObject(PageWebContent);

                    WebDownloader.DownloadProgressChanged -= ProgressEventHandler;
                }
                if (!Equals(progress, null))
                {
                    progress.Report(100);
                }
                return(PageObject);
            }
            finally { TaskConcurrencySemaphore.Release(); }
        }
コード例 #5
0
        /// <summary>
        /// Download the content of the IExtension for the MangaObject
        /// </summary>
        /// <param name="Extension">The IExtension to use for Cookies and Referer</param>
        /// <param name="MangaObject">The MangaObject used</param>
        /// <returns></returns>
        private async Task <ExtensionContentResult> LoadExtensionSearchContent(IExtension Extension, String SearchTerm)
        {
            using (WebDownloader WebDownloader = new WebDownloader(Extension.Cookies))
            {
                WebDownloader.Encoding = System.Text.Encoding.UTF8;
                WebDownloader.Referer  = Extension.ExtensionDescriptionAttribute.RefererHeader;
                try
                {
                    SearchRequestObject sro = Extension.GetSearchRequestObject(SearchTerm);
                    String Content          = await ProcessSearchRequest(sro).Retry(DOWNLOAD_TIMEOUT);

                    return(new ExtensionContentResult()
                    {
                        Extension = Extension,
                        Location = new LocationObject()
                        {
                            Enabled = true,
                            Url = sro.Url,
                            ExtensionName = Extension.ExtensionDescriptionAttribute.Name,
                            ExtensionLanguage = Extension.ExtensionDescriptionAttribute.Language
                        },
                        Content = Content
                    });
                }
                catch (Exception ex)
                {
                    String Name     = Extension.ExtensionDescriptionAttribute.Name,
                           Language = Extension.ExtensionDescriptionAttribute.Language;
                    if (!Equals(CORE.Logger, null))
                    {
                        CORE.Logger.Warn(String.Format("Unable to load search content from {0}-{1} for {2}.", Name, Language, SearchTerm), ex);
                    }
                    return(null);
                }
            }
        }
コード例 #6
0
        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(); }
        }