-
Notifications
You must be signed in to change notification settings - Fork 10
/
Novel.cs
672 lines (624 loc) · 26.1 KB
/
Novel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Drawing;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.IO.Compression;
namespace jjget
{
class Novel
{
public string name;
public string author;
public string descriptions;
public int chapterCount;
public int chapterDone;
public int startDlChapter;
public int endDlChapter;
private int novelid;
private string savePath;
public bool isFinnished;
private bool useMobileEdition = true;
private bool includeAuthorsWords = true;
private HttpUtil _savedHttpUtil = null;
private string cookiestr;
public string userDetail;
public bool hasLogin = false;
private string lastParsedHTML;
private WebProxy proxy = null;
private Action<string, Color> setProgressDelegate;
private Action<byte[]> setVerifyCodeDelegate;
private List<int> vipChapters = new List<int>();
private FontDecoder fontDecoder = new FontDecoder();
private Decryptor decryptor = new Decryptor();
public struct Chapter
{
public int chapterIndex;
public string title;
public string content;
public bool isVip;
public override string ToString()
{
return "第" + chapterIndex + "章 " + (isVip ? "[VIP]" : "") + " " + title + "\r\n" + content;
}
}
public void setSaveLoc(string loc)
{
this.savePath = loc;
this.readProgress();
this.readSavedUser();
}
public void setUseMobile(bool use)
{
this.useMobileEdition = use;
}
public void setIncludeAuthorsWords(bool inc)
{
this.includeAuthorsWords = inc;
}
public void registerSetProgressDelegate(Action<String, Color> d)
{
this.setProgressDelegate = d;
this.fontDecoder.registerSetProgressDelegate(d);
this.decryptor.registerSetProgressDelegate(d);
}
private void setPrompt(string text)
{
setProgressDelegate(text, Color.DarkGreen);
}
private void setPrompt(string text, Color c)
{
setProgressDelegate(text, c);
}
public void registerSetVerifyCodeDelegate(Action<byte[]> d)
{
this.setVerifyCodeDelegate = d;
}
public void setVerifyCode(byte[] b)
{
this.setVerifyCodeDelegate(b);
}
public void setHttpUtilProxy(string uri)
{
proxy = new WebProxy();
proxy.Address = new Uri(uri);
}
private string getNovelURL()
{
return getNovelURL(false, true, true);
}
private string getNovelURL(bool isVip, bool more, bool whole)
{
if (this.useMobileEdition)
if (isVip)
return "http://m.jjwxc.com/vip/" + this.novelid + (whole ? "?whole=1" : "");
else
return "http://m.jjwxc.com/book2/" + this.novelid +
(whole ? "?whole=1": "") +
(more ? (whole ? "&":"?") + "more=1" : "");
else
{
if (isVip)
return "http://my.jjwxc.net/onebook_vip.php?novelid=" + this.novelid;
else
return "http://www.jjwxc.net/onebook.php?novelid=" + this.novelid;
}
}
private string getChapterURL(int chap, bool isVip)
{
if (this.useMobileEdition)
return getNovelURL(isVip, false, false) + "/" + chap +
(isVip ? ("?ctime=" + Math.Floor(new Random().NextDouble() * 2000000000)) : "");
else
return getNovelURL(isVip, false, false) + "&chapterid=" + chap;
}
private string getReferer()
{
if (this.useMobileEdition)
return "http://m.jjwxc.com/";
else
return "http://www.jjwxc.net/";
}
private HttpUtil getHTTPUtil() {
if (this.useMobileEdition || _savedHttpUtil == null)
{
HttpUtil hu = new HttpUtil();
hu.cookiestr = cookiestr;
hu.setProxy(this.proxy);
hu.setEncoding("gb2312");
_savedHttpUtil = hu;
return hu;
}
else
return _savedHttpUtil;
}
private string parseText_Img_Link(HtmlNode node)
{
HtmlNode nd = null;
if (node == null) return "";
while ((nd = node.SelectSingleNode("./hr")) != null)
{
try
{
node.RemoveChild(nd);
}
catch (ArgumentOutOfRangeException) { break; }
}
string result = node.InnerHtml;
HtmlNodeCollection hnc = node.SelectNodes("./img|./input");
if (hnc != null)
{
List<HtmlNode> hnl = hnc.ToList();
if (hnl != null)
{
foreach (HtmlNode hn in hnl)
{
string onclick = hn.GetAttributeValue("onclick", "window.open(\"\")");
result = result.Replace(hn.OuterHtml,
hn.GetAttributeValue("src", "1")
+ hn.GetAttributeValue("value", "")
+ onclick.Substring(13, onclick.Length - 15));
}
}
}
return "\r\n" + result.Replace("<br>", "\r\n").Replace("</br>", "\r\n").Trim();
}
private string decodeCustomFont(string ct, string fontName)
{
return fontDecoder.Decode(ct, fontName);
}
public bool checkVerifyCode(string username, bool force)
{
_savedHttpUtil= null; // force a new httputil to not reuse connection pool
HttpUtil hu = getHTTPUtil();
Random rnd = new Random();
string result = hu.Get("http://my.jjwxc.net/login.php?action=checkneedauthnum&" +
"r=" + rnd.NextDouble() + "00&username=" + username);
Regex regex = new Regex(@"isneed""\s*:\s*true");
if (regex.Match(result).Success || force)
{
cookiestr = "";
hu.cookiestr = "";
byte[] img = hu.GetBinary("http://my.jjwxc.net/include/checkImage.php?random=" +
rnd.NextDouble() + "00", "image/webp,image/apng,image/*,*/*;q=0.8");
this.setVerifyCode(img);
cookiestr = hu.cookiestr;
return true;
}
return false;
}
public bool jjLogin(string username, string pwd, string verifycode, string customCookiestr)
{
setPrompt("正在登陆……");
HttpUtil hu = getHTTPUtil();
if(customCookiestr != null)
{
hu.cookiestr = customCookiestr;
cookiestr = customCookiestr;
} else
{
string result = hu.Get("http://my.jjwxc.net/login.php?" +
"action=login&login_mode=ajax&loginname=" + username + "&loginpassword=" + pwd + "&" +
"Ekey=&Challenge=&auth_num=" + verifycode + "&cookietime=1&" +
"client_time=" + HttpUtil.getTimeStamp() +
"&jsonp=jQuery18008677560358499031_" + HttpUtil.getTimeStamp() + "123" +
"&_=" + HttpUtil.getTimeStamp() + "123");
Regex jsonpRegex = new Regex(@"^[^(]+\((.+)\)$");
Match m = jsonpRegex.Match(result);
if (!m.Success)
{
setPrompt("登陆响应无法解析,试试用邮箱登陆? " + result);
}
else
{
result = m.Groups[1].Captures[0].ToString();
JObject jresult = JObject.Parse(result);
if (jresult["state"].ToString() == "10")
{
checkVerifyCode(username, true);
}
setPrompt("登陆失败QAQ: " + jresult["message"].ToString(), Color.Red);
}
cookiestr = hu.cookiestr;
}
hasLogin = cookiestr != null && cookiestr.IndexOf("token", 0) > -1;
if (hasLogin)
{
setPrompt("正在获取用户信息……");
getUserDetail();
writeSavedUser();
setPrompt("登陆成功(* ̄▽ ̄)y ");
return true;
}
return false;
}
private void getUserDetail()
{
HttpUtil hu = getHTTPUtil();
string my = hu.Get("http://my.jjwxc.net/backend/userinfo.php");
HtmlDocument hd = new HtmlDocument();
hd.LoadHtml(my);
HtmlNode root = hd.DocumentNode;
HtmlNode tb = root.SelectSingleNode("//table[@bgcolor='#009900']");
bool isWriter = false;
var detailTr = tb.SelectSingleNode(".//tr//font[@class='readerid']");
if (detailTr == null)
{
detailTr = tb.SelectSingleNode("./tr[1]//div[1]");
isWriter = true;
}
userDetail = "已登录 ID: " + (detailTr != null ? detailTr.InnerText.Trim(): "未知ID");
var _nickInput = tb.SelectSingleNode("//input[@name='birthday']");
string _nick = _nickInput == null ? _nickInput.GetAttributeValue("value", "未知昵称") : "未知昵称";
if (_nick != "您还没有设置昵称")
userDetail += " " + _nick;
// disable email for now
// userDetail += " "+tb.SelectSingleNode("./tr[" + (isWriter ? "8" : "7") + "]//td[@id='emailtd']/text()").InnerText;
}
private string stripEmpty(string input)
{
return input.Replace("\n", "").Replace("\r", "").
Replace(" ", "");
}
public bool getIndex(int novelid){
this.novelid = novelid;
setPrompt("下载首页中……", Color.Orange);
HttpUtil hu = getHTTPUtil();
string index = hu.Get(getNovelURL(), getReferer());
//StreamReader sFile = new StreamReader("z://1.htm", Encoding.GetEncoding("gb2312"));
//string index = sFile.ReadToEnd();
HtmlAgilityPack.HtmlDocument hd = new HtmlAgilityPack.HtmlDocument();
hd.LoadHtml(index);
//setPrompt("分析中……",Color.Orange);
HtmlNode hn;
HtmlNodeCollection hnc;
HtmlNode root = hd.DocumentNode;
if (useMobileEdition)
{
//book title
hn = root.SelectSingleNode("//h2");
this.name = hn.InnerText.Substring(3);
//descriptions
hnc = root.SelectNodes("//div[@class='b module']/ul//li");
foreach (HtmlNode n in hnc)
{
if (n.SelectSingleNode("a") != null && this.author == "")
{
this.author = hnc[0].SelectSingleNode("a").InnerText;
continue;
}
this.descriptions += stripEmpty(n.InnerText).Replace(":", ":") + "\n";
}
//chapters
HtmlNodeCollection chaps = root.SelectNodes("//div[@style='padding-left:10px']/a");
this.chapterCount = chaps.Count;
foreach (HtmlNode c in chaps)
{
if (c.OuterHtml.IndexOf("vip") != -1)
vipChapters.Add(chaps.IndexOf(c)+1);
}
}
else
{
var nameElem = root.SelectSingleNode("//span[@itemprop='articleSection']");
if (nameElem == null)
{
nameElem = root.SelectSingleNode("//span[@class='bigtext']");
}
this.name = stripEmpty(nameElem.InnerText);
var authorElem = root.SelectSingleNode("//span[@itemprop='author']");
if(authorElem == null)
{
authorElem = root.SelectSingleNode("//td[@class='noveltitle']/a");
}
this.author = authorElem.InnerText;
hnc = root.SelectNodes("//ul[@class='rightul']/li");
for (int idx=0;idx<8;idx++)
{
HtmlNode n = hnc[idx];
this.descriptions += HtmlEntity.DeEntitize(n.InnerText.Replace("\n", "").Replace("\r", "").
Replace(" ", "").Replace(":", ":"));
// 出版信息
var imgs = n.SelectNodes(".//img");
if(imgs != null )
{
foreach (var img in imgs)
{
this.descriptions += img.GetAttributeValue("title", "") + " ";
}
}
this.descriptions += "\n";
}
this.descriptions += root.SelectSingleNode("//div[@class='smallreadbody']/div[@id='novelintro']").InnerText;
HtmlNodeCollection chaps = root.SelectNodes("//table[@class='cytable']/tbody/tr[@itemtype='http://schema.org/Chapter']");
if(chaps != null)
{
foreach (HtmlNode c in chaps)
{
if (c.InnerHtml.IndexOf("onebook_vip") != -1)
vipChapters.Add(int.Parse(c.SelectSingleNode("./td[1]").InnerText.Trim()));
}
this.chapterCount = chaps.Count;
} else
{
// 没有独立章节
this.chapterCount = 1;
}
}
isFinnished = descriptions.IndexOf("连载中") == -1;
setPrompt("首页分析完成,可以开始了");
readProgress();
return true;
}
private string getInputLabelValue(HtmlNode root, string name)
{
var node = root.SelectSingleNode("//input[@name='" + name + "']");
if(node == null)
{
return "";
}
return node.GetAttributeValue("value", "");
}
public Chapter getSingleChapter(int chapter)
{
if (chapter > this.chapterCount)
throw new IndexOutOfRangeException("章节号"+chapter+"超出总数("+this.chapterCount+")");
bool isVip = isVipChapter(chapter);
if (isVip)
{
if(!hasLogin)
throw new Exception("章节" + chapter + "是VIP章节,请登陆ww");
if (useMobileEdition)
{
setPrompt("已切换回电脑版", Color.Cyan);
useMobileEdition = false;
}
}
HttpUtil hu = getHTTPUtil();
setPrompt("章节" + chapter + (isVip?"[VIP]":"") + "下载中……", Color.Orange);
// VIP章节忽略返回的cookie,好像是假的
string html = hu.Get(getChapterURL(chapter, isVip), getNovelURL(), isVip);
//FileStream fs = new FileStream(@"D:\Workspace\JJGet\s.txt", FileMode.Open);
//StreamReader sFile = new StreamReader(fs);
//string html = sFile.ReadToEnd();
//setPrompt("分析中……", Color.Orange);
lastParsedHTML = html;
HtmlAgilityPack.HtmlDocument hd = new HtmlAgilityPack.HtmlDocument();
hd.LoadHtml(html);
HtmlNodeCollection hnc;
HtmlNode root = hd.DocumentNode;
Chapter chpt = new Chapter();
chpt.isVip = isVip;
if (useMobileEdition)
{
chpt.title = root.SelectSingleNode("//h2[@class='big o']").InnerText;
chpt.title = chpt.title.Substring(chpt.title.IndexOf("、") + 1, chpt.title.Length - 4 - chpt.title.IndexOf("、")).Trim();
hnc = root.SelectNodes("//body/div/div/div/div/ul/li");
chpt.content = HtmlEntity.DeEntitize(hnc[0].InnerHtml.Replace("<br>", "\r\n"));
string author_words = HtmlEntity.DeEntitize(hnc[1].InnerText);
if (author_words.Length > 9)
chpt.content += "\r\n" + author_words;
}
else
{
HtmlNode novelnode = root.SelectSingleNode("//div[@class='novelbody']/div");
if(novelnode == null)
{
throw new Exception("章节" + chapter + (isVip ? "是VIP章节,请登陆ww":"解析失败"));
}
//自定义字体
string fontName = "";
foreach (string cls in novelnode.GetClasses())
{
if (cls.StartsWith("jjwxcfont_"))
{
fontName = cls;
}
}
chpt.title = novelnode.SelectSingleNode("./div/h2").InnerText;
//作者的话
if (this.includeAuthorsWords)
{
HtmlNode hn2 = root.SelectSingleNode("//div[@class='readsmall']");
chpt.content = parseText_Img_Link(hn2);
} else
{
chpt.content = "";
}
//内容
string mainbody;
var encryptedContent = getInputLabelValue(root, "content");
if(encryptedContent != "")
{
string[] contextKeys = {
"jsver", "cryptInfo", "accessKey",
"contentid", "novelid", "chapterid",
};
var context = new Dictionary<string, string>();
foreach(string key in contextKeys)
{
context[key] = getInputLabelValue(root, key);
}
// jsid 就是用户ID
Regex jsidRegex = new Regex(@"//my\.jjwxc\.net/backend/favorite\.php\?jsid=(\d+)");
var match = jsidRegex.Match(root.InnerHtml);
if(!match.Success)
{
throw new Exception("jsid提取失败");
}
context["jsid"] = match.Groups[1].Value;
mainbody = this.decryptor.Decrypt(encryptedContent, context);
} else
{
novelnode = novelnode.Clone(); // 避免修改DocumentNode
List<HtmlNode> hnl = novelnode.SelectNodes("./div|./font|./hr|./img|./script").ToList();
foreach (HtmlNode hn in hnl)
{
novelnode.RemoveChild(hn);
}
mainbody = novelnode.InnerHtml;
}
var styles = "";
foreach (var e in root.SelectNodes("//style"))
{
styles += e.InnerText;
}
var cssRegexStr = @"[^{]+{\s*content\s*:\s*['""](.*?)['""]\s*;*\s*}";
mainbody = Regex.Replace(mainbody, @"<span\s+class='([^']+)'>([^<]+)</span>", m => {
var clsName = m.Groups[1].Value;
var text = m.Groups[2].Value;
var mb = Regex.Match(styles, clsName + ":before" + cssRegexStr);
if(mb.Success)
{
text = mb.Groups[1].Value + text;
}
var ma = Regex.Match(styles, clsName + ":after" + cssRegexStr);
if (ma.Success)
{
text += ma.Groups[1].Value;
}
return text;
});
mainbody = HtmlEntity.DeEntitize(
mainbody.Replace("<br>", "\r\n").Replace("</br>", "\r\n").Replace("‌", ""));
if (fontName != "")
{
mainbody = decodeCustomFont(mainbody, fontName);
}
chpt.content = " " +
mainbody.Replace("@无限好文,尽在晋江文学城", "").Replace("@无限好文,尽晋江文学城","").Trim() +
chpt.content;
}
chpt.chapterIndex = chapter;
setPrompt("章节"+chapter+"("+chpt.title+")已完成");
chapterDone = chapter;
return chpt;
}
public bool isVipChapter(int chptindx)
{
return vipChapters.IndexOf(chptindx) != -1;
}
Regex legalPath = new Regex(@"[|:?\\/*'<>]|\.+(?:$)");
public void saveChapter(Chapter chpt, bool split)
{
string savepath;
var legalName = legalPath.Replace(this.name, "_", -1);
if (split)
savepath = this.savePath + "\\" + legalName + "-" + chpt.chapterIndex.ToString("D3") + ".txt";
else
savepath = this.savePath + "\\" + legalName + ".txt";
FileStream fs = new FileStream(savepath, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
if(chpt.chapterIndex == 1)
{
sw.WriteLine(this.descriptions + "\r\n");
}
sw.WriteLine(chpt.ToString() + "\r\n");
sw.Close();
fs.Close();
//setPrompt("章节" + chpt.chapterIndex + "(" + chpt.title + ")已写入磁盘");
saveProgress(chpt.chapterIndex);
}
private void saveProgress(int chpt)
{
var legalName = legalPath.Replace(this.name, "_", -1);
string savepath = this.savePath + "\\" + legalName + ".jjget";
FileStream fs = new FileStream(savepath, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write(chpt.ToString());
sw.Close();
fs.Close();
}
private void readProgress()
{
if (this.name == "" || this.name == null) return;
string savepath = this.savePath + "\\" + this.name + ".jjget";
try
{
FileStream fs = new FileStream(savepath, FileMode.Open);
StreamReader sr = new StreamReader(fs);
chapterDone = int.Parse(sr.ReadToEnd());
sr.Close();
fs.Close();
}
catch (Exception)
{
chapterDone = 0;
}
}
public void deleteProgress()
{
FileInfo file = new FileInfo(this.savePath + "\\" + this.name + ".jjget");
file.Delete();
}
public void readSavedUser()
{
try
{
FileStream fs = new FileStream(this.savePath + "\\.jjsave", FileMode.Open);
StreamReader sr = new StreamReader(fs);
cookiestr = sr.ReadLine();
userDetail = sr.ReadLine();
sr.Close();
fs.Close();
hasLogin = true;
}
catch (Exception)
{
}
}
public void writeSavedUser()
{
if (cookiestr == "")
return;
FileStream fs = new FileStream(this.savePath + "\\.jjsave", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(cookiestr);
sw.WriteLine(userDetail);
sw.Close();
fs.Close();
}
public void deleteSavedUser()
{
FileInfo file = new FileInfo(this.savePath + "\\.jjsave");
file.Delete();
cookiestr = "";
userDetail = "";
hasLogin = false;
}
public FontDecoder getFontDecoder()
{
return fontDecoder;
}
public void writeDebugInfo(string path)
{
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
using (var entryStream = archive.CreateEntry("singlePage.html").Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write(lastParsedHTML);
}
using (var entryStream = archive.CreateEntry("config.txt").Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.WriteLine("novelid: " + novelid);
streamWriter.WriteLine("useMobileEdition: " + useMobileEdition);
streamWriter.WriteLine("hasLogin: " + hasLogin);
}
}
using (var fileStream = new FileStream(path, FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
}
}
}