Advertisement
Guest User

WuxiaWorld_API

a guest
Aug 25th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Extenstions;
  7. using HtmlAgilityPack;
  8. using System.Drawing;
  9. using System.Net;
  10. using System.Threading;
  11.  
  12. namespace Break
  13. {
  14.     class WuxiaAPI
  15.     {
  16.         public delegate void MyFunc();
  17.         public delegate void MyFunc<T>(T f);
  18.         public delegate void MyFunc<T, Y>(T f, Y g);
  19.  
  20.         public event MyFunc onChaptersLoad;
  21.         public event MyFunc onChapterContentLoad;
  22.         public event MyFunc<int> onChapterGet;
  23.         public event MyFunc<int> onContentGet;
  24.  
  25.         public string novel_name { get; private set; }
  26.         public string novel_url { get; private set; }
  27.         public string chapter_url { get; private set; }
  28.         public string novel_img_url { get; private set; }
  29.         public int chapter_count { get; private set; }
  30.  
  31.         public List<string> chapters { get; private set; } = new List<string>();
  32.         public List<string> content { get; private set; } = new List<string>();
  33.         public Image novel_img { get; private set; }
  34.  
  35.         public WuxiaAPI(string nv)
  36.         {
  37.             novel_name = nv.Trim();
  38.             string name = novel_name.ToLower().Replace(' ', '-');
  39.             novel_url = $"https://www.wuxiaworld.com/novel/{name}";
  40.             chapter_url = $"{novel_url}/{name.GetFirstIndex("-","with")}-chapter-";
  41.         }
  42.  
  43.         public List<string> GetChapterContent(int chapter, params string[] bad_str)
  44.         {
  45.             int i = 0;
  46.             HtmlWeb web = new HtmlWeb();
  47.             HtmlDocument doc = web.Load(chapter_url + chapter.ToString());
  48.             IEnumerable<HtmlNode> node = doc.GetElementbyId("chapter-content").Descendants();
  49.             foreach (HtmlNode nd in node)
  50.               if (!content.Contains(nd.InnerText) && !(bad_str ?? new string[] { }).Contains(nd.InnerText.Trim()))
  51.               {
  52.                   FireEvent<int>(onContentGet, i);
  53.                   content.Add(nd.InnerText);
  54.                   i++;
  55.               }
  56.               FireEvent(onChapterContentLoad);
  57.             return content;
  58.         }
  59.  
  60.         public List<string> GetChapters()
  61.         {
  62.  
  63.             HtmlWeb web = new HtmlWeb();
  64.             HtmlDocument doc = web.Load(novel_url);
  65.             HtmlNodeCollection node = doc.DocumentNode.SelectNodes("//*[@class=\"chapter-item\"]");
  66.             for(int i = 0; i < node.Count; i++)
  67.             {
  68.                FireEvent<int>(onChapterGet, i);
  69.                chapters.Add(node[i].InnerText);
  70.             }
  71.             chapter_count = chapters.Count;
  72.             FireEvent(onChaptersLoad);
  73.             return chapters;
  74.         }
  75.  
  76.         public Image GetImage()
  77.         {
  78.             if (novel_img != null)
  79.                 return novel_img;
  80.  
  81.             HtmlWeb web = new HtmlWeb();
  82.             HtmlDocument doc = web.Load(novel_url);
  83.             HtmlNode node = doc.DocumentNode.SelectNodes("//*[@class=\"media-object\"]")[0];
  84.             using (var response = WebRequest.Create(node.GetAttributeValue("src", "http://leeford.in/wp-content/uploads/2017/09/image-not-found.jpg")).GetResponse())
  85.                 using (var stream = response.GetResponseStream())
  86.                 {
  87.                 novel_img_url = node.GetAttributeValue("src", "http://leeford.in/wp-content/uploads/2017/09/image-not-found.jpg");
  88.                     novel_img = Bitmap.FromStream(stream);
  89.                     return novel_img;
  90.                 }
  91.         }
  92.  
  93.         public int GetChapterCount()
  94.         {
  95.             return chapter_count == 0 ? GetChapters().Count : chapter_count;
  96.         }
  97.  
  98.         private void FireEvent(MyFunc d)
  99.         {
  100.             (d ?? (() => { }))();
  101.         }
  102.  
  103.         private void FireEvent<T>(MyFunc<T> f, T t)
  104.         {
  105.             (f ?? ((q) => { }))(t);
  106.         }
  107.  
  108.         private void FireEvent<T,Y>(MyFunc<T,Y> f, T t, Y g)
  109.         {
  110.             (f ?? ((q,k) => { }))(t,g);
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116. namespace Extenstions
  117. {
  118.     public static class StringExtenstions
  119.     {
  120.         public static bool CompareFrom(this string f, params string[] n)
  121.         {
  122.             foreach (string z in n)
  123.                 if (z == f)
  124.                     return true;
  125.             return false;
  126.         }
  127.  
  128.         public static string GetFirstIndex(this string f, string sep, params string[] non)
  129.         {
  130.             string res = null;
  131.             foreach (string st in f.Split(sep[0]))
  132.                 if (!non.Contains(st))
  133.                     res += st[0];
  134.             return res;
  135.         }
  136.  
  137.         public static string Single(this List<string> f, string sep)
  138.         {
  139.             string res = null;
  140.             foreach (string k in f)
  141.                 res += $"{k}{sep}";
  142.             return res;
  143.         }
  144.  
  145.         public static List<string> RemoveTo(this List<string> f, params int[] i)
  146.         {
  147.             foreach (int z in i)
  148.                 f.RemoveAt(z);
  149.             return f;
  150.         }
  151.  
  152.         public static List<string> RemoveTo(this List<string> f, params string[] i)
  153.         {
  154.             foreach (int a in f.IndexOfAll(i))
  155.                 f.RemoveAt(a);
  156.             return f;
  157.         }
  158.  
  159.         public static IEnumerable<int> IndexOfAll(this List<string> f, params string[] str)
  160.         {
  161.             List<int> a = new List<int>();
  162.             for (int i = 0; i < f.Count; i++)
  163.                 if (str.Contains(f[i]))
  164.                     a.Add(i);
  165.             return a;
  166.  
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement