Example #1
0
        public async Task GivenProvider_Load_LoadingAndLoadedEventMustFired()
        {
            var    visit   = ComicVisitingHelper.CreateResrouceVisitor();
            object sender1 = null;
            string addr1   = null;

            visit.Loading += (o, e) =>
            {
                sender1 = o;
                addr1   = e;
            };
            object      sender2 = null;
            ComicEntity entity  = null;

            visit.Loaded += (o, e) =>
            {
                sender2 = o;
                entity  = e;
            };
            var res = await visit.LoadAsync(ComicVisitingHelper.AnyUri.AbsoluteUri);

            Assert.AreEqual(visit, sender1);
            Assert.AreEqual(ComicVisitingHelper.AnyUri.AbsoluteUri, addr1);

            Assert.AreEqual(visit, sender2);
            Assert.AreEqual(visit.Entity, entity);
            visit.Dispose();
        }
Example #2
0
 public ComicDownloadRequest(IComicSaver saver,
                             ComicEntity entity,
                             ComicDetail detail,
                             IReadOnlyCollection <DownloadItemRequest> requests,
                             IComicSourceProvider provider)
 {
     Detail           = detail;
     Entity           = entity;
     Saver            = saver;
     DownloadRequests = requests;
     Provider         = provider;
 }
Example #3
0
 public ComicDownloadContext(ComicEntity entity,
                             ComicChapter chapter,
                             ComicPage page,
                             Stream sourceStream,
                             CancellationToken token)
 {
     Token        = token;
     Entity       = entity;
     Chapter      = chapter;
     Page         = page;
     SourceStream = sourceStream;
 }
Example #4
0
        public async Task <ComicEntity> GetChaptersAsync(string targetUrl)
        {
            var mc   = new Uri(targetUrl).Segments.Last();
            var part = mc.Remove(0, 2);

            using (var mem = GetStream())
            {
                var arg = $"{{\"comic_id\":{part}}}";
                WrtieStream(mem, arg);
                var stream = await GetStreamAsync(detailUri, "POST", mem);

                string str = null;
                using (var sr = new StreamReader(stream))
                {
                    str = sr.ReadToEnd();
                }
                using (var jobj = JsonVisitor.FromString(str))
                {
                    var data   = jobj["data"];
                    var entity = new ComicEntity
                    {
                        Name     = data["title"].ToString(),
                        Descript = data["evaluate"].ToString(),
                        ComicUrl = targetUrl,
                        ImageUrl = data["vertical_cover"].ToString()
                    };
                    var ep     = data["ep_list"].ToEnumerable();
                    var len    = ep.Count();
                    var chapts = new List <ComicChapter>(ep.Count());
                    foreach (var item in ep)
                    {
                        var title = item["title"].ToString();
                        if (string.IsNullOrWhiteSpace(title))
                        {
                            title = entity.Name + "-" + (len - chapts.Count);
                        }
                        var id  = item["id"].ToString();
                        var url = "https://manga.bilibili.com/" + mc + "/mc" + id;
                        var chp = new ComicChapter
                        {
                            TargetUrl = url,
                            Title     = title
                        };
                        chapts.Add(chp);
                    }
                    chapts.Reverse();
                    entity.Chapters = chapts.ToArray();
                    return(entity);
                }
            }
        }
Example #5
0
        public string Store(ComicEntity entity, bool superFavorite = false)
        {
            var model = new ComicStoreModel
            {
                Chapters      = entity.Chapters,
                ComicUrl      = entity.ComicUrl,
                CreateTime    = DateTime.Now.Ticks,
                ImageUrl      = entity.ImageUrl,
                Name          = entity.Name,
                Descript      = entity.Descript,
                SuperFavorite = superFavorite
            };

            return(Store(model));
        }
Example #6
0
        protected virtual async Task <ChapterWithPage> StoreChapterAsync(ComicEntity entity, ComicChapter chapter)
        {
            var pages = await Provider.GetPagesAsync(chapter.TargetUrl);

            if (pages != null)
            {
                foreach (var item in pages)
                {
                    if (tokenSource.IsCancellationRequested)
                    {
                        break;
                    }
                    await StorePageAsync(entity, chapter, item);
                }
            }
            return(new ChapterWithPage(chapter, pages));
        }
Example #7
0
        protected virtual async Task StorePageAsync(ComicEntity entity, ComicChapter chapter, ComicPage page)
        {
            KeepingAction?.Invoke(entity, chapter, page, null, KeepingActionTypes.BeginStorePage);
            OnBeginStorePage(chapter, page);
            try
            {
                var mode = WriteMode;
                if (mode == ComicStoreWriteModes.NotExists)
                {
                    var ok = await storeService.ExistsAsync(page.TargetUrl);

                    if (ok)
                    {
                        KeepingAction?.Invoke(entity, chapter, page, null, KeepingActionTypes.EndStorePage);
                        OnEndStorePage(chapter, page, false);
                        return;
                    }
                }
                var path = page.TargetUrl;
                if (HasImageFolderName)
                {
                    path = string.Concat(ImageFolderName, "/", page.TargetUrl);
                }
                var stream = await fetchService.GetStreamAsync(page.TargetUrl);

                if (stream is null)
                {
                    stream = await Provider.GetImageStreamAsync(page.TargetUrl);

                    if (fetchService != storeService)
                    {
                        await fetchService.SaveAsync(path, stream);
                    }
                }
                stream.Seek(0, SeekOrigin.Begin);
                await storeService.SaveAsync(path, stream);
            }
            catch (Exception ex)
            {
                KeepingAction?.Invoke(entity, chapter, page, ex, KeepingActionTypes.StorePageException);
                OnStorePageException(chapter, page, ex);
            }
            KeepingAction?.Invoke(entity, chapter, page, null, KeepingActionTypes.EndStorePage);
            OnEndStorePage(chapter, page, true);
        }
Example #8
0
        public void GivenValueInit_PropertyMustEqualGiven()
        {
            var saver    = new NullSaver();
            var entity   = new ComicEntity();
            var detail   = new ComicDetail();
            var reqs     = new List <DownloadItemRequest>();
            var provider = new NullSourceProvider();
            var req      = new ComicDownloadRequest(saver, entity, detail, reqs, provider);

            Assert.AreEqual(saver, req.Saver);
            Assert.AreEqual(entity, req.Entity);
            Assert.AreEqual(detail, req.Detail);
            Assert.AreEqual(reqs, req.DownloadRequests);
            Assert.AreEqual(provider, req.Provider);
            var listener = new NullDownloadListener();

            req.Listener = listener;
            Assert.AreEqual(listener, req.Listener);
        }
Example #9
0
        public async Task <ComicEntity> GetChaptersAsync(string targetUrl)
        {
            var str = string.Empty;

            using (var sr = new StreamReader(await GetStreamAsync(targetUrl)))
            {
                str = sr.ReadToEnd();
            }
            var html = new HtmlDocument();

            html.LoadHtml(str);
            var img   = html.DocumentNode.SelectSingleNode("//div[@class='banner_detail_form']/div[@class='cover']/img");
            var title = html.DocumentNode.SelectSingleNode("//div[@class='banner_detail_form']/div[@class='info']/p[@class='title']");
            var desc  = html.DocumentNode.SelectSingleNode("//p[@id='a_closes']");

            var chpsBox = html.DocumentNode.SelectNodes("//div[@id='chapterlistload']/ul/li/a");
            var chps    = new List <ComicChapter>();

            foreach (var item in chpsBox)
            {
                var name = item.InnerText.Trim();
                var addr = item.Attributes["href"].Value;
                var chp  = new ComicChapter
                {
                    Title     = name,
                    TargetUrl = "http://www.bikabika.com" + addr
                };
                chps.Add(chp);
            }
            var entity = new ComicEntity
            {
                ComicUrl = targetUrl,
                Descript = desc?.InnerText,
                ImageUrl = img?.Attributes["src"].Value,
                Name     = title?.InnerText,
                Chapters = chps.ToArray()
            };

            return(entity);
        }
Example #10
0
 public async Task <ComicEntity> GetChaptersAsync(string targetUrl)
 {
     using (var stream = await GetStreamAsync(targetUrl))
     {
         var str = string.Empty;
         using (var sr = new StreamReader(stream))
         {
             str = sr.ReadToEnd();
         }
         var html = new HtmlDocument();
         html.LoadHtml(str);
         var img  = html.DocumentNode.SelectSingleNode("//div[@class='inner']/div[@class='ctdbLeft ']/a/img");
         var desc = html.DocumentNode.SelectSingleNode("//p[@id='worksDesc']");
         var list = html.DocumentNode.SelectNodes("//div[@class='comic-content-list']/ul/li/a");
         var chps = new List <ComicChapter>();
         foreach (var item in list)
         {
             var url  = item.Attributes["href"].Value;
             var name = item.Attributes["title"].Value;
             var chp  = new ComicChapter
             {
                 TargetUrl = "https://www.qimiaomh.com" + url,
                 Title     = name
             };
             chps.Add(chp);
         }
         var entity = new ComicEntity
         {
             Name     = img.Attributes["alt"].Value,
             Chapters = chps.ToArray(),
             ComicUrl = targetUrl,
             Descript = desc?.InnerText,
             ImageUrl = img.Attributes["src"].Value
         };
         return(entity);
     }
 }
Example #11
0
        public async Task <ComicEntity> GetChaptersAsync(string targetUrl)
        {
            targetUrl = GetTrueUrl(targetUrl);
            string str = null;

            using (var sr = new StreamReader(await CreateRequest(targetUrl)))
            {
                str = sr.ReadToEnd();
            }
            var doc = new HtmlDocument();

            doc.LoadHtml(str);
            var title = doc.DocumentNode.SelectSingleNode("//div[@class='comic_deCon']/h1/a")?.InnerText;

            if (title == null)
            {
                title = doc.DocumentNode.SelectSingleNode("//span[@class='anim_title_text']/a/h1")?.InnerText;
            }
            if (title == null)
            {
                title = targetUrl.Split('/').Last();
            }
            var desc = doc.DocumentNode.SelectSingleNode("//div[@clas='wrap_intro_l_comic']/div[@class='comic_deCon']/p[@class='comic_deCon_d']");
            var node = doc.DocumentNode.SelectNodes("//div[@class='cartoon_online_border']/ul/li/a");

            if (node == null)
            {
                node = doc.DocumentNode.SelectNodes("//ul[@class='list_con_li autoHeight']/li/a");
            }
            var img     = doc.DocumentNode.SelectSingleNode("//div[@class='wrap_intro_l_comic']/div[@class='comic_i']/div[@class='comic_i_img']/a/img");
            var chartps = new List <ComicChapter>();

            for (int i = node.Count - 1; i >= 0; i--)
            {
                var item = node[i];
                var name = item.InnerText;
                var url  = item.Attributes["href"]?.Value;
                if (url != null && !url.StartsWith("http://") &&
                    !url.StartsWith("https://"))
                {
                    url = "https://manhua.dmzj.com" + url;
                }
                var chap = new ComicChapter
                {
                    TargetUrl = url,
                    Title     = HttpUtility.UrlDecode(name)
                };
                chartps.Add(chap);
            }
            chartps.Reverse();
            var entity = new ComicEntity
            {
                Chapters = chartps.ToArray(),
                Descript = desc?.InnerText,
                ComicUrl = targetUrl,
                ImageUrl = img?.Attributes["src"]?.Value,
                Name     = title?.Trim()
            };

            return(entity);
        }
Example #12
0
 protected virtual void OnEndGetEntity(ComicEntity entity)
 {
 }
Example #13
0
 protected virtual void OnEndStoreChapter(ComicEntity entity, ComicChapter chapter)
 {
 }
Example #14
0
 protected virtual void OnStoreChapterException(ComicEntity entity, ComicChapter chapter, Exception ex)
 {
 }