-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
479 lines (410 loc) · 16.6 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Fetching...");
Console.WriteLine("Type h for help");
XDocument rss = XDocument.Load(@"http://www.reddit.com/.rss");
List<XElement> stories = rss.Descendants("item").ToList<XElement>();
int oldindex = 1;
int hackindex = 1;
int index = 1;
bool viewingComments = false;
List<XElement> comments = new List<XElement>();
int index_c = 1;
string[] input = new string[] {"n"};
while (input[0] != "q" && input[0] != "exit")
{
if (input[0] == "")
{
input[0] = "n";
}
if (input[0] == "s")
{
viewingComments = false;
index = hackindex;
input[0] = "n";
}
if (input[0] == "n")
{
if (!viewingComments)
{
oldindex = hackindex;
hackindex = index; // lol @ hack
index = displayHeadlines(index, stories, rss.Root.Element("channel").Element("title").Value);
}
else
{
index_c = displayComments(index_c, comments);
}
}
if (input[0] == "p")
{
index = displayHeadlines(oldindex, stories, rss.Root.Element("channel").Element("title").Value);
}
if (input[0] == "r")
{
if (input.Length == 2)
{
Console.WriteLine("Fetching " + input[1] + "...");
try
{
rss = XDocument.Load(@"http://www.reddit.com/r/" + input[1] + "/.rss");
stories = rss.Descendants("item").ToList<XElement>();
viewingComments = false;
oldindex = 1;
hackindex = 1;
index = displayHeadlines(1, stories, rss.Root.Element("channel").Element("title").Value);
}
catch (Exception ex)
{
Console.WriteLine("Failed! " + ex.Message);
}
}
}
if (input[0] == "o")
{
int num;
if (int.TryParse(input[1], out num))
{
if (num > 0 && num < 26)
{
try
{
Match link = Regex.Match(stories[--num].Element("description").Value, "(?<=<a href=\")[^\"]*(?=\">\\[link])");
if (link.Captures.Count > 0)
System.Diagnostics.Process.Start(link.Captures[0].Value);
}
catch (Exception)
{
}
}
}
}
if (input[0] == "h")
{
Console.Clear();
index = hackindex;
Console.WriteLine("rd Help");
Console.WriteLine("\nGeneral commands:\n");
Console.WriteLine("\tr subreddit\tLoads the provided subreddit");
Console.WriteLine("\tb\t\tBoss mode (Shows contents of C:\\Windows)");
Console.WriteLine("\tq or exit\tExits rd");
Console.WriteLine("\nWhile viewing stories:\n");
Console.WriteLine("\tn or <Enter>\tSee the next page of stories");
Console.WriteLine("\tp\t\tSee the preview page of stories");
Console.WriteLine("\t<number>\tShows the URL (or description for self.reddit)");
Console.WriteLine("\to <number>\tOpens the story in your default browser");
Console.WriteLine("\tc <number>\tView the comments");
Console.WriteLine("\nWhile viewing comments:\n");
Console.WriteLine("\tn or <Enter>\tSee the next page of comments");
Console.WriteLine("\ts\t\tSwitch back to the stories");
}
if (input[0] == "c")
{
int num;
if (int.TryParse(input[1], out num))
{
if (num > 0 && num < 26)
{
Console.Write("Fetching comments... ");
comments = loadComments(stories[--num].Element("link").Value);
if (comments.Count > 1)
{
index_c = 1;
index_c = displayComments(index_c, comments);
viewingComments = true;
}
else
{
Console.WriteLine("failed. No comments found.");
}
}
}
}
if (input[0] == "b")
{
Console.Title = @"C:\WINDOWS\System32\cmd.exe";
System.Diagnostics.Process boss = new System.Diagnostics.Process();
boss.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
boss.StartInfo.CreateNoWindow = true;
boss.StartInfo.RedirectStandardOutput = true;
boss.StartInfo.RedirectStandardInput = true;
boss.StartInfo.UseShellExecute = false;
boss.Start();
System.IO.StreamReader reader = new System.IO.StreamReader(boss.StandardOutput.BaseStream);
System.IO.StreamWriter writer = new System.IO.StreamWriter(boss.StandardInput.BaseStream);
writer.WriteLine("C:");
writer.Flush();
writer.WriteLine("cd \\Windows");
writer.Flush();
writer.WriteLine("dir C:\\Windows");
writer.Close();
writer.Dispose();
Console.Write(reader.ReadToEnd());
reader.Close();
reader.Dispose();
}
else
{
int num;
if (int.TryParse(input[0], out num))
{
if (num > 0 && num < 26)
{
index = hackindex;
displayDescription(--num, stories);
}
}
}
if (input[0] != "b")
Console.Write("\n> ");
input = Console.ReadLine().Split(' ');
}
}
static int displayHeadlines(int index, List<XElement> stories, string title)
{
if (index > 25)
return 1;
int rows = 0;
int i = index;
string[] story;
// Clear the window
Console.Clear();
// Print the title
Console.Title = title;
Console.WriteLine("Now viewing: " + title + "\n");
rows++; rows++;
while (i <= stories.Count)
{
story = printHeadline(i, stories[i - 1]);
if (rows + story.Length < Console.WindowHeight - 2)
{
i++;
foreach (string line in story)
{
Console.WriteLine(line);
rows++;
}
}
else
{
break;
}
}
return i;
}
static int displayComments(int index, List<XElement> comments)
{
if (index >= comments.Count)
return 1;
int i = index;
int rows = 0;
string[] comment;
Console.Clear();
//Print the headline
if (comments[0].Element("title").Value.Length > Console.WindowWidth - 17)
Console.WriteLine("Comments on: " + comments[0].Element("title").Value.Substring(0,Console.WindowWidth - 17) + "...");
else
Console.WriteLine("Comments on: " + comments[0].Element("title").Value);
rows++;
while (i < comments.Count)
{
comment = printComment(i, comments[i]);
if (rows + comment.Length < Console.WindowHeight - 2)
{
i++;
foreach (string line in comment)
{
Console.WriteLine(line);
rows++;
}
}
else
{
break;
}
}
return i;
}
static void displayDescription(int num, List<XElement> stories)
{
string description = stories[num].Element("description").Value;
if (description.Contains("<!-- SC_OFF -->"))
{
string title = html(stories[num].Element("title").Value);
string[] splittitle = splitByLength(title, Console.WindowWidth - 1);
Console.Clear();
foreach (string line in splittitle)
Console.WriteLine(line);
Console.WriteLine("");
description = description.Substring(description.IndexOf("<!-- SC_OFF -->") + 15);
description = description.Substring(0, description.IndexOf("<!-- SC_ON -->"));
description = html(description);
string[] split = splitByLength(description, Console.WindowWidth - 1);
foreach (string line in split)
Console.WriteLine(line);
Console.WriteLine("\tType \"c " + (num + 1).ToString() + "\" to view the comments.");
}
else
{
Match link = Regex.Match(description, "(?<=<a href=\")[^\"]*(?=\">\\[link])");
if (link.Captures.Count > 0)
Console.WriteLine("Links to " + link.Captures[0].Value);
Console.WriteLine("Type \"o " + (num + 1).ToString() +
"\" to open this link in a browser, or \"c " + (num + 1).ToString() + "\" to view the comments.");
}
}
static string[] splitByLength(string input, int length)
{
int k = 0;
string[] words = input.Split(' ');
List<string> output = new List<string>();
string temp = "";
while (k < words.Length)
{
if (temp.Length + words[k].Length + 1 > length)
{
if (temp == "")
{
temp = words[k].Substring(0, length - 2) + "-";
words[k] = words[k].Substring(length - 2);
}
output.Add(temp);
temp = "";
}
else
{
if (words[k] == "\n")
{
output.Add(temp);
temp = "";
k++;
}
else
{
if (words[k] != "")
temp += " " + words[k++];
else
k++;
}
}
}
if (temp != "")
output.Add(temp);
return output.ToArray();
}
static void printInfo(string htmlin)
{
Regex submitter = new Regex("(?<=\"http://www.reddit.com/user/)[^\"]*(?=\")");
Regex comments = new Regex(@"(?<=\[)\d{1,5}(?= comments])");
if (submitter.IsMatch(htmlin))
{
Console.Write("\tSubmitted by " + submitter.Match(htmlin).Captures[0].Value);
}
if (comments.IsMatch(htmlin))
{
Console.Write("\t\t" + comments.Match(htmlin).Captures[0].Value + " comment(s)");
}
Console.Write("\n\n");
}
static string[] printHeadline(int index, XElement story)
{
List<string> output = new List<string>();
string temp;
// Write the index
temp = index.ToString() + ".";
// Print the story's title
string[] split = splitByLength(html(story.Element("title").Value), Console.WindowWidth - 10);
foreach (string line in split)
{
temp += ("\t" + line);
output.Add(temp);
temp = "";
}
// Print the submitter
Match submitter = Regex.Match(story.Element("description").Value, "(?<=\"http://www.reddit.com/user/)[^\"]*(?=\")");
if (submitter.Captures.Count > 0)
{
temp = new string(' ', 16);
temp += "Submitted by " + submitter.Captures[0].Value;
}
// Print the number of comments
Match comments = Regex.Match(story.Element("description").Value, @"(?<=\[)\d{1,5}(?= comments?])");
if (comments.Captures.Count > 0)
{
if (temp.Length < 48)
temp += new string(' ', 48 - temp.Length);
else
temp += " ";
temp += comments.Captures[0].Value + " comment";
if (int.Parse(comments.Captures[0].Value) != 1)
temp += "s";
}
output.Add(temp);
output.Add("");
return output.ToArray();
}
static string[] printComment(int index, XElement comment)
{
List<string> output = new List<string>();
string temp = "";
Match username = Regex.Match(comment.Element("title").Value, "[^ ]*(?= )");
if (username.Captures.Count > 0)
{
temp += username + " says:";
output.Add(temp);
temp = "";
}
string[] split = splitByLength(html(comment.Element("description").Value), Console.WindowWidth - 3);
foreach (string line in split)
{
temp += (" " + line);
output.Add(temp);
temp = "";
}
return output.ToArray();
}
static List<XElement> loadComments(string link)
{
string url = link + ".rss";
List<XElement> ret = new List<XElement>();
try
{
XDocument comments = XDocument.Load(url);
ret = comments.Descendants("item").ToList<XElement>();
}
catch (Exception)
{
}
return ret;
}
private static string html(string input)
{
string output = input;
output = output.Replace("<div class=\"md\">", "");
output = output.Replace("</div>", "");
output = output.Replace(""", "\"");
output = output.Replace("<", "<");
output = output.Replace(">", ">");
output = output.Replace("&", "&");
output = output.Replace("<p>", "");
output = output.Replace("</p>", " \n \n ");
output = output.Replace("<blockquote>", "");
output = output.Replace("</blockquote>", "");
output = output.Replace("<strong>", "[");
output = output.Replace("</strong>", "]");
output = output.Replace("<a href=\"", "(").Replace("\" >", ")");
output = output.Replace("</a>", "");
return output;
}
}
}