-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
415 lines (345 loc) · 15.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
//
// License: MIT
//
// On Linux (including ARM distributions), you can compile and run this file using either
// Mono or .NET 7/8. On Windows, there is an additional possibility: using the .NET Framework.
// This code has been tested in all these 3 cases. The only dependency is Newtonsoft.Json.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.IO;
namespace HN
{
class Program
{
private const bool _testMode = false;
private const bool _printfOfFileOnCommit = true; // set this to false if running as a Linux or Windows service.
private const int _totalNum = 90;
private const int _observedNum = 30;
private const int _sleepInterval = 60;
private const int _poolHtmlInterval = 60;
private const string _filePath = @"README.md";
private const int _autoArchiveNumOfDays = 7;
private const string _autoArchiveFilenameStart = @"OLD/ARCHIVE-";
private const string _autoArchiveFilenameEnd = @".md";
private const int _autoArchiveInterval = 60 * 60;
private const string _tagStart = "<!-- HN:";
private const string _tagEnd0 = ":start -->";
private const string _tagEnd1 = ":end -->";
private static HttpClient _client = new HttpClient();
private static Dictionary<int, JObject> _storiesCache = new Dictionary<int, JObject>();
private static int[] _prevTopStories = null;
private static Tuple<int, int, int>[] _alreadyAdded = null;
private static bool _commitPending = false;
private static string _poolHtml = null;
private static DateTime _poolHtmlLastTime = DateTime.MinValue;
private static DateTime _autoArchiveLastTime = DateTime.MinValue;
private class HNFatalError : Exception
{
public HNFatalError(string message, Exception inner) : base(message, inner)
{
}
}
public static async Task Main(string[] args)
{
Console.WriteLine("(HNR) Starting...");
while (true)
{
try
{
if (_alreadyAdded == null)
_alreadyAdded = MarkdownGetAll();
var topStories = (await Program.GetTopStories()).Take(_totalNum).ToArray();
var keysToCache = topStories.Take(_observedNum).Where(i => !_storiesCache.ContainsKey(i)).ToArray();
var valuesToCache = await Task.WhenAll(keysToCache.Select(i => GetStory(i)));
foreach (var t in keysToCache.Zip(valuesToCache, (first, second) => new Tuple<int, JObject>(first, second)))
_storiesCache.Add(t.Item1, t.Item2);
if (_prevTopStories != null)
{
var missing = _prevTopStories.Take(_observedNum).Where(i => !topStories.Contains(i)).Select(i => _storiesCache[i]).ToArray();
foreach (var story in missing)
await Add(story);
}
var toDeleteIds = _alreadyAdded.Where(t => topStories.Contains(t.Item1)).Select(t => t.Item1).ToArray();
foreach (var storyId in toDeleteIds)
MarkdownDelete(storyId);
if (AutoArchive())
{
_alreadyAdded = MarkdownGetAll();
_commitPending = true;
}
if (_commitPending && !_testMode)
{
if (_printfOfFileOnCommit)
Console.WriteLine(File.ReadAllText(_filePath));
Func<string, bool> git =
(string a) =>
{
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = a;
process.Start();
process.WaitForExit();
return process.ExitCode == 0;
};
bool res0 = git("add .");
bool res1 = git("commit -a -m \"Update " + DateTime.UtcNow.ToString() + "\"");
bool res2 = git("push");
if (res0 && res1 && res2)
_commitPending = false;
}
_prevTopStories = topStories;
}
catch (Exception e)
{
if (e is HNFatalError)
{
Console.WriteLine(e.ToString());
return;
}
else
{
_prevTopStories = null;
_alreadyAdded = null;
_storiesCache.Clear();
Console.Write("§");
}
}
Thread.Sleep(_sleepInterval * 1000);
}
}
private static async Task<T> GetJson<T>(string url)
{
var response = await _client.GetAsync(new Uri(url));
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
private static async Task<int[]> GetTopStories()
{
var jsonData = await GetJson<JArray>("https://hacker-news.firebaseio.com/v0/topstories.json");
return jsonData.Take(_totalNum).Select(v => v.ToObject<int>()).ToArray();
}
private static async Task<JObject> GetStory(int id)
{
return await GetJson<JObject>("https://hacker-news.firebaseio.com/v0/item/" + id.ToString() + ".json");
}
private static T CannotFail<T>(Func<T> f)
{
try
{
return f();
}
catch (Exception e)
{
if (e is HNFatalError)
throw e;
else
throw new HNFatalError(e.Message, e);
}
}
private static Tuple<int, int, int>[] MarkdownGetAll()
{
return CannotFail(() =>
{
string file = File.ReadAllText(_filePath);
var ret = new List<Tuple<int, int, int>>();
int start = 0;
while (true)
{
int pos = file.IndexOf(_tagStart, start);
if (pos < 0)
break;
int textStart = pos;
start = pos + _tagStart.Length;
pos = file.IndexOf(_tagEnd0, start);
if (pos < 0)
throw new Exception("MarkdownGetAll: _tagEnd0 not found.");
int storyId = int.Parse(file.Substring(start, pos - start));
string storyEndTag = _tagStart + storyId.ToString() + _tagEnd1;
pos = file.IndexOf(storyEndTag, start);
if (pos < 0)
throw new Exception("MarkdownGetAll: storyEndTag not found.");
int textEnd = pos + storyEndTag.Length;
ret.Add(new Tuple<int, int, int>(storyId, textStart, textEnd));
start = textEnd;
}
if (ret.Select(t => t.Item1).Distinct().Count() != ret.Count())
throw new Exception("MarkdownGetAll: return value contains duplicates.");
return ret.ToArray();
});
}
private static void MarkdownAdd(int id, string text)
{
text =
_tagStart + id.ToString() + _tagEnd0 +
Environment.NewLine + "* " + text +
_tagStart + id.ToString() + _tagEnd1;
CannotFail(() =>
{
string file = File.ReadAllText(_filePath).TrimEnd(' ', '\r', '\n');
string today = "**" + DateTime.UtcNow.ToLongDateString() + "**";
if (file.IndexOf(today) < 0)
text = Environment.NewLine + "#### " + today + Environment.NewLine + text;
File.WriteAllText(_filePath, file + text);
return true;
});
_alreadyAdded = MarkdownGetAll();
_commitPending = true;
}
private static void MarkdownDelete(int id)
{
CannotFail(() =>
{
string file = File.ReadAllText(_filePath);
var toDelete = _alreadyAdded.Where(t => t.Item1 == id).ToArray();
if (toDelete.Length != 1)
throw new Exception("MarkdownDelete: id not found.");
file = file.Substring(0, toDelete[0].Item2) + file.Substring(toDelete[0].Item3);
File.WriteAllText(_filePath, file);
return true;
});
_alreadyAdded = MarkdownGetAll();
_commitPending = true;
}
private static async Task<bool> IsStoryInPoolNoExcept(int id)
{
bool ret = false;
try
{
if (_poolHtml == null || DateTime.UtcNow - _poolHtmlLastTime > new TimeSpan(0, 0, _poolHtmlInterval))
{
try
{
var response = await _client.GetAsync(new Uri("https://news.ycombinator.com/pool"));
_poolHtml = await response.Content.ReadAsStringAsync();
_poolHtmlLastTime = DateTime.UtcNow;
}
catch
{
}
}
ret =
_poolHtml.IndexOf(id.ToString() + "\"") >= 0 ||
_poolHtml.IndexOf(id.ToString() + "'") >= 0;
}
catch
{
}
return ret;
}
private static async Task Add(JObject story)
{
if (story["type"].ToObject<string>() == "story")
{
int id = story["id"].ToObject<int>();
if (_alreadyAdded.Where(t => t.Item1 == id).Count() == 0 && !await IsStoryInPoolNoExcept(id))
{
string title = story["title"].ToObject<string>();
string url = story["url"].ToObject<string>();
int pos = _prevTopStories.TakeWhile(i => i != id).Count() + 1;
string moreData = "";
try
{
var updated = await GetStory(id);
int comments = updated["descendants"].ToObject<int>();
int points = updated["score"].ToObject<int>();
moreData = points.ToString() + " points " + comments.ToString() + " comments ";
}
catch
{
}
Func<string, string> sanitize =
(string s) =>
{
return string.Concat(s.ToCharArray().Select(
c =>
{
string t = c.ToString();
if (t == "<")
t = "<";
else if (t == ">")
t = ">";
return t;
}).ToArray());
};
MarkdownAdd(id,
"[" + id.ToString() + "](https://news.social-protocols.org/stats?id=" + id.ToString() + ") #" + pos.ToString() + " " +
moreData + "-> [" + sanitize(title) + "](" + sanitize(url) + ")");
}
}
}
private static bool AutoArchive()
{
return CannotFail(() =>
{
if (DateTime.UtcNow - _autoArchiveLastTime > new TimeSpan(0, 0, _autoArchiveInterval))
_autoArchiveLastTime = DateTime.UtcNow;
else
return false;
string file = File.ReadAllText(_filePath);
DateTime pos = DateTime.UtcNow - new TimeSpan(_autoArchiveNumOfDays, 0, 0, 0, 0, 0);
DateTime first = DateTime.MinValue;
DateTime last = pos;
int notFoundNum = 0;
const int notFoundMax = 30;
while (true)
{
if (file.IndexOf("#### " + "**" + pos.ToLongDateString() + "**") < 0)
{
if (++notFoundNum > notFoundMax)
break;
}
else
{
notFoundNum = 0;
first = pos;
}
pos -= new TimeSpan(1, 0, 0, 0, 0, 0);
}
if (first == DateTime.MinValue)
return false;
bool fileModified = false;
pos = first;
while (true)
{
int textBegin = file.IndexOf("#### " + "**" + pos.ToLongDateString() + "**");
int textEnd = -1;
if (textBegin >= 0)
{
DateTime pos2 = pos;
while (true)
{
pos2 += new TimeSpan(1, 0, 0, 0, 0, 0);
if (pos2 > DateTime.UtcNow)
break;
textEnd = file.IndexOf("#### " + "**" + pos2.ToLongDateString() + "**");
if (textEnd >= 0)
break;
}
}
if (textBegin >= 0 && textEnd >= 0 && textEnd > textBegin)
{
string portion = file.Substring(textBegin, textEnd - textBegin);
file = file.Substring(0, textBegin) + file.Substring(textEnd);
fileModified = true;
File.AppendAllText(
_autoArchiveFilenameStart + pos.Year.ToString() + "-" + pos.Month.ToString("D2") + _autoArchiveFilenameEnd,
portion.Replace("https://news.social-protocols.org/stats?id=", "https://news.ycombinator.com/item?id="));
}
pos += new TimeSpan(1, 0, 0, 0, 0, 0);
if (pos > last)
break;
}
if (fileModified)
File.WriteAllText(_filePath, file);
return fileModified;
});
}
}
}