forked from morcefaster/corpthingdosomethingpls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
146 lines (131 loc) · 6.19 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Threading;
using Newtonsoft.Json.Linq;
namespace CorpthingPls
{
class Program
{
private static string nightcorpHtmlNew { get; set; }
private static string nightcorpHtml { get; set; }
private static string commentsJson { get; set; }
private static string postsJson { get; set; }
private static int oldcomments = 0;
private static int oldposts = 0;
private static int newcomments = 0;
private static int newposts = 0;
private const string nightcorpUrl = "https://nightcorp.net/";
private const string commentsUrl = "https://www.reddit.com/user/corpthing/comments.json";
private const string postsUrl = "https://www.reddit.com/user/corpthing/submitted.json";
private const string logFile = "log.txt";
static void Main(string[] args)
{
nightcorpHtml = GetHtml(nightcorpUrl, null);
commentsJson = GetHtml(commentsUrl, null);
postsJson = GetHtml(postsUrl, null);
Console.WriteLine(nightcorpHtml);
Console.WriteLine(commentsJson);
Console.WriteLine(postsJson);
var json = JObject.Parse(commentsJson);
oldcomments = GetChildrenCount(json);
json = JObject.Parse(postsJson);
oldposts = GetChildrenCount(json);
Console.WriteLine("============================================");
Console.WriteLine("Oh I sure hope CorpThing does something now");
Console.WriteLine("=============================================");
while (true)
{
Thread.Sleep(15000);
Console.WriteLine("[{0}] Requesting... ({1} comments {2} posts) ", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), oldcomments, oldposts);
nightcorpHtmlNew = GetHtml(nightcorpUrl, nightcorpHtml);
if (!nightcorpHtmlNew.Equals(nightcorpHtml))
{
Ding();
Log("[{0}] Nightcorp site changed\nNew html:\n{1}\n", DateTime.UtcNow.ToString("YYYY-MM-dd hh:mm:ss"), nightcorpHtmlNew);
Console.WriteLine("==========================");
Console.WriteLine("=====Nightcorp site change=====");
Console.WriteLine("==========================");
Console.WriteLine(nightcorpHtmlNew);
Console.WriteLine("==========================");
Console.WriteLine("=====Nightcorp site change=====");
Console.WriteLine("==========================");
nightcorpHtml = nightcorpHtmlNew;
}
commentsJson = GetHtml(commentsUrl, null);
if (!string.IsNullOrEmpty(commentsJson))
{
json = JObject.Parse(commentsJson);
newcomments = ((JArray)(json["data"]["children"])).Count;
if (newcomments != oldcomments)
{
Ding();
Log("[{0}] Comments count changed\nNew json:\n{1}\n", DateTime.UtcNow.ToString("YYYY-MM-dd hh:mm:ss"), commentsJson);
Console.WriteLine("==========================");
Console.WriteLine("=====Reddit comments change===");
Console.WriteLine("==========================");
Console.WriteLine(commentsJson);
Console.WriteLine("==========================");
Console.WriteLine("=====Reddit comments change===");
Console.WriteLine("==========================");
oldcomments = newcomments;
}
}
postsJson = GetHtml(postsUrl, "");
if (!string.IsNullOrEmpty(postsJson))
{
json = JObject.Parse(postsJson);
newposts = ((JArray)(json["data"]["children"])).Count;
if (newposts != oldposts)
{
Ding();
Log("[{0}] Posts count changed\nNew json:\n{1}\n", DateTime.UtcNow.ToString("YYYY-MM-dd hh:mm:ss"), postsJson);
Console.WriteLine("==========================");
Console.WriteLine("=====Reddit submitted change===");
Console.WriteLine("==========================");
Console.WriteLine(postsJson);
Console.WriteLine("==========================");
Console.WriteLine("=====Reddit submitted change===");
Console.WriteLine("==========================");
}
oldposts = newposts;
}
}
}
public static int GetChildrenCount(JObject json)
{
return ((JArray)(json["data"]["children"])).Count;
}
public static void Log(string format, params string[] args)
{
File.AppendAllText(logFile, string.Format(format, args));
}
public static void Ding()
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"264594_65641-lq.wav");
player.Play();
}
static string GetHtml(string url, string defaultHtml)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Windows:cpargchecker:1.0"; // to make reddit less angery
var html = defaultHtml;
try {
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
} catch (WebException ex)
{
Console.WriteLine("Exception requesting {0}: {1}", url, ex.Message);
}
return html;
}
}
}