-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestGet.js
More file actions
135 lines (127 loc) · 3.99 KB
/
Copy pathtestGet.js
File metadata and controls
135 lines (127 loc) · 3.99 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
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
var url1 = 'http://www.qingdi.com/book/';
var qingdiId = process.argv[2];
var crawler = require('crawler').Crawler;
var model = require('./lib/model.js');
var tools = require('./lib/tools.js');
var bookDao = require('./testDao.js').bookDao;
var articleDao = require('./testDao.js').articleDao;
if(qingdiId){
getBook(url1+qingdiId+'.html');
}else{
console.log('please input qingdi book id');
process.exit(0);
}
//setDirectory();
//getDir();
//downloadArt(1);
function downloadArt(book_id) {
articleDao.getUnDownload(book_id, function (err, arts) {
if (err) {
console.error(err)
process.exit(0);
} else {
console.log(arts.length);
arts.forEach(function(art){
doDonwload(art.refer,art);
})
}
})
}
function doDonwload(refer, art) {
new crawler({
"forceUTF8": true,
"callback": function(err, result, $) {
if (err) {
console.error(err);
}
else {
art.title = $('#h1').text();
art.content = $('#content').html();
art.download = true;
articleDao.updateArticle(art, function(err, artc) {
!err || console.log('update ' + err);
console.log(artc._id + ":download ok");
});
}
}
}).queue(refer);
}
function getDir() {
articleDao.getDirectory(1, function (err, cc) {
console.log(err);
console.log(cc);
})
}
function getBook(url) {
new crawler({
"forceUTF8": true,
"callback": makeBook
}).queue(url);
}
function makeBook(err, result, $) {
if (err) {
console.error(err);
}
else {
var book = tools.union(model.Book);
var pic = $('.articleInfoPic');
book.cover = pic.attr('src');
book.title = pic.attr('title').split(':')[1];
book.tagsList.push(pic.attr('title').split(':')[0]);
book.description = $('.warp').text();
book.author = $('meta[name=author]').attr("content");
book.dirpath = $('.reader').attr("href");
book.date = new Date();
var href = result.request.href;
book.siteid = href.slice(href.lastIndexOf('/'), href.lastIndexOf('.'));
bookDao.addNewBook(book, function(err, b) {
if (err) {
console.error(err);
}
else {
console.log(b.title + ":insert ok");
book._id = b._id;
setDirectory(book);
}
});
}
}
function setDirectory(book) {
new crawler({
"forceUTF8": true,
"callback": function (err, result, $) {
if (err) {
console.error(err);
} else {
var stop = false;
var dic = $('.ccss');
console.log(dic.length);
dic.each(function (index) {
var art = tools.union(model.Article);
art.title = $(this).children('a').text();
if (art.title == '') {
return;
}
art.book_id = book._id;
art.date = new Date();
art.index = index;
art._id = art.book_id + '_' + index;
var href = result.request.href;
art.refer = href.slice(0, href.lastIndexOf('/') + 1) + $(this).children('a').attr('href');
articleDao.insert(art, function (err) {
if(err){
console.error(err);
stop = true;
}else{
console.log(art.index +':insert ok');
}
});
if(stop == true){
return false;
}
});
downloadArt(book._id);
}
}
}).queue(book.dirpath);
}