-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
94 lines (62 loc) · 1.92 KB
/
app.js
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
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var functions = require('./lib/functions');
var config = require('./config.json');
/**
* pool relates to the parallel http request pool size
* queue refers to the number of http requests active now.
*/
var pool = parseInt(config.req_pool);
var queue = 0;
var express = require('express');
var app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(config.application_port);
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
/**
* Scrapper application dashboard
*/
app.get('/', function(req, res){
res.render('index', { port:config.application_port });
});
MongoClient.connect(config.mongodb, function(err, db) {
var collection = db.collection(config.mongocollection);
var ProcessHtmlResponse = function(result) {
if(++queue <= pool) {
functions.GetHtmlData(ProcessHtmlResponse);
}
var record = functions.ParseHtml(result);
/**
* feed to dashboard
*/
io.sockets.volatile.emit('movie_feed', record);
collection.insert(record, function(err, docs) {
if(typeof(record.image)!="undefined") {
functions.GetImageByUrl(record.image,function(data){
var gs = new mongodb.GridStore(db, result.id+".jpg", "w", {
"content_type": "image/jpg",
"metadata":{
"author": "author"
},
"chunk_size": 1024*4
});
gs.open(function(err, gs){
gs.write(data, function(){
--queue;
return functions.GetHtmlData(ProcessHtmlResponse);
})
});
});
} else {
--queue;
return functions.GetHtmlData(ProcessHtmlResponse);
}
});
}
functions.GetHtmlData(ProcessHtmlResponse);
});