-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
87 lines (64 loc) · 1.96 KB
/
Copy pathmain.cpp
File metadata and controls
87 lines (64 loc) · 1.96 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
#include "Frontier.hpp"
#include "RedisReplyPtr.hpp"
#include "DNS_Resolver.hpp"
#include "WebScraper.hpp"
#include <iostream>
#include <unistd.h>
#include <hiredis/hiredis.h>
#include <nlohmann/json.hpp>
int main(){
std::cout << "--- Web Crawler ---" << "\n" << std::endl;
// Connect to Redis DB
redisContext* c = redisConnect("redisdb", 6379);
// Check for NULL or Error
if (c == NULL || c->err){
if (c != NULL) {
std::cout << c->err << std::endl;
} else {
std::cout << "Can't allocate redis context" << std::endl;
}
exit(1);
}
std::cout << "Redis Connected" << std::endl;
// Initialize frontier
Frontier frontier(c, "/usr/local/bin/Seeds.txt");
// Initialize DNS Resolver
DNS_Resolver resolver(c);
// Initialize Web Scraper to end-point
WebScraper webScraper("http://scraper:80");
std::cout << "\n" << "-------------------" << std::endl;
//Main Crawler Loop
while(frontier.remainingSeeds() > 0){
RedisReplyPtr readySeeds = frontier.getReadySeeds();
for(int i = 0; i < readySeeds.reply->elements; i++){
std::string seed = readySeeds.reply->element[i]->str;
// If ip is not cached use DNS Resolver
if(resolver.is_cached(seed) == false){
std::cout << "Searching for ipv4 of " << seed << "..." << std::endl;
resolver.lookup(seed);
continue;
}
// Get next url and parse it
std::string url = frontier.popUrl(seed);
// If no more urls to scrape remove seed
if(url == ""){
frontier.removeSeed(seed);
std::cout << "... " << seed << " crawled" << std::endl;
continue;
}
nlohmann::json pageData = webScraper.parsePage(seed, url);
// Add current URL to seen Bloom Filter
frontier.addToBf("seen", seed, url);
// Add all unscraped urls to queue
for(std::string newUrl : pageData["urls"]){
// Skip url if already scrapped
if(frontier.checkBf("seen", seed, newUrl) == 1){
continue;
}
frontier.addUrl(seed, newUrl);
}
frontier.reQueueSeed(seed);
}
}
redisFree(c);
}