-
Notifications
You must be signed in to change notification settings - Fork 84
/
MainForm.cs
380 lines (347 loc) · 12.9 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace ExportBlog
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
textBox2.ReadOnly = true;
tbUser.Select();
foreach (Source src in Enum.GetValues(typeof(Source)))
{
comboBox1.Items.Add(App.GetDescription(src));
}
comboBox1.SelectedIndex = (int)Source.csdn - 1;
label9.Text = string.Empty;
}
private void StartExport()
{
var put = GetInput();
if (!put.Status) return;
Source src = Source.csdn;
switch (put.Type)
{
case Type.Blog:
src = (Source)(comboBox1.SelectedIndex + 1);
break;
case Type.Url:
src = GetSourceByDomain();
break;
case Type.Column:
src = Source.csdn;
break;
}
string user = put.Text;
if (put.Type == Type.Url) user = src.ToString();
string title = App.GetDescription(src);
FeedService fs = new FeedService(src, user);
if (put.Type == Type.Blog)
{
ArticleListForm frm = new ArticleListForm(fs);
if (frm.ShowDialog() != DialogResult.OK)
{
return;
}
}
button1.Enabled = false;
Thread thd = new Thread(() =>
{
string[] urls = null;
if (put.Type == Type.Url)
{
urls = put.Text.Replace("\r", "").Split(new char[] { '\n', ',', ',', ';', ';' }, StringSplitOptions.RemoveEmptyEntries);
}
else if (put.Type == Type.Column)
{//抓取URL
urls = GetColumnUrls(put.Text);
}
if ((put.Format & Format.CHM) != 0)
{
SetLbText("开始生成CHM文档");
ChmPackage chm = new ChmPackage(user, title, fs, urls, SetLbText);
if (chm.Build())
SetLbText("CHM文档生成成功!");
}
if ((put.Format & Format.PDF) != 0)
{
SetLbText("开始生成PDF文档");
PdfPackage pdf = new PdfPackage(user, title, fs, urls, SetLbText);
pdf.Build();
SetLbText("PDF文档生成成功!");
}
if ((put.Format & Format.HTML) != 0)
{
SetLbText("开始生成HTML文档");
HtmlPackage htm = new HtmlPackage(user, title, fs, urls, SetLbText);
htm.Build();
SetLbText("HTML文档生成成功!");
}
if ((put.Format & Format.TXT) != 0)
{
SetLbText("开始生成TXT文档");
TxtPackage txt = new TxtPackage(user, title, fs, urls, SetLbText);
txt.Build();
SetLbText("TXT文档生成成功!");
}
if ((put.Format & Format.EPUB) != 0)
{
SetLbText("开始生成EPUB文档");
EpubPackage epub = new EpubPackage(user, title, fs, urls, SetLbText);
epub.Build();
SetLbText("EPUB文档生成成功!");
}
MessageBox.Show("全部导出完成!");
button1.Invoke(new SetText(delegate(string s)
{
button1.Enabled = true;
}), string.Empty);
});
thd.Start();
}
#region helper
private Input GetInput()
{
var put = new Input();
switch (tabControl1.SelectedIndex)
{
case 0:
put.Type = Type.Blog;
put.Text = tbUser.Text.Trim();
break;
case 1:
put.Type = Type.Url;
put.Text = tbUrl.Text.Trim();
break;
case 2:
put.Type = Type.Column;
put.Text = tbColumn.Text.Trim();
break;
}
if (put.Text == string.Empty)
{
string msg = string.Empty;
if (put.Type == Type.Blog) msg = "请输入博客用户名";
else if (put.Type == Type.Url) msg = "请输入要导出的博客URL";
else if (put.Type == Type.Column) msg = "请输入专栏别名";
MessageBox.Show(msg);
return put;
}
if (chk1_chm.Checked) put.Format |= Format.CHM;
if (chk1_pdf.Checked) put.Format |= Format.PDF;
if (chk1_htm.Checked) put.Format |= Format.HTML;
if (chk1_txt.Checked) put.Format |= Format.TXT;
if (chk1_epub.Checked) put.Format |= Format.EPUB;
if ((int)put.Format == 0)
{
MessageBox.Show("请选择要导出的格式");
return put;
}
FolderDialog fDialog = new FolderDialog();
var re = fDialog.DisplayDialog();
if (re != DialogResult.OK)
{
return put;
}
App.BaseDirectory = fDialog.Path + "\\";
put.Status = true;
return put;
}
private delegate void SetText(string text);
private void SetLbText(string str)
{
str = "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + str + "\n";
if (textBox2.InvokeRequired)
{
SetText st = new SetText(delegate(string text)
{
textBox2.AppendText(text);
});
textBox2.Invoke(st, str);
}
else
{
textBox2.AppendText(str);
}
}
Regex reg_title = new Regex(@"<a name=""\d+"" href=""(.+?)"" target=""_blank"">.+?</a>", RegexOptions.Compiled);
private string[] GetColumnUrls(string alias)
{
IList<string> urls = new List<string>();
string url = "http://blog.csdn.net/column/details/" + alias + ".html?page={0}";
int p = 0;
WebUtility web = new WebUtility();
web.Encode = Encoding.UTF8;
for (int i = 1; i < 100; i++)
{
if (p > 0 && i > p) break;
web.URL = string.Format(url, i);
string html = web.Get();
if (p == 0)
{
var mp = Regex.Match(html, @"共(\d+)页");
if (mp.Success) p = App.ToInt(mp.Groups[1].Value);
else p = 1;
}
var mats = reg_title.Matches(html);
if (mats.Count == 0) break;
foreach (Match mat in mats)
{
urls.Add(mat.Groups[1].Value);
}
}
string[] ss = new string[urls.Count];
for (int i = 0; i < urls.Count; i++)
{
ss[i] = urls[i];
}
return ss;
}
private Source GetSourceByDomain()
{
Source src = Source.csdn;
string url = tbUrl.Text.Split('\n')[0];
if (url.Contains("163.com")) src = Source._163;
else if (url.Contains("51cto.com")) src = Source._51cto;
else if (url.Contains("chinaunix.net")) src = Source.chinaunix;
else if (url.Contains("cnblogs.com")) src = Source.cnblogs;
else if (url.Contains("csdn.net")) src = Source.csdn;
else if (url.Contains("hexun.com")) src = Source.hexun;
else if (url.Contains("iteye.com")) src = Source.iteye;
else if (url.Contains("oschina.net")) src = Source.oschina;
else if (url.Contains("sina.com.cn")) src = Source.sina;
else if (url.Contains("sohu.com")) src = Source.sohu;
return src;
}
#endregion
#region close button
private void linkLabel1_MouseHover(object sender, EventArgs e)
{
linkLabel1.ImageIndex = 3;
}
private void linkLabel1_MouseLeave(object sender, EventArgs e)
{
linkLabel1.ImageIndex = 2;
}
private void linkLabel1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void linkLabel2_MouseHover(object sender, EventArgs e)
{
linkLabel2.ImageIndex = 1;
}
private void linkLabel2_MouseLeave(object sender, EventArgs e)
{
linkLabel2.ImageIndex = 0;
}
private void linkLabel2_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
#endregion
#region move
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
#endregion
#region events
private void button1_Click(object sender, EventArgs e)
{
StartExport();
}
private void button2_Click(object sender, EventArgs e)
{
StartExport();
}
private void button3_Click(object sender, EventArgs e)
{
StartExport();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = null;
Source src = (Source)(comboBox1.SelectedIndex + 1);
switch (src)
{
case Source._163:
s = "http://blog.163.com/,";
break;
case Source._51cto:
s = "http://,.blog.51cto.com/";
break;
case Source.chinaunix:
s = "http://blog.chinaunix.net/uid/,.html";
break;
case Source.cnblogs:
s = "http://www.cnblogs.com/,";
break;
case Source.csdn:
s = "http://blog.csdn.net/,";
break;
case Source.hexun:
s = "http://,.blog.hexun.com/";
break;
case Source.iteye:
s = "http://,.iteye.com/";
break;
case Source.oschina:
s = "http://my.oschina.net/,/blog";
break;
case Source.sina:
s = "http://blog.sina.com.cn/,";
break;
case Source.sohu:
s = "http://,.blog.sohu.com/";
break;
}
label1.Text = s.Split(',')[0];
label9.Text = s.Split(',')[1];
tbUser.Location = new Point(label1.Location.X + label1.Width, tbUser.Location.Y);
label9.Location = new Point(tbUser.Location.X + tbUser.Width, label9.Location.Y);
}
private void tbUrl_Click(object sender, EventArgs e)
{
if (tbUrl.Text == "回车分割多个URL")
{
tbUrl.Text = string.Empty;
}
}
#endregion
}
public class Input
{
public Input()
{
Status = false;
}
public Type Type { get; set; }
public string Text { get; set; }
public Format Format { get; set; }
public bool Status { get; set; }
}
}