-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
143 lines (130 loc) · 4.6 KB
/
Copy pathProgram.cs
File metadata and controls
143 lines (130 loc) · 4.6 KB
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace InStockNotifier
{
class Program
{
static void Main(string[] args)
{
int sleep_time = 60;
while (true)
{
var arr = JArray.Parse(File.ReadAllText(@"config.json"));
var index = 0;
foreach (dynamic item in arr)
{
bool enabled;
string url = null;
string kword = null;
index++;
try
{
enabled = item.enabled ?? false;
if (!enabled)
continue;
url = item.url;
if (url is null)
{
Console.WriteLine($"Invalid configuration [url], skipping item #{index}.");
continue;
}
kword = item.keyword;
if (kword is null)
{
Console.WriteLine($"Invalid configuration [keyword], skipping item #{index}.");
continue;
}
}
catch
{
Console.WriteLine($"Invalid configuration, skipping item #{index}.");
continue;
}
bool alert = item.alert ?? true;
string name = item.name ?? url;
Console.Write($"Checking {name} @ {url} => ");
var data = HttpGet(url);
if (data.Item1)
{
if (data.Item2.ToUpper().Contains(kword.ToUpper()))
{
Console.WriteLine("Success!");
if (alert)
{
OpenBrowser(url);
Beeps.GoingUp();
}
}
else
Console.WriteLine($"Failed :(");
}
else
Console.WriteLine($"Failed ({data.Item2})");
}
Console.WriteLine($"Sleeping {sleep_time} seconds...");
Thread.Sleep(sleep_time * 1000);
Console.WriteLine();
}
}
public static (bool, string) HttpGet(string url)
{
string data = null;
bool isSuccess = false;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("Content-Encoding", "gzip");
request.AutomaticDecompression = DecompressionMethods.GZip;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
data = reader.ReadToEnd();
reader.Close();
}
isSuccess = true;
}
else
data = response.StatusCode.ToString();
return (isSuccess, data);
}
public static void OpenBrowser(string url)
{
try
{
Process.Start(url);
}
catch (Exception ex)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
Console.Write($"Cannot launch browser: {ex.Message} => {ex.StackTrace}");
}
}
}
}
}