Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Extenstions;
- using HtmlAgilityPack;
- using System.Drawing;
- using System.Net;
- using System.Threading;
- namespace Break
- {
- class WuxiaAPI
- {
- public delegate void MyFunc();
- public delegate void MyFunc<T>(T f);
- public delegate void MyFunc<T, Y>(T f, Y g);
- public event MyFunc onChaptersLoad;
- public event MyFunc onChapterContentLoad;
- public event MyFunc<int> onChapterGet;
- public event MyFunc<int> onContentGet;
- public string novel_name { get; private set; }
- public string novel_url { get; private set; }
- public string chapter_url { get; private set; }
- public string novel_img_url { get; private set; }
- public int chapter_count { get; private set; }
- public List<string> chapters { get; private set; } = new List<string>();
- public List<string> content { get; private set; } = new List<string>();
- public Image novel_img { get; private set; }
- public WuxiaAPI(string nv)
- {
- novel_name = nv.Trim();
- string name = novel_name.ToLower().Replace(' ', '-');
- novel_url = $"https://www.wuxiaworld.com/novel/{name}";
- chapter_url = $"{novel_url}/{name.GetFirstIndex("-","with")}-chapter-";
- }
- public List<string> GetChapterContent(int chapter, params string[] bad_str)
- {
- int i = 0;
- HtmlWeb web = new HtmlWeb();
- HtmlDocument doc = web.Load(chapter_url + chapter.ToString());
- IEnumerable<HtmlNode> node = doc.GetElementbyId("chapter-content").Descendants();
- foreach (HtmlNode nd in node)
- if (!content.Contains(nd.InnerText) && !(bad_str ?? new string[] { }).Contains(nd.InnerText.Trim()))
- {
- FireEvent<int>(onContentGet, i);
- content.Add(nd.InnerText);
- i++;
- }
- FireEvent(onChapterContentLoad);
- return content;
- }
- public List<string> GetChapters()
- {
- HtmlWeb web = new HtmlWeb();
- HtmlDocument doc = web.Load(novel_url);
- HtmlNodeCollection node = doc.DocumentNode.SelectNodes("//*[@class=\"chapter-item\"]");
- for(int i = 0; i < node.Count; i++)
- {
- FireEvent<int>(onChapterGet, i);
- chapters.Add(node[i].InnerText);
- }
- chapter_count = chapters.Count;
- FireEvent(onChaptersLoad);
- return chapters;
- }
- public Image GetImage()
- {
- if (novel_img != null)
- return novel_img;
- HtmlWeb web = new HtmlWeb();
- HtmlDocument doc = web.Load(novel_url);
- HtmlNode node = doc.DocumentNode.SelectNodes("//*[@class=\"media-object\"]")[0];
- using (var response = WebRequest.Create(node.GetAttributeValue("src", "http://leeford.in/wp-content/uploads/2017/09/image-not-found.jpg")).GetResponse())
- using (var stream = response.GetResponseStream())
- {
- novel_img_url = node.GetAttributeValue("src", "http://leeford.in/wp-content/uploads/2017/09/image-not-found.jpg");
- novel_img = Bitmap.FromStream(stream);
- return novel_img;
- }
- }
- public int GetChapterCount()
- {
- return chapter_count == 0 ? GetChapters().Count : chapter_count;
- }
- private void FireEvent(MyFunc d)
- {
- (d ?? (() => { }))();
- }
- private void FireEvent<T>(MyFunc<T> f, T t)
- {
- (f ?? ((q) => { }))(t);
- }
- private void FireEvent<T,Y>(MyFunc<T,Y> f, T t, Y g)
- {
- (f ?? ((q,k) => { }))(t,g);
- }
- }
- }
- namespace Extenstions
- {
- public static class StringExtenstions
- {
- public static bool CompareFrom(this string f, params string[] n)
- {
- foreach (string z in n)
- if (z == f)
- return true;
- return false;
- }
- public static string GetFirstIndex(this string f, string sep, params string[] non)
- {
- string res = null;
- foreach (string st in f.Split(sep[0]))
- if (!non.Contains(st))
- res += st[0];
- return res;
- }
- public static string Single(this List<string> f, string sep)
- {
- string res = null;
- foreach (string k in f)
- res += $"{k}{sep}";
- return res;
- }
- public static List<string> RemoveTo(this List<string> f, params int[] i)
- {
- foreach (int z in i)
- f.RemoveAt(z);
- return f;
- }
- public static List<string> RemoveTo(this List<string> f, params string[] i)
- {
- foreach (int a in f.IndexOfAll(i))
- f.RemoveAt(a);
- return f;
- }
- public static IEnumerable<int> IndexOfAll(this List<string> f, params string[] str)
- {
- List<int> a = new List<int>();
- for (int i = 0; i < f.Count; i++)
- if (str.Contains(f[i]))
- a.Add(i);
- return a;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement