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
|
#include "Fetcher.hpp"
#include "Frontier.hpp"
#include "Deduper.hpp"
#include "Processor.hpp"
#include "URLSeen.hpp"
#include "URLNormalizer.hpp"
#include "URLFilter.hpp"
#include "TypeDetect.hpp"
#include "ModuleLoader.hpp"
#include "Logger.hpp"
#include "LuaVM.hpp"
#include <set>
#include <vector>
#include <list>
#ifndef _WIN32
#include <signal.h>
#include <unistd.h>
#else
#define WIN32_MEAN_AND_LEAN
#endif
using namespace std;
static bool term = false;
#ifndef _WIN32
static void terminate_func( int sig )
{
(void)sig;
term = true;
}
#else
BOOL WINAPI termHandler( DWORD ctrlType )
{
switch( ctrlType ){
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
term = true;
return TRUE;
default:
return FALSE;
}
}
#endif
#ifdef WITH_LUA
// TODO: should be in the laoding function of libcrawl
#include "tolua.h"
#include "URLLua.hpp"
#endif
static int counter = 0;
int main( int /* argc */, char *argv[] )
{
try {
LuaVM luaVm;
//Logger::instance( ).openConsoleLog( logDEBUG );
// load configuration (Lua) and execute main (to
// get basic configuration in form of global
// variables)
luaVm.loadSource( argv[1] );
luaVm.executeMain( );
std::string logLevel = luaVm.getString( "logger.level" );
Logger::instance( ).openConsoleLog( Logger::fromString( logLevel ) );
#ifndef _WIN32
struct sigaction sa;
memset( &sa, 0, sizeof( struct sigaction ) );
sa.sa_handler = terminate_func;
sa.sa_flags = SA_RESTART;
if( sigaction( SIGINT, &sa, NULL ) < 0 ) {
cerr << "Unable to install termianation signal handler" << endl;
}
#else
SetConsoleCtrlHandler( termHandler, TRUE );
#endif
// go through all type of modules and load them with the proper loader
string modulePath = luaVm.getString( "crawler.module_path" );
bool modulesSearchRecursive = luaVm.getBoolean( "crawler.modules_search_recursive" );
LOG( logNOTICE ) << "Loading modules from path '" << modulePath << "' "
<< ( modulesSearchRecursive ? "(recursive)" : "" );
vector<string> normalizerModules;
#ifndef _WIN32
normalizerModules.push_back( "./modules/urlnormalizer/simpleurl/mod_urlnormalizer_simple.so" );
normalizerModules.push_back( "./modules/urlnormalizer/googleurl/mod_urlnormalizer_googleurl.so" );
#else
normalizerModules.push_back( ".\\modules\\urlnormalizer\\simpleurl\\mod_urlnormalizer_simple.dll" );
normalizerModules.push_back( ".\\modules\\urlnormalizer\\googleurl\\mod_urlnormalizer_googleurl.dll" );
#endif
ModuleLoader<URLNormalizer> urlNormalizers( normalizerModules, CLOSE_DEFERRED, (void *)&luaVm );
#ifdef WITH_LUA
// TODO: should be in the laoding function of libcrawl
tolua_URL_open( luaVm.handle( ) );
#endif
// initialize crawler function
luaVm.executeFunction( "init" );
// perform a crawl step
luaVm.executeFunction( "crawl" );
// cleaning up
luaVm.executeFunction( "destroy" );
return 0;
vector<string> filterModules;
#ifndef _WIN32
filterModules.push_back( "./modules/urlfilter/protocol/mod_urlfilter_protocol.so" );
filterModules.push_back( "./modules/urlfilter/host/mod_urlfilter_host.so" );
#else
filterModules.push_back( ".\\modules\\urlfilter\\protocol\\mod_urlfilter_protocol.dll" );
filterModules.push_back( ".\\modules\\urlfilter\\host\\mod_urlfilter_host.dll" );
#endif
ModuleLoader<URLFilter, TYPELIST_1( const set<string> ) > urlFilters( filterModules );
vector<string> filterChainModules;
#ifndef _WIN32
filterChainModules.push_back( "./modules/urlfilter/chain/mod_urlfilter_chain.so" );
#else
filterChainModules.push_back( ".\\modules\\urlfilter\\chain\\mod_urlfilter_chain.dll" );
#endif
ModuleLoader<URLFilter, TYPELIST_1( const list<URLFilter *> ) > urlChainFilter( filterChainModules );
vector<string> frontierModules;
#ifndef _WIN32
frontierModules.push_back( "./modules/frontier/memory/mod_frontier_memory.so" );
#else
frontierModules.push_back( ".\\modules\\frontier\\memory\\mod_frontier_memory.dll" );
#endif
ModuleLoader<Frontier> frontiers( frontierModules );
vector<string> fetcherModules;
#ifndef _WIN32
fetcherModules.push_back( "./modules/fetcher/libfetch/mod_fetcher_libfetch.so" );
fetcherModules.push_back( "./modules/fetcher/libcurl/mod_fetcher_libcurl.so" );
#else
fetcherModules.push_back( ".\\modules\\fetcher\\winhttp\\mod_fetcher_winhttp.dll" );
#endif
ModuleLoader<Fetcher> fetchers( fetcherModules );
vector<string> urlseenModules;
#ifndef _WIN32
urlseenModules.push_back( "./modules/urlseen/memory/mod_urlseen_memory.so" );
#else
urlseenModules.push_back( ".\\modules\\urlseen\\memory\\mod_urlseen_memory.dll" );
#endif
ModuleLoader<URLSeen> urlSeens( urlseenModules );
vector<string> deduperModules;
#ifndef _WIN32
deduperModules.push_back( "./modules/deduper/null/mod_deduper_null.so" );
#else
deduperModules.push_back( ".\\modules\\deduper\\null\\mod_deduper_null.dll" );
#endif
ModuleLoader<Deduper> dedupers( deduperModules );
vector<string> processorModules;
#ifndef _WIN32
processorModules.push_back( "./modules/processor/htmllinkextract/mod_processor_htmllinkextract.so" );
processorModules.push_back( "./modules/processor/robotstxt/mod_processor_robotstxt.so" );
processorModules.push_back( "./modules/processor/sitemap/mod_processor_sitemap.so" );
#else
processorModules.push_back( ".\\modules\\processor\\htmllinkextract\\mod_processor_htmllinkextract.dll" );
processorModules.push_back( ".\\modules\\processor\\robotstxt\\mod_processor_robotstxt.dll" );
processorModules.push_back( ".\\modules\\processor\\sitemap\\mod_processor_sitemap.dll" );
#endif
ModuleLoader<Processor, TYPELIST_4( URLNormalizer *, Frontier *, URLFilter *, URLSeen * ) > processors( processorModules );
vector<string> typeDetectModules;
#ifndef _WIN32
typeDetectModules.push_back( "./modules/typedetect/libmagic/mod_typedetect_libmagic.so" );
#endif
ModuleLoader<TypeDetect> typeDetectors( typeDetectModules );
Frontier *frontier = frontiers.create( "memory_frontier" );
#ifndef _WIN32
// Fetcher *fetcher = fetchers.create( "libfetch_fetcher" );
Fetcher *fetcher = fetchers.create( "libcurl_fetcher" );
#else
Fetcher *fetcher = fetchers.create( "winhttp_fetcher" );
#endif
Deduper *deduper = dedupers.create( "null_deduper" );
URLSeen *urlSeen = urlSeens.create( "memory_urlseen" );
#ifndef _WIN32
TypeDetect *typeDetect = typeDetectors.create( "libmagic_typedetect" );
#endif
set<string> protocols;
protocols.insert( "http" );
protocols.insert( "https" );
URLFilter *protocolFilter = urlFilters.create( "protocol_urlfilter", protocols );
set<string> hosts;
hosts.insert( "andreasbaumann.dyndns.org" );
// hosts.insert( "www.andreasbaumann.cc" );
// hosts.insert( "relevancy.bger.ch" );
// hosts.insert( "wolframe.net" );
URLFilter *hostFilter = urlFilters.create( "host_urlfilter", hosts );
list<URLFilter *> filters;
filters.push_back( hostFilter );
filters.push_back( protocolFilter );
URLFilter *chainFilter = urlChainFilter.create( "chain_urlfilter", filters );
URLNormalizer *normalizer = urlNormalizers.create( "google_urlnormalizer" );
// URLNormalizer *normalizer = urlNormalizers.create( "simple_urlnormalizer" );
Processor *htmlParser = processors.create( "htmllinkextract_processor",
normalizer, frontier, chainFilter, urlSeen );
Processor *robotsTxtParser = processors.create( "robotstxt_processor",
normalizer, frontier, chainFilter, urlSeen );
Processor *sitemapParser = processors.create( "sitemap_processor",
normalizer, frontier, chainFilter, urlSeen );
LOG( logNOTICE ) << "Crawler started..";
// frontier->addUrl( normalizer->parseUrl( "http://www.andreasbaumann.cc" ) );
// frontier->addUrl( normalizer->parseUrl( "http://relevancy.bger.ch/robots.txt" ) );
// frontier->addUrl( normalizer->parseUrl( "http://wolframe.net" ) );
frontier->addUrl( normalizer->parseUrl( "http://andreasbaumann.dyndns.org/test/" ) );
URL url;
while( !term && ( url = frontier->getNextUrl( ) ) != URL::Null ) {
LOG( logINFO ) << "Got URL " << url;
RewindInputStream *s = fetcher->fetch( url );
if( !s->good( ) ) {
LOG( logERROR ) << "Fetching URL '" << url << "' failed!";
continue;
}
if( deduper->contentSeen( url, s ) ) {
LOG( logINFO ) << "URL " << url << " is a duplicate, content already seen";
delete s;
continue;
}
#ifndef _WIN32
MIMEType mimeType = typeDetect->detect( s );
if( mimeType != MIMEType::Null ) {
LOG( logDEBUG ) << "MIME type of '" << url << "' is '" << mimeType << "'";
if( mimeType == "text/html" ) {
s->rewind( );
htmlParser->process( s );
} else if( mimeType == "application/x-gzip" ) {
s->rewind( );
LOG( logINFO ) << "Storing archive " << url;
} else if( mimeType == "text/plain" ) {
if( url.path( ) == "/robots.txt" ) {
LOG( logINFO ) << "Checking " << url.path( );
s->rewind( );
robotsTxtParser->process( s );
}
} else if( mimeType == "application/xml" ) {
s->rewind( );
sitemapParser->process( s );
} else if( mimeType == "application/pdf" ) {
s->rewind( );
}
}
//~ sleep( 2 );
counter++;
if( counter > 0 ) {
term = true;
}
#else
htmlParser->process( s );
#endif
delete s;
}
processors.destroy( sitemapParser );
processors.destroy( robotsTxtParser );
processors.destroy( htmlParser );
urlNormalizers.destroy( normalizer );
urlChainFilter.destroy( chainFilter );
urlFilters.destroy( protocolFilter );
urlFilters.destroy( hostFilter );
#ifndef _WIN32
typeDetectors.destroy( typeDetect );
#endif
urlSeens.destroy( urlSeen );
dedupers.destroy( deduper );
fetchers.destroy( fetcher );
frontiers.destroy( frontier );
LOG( logNOTICE ) << "Crawler stopped.. normal shutdown..";
} catch( exception &e ) {
LOG( logFATAL ) << "Crawler stopped: " << e.what( );
return 1;
} catch( ... ) {
LOG( logFATAL ) << "Crawler stopped due to unknown exception!";
return 1;
}
}
|