-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.cpp
211 lines (178 loc) · 5.38 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
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
#define CURL_STATICLIB
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iterator>
#include <set>
#include <regex>
#include<typeinfo>
#include <queue>
#include<unordered_map>
#include<mysql.h>
#include "curl/curl.h"
int qstate;
std::unordered_map<std::string, int> urlList;
#ifdef _DEBUG
#pragma comment (lib , "curl/libcurl_a_debug.lib")
#else
#pragma comment (lib,"curl/libcurl_a.lib")
#endif
std::string file_to_string(std::string file_name) {
std::ifstream file(file_name);
return { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>{} };
}
std::set<std::string> extract_hyperlinks(std::string html_file_name)
{
static const std::regex hl_regex("href=\"(.*?)\"", std::regex_constants::icase);
const std::string text = file_to_string(html_file_name);
return { std::sregex_token_iterator(text.begin(), text.end(), hl_regex, 1),
std::sregex_token_iterator{} };
}
std::string extract_domain(std::string urlName)
{
std::regex expression("/^(http://|https://)[A-Za-z0-9.-]+(?!.*\|\w*$)/");
std::regex re("(http|https)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)");
std::smatch match;
std::string str(urlName);
std::string val = urlName;
std::string retVal1,retVal2,retVal;
if (regex_match(val, match, re)) {
retVal1 = (match[1]);
retVal2 = (match[2]);
}
retVal = retVal1 + "://" + retVal2;
return retVal;
}
static int writer(char *data, size_t size, size_t nmemb, std::string *writerData) {
if (writerData == NULL)
return 0;
writerData->append(data, size*nmemb);
return size * nmemb;
}
void showq(std::queue <std::string> gq){
std::queue <std::string> g = gq;
while (!g.empty())
{
std::cout << '\n' << g.front();
g.pop();
}
std::cout << '\n';
}
int main()
{
MYSQL *conn;
MYSQL_ROW row;
MYSQL_RES *res;
conn = mysql_init(NULL);
conn = mysql_real_connect(conn, "127.0.0.1", "root", "password", "crawler", 3306, NULL, 0); // "localhost" fails
if (conn) {
puts("succesfully connected to the database");
}
else {
puts("cannot connect");
}
std::string fetchedUrl;
int fetchedUrlLinked;
std::string content;
std::string filename = "C:\\Users\\Admin\\junk\\newfile.html";
std::queue<std::string> urls;
std::string start = "https://github.com/sunitasen";
std::string query = "select name,linked from urls";
const char *q = query.c_str();
qstate = mysql_query(conn, q);
if (!qstate) {
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res)) {
fetchedUrl = row[0];
fetchedUrlLinked = atoi(row[1]);
urlList[fetchedUrl] = fetchedUrlLinked;
}
}
else {
std::cout << "query failed" << mysql_error(conn) << "\n";
}
urls.push(start);
int i = 0;
while (i < 10) {
curl_global_init(CURL_GLOBAL_ALL);
std::ofstream fp;
std::string starturl = urls.front();
std::string str(starturl);
std::string domainName = extract_domain(starturl);
const char *parUrl = starturl.c_str();
std::cout << starturl << "\n";
CURL *curl = nullptr;
curl = curl_easy_init();
if (curl) {
fp.open(filename, std::ios::out);
curl_easy_setopt(curl, CURLOPT_URL, parUrl);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
fp << content;
for (auto str : extract_hyperlinks(filename)) {
std::cout << str << '\n';
if (str[0] == 'h') {
if (urlList.find(str) == urlList.end()) {
urls.push(str);
urlList[str] = 1;
std::string queryinit = "insert into urls values( \"";
queryinit += str + "\" , 1)";
qstate = mysql_query(conn, queryinit.c_str());
if (!qstate) std::cout << "done!";
else std::cout << "nope";
}
else {
std::string query = "select name,linked from urls where name=\"";
query += str + "\"";
mysql_query(conn, query.c_str());
res = mysql_store_result(conn);
row = mysql_fetch_row(res);
fetchedUrlLinked = atoi(row[1]);
std::string queryinit = "update urls set linked =";
fetchedUrlLinked += 1;
urlList[str] = fetchedUrlLinked;
queryinit += std::to_string(fetchedUrlLinked) + " where name = \"" + str + "\"";
qstate = mysql_query(conn, queryinit.c_str());
if (!qstate) std::cout << "updated!\n";
}
}
else if(str[0] == '/'){
std::string subUrl = domainName + str;
if (urlList.find(subUrl) == urlList.end()) {
urls.push(subUrl);
urlList[subUrl] = 1;
std::string queryinit = "insert into urls values( \"";
queryinit += subUrl + "\" , 1)";
qstate = mysql_query(conn, queryinit.c_str());
if (!qstate) std::cout << "worked";
else std::cout << "nope";
}
else {
std::string query = "select name,linked from urls where name=\"";
query += subUrl + "\"";
mysql_query(conn, query.c_str());
res = mysql_store_result(conn);
row = mysql_fetch_row(res);
fetchedUrlLinked = atoi(row[1]);
std::string queryinit = "update urls set linked =";
fetchedUrlLinked += 1;
urlList[subUrl] = fetchedUrlLinked;
queryinit += std::to_string(fetchedUrlLinked) + " where name = \"" + subUrl + "\"";
qstate = mysql_query(conn, queryinit.c_str());
if (!qstate) std::cout << "updated";
}
}
}
filename = "C:\\Users\\Admin\\junk\\file" + std::to_string(i) + ".html";
urls.pop();
fp.close();
i++;
}
std::cout << "end reached";
return 0;
}