-
Notifications
You must be signed in to change notification settings - Fork 0
/
RealtimeScraper.h
256 lines (211 loc) · 12.8 KB
/
RealtimeScraper.h
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
#include<iostream>
#include<vector>
#include<cstdlib>
#include<fstream>
#include<webdriverxx.h>
#include<webdriverxx/browsers/chrome.h>
#include "baken_format.h"
#include<thread>
#include<chrono>
std::string P_ARS = getenv("P_ARS");
std::string INET_ID = getenv("INET_ID");
std::string USER_NUM = getenv("USER_NUM");
std::string PIN = getenv("PIN");
std::string root_url = "https://www.ipat.jra.go.jp/";
using namespace webdriverxx;
void TanBatanRenScraper(int num_of_horses,std::vector<std::vector<double>>& batan,std::vector<std::vector<double>>& baren,std::vector<double>& tansyou,bool fswitch,int columun,int row,int race_num)
{
WebDriver chrome = Start(Chrome());
chrome.Navigate(root_url);
std::string login_text_box_xpath = "//*[@id=\"top\"]/div[3]/div/table/tbody/tr/td[2]/div/div/form/table[1]/tbody/tr/td[2]/span/input";
std::string click_button_xpath = "//*[@id=\"top\"]/div[3]/div/table/tbody/tr/td[2]/div/div/form/table[1]/tbody/tr/td[3]/p/a";
chrome.FindElement(ByXPath(login_text_box_xpath)).SendKeys(INET_ID);
chrome.FindElement(ByXPath(click_button_xpath)).Click();
//inputting the required information
std::string user_num_input_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[1]/td[2]/span/input";
std::string pin_input_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[2]/td[2]/span/input";
std::string p_ars_input_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[3]/td[2]/span/input";
click_button_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[1]/td[3]/p/a";
chrome.FindElement(ByXPath(user_num_input_xpath)).SendKeys(USER_NUM);
chrome.FindElement(ByXPath(pin_input_xpath)).SendKeys(PIN);
chrome.FindElement(ByXPath(p_ars_input_xpath)).SendKeys(P_ARS);
chrome.FindElement(ByXPath(click_button_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(2));
//期間限定の画面
////*[@id="main"]/ui-view/div[2]/div[2]/button
////*[@id="main"]/ui-view/div[2]/div[2]/button
std::string xpaht = "//*[@id=\"main\"]/ui-view/div[2]/div[2]/button";
chrome.FindElement(ByXPath(xpaht)).Click();
std::this_thread::sleep_for(std::chrono::seconds(2));
//login complete.going to odds vote section(オッズ投票画面への遷移)
std::string odds_vote_xpath = "//*[@id=\"main\"]/ui-view/main/div[2]/div[1]/div[2]/button";
chrome.FindElement(ByXPath(odds_vote_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(1));
//馬場選択画面への遷移
std::string baba_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/select-course-race/div/div[2]/div[2]/div[2]/div[" + std::to_string(row) + "]/div["+ std::to_string(columun)+ "]/button";
chrome.FindElement(ByXPath(baba_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(1));
//レース番号選択
std::string race_num_xpath="//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/select-course-race/div/div[2]/div[2]/div[4]/div[" + std::to_string(race_num)+ "]/button";
chrome.FindElement(ByXPath(race_num_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(2));
//opening the file
std::string file_name = "real_time_odds";
std::ofstream file("real_time_odds.txt");//file(file_name);
//scraping tansyou odds
for(int i=1;i<num_of_horses+1;i++){
std::string odds_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/span/span/bet-odds-type-winplace-basic/div/div/table/tbody/tr[" +std::to_string(i) +"]/td[3]/button/odds-num/span/span";
if(fswitch)
{
file << i << " " << chrome.FindElement(ByXPath(odds_xpath)).GetText() << std::endl;
}
tansyou[i] = (double)std::stod(chrome.FindElement(ByXPath(odds_xpath)).GetText());
}
if(fswitch)
file << "\n";
//scraping umaren
std::string odds_type_change_xpath = "//*[@id=\"bet-odds-type\"]";
std::this_thread::sleep_for(std::chrono::seconds(1));
chrome.FindElement(ByXPath(odds_type_change_xpath)).Click();
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Down);
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Down);
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Enter);
std::this_thread::sleep_for(std::chrono::seconds(1));
for(int first = 1;first < num_of_horses+1;first++){
for(int second=1;second<num_of_horses+1-first;second++){
std::string odds_xpath="//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/span/span/bet-odds-type-quinella-basic/div/div/div[1]/div["+ std::to_string(first) +"]/div/div["+ std::to_string(second+1) +"]/div[2]/button/odds-num/span/span";
if(fswitch)
{
file << first << "-" << first+second << " " << chrome.FindElement(ByXPath(odds_xpath)).GetText() << std::endl;
}
baren[first][first+second] = std::stod(chrome.FindElement(ByXPath(odds_xpath)).GetText());
}
}
if(fswitch)
file << "\n";
//scraping umatan
std::this_thread::sleep_for(std::chrono::seconds(1));
odds_type_change_xpath = "//*[@id=\"bet-odds-type\"]";
chrome.FindElement(ByXPath(odds_type_change_xpath)).Click();
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Down);
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Down);
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Enter);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string odds;
for(int first=1;first<num_of_horses+1;first++){
for(int second=1;second<num_of_horses+1;second++){
std::string odds_xpath="//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/span/span/bet-odds-type-exacta-basic/div/div/div[1]/div[" +std::to_string(first)+ "]/div/div["+std::to_string(second+1) +"]/div[2]/button/odds-num/span/span";
if(fswitch)
{
file << first << ">" << second << " " << chrome.FindElement(ByXPath(odds_xpath)).GetText() << std::endl;
}
odds = chrome.FindElement(ByXPath(odds_xpath)).GetText();
if(odds!="---")
batan[first][second] = std::stod(chrome.FindElement(ByXPath(odds_xpath)).GetText());
}
}
file.close();
}
void SantanScraper(int worker_id,int row,int columun,int race_num,int step,int mod,int num_of_horses,std::vector<std::vector<std::vector<double>>>& santan,int time,bool fswitch){
WebDriver chrome = Start(Chrome());
chrome.Navigate(root_url);
std::string login_text_box_xpath = "//*[@id=\"top\"]/div[3]/div/table/tbody/tr/td[2]/div/div/form/table[1]/tbody/tr/td[2]/span/input";
std::string click_button_xpath = "//*[@id=\"top\"]/div[3]/div/table/tbody/tr/td[2]/div/div/form/table[1]/tbody/tr/td[3]/p/a";
chrome.FindElement(ByXPath(login_text_box_xpath)).SendKeys(INET_ID);
chrome.FindElement(ByXPath(click_button_xpath)).Click();
//inputting the required information
std::string user_num_input_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[1]/td[2]/span/input";
std::string pin_input_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[2]/td[2]/span/input";
std::string p_ars_input_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[3]/td[2]/span/input";
click_button_xpath = "//*[@id=\"main_area\"]/div/div[1]/table/tbody/tr[1]/td[3]/p/a";
chrome.FindElement(ByXPath(user_num_input_xpath)).SendKeys(USER_NUM);
chrome.FindElement(ByXPath(pin_input_xpath)).SendKeys(PIN);
chrome.FindElement(ByXPath(p_ars_input_xpath)).SendKeys(P_ARS);
chrome.FindElement(ByXPath(click_button_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(1));
//期間限定
std::string xpaht = "//*[@id=\"main\"]/ui-view/div[2]/div[2]/button";
////*[@id="main"]/ui-view/div[2]/div[2]/button
chrome.FindElement(ByXPath(xpaht)).Click();
std::this_thread::sleep_for(std::chrono::seconds(2));
//login complete.going to odds vote screen
std::string odds_vote_xpath = "//*[@id=\"main\"]/ui-view/main/div[2]/div[1]/div[2]/button";
chrome.FindElement(ByXPath(odds_vote_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string baba_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/select-course-race/div/div[2]/div[2]/div[2]/div[" + std::to_string(row) + "]/div["+ std::to_string(columun)+ "]/button";
chrome.FindElement(ByXPath(baba_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string race_num_xpath="//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/select-course-race/div/div[2]/div[2]/div[4]/div[" + std::to_string(race_num)+ "]/button";
chrome.FindElement(ByXPath(race_num_xpath)).Click();
std::this_thread::sleep_for(std::chrono::seconds(1));
//opening the file
std::string file_name = "real_time_odds"+std::to_string(worker_id);
std::ofstream file(file_name);//file(file_name);
//scraping sanrentan
std::this_thread::sleep_for(std::chrono::seconds(2));
std::string odds_type_change_xpath = "//*[@id=\"bet-odds-type\"]";
chrome.FindElement(ByXPath(odds_type_change_xpath)).Click();
for(int i=0;i<6;i++)
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Down);
chrome.FindElement(ByXPath(odds_type_change_xpath)).SendKeys(keys::Enter);
std::string selector_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/div[2]/div[2]/div[2]/select";
chrome.FindElement(ByXPath(selector_xpath)).Click();
for(int i=0;i<step*worker_id;i++)
chrome.FindElement(ByXPath(selector_xpath)).SendKeys(keys::Down);
chrome.FindElement(ByXPath(selector_xpath)).SendKeys(keys::Enter);
std::this_thread::sleep_for(std::chrono::seconds(1));
for(int first=step*worker_id+1;first<step*worker_id+step+mod+1;first++){
if(first%step!=1){
std::string selector_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/div[2]/div[2]/div[2]/select";
chrome.FindElement(ByXPath(selector_xpath)).Click();
chrome.FindElement(ByXPath(selector_xpath)).SendKeys(keys::Down);
chrome.FindElement(ByXPath(selector_xpath)).SendKeys(keys::Enter);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
for(int second = 1;second<num_of_horses;second++){
std::string nichaku_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/span/span/bet-odds-type-trifecta-basic/div/div/div[2]/div[" +std::to_string(second) +"]/div[2]/div[1]";
for(int third=1;third<num_of_horses;third++){
std::string sanchaku_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/span/span/bet-odds-type-trifecta-basic/div/div/div[2]/div[1]/div[2]/div["+ std::to_string(third+1)+"]/div[1]";
std::string odds_xpath = "//*[@id=\"main\"]/ui-view/div[2]/ui-view/ui-view/main/div/span/span/bet-odds-type-trifecta-basic/div/div/div[2]/div["+std::to_string(second)+"]/div[2]/div["+std::to_string(third+1)+"]/div[2]/button/odds-num/span/span";
if(fswitch)
{
file << first << ">" << chrome.FindElement(ByXPath(nichaku_xpath)).GetText()
<<">" << chrome.FindElement(ByXPath(sanchaku_xpath)).GetText()
<< " " << chrome.FindElement(ByXPath(odds_xpath)).GetText() << std::endl;
}
int nichaku_int = std::stoi(chrome.FindElement(ByXPath(nichaku_xpath)).GetText());
int sanchaku_int = std::stoi(chrome.FindElement(ByXPath(sanchaku_xpath)).GetText());
std::string odds=chrome.FindElement(ByXPath(odds_xpath)).GetText();
if(odds!="---")
santan[first][nichaku_int][sanchaku_int] = std::stod(chrome.FindElement(ByXPath(odds_xpath)).GetText());
}
}
}
file.close();
}
int ThreadNumOpt(int num_of_horses){
if(num_of_horses==5||num_of_horses==10||num_of_horses==15||num_of_horses==11)
{
return 5;
}
else if(num_of_horses==6||num_of_horses==12||num_of_horses==13)
{
return 6;
}
else if(num_of_horses==7||num_of_horses==14)
{
return 7;
}
else if(num_of_horses==8||num_of_horses==16||num_of_horses==17)
{
return 8;
}
else if(num_of_horses==9||num_of_horses==18)
{
return 9;
}
else
{
return -1;
}
}