@@ -24,6 +24,24 @@ using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
24
24
25
25
Crowler::Crowler () {}
26
26
27
+ void Crowler::processUrl (std::string url, short depth)
28
+ {
29
+ // полный процессинг ресурса: получение слов, сохранение а базу данных, получение внутренних ресурсов
30
+ std::string html = download (url);
31
+ std::vector<std::string> words = getWords (html);
32
+ std::vector<std::string> subUrls = getSubUrls (html);
33
+ savePresencesToDb (words, url);
34
+
35
+ // если глубина не 1, обход внутренних ресурсов с уменьшенной на 1 глубиной
36
+ // если рекурсивно (или сразу) попали сюда с глубиной 1, дальнейшего обхода не будет
37
+ if (depth != 1 ) {
38
+ depth--;
39
+ for (auto & subUrl : subUrls) {
40
+ processUrl (subUrl, depth);
41
+ }
42
+ }
43
+ }
44
+
27
45
std::string Crowler::download (std::string url)
28
46
{
29
47
try
@@ -49,13 +67,6 @@ std::string Crowler::download(std::string url)
49
67
tcp::resolver resolver (ioc);
50
68
beast::ssl_stream<beast::tcp_stream> stream (ioc, ctx);
51
69
52
- // Set SNI Hostname (many hosts need this to handshake successfully)
53
- // if(! SSL_set_tlsext_host_name(stream.native_handle(), host))
54
- // {
55
- // beast::error_code ec{static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()};
56
- // throw beast::system_error{ec};
57
- // }
58
-
59
70
// Look up the domain name
60
71
auto const results = resolver.resolve (host, port);
61
72
@@ -83,22 +94,8 @@ std::string Crowler::download(std::string url)
83
94
http::read (stream, buffer, res);
84
95
85
96
std::string const strBody = boost::beast::buffers_to_string (res.body ().data ());
86
- // std::cout << res << std::endl;
87
- // std::cout << boost::beast::buffers_to_string(res.body().data()) << std::endl;
88
-
89
- // Gracefully close the stream
90
97
beast::error_code ec;
91
98
stream.shutdown (ec);
92
- // if(ec == net::error::eof)
93
- // {
94
- // // Rationale:
95
- // // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
96
- // ec = {};
97
- // }
98
- // if(ec)
99
- // throw beast::system_error{ec};
100
-
101
- // If we get here then the connection is closed gracefully
102
99
return strBody;
103
100
}
104
101
catch (std::exception const & e)
@@ -136,7 +133,7 @@ std::vector<std::string> Crowler::getSubUrls(std::string s)
136
133
return getDataFromHtml (s, word_regex);
137
134
}
138
135
139
- std::vector<WordPresence> Crowler::calculatePresences (std::vector<std::string> words, std::string url)
136
+ void Crowler::savePresencesToDb (std::vector<std::string> words, std::string url)
140
137
{
141
138
std::vector<WordPresence> wordsPresence;
142
139
@@ -146,11 +143,11 @@ std::vector<WordPresence> Crowler::calculatePresences(std::vector<std::string> w
146
143
++map[word];
147
144
}
148
145
149
- // преобразование map к вектору структур
146
+ // преобразование map к структуре и сохранение в базу данных
147
+ DbManager dbManager = DbManager ();
150
148
for (const auto & wordFrequency : map) {
151
149
WordPresence presence = WordPresence{wordFrequency.first , url, wordFrequency.second };
152
- wordsPresence. push_back (presence);
150
+ dbManager. insertPresence (presence);
153
151
}
154
152
155
- return wordsPresence;
156
153
}
0 commit comments