-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper_definitions.cpp
More file actions
427 lines (349 loc) · 11.5 KB
/
scraper_definitions.cpp
File metadata and controls
427 lines (349 loc) · 11.5 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
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
416
417
418
419
420
421
422
423
424
425
426
427
#include "scraping-handler.h"
std::string Scraper::baseURL;
std::vector<std::string> Scraper::keywords = {"Hello"};
bool Scraper::isCanceled = false;
bool AnalyzePages::hasStarted = false;
bool Scraper::hasDisconnected = false;
bool Scraper::CheckForConnection()
{
std::string createCommand = "ping www.google.com -c1";
int getVal = std::system(createCommand.c_str());
if (getVal != 0)
{
return false;
}
return true;
}
void Scraper::SetupScraper(std::vector<std::string> inputKeywords, std::string url)
{
keywords.clear();
baseURL = url;
keywords = inputKeywords;
}
// You need to define static variables to use them, a declaration is not enough
std::vector<std::string> Scraper::urls;
bool Scraper::analysis = false;
// Get Info
cpr::Response Scraper::request_info(std::string url)
{
// Use a user agent to mask the scraper
// Source: https://brightdata.com/blog/how-tos/web-scraping-in-c-plus-plus
cpr:: Header headers = {{"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0"}};
// Get Data
// Source: https://docs.libcpr.org/introduction.html
cpr::Response r = cpr::Get(cpr::Url{url}, headers);
return r;
}
// Parse content
lxb_html_document_t* Scraper::Parse(const lxb_char_t* html, size_t html_len)
{
lxb_status_t status;
lxb_html_parser_t* parser;
lxb_html_document_t* document;
// Initialize
parser = lxb_html_parser_create();
status = lxb_html_parser_init(parser);
if (status != LXB_STATUS_OK)
{
exit(EXIT_FAILURE);
}
// Parse
document = lxb_html_parse(parser, html,html_len);
if (document == nullptr)
{
exit(EXIT_FAILURE);
}
// Destroy parser
lxb_html_parser_destroy(parser);
return document;
}
// Get urls from the serializer_callback
void Scraper::getUrls(char* data)
{
std::string getData = (std::string) data;
// characters use single quotes!!!
char symbol = '/';
char dataFirstChar = getData.at(0);
if (dataFirstChar == symbol)
{
urls.push_back(baseURL + getData);
}
}
// serializer
lxb_status_t Scraper::serializer_callback(const lxb_char_t *data, size_t len, void *ctx)
{
//printf("%.*s", (int) len, (const char *) data);
if (analysis)
{
std::cin.get();
}
if (!analysis)
{
getUrls((char *) data);
}
return LXB_STATUS_OK;
}
// Serialize node
void Scraper::serialize_node(lxb_dom_node_t *node)
{
lxb_status_t status;
status = lxb_html_serialize_pretty_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
0, serializer_callback, nullptr);
if (status != LXB_STATUS_OK) {
exit(EXIT_FAILURE);
}
}
// Find the right content
// Source 1: http://lexbor.com/docs/lexbor/
// Source 2: https://github.com/lexbor/lexbor/tree/master/examples
// Get elements by attribute: https://github.com/lexbor/lexbor/blob/master/examples/lexbor/html/elements_by_attr.c
std::vector<std::string> Scraper::ParseContent(std::string content) {
lxb_status_t status;
lxb_dom_element_t* body = nullptr;
lxb_dom_element_t* gather_collection = nullptr;
lxb_html_document_t* document = nullptr;
lxb_dom_collection_t* collection = nullptr;
lxb_html_parser_t* parser = nullptr;
std::vector<std::string> output;
// Initialize parser
parser = lxb_html_parser_create();
status = lxb_html_parser_init(parser);
if (status != LXB_STATUS_OK)
{
exit(EXIT_FAILURE);
}
auto *html = new lxb_char_t[content.size() + 1];
for (int i = 0; i < content.length(); i++) {
html[i] = content[i];
}
size_t html_len = content.length() + 1;
document = lxb_html_parse(parser, html, html_len);
if (document == nullptr) {
exit(EXIT_FAILURE);
}
// Destroy parser now that the process has finished
lxb_html_parser_destroy(parser);
body = lxb_dom_interface_element(document->body);
collection = lxb_dom_collection_make(&document->dom_document, 128);
if (collection == nullptr)
{
exit(EXIT_FAILURE);
}
status = lxb_dom_elements_by_tag_name(lxb_dom_interface_element(document->body), collection,
(const lxb_char_t *) "a", 1);
if (status != LXB_STATUS_OK)
{
exit(EXIT_FAILURE);
}
size_t get_collection_length = lxb_dom_collection_length(collection);
// Print out results
for (size_t i = 0; i < get_collection_length; i++)
{
gather_collection = lxb_dom_collection_element(collection, i);
serialize_node(lxb_dom_interface_node(gather_collection));
}
output.reserve(urls.size());
for (std::string& item : urls)
{
if (std::find(output.begin(), output.end(), item) == output.end())
{
output.push_back(item);
}
}
// Destroy objects to avoid memory leaks
lxb_dom_collection_destroy(collection, true);
lxb_html_document_destroy(document);
delete [] html;
return output;
}
void AnalyzePages::analyzeEntry(std::string input, std::vector<std::string> grabKeywords, Scraper scraper)
{
if (Scraper::isCanceled)
{
Scraper::isCanceled = true;
return;
}
std::ofstream contentFile;
if(hasStarted)
{
contentFile.open("../content/content.txt", std::ios::app);
}else
{
contentFile.open("../content/content.txt");
hasStarted = true;
}
if (!contentFile.is_open())
{
std::cout << "Failed to open file" << std::endl;
std::cout << std::strerror(errno) << std::endl;
exit(errno);
}
Scraper::analysis = true;
bool keywordsFound = false;
cpr::Response getRes = Scraper::request_info(input);
lxb_status_t status;
lxb_dom_element_t* elem = nullptr;
lxb_html_document_t* document = nullptr;
lxb_dom_collection_t* collection = nullptr;
lxb_dom_node_t* node = nullptr;
lxb_dom_character_data_t *ch_data = nullptr;
auto *html = new lxb_char_t[getRes.text.size() + 1];
// Check for string, span and a tags, if a tags are found, then replace them with paragraph tags
for (int i = 0; i < getRes.text.length();) {
// This might need a refactor but I am unable to find a way to do so
std::string keyword1 = "<strong>";
std::string keyword2 = "</strong>";
std::string keyword3 = "<span>";
std::string keyword4 = "</span>";
std::string keyword5 = "<a ";
std::string keyword6 = "</a>";
bool isTheSame = true;
for (int j = 0; j < keyword1.length(); j++) {
if (getRes.text[i + j] != keyword1[j]) {
isTheSame = false;
break;
}
}
if (isTheSame) {
i += int(keyword1.length()); // Move past
continue;
}else{
isTheSame = true;
}
for (int k = 0; k < keyword2.length(); k++) {
if (getRes.text[i + k] != keyword2[k]) {
isTheSame = false;
break;
}
}
if (isTheSame) {
i += int(keyword2.length()); // Move past
continue;
}else{
isTheSame = true;
}
for (int l = 0; l < keyword3.length(); l++) {
if (getRes.text[i + l] != keyword3[l]) {
isTheSame = false;
break;
}
}
if (isTheSame) {
i += int(keyword3.length()); // Move past
continue;
}else{
isTheSame = true;
}
for (int m = 0; m < keyword4.length(); m++) {
if (getRes.text[i + m] != keyword4[m]) {
isTheSame = false;
break;
}
}
if (isTheSame) {
i += int(keyword4.length()); // Move past
continue;
}else{
isTheSame = true;
}
for (int n = 0; n < keyword5.length(); n++) {
if (getRes.text[i + n] != keyword5[n]) {
isTheSame = false;
break;
}
}
if (isTheSame) {
html[i] = '<';
html[i+1] = 'p';
html[i+2] = ' ';
i += int(keyword5.length()); // Move past
continue;
}else{
isTheSame = true;
}
for (int o = 0; o < keyword6.length(); o++) {
if (getRes.text[i + o] != keyword6[o]) {
isTheSame = false;
break;
}
}
if (isTheSame) {
html[i] = '<';
html[i+1] = '/';
html[i+2] = 'p';
html[i+3] = '>';
i += int(keyword6.length()); // Move past
continue;
}
html[i] = getRes.text[i];
i++;
}
std::string getHTML;
for (int i = 0; html[i] != '\0'; i++)
{
getHTML.push_back(html[i]);
}
std::string convertToLower = boost::locale::to_lower(getHTML);
size_t html_len = convertToLower.length() - 1;
for (int i = 0; i < html_len; i++)
{
html[i] = convertToLower[i];
}
document = Scraper::Parse(html, html_len);
collection = lxb_dom_collection_make(&document->dom_document, 128);
if (collection == nullptr)
{
exit(EXIT_FAILURE);
}
// Get status of creating a collection creation for paragraph tags
status = lxb_dom_elements_by_tag_name(lxb_dom_interface_element(document->body),
collection, (const lxb_char_t*) "p", 1);
if (status != LXB_STATUS_OK)
{
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < lxb_dom_collection_length(collection); i++)
{
// Get the text from any paragraphs
// Special thanks to Alexander Lexborisov for providing me with the solution
// to get the text. Source: https://github.com/lexbor/lexbor/issues/196
elem = lxb_dom_collection_element(collection, i);
node = lxb_dom_interface_node(elem)->first_child;
if (node != nullptr && node->local_name == LXB_TAG__TEXT)
{
ch_data = lxb_dom_interface_character_data(node);
const auto getData = ch_data->data.data;
// To cast an unsigned char* to string, we need to use reinterpret_cast
// This is not very safe but it works Source:
// https://stackoverflow.com/questions/17746688/convert-unsigned-char-to-stdstring
std::string toString(reinterpret_cast<char*>(getData));
for (std::string& keyword : grabKeywords)
{
std::cout << "Keyword compare: " << keyword << std::endl;
std::cout << "Result: " << toString.find(keyword) << std::endl;
if (toString.find(keyword) != std::string::npos)
{
keywordsFound = true;
break;
}
}
std::cout << "Current String: " << toString << std::endl;
std::cout << "Has been found? " << keywordsFound << std::endl;
// Output
if (keywordsFound)
{
contentFile << input << std::endl;
std::cout << "Written: " << input << std::endl;
contentFile << toString << std::endl;
std::cout << "Written: " << toString << std::endl;
keywordsFound = false;
}
}
}
lxb_dom_collection_destroy(collection, true);
lxb_html_document_destroy(document);
delete [] html;
contentFile.close();
scraper.analysis = false;
}