forked from LitoMore/bingfan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.js
More file actions
175 lines (164 loc) · 3.96 KB
/
crawler.js
File metadata and controls
175 lines (164 loc) · 3.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
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
'use strict';
const http = require('http');
const qs = require('querystring');
const async = require('async');
const fs = require('fs');
const MSS = require('mss-sdk');
const crypto = require('crypto');
const {
LOCAL_PATH
} = require('./config');
// Bing API
const options_1 = {
method: 'GET',
hostname: 'cn.bing.com',
port: 80,
path: '/HPImageArchive.aspx?' + qs.stringify({
format: 'js',
idx: 0,
n: 1,
mkt: 'zh-CN'
})
};
const bing_image = {
url: null,
filename: null,
data: null,
content: null,
md5: null,
};
const mss_config = {
s3: null
};
exports.crawl = (callback) => {
async.parallel({
// Fetch bing image
img: (callback) => {
getImg(callback);
},
// Fetch config
config: (callback) => {
getConfig(callback);
}
}, (request_err, request_result) => {
if (request_err) {
console.log(request_err);
} else {
console.log('Requesting')
bing_image.url = request_result.img.url;
bing_image.filename = request_result.img.fullstartdate + '.jpg';
async.parallel({
// Download image
download: (callback) => {
console.log('Downloading')
downloadImage(callback);
},
// Prepare local dir
local_dir: (callback) => {
console.log('Preparing')
initLocalDir(callback);
}
}, (crawler_err, crawler_result) => {
bing_image.data = crawler_result.download;
if (crawler_err) {
console.log(crawler_err)
} else {
async.parallel({
// Save file to local
save: (callback) => {
console.log('Saving')
localSave(callback);
},
}, (save_err, save_result) => {
if (save_err) {
console.log(save_err)
} else {
console.log('Naming')
bing_image.md5 = save_result.save;
bing_image.content = request_result.img.copyright;
callback(LOCAL_PATH, bing_image.filename, bing_image.content);
}
});
}
});
}
});
};
// Fetch image
function getImg(callback) {
http.request(options_1, (res) => {
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
callback(null, JSON.parse(data).images[0]);
});
}).on('error', (e) => {
console.log('Error: ' + e.message);
}).end();
}
// Fetch config
function getConfig(callback) {
fs.readFile(__dirname + '/config.json', 'utf-8', (read_err, read_result) => {
if (!read_err) {
callback(null, JSON.parse(read_result));
}
});
}
// Download image
function downloadImage(callback) {
const download_option = {
method: 'GET',
hostname: 'cn.bing.com',
port: 80,
path: bing_image.url
};
http.request(download_option, (res) => {
let data = '';
res.setEncoding('binary');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
callback(null, data);
});
}).on('error', (e) => {
console.log('Error: ' + e.message);
}).end();
}
// Prepare local dir
function initLocalDir(callback) {
fs.exists(__dirname + LOCAL_PATH, (exist) => {
if (!exist) {
fs.mkdir(__dirname + LOCAL_PATH, (mkdir_err, mkdir_result) => {
if (!mkdir_err) {
callback(null, mkdir_result);
}
});
} else {
callback(null, exist);
}
});
}
// Save file to local
function localSave(callback) {
fs.writeFile(
__dirname + LOCAL_PATH + bing_image.filename,
bing_image.data,
'binary',
(fs_err) => {
if (!fs_err) {
// Get file MD5 hash
const rs = fs.createReadStream(__dirname + LOCAL_PATH + bing_image.filename);
const hash = crypto.createHash('md5');
rs.on('data', hash.update.bind(hash));
rs.on('end', () => {
console.log('Save end')
callback(null, hash.digest('hex'));
});
}
}
);
}