-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.cpp
182 lines (167 loc) · 6.39 KB
/
crawler.cpp
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
#include <vector>
#include <string>
#include <iostream>
#include <curl/curl.h>
#include <gumbo.h>
#include "crawler.h"
#include <regex>
#include <fstream>
WebCrawler::WebCrawler() {
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
}
WebCrawler::~WebCrawler() {
curl_easy_cleanup(curl);
curl_global_cleanup();
gumbo_destroy_output(&kGumboDefaultOptions, output);
}
std::string WebCrawler::crawl(const std::string& url) {
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
output = gumbo_parse(readBuffer.c_str());
return readBuffer;
} else {
std::cerr << "curl_easy_init() failed to initialize libcurl" << std::endl;
return "";
}
}
std::vector<std::string> WebCrawler::extractLinks(GumboNode* html) {
std::vector<std::string> links;
extractLinksRecursive(html, links);
return links;
}
// searchKeywords
std::vector<std::string> WebCrawler::searchKeywords(const std::string& html, const std::vector<std::string>& keywords, const std::string& source) {
std::vector<std::string> results;
GumboOutput* output = gumbo_parse(html.c_str());
std::string text = extractTextRecursive(output->root);
gumbo_destroy_output(&kGumboDefaultOptions, output);
// priorize searching in the body content
std::vector<std::string> sentences = splitIntoSentences(text);
for (const auto& sentence : sentences) {
for (const auto& keyword : keywords) {
if (containsKeyword(sentence, keyword)) {
std::string resultWithSource = sentence + + " [Source: " + source + "]";
results.push_back(resultWithSource);
break;
}
}
}
return results;
}
//extractText
std::string WebCrawler::extractText(const std::string& html, const std::string& source) {
GumboOutput* output = gumbo_parse(html.c_str());
std::string text = extractTextRecursive(output->root);
gumbo_destroy_output(&kGumboDefaultOptions, output);
text += " [Source: " + source + "]";
// add to txt file
std::ofstream myfile;
myfile.open ("raw_data.txt", std::ios_base::app);
myfile << text << std::endl;
myfile.close();
return text;
}
//isValidUrl
bool WebCrawler::isValidUrl(const std::string& url) {
std::regex urlRegex("(https?|ftp)://[\\w\\-_]+(\\.[\\w\\-_]+)+([a-zA-Z0-9\\-.,@?^=%&:/~+#]*[a-zA-Z0-9\\-@?^=%&/~+#])?");
return std::regex_match(url, urlRegex);
}
//extractPaginationLinks
std::vector<std::string> WebCrawler::extractPaginationLinks(const std::string& html) {
std::vector<std::string> paginationLinks;
GumboOutput* output = gumbo_parse(html.c_str());
extractLinksRecursive(output->root, paginationLinks);
gumbo_destroy_output(&kGumboDefaultOptions, output);
return paginationLinks;
}
// searchHeadlines
std::vector<std::string> WebCrawler::searchHeadlines(const std::string& html, const std::string& source) {
std::vector<std::string> headlines;
GumboOutput* output = gumbo_parse(html.c_str());
std::string text = extractTextRecursive(output->root);
gumbo_destroy_output(&kGumboDefaultOptions, output);
// priorize searching in the body content
std::vector<std::string> sentences = splitIntoSentences(text);
for (const auto& sentence : sentences) {
std::string resultWithSource = sentence + + " [Source: " + source + "]";
headlines.push_back(resultWithSource);
}
return headlines;
}
// getOutputRoot
GumboNode* WebCrawler::getOutputRoot() const {
return output->root;
}
// private methods
size_t WebCrawler::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
void WebCrawler::extractLinksRecursive(GumboNode* node, std::vector<std::string>& links) {
if (node->type != GUMBO_NODE_ELEMENT) {
return;
}
GumboAttribute* href;
if (node->v.element.tag == GUMBO_TAG_A &&
(href = gumbo_get_attribute(&node->v.element.attributes, "href"))) {
links.push_back(href->value);
}
GumboVector* children = &node->v.element.children;
for (unsigned int i = 0; i < children->length; ++i) {
extractLinksRecursive(static_cast<GumboNode*>(children->data[i]), links);
}
}
std::vector<std::string> WebCrawler::splitIntoSentences(const std::string& text) {
std::vector<std::string> sentences;
std::string sentence;
for (const auto& c : text) {
if (c == '.' || c == '?' || c == '!') {
sentences.push_back(sentence);
sentence.clear();
} else {
sentence += c;
}
}
return sentences;
}
bool WebCrawler::containsKeyword(const std::string& text, const std::string& keyword) {
return text.find(keyword) != std::string::npos;
}
std::string WebCrawler::extractTextRecursive(GumboNode* node) {
if (node->type == GUMBO_NODE_TEXT) {
return std::string(node->v.text.text);
} else if (node->type == GUMBO_NODE_ELEMENT &&
node->v.element.tag != GUMBO_TAG_SCRIPT &&
node->v.element.tag != GUMBO_TAG_STYLE) {
// prioritize searching from headline
if (node->v.element.tag == GUMBO_TAG_H1
|| node->v.element.tag == GUMBO_TAG_H2
|| node->v.element.tag == GUMBO_TAG_H3
|| node->v.element.tag == GUMBO_TAG_H4
|| node->v.element.tag == GUMBO_TAG_H5
|| node->v.element.tag == GUMBO_TAG_H6) {
return extractTextRecursive(static_cast<GumboNode*>(node->v.element.children.data[0]));
} else {
std::string contents = "";
GumboVector* children = &node->v.element.children;
for (unsigned int i = 0; i < children->length; ++i) {
const std::string text = extractTextRecursive(static_cast<GumboNode*>(children->data[i]));
if (i != 0 && !text.empty()) {
contents.append(" ");
}
contents.append(text);
}
return contents;
}
} else {
return "";
}
}