/// <summary> /// download image and save to folder /// </summary> /// <param name="url"></param> private void Download(string url, int pageNo) { try { using (WebResponse response = HtmlUti.Request(url)) { using (Stream stream = response.GetResponseStream()) { Image img = Image.FromStream(stream, false); string saveFilePath = this.Chapter.SavePath + "\\" + pageNo.ToString() + img.RawFormat.GetFilenameExtension().Replace("*", "").Split(';').FirstOrDefault(); if (!Directory.Exists(saveFilePath)) { Directory.CreateDirectory(Path.GetDirectoryName(saveFilePath)); } img.Save(saveFilePath); } } } catch (Exception) { throw; } }
/// <summary> /// update list of manga /// </summary> public void update(string fileName) { try { WebResponse response; Stream stream; HtmlDocument document = new HtmlDocument(); //offset of manga directory current page int currentPage = 1; //offset of manga directory last page int lastPage; response = HtmlUti.Request(this.directoryURL); stream = response.GetResponseStream(); document.Load(stream, Encoding.UTF8); lastPage = this.getLastPage(document); do { IEnumerable <HtmlNode> trNodes = (from node in document.DocumentNode.Descendants() where node.Name == "table" && node.Attributes.Contains("class") && node.Attributes["class"].Value == "listing" select node).FirstOrDefault().ChildNodes.Where(x => x.Name == "tr").Skip(2); foreach (HtmlNode node in trNodes) { Manga manga = new Manga(EnmSite.VnSharing); manga.URL = this.rootURL + node.ChildNodes.Where(x => x.Name == "td").FirstOrDefault().ChildNodes["a"].Attributes["href"].Value + "&confirm=yes"; manga.Name = node.ChildNodes.Where(x => x.Name == "td").FirstOrDefault().ChildNodes["a"].InnerText.Trim(); this.Mangas.Add(manga); } //close current connection //in order to make a new one stream.Close(); response.Close(); currentPage++; //make new connection for next loop if (currentPage <= lastPage) { response = HtmlUti.Request(this.directoryURL + "?Page=" + currentPage.ToString()); stream = response.GetResponseStream(); document.Load(stream, Encoding.UTF8); } }while(currentPage <= lastPage); this.write(fileName); } catch (Exception) { throw; } }
/// <summary> /// load list of chapter /// </summary> /// <param name="manga"></param> /// <returns></returns> public List <Model.Chapter> loadChapter(Manga manga) { try { List <Chapter> chapters = new List <Chapter>(); using (WebResponse response = HtmlUti.Request(manga.URL)) { using (Stream stream = response.GetResponseStream()) { HtmlDocument document = new HtmlDocument(); document.Load(stream, Encoding.UTF8); IEnumerable <HtmlNode> listingNodes = document.DocumentNode.Descendants() .Where(x => x.Name == "table" && x.Attributes.Contains("class") && x.Attributes["class"].Value == "listing").FirstOrDefault() .ChildNodes.Where(x => x.Name == "tr").Skip(2); foreach (HtmlNode node in listingNodes) { HtmlNode chapterNode = node.ChildNodes.Where(x => x.Name == "td").FirstOrDefault().ChildNodes["a"]; Chapter chapter = new Chapter(manga, new VnSharingDownloader()); chapter.Name = chapterNode.InnerText.Trim(); chapter.URL = this.rootURL + chapterNode.Attributes["href"].Value; chapters.Insert(0, chapter); } } } return(chapters); } catch (Exception) { throw; } }