-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
215 lines (197 loc) · 8.7 KB
/
Form1.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
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace HoshForm
{
public partial class HoshForm : Form
{
public HoshForm()
{
InitializeComponent();
AllocConsole();
}
//コンソールを使用
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
private static long ToUnixTime(DateTime dt)
{
var dto = new DateTimeOffset(dt.Ticks, new TimeSpan(+09, 00, 00));
return dto.ToUnixTimeSeconds();
}
private static void Wait(int Seconds)
{
for (int i = 1; i <= Seconds; i++)
{
Thread.Sleep(1000);
Console.Write("{0:D4}", Seconds - i);
Console.SetCursorPosition(0, Console.CursorTop);
}
Console.Beep();
}
private static string Read(string Url, Encoding enc)
{
Stream st = client.OpenRead(Url);
StreamReader sr = new StreamReader(st, enc);
string Result = sr.ReadToEnd();
sr.Close();
st.Close();
return Result;
}
//WebClient起動
public static WebClient client = new WebClient();
//ChromeDriverOptions宣言
public static ChromeOptions options = new ChromeOptions();
private void btnStart_Click(object sender, EventArgs e)
{
//各変数を取得
string UrlBoard = tbUrlBoard.Text;
string Target = tbTarget.Text;
long TimeInterval = (long)numTimeInterval.Value;
string HN = tbHN.Text;
string Message = tbMessage.Text;
//フォームを閉じる
this.Close();
//ChromeDriver起動
options.AddArgument("--headless");
options.AddArgument("--incognito");
IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), options);
WebDriverWait driverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
//本文を切り分ける
string[] Messages = Message.Split("\n");
//正規表現を用意
string Pattern1 = "https://(?<Server>.+?).5ch.net/(?<Board>.+?)/";
string Pattern2 = "<a href=\"(?<Number>[0-9]{10}/l50)\">.+?" + Target + ".+?</a>";
string Pattern3 = "data-date=\"(?<Date>[0-9]+)\"";
string Pattern4 = "<input class=\"formelem maxwidth\" placeholder=\"名前(省略可)\" name=\"FROM\" size=\"70\">";
//各変数を出力
Console.Write
(
"UrlBoard:\t" + UrlBoard + "\r\n" +
"Target:\t\t" + Target + "\r\n" +
"TimeInterval:\t" + TimeInterval.ToString() + "\r\n" +
"HN:\t\t" + HN + "\r\n"
);
foreach (string msg in Messages)
{
Console.WriteLine("Message:\t" + msg);
}
//エンコードの指定
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding enc = Encoding.GetEncoding("Shift_JIS");
//スレ一覧とスレのURLの設定
string UrlThreadList = UrlBoard + "subback.html";
Match match = Regex.Match(UrlThreadList, Pattern1);
string UrlThreadBase = "https://" + match.Groups["Server"] + ".5ch.net/test/read.cgi/" + match.Groups["Board"] + "/";
//初期待ち時間を投稿間隔時間に設定
long TimeWait = TimeInterval;
//ずっと繰り返す
while (true)
{
//スレ一覧の読み込み
string Result = Read(UrlThreadList, enc);
//目標のスレを取得
MatchCollection Threads = Regex.Matches(Result, Pattern2);
//目標のスレが存在しなければ待機
if (Threads.Count == 0)
{
Console.WriteLine
(
"THREAD NOT FOUND" + "\r\n" +
"TimeWait:\t" + TimeWait + "\r\n" +
"WAIT" + "\r\n"
);
//待機
Wait((int)TimeWait);
}
//目標のスレが存在するならスレを開く
else
{
Console.WriteLine("THREAD FOUND");
//スレを開く
string ThreadNumber = Threads[0].Result("${Number}").Replace("l50", "l0");
string UrlThread = UrlThreadBase + ThreadNumber;
Console.WriteLine("UrlThread:\t" + UrlThread);
Result = Read(UrlThread, enc);
//dat落ちを判定
while (Result.Contains(Pattern4))
{
//もしdat落ちでないなら最終書込時刻を取得
Console.WriteLine("THREAD NOT ARCHIEVED");
MatchCollection Dates = Regex.Matches(Result, Pattern3);
long DateLatest = long.Parse(Dates[Dates.Count - 1].Result("${Date}"));
DateTime TargetTime = DateTime.Now;
long DateNow = ToUnixTime(TargetTime) + 60 * 60 * 9;
long TimeDiff = DateNow - DateLatest;
//最終書込時刻と現在時刻、その差分を出力
Console.Write
(
"DateLatest:\t" + DateLatest + "\r\n" +
"DateNow:\t" + DateNow + "\r\n" +
"TimeDiff:\t" + TimeDiff + "\r\n"
);
//もし最終書込時刻から投稿間隔以上の時間が経過していれば書き込む
if (TimeDiff >= TimeInterval)
{
Console.WriteLine("POST");
TimeWait = TimeInterval;
driver.Url = UrlThread;
IWebElement Element = driver.FindElement(By.Name("FROM"));
Element.SendKeys(HN);
Element = driver.FindElement(By.Name("mail"));
Element.SendKeys(TimeInterval.ToString() + "秒");
Random rnd = new System.Random();
Element = driver.FindElement(By.Name("MESSAGE"));
Element.SendKeys(Messages[rnd.Next(0, Messages.Length)]);
Element = driver.FindElement(By.Name("submit"));
Element.Click();
//Cookie切れ対応
try
{
Element = driverWait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath("//input[@value=\"上記全てを承諾して書き込む\"]")));
Element.Click();
}
catch (WebDriverTimeoutException)
{
}
}
//もし最終書込時刻が投稿間隔時間内にあれば超過予想時刻まで待機
else
{
Console.WriteLine("NOT POST");
TimeWait = TimeInterval - TimeDiff;
}
//待機時間を出力
Console.Write
(
"TimeWait:\t" + TimeWait + "\r\n" +
"WAIT\n"
);
//待機
Wait((int)TimeWait);
//スレを開く
Result = Read(UrlThread, enc);
}
//待機時間を出力
Console.Write
(
"THREAD ARCHIEVED" + "\r\n" +
"TimeWait:\t" + TimeWait + "\r\n" +
"WAIT\n"
);
//待機
Wait((int)TimeWait);
}
}
}
}
}