-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMY.cs
286 lines (266 loc) · 8.01 KB
/
BMY.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
namespace BMY
{
public class Article
{
public string artToken{ get; private set; }
public string artTitle{ get; private set; }
public string artText{ get; private set; }
public string artRef { get; private set; }
private Topic artTopic{ get; set; }
public Article (Topic top, string tok, string t)
{
artTopic = top;
artTitle = t;
artToken = tok;
var m = Regex.Match (artToken, @"&F=(.+)$");
artRef = m.Groups [1].Value;
}
private void fetchText (BMYClient client)
{
if (artText != null) {
return;
}
var url = string.Format ("{0}{1}{2}", BMYClient.firstURL, client.bbsToken, artToken);
var page = client.Client.GetSrc (url, "GBK");
var match = Regex.Match (page, @"(发信人:.+)本文链接", RegexOptions.Multiline);
if (match.Success) {
var text = match.Groups [1].Value;
text = text.Replace ("<br>", "\n");
text = Regex.Replace (text, @"<[^>]+>", "");
artText = text;
}
}
public bool reply (BMYClient client, string text, string title = null, string qmd = "---\nFrom Mono C# Client")
{
if (title == null) {
if (artTitle.Substring (0, 3) != "Re:") {
title = "Re: " + artTitle;
} else {
title = artTitle;
}
}
var url = string.Format ("{0}{1}bbssnd?board={2}&th={3}&ref={4}",
BMYClient.firstURL,
client.bbsToken,
artTopic.boardID,
artTopic.topicID,
artRef);
Console.WriteLine (url);
var data = string.Format ("title={0}&text={1}", title, text + "\n" + qmd);
var rsp = client.Client.PostData (url, data, "GBK", "GBK");
Console.WriteLine ("Response:");
Console.WriteLine (rsp);
return true;
}
}
public class Topic
{
public string topicID{ get; private set; }
public string topicTitle{ get; private set; }
public string boardID { get; private set; }
public List<Article> articleList{ get; private set; }
public BBSBoard boardHandle { get; private set; }
public Topic (BBSBoard bn, string id, string title)
{
boardHandle = bn;
boardID = boardHandle.boardID;
topicTitle = title;
topicID = id;
articleList = new List<Article> ();
}
public void Refresh (BMYClient client)
{
if (articleList.Count != 0) {
articleList.Clear ();
}
fetchArticles (client);
}
private void fetchArticles (BMYClient client)
{
var url = string.Format ("{0}{1}tfind?B={2}&th={3}", BMYClient.firstURL, client.bbsToken, boardID, topicID);
var page = client.Client.GetSrc (url);
Console.WriteLine (page);
var matchs = Regex.Matches (page, @"<a href=(con\?B=[^>]+)>([^<]+)</a>", RegexOptions.Multiline);
foreach (Match m in matchs) {
articleList.Add (new Article (this, m.Groups [1].Value, m.Groups [2].Value));
}
}
}
public class Topics
{
public BBSBoard boardHandle{ get; private set; }
public List<Topic> topicList {
get;
private set;
}
private string nextURL{ get; set; }
private string preURL{ get; set; }
public Topics (BBSBoard b)
{
boardHandle = b;
topicList = new List<Topic> ();
}
private string getBoardPage (BMYClient client)
{
var url = string.Format ("{0}{1}tdoc?B={2}", BMYClient.firstURL, client.bbsToken, boardHandle.boardID);
var page = client.Client.GetSrc (url);
return page;
}
private void fetchList (BMYClient client)
{
var page = getBoardPage (client);
Console.WriteLine (page);
var matchs = Regex.Matches (page, "<a href=[^>]*&th=([^>\"]+)>([^<]+)</a>", RegexOptions.Multiline);
foreach (Match m in matchs) {
topicList.Add (new Topic (boardHandle, m.Groups [1].Value, m.Groups [2].Value));
}
}
public void Refresh (BMYClient client)
{
if (topicList.Count != 0) {
topicList.Clear ();
}
fetchList (client);
}
public override string ToString ()
{
var titles = from Topic t in this.topicList
select t.topicTitle;
var res = string.Empty;
foreach (var i in titles) {
res += (i + "\n");
}
return res;
}
}
public class BBSBoard
{
public string boardID { get; private set; }
public string boardName { get; private set; }
public Topics topics { get; private set; }
public BBSBoard (string id, string name)
{
boardID = id;
boardName = name;
topics = new Topics (this);
}
public bool post (BMYClient client, string title, string text, string qmd = "---\nFrom Mono C#\n")
{
var url = string.Format ("{0}{1}bbssnd?board={2}&th=-1", BMYClient.firstURL, client.bbsToken, boardID);
var data = string.Format ("title={0}&text=\n{1}", title, text + "\n" + qmd);
var rsp = client.Client.PostData (url, data, "GBK", "GBK");
Console.WriteLine (rsp);
return true;
}
}
public class BMYClient
{
public static string firstURL = "http://bbs.xjtu.edu.cn/";
static string sectionURL = "boa?secstr={0}";
static Dictionary<string, string> secList = new Dictionary<string, string>
{
{"交通大学","1"},
{"开发技术","2"},
{"电脑应用","3"},
{"学术科学","4"},
{"社会科学","5"},
{"文学艺术","6"},
{"知性感性","7"},
{"体育运动","8"},
{"休闲音乐","9"},
{"游戏天地","G"},
{"新闻信息","N"},
{"乡音乡情", "H"},
{"校务信息", "A"},
{"俱乐部区", "C"},
};
public string passWord { get; set; }
public string userName { get; set; }
public bool isLogin { get; private set; }
public string bbsToken { get; private set; }
public HttpClient Client { get; private set; }
public BMYClient (string user, string pass)
{
userName = user;
passWord = pass;
Client = new HttpClient ();
}
public string login ()
{
Client.Headers ["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
"(compatible; MSIE 6.0; Windows NT 5.1; " +
".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
Console.WriteLine (firstURL);
string loginToken = string.Format ("{0}={1}&{2}={3}", "id", userName, "pw", passWord);
Console.WriteLine (loginToken);
string result = Client.PostData (firstURL + "BMY/bbslogin", loginToken, "ASCII", "GBK");
Console.WriteLine ("Login:");
Console.WriteLine (result);
Match match = Regex.Match (result, @" url=(.+/)");
if (match.Success) {
var uri = match.Groups [1].Value;
Console.WriteLine (uri);
bbsToken = uri;
isLogin = true;
return uri;
} else {
throw new Exception ("Login Failed");
}
}
public string getMain (string token)
{
var uri = firstURL + token;
return Client.GetSrc (uri, "GBK");
}
public string getNav (string page)
{
Match match = Regex.Match (page, @"src=(bbsleft.+) ");
var navToken = match.Groups [1].Value;
var navURL = firstURL + bbsToken + navToken;
return Client.GetSrc (navURL, "GBK");
}
private string getSecURL (string token)
{
var url = string.Format ("{0}{1}{2}", firstURL, bbsToken, string.Format (sectionURL, token));
Console.WriteLine (url);
return url;
}
private string getSecPage (string token)
{
var url = getSecURL (token);
return Client.GetSrc (url);
}
private string getBoardURL (string token)
{
var url = string.Format ("{0}{1}{2}", firstURL, bbsToken, token);
return url;
}
private List<BBSBoard> parserBoardList (string page)
{
var dict = new List<BBSBoard> ();
//var pattern = new Regex ("<a href=(.*)>(.*)</a>");
var matchs = Regex.Matches (page, @"<a href=home\?B=([^>]*)>([^<]+)</a>", RegexOptions.Multiline);
foreach (Match m in matchs) {
//Console.WriteLine (m);
if (!Regex.IsMatch (m.Groups [2].Value, @"[a-zA-Z0-9 ]+")) {
dict.Add (new BBSBoard (m.Groups [1].Value, m.Groups [2].Value));
}
}
return dict;
}
public List<BBSBoard> getBoardList (string secName)
{
var secNo = secList [secName];
var page = getSecPage (secNo);
Console.WriteLine ("Sec:\n {0}{1}\n{2}", secName, secNo, page);
return parserBoardList (page);
}
}
}