Skip to content

Commit 529e5e6

Browse files
熊长江熊长江
authored andcommitted
ip资源池,定时更新
1 parent ca463fe commit 529e5e6

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

db/db.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const mongoose = require('mongoose');
2+
3+
mongoose.connect('mongodb://localhost:27017/test');
4+
5+
let db = mongoose.connection;
6+
7+
db.on('error', console.error.bind(console, 'mongoDB 链接错误:'));
8+
9+
db.once('open', function(callback){
10+
console.log('MongoDB 打开!');
11+
});
12+
13+
module.exports = mongoose;

db/models_ip.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let mongoose = require('./db.js');
2+
3+
let ipSchema = mongoose.Schema({
4+
country: {type: String},
5+
ip: {type: String},
6+
port: {type: String},
7+
area: {type: String},
8+
types: {type: String},
9+
protocol: {type: String},
10+
speed: {type: String},
11+
time: {type: String},
12+
});
13+
14+
let ipModel = mongoose.model('ipSchema',ipSchema);
15+
16+
module.exports = ipModel;
17+
18+
19+
// let ipOne = new ipModel({name: 'fff'});
20+
21+
// ipOne.save(function(err){
22+
// if(err) return console.error(err);
23+
// console.log('写入成功!');
24+
25+
// ipModel.find(function(err, docs){
26+
// if(err) return console.error(err);
27+
// console.log(docs);
28+
// });
29+
// });
30+

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"dependencies": {
2020
"cheerio": "^1.0.0-rc.2",
2121
"express": "^4.15.3",
22+
"mongoose": "^4.11.3",
23+
"node-schedule": "^1.2.3",
2224
"request": "^2.81.0"
2325
}
2426
}

proxy.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const http = require('http');
22
const fs = require('fs');
3+
34
let request = require('request');
45
let cheerio = require('cheerio');
6+
let schedule = require('node-schedule');
7+
8+
let ipModel = require('./db/models_ip');
59

610
let ipListURL = 'http://www.xicidaili.com/nn/';
711
let pageSize = 0;
@@ -18,6 +22,11 @@ function startRequest(ipListURL) {
1822
});
1923

2024
res.on('end', function () {
25+
ipModel.remove({}, function(err){
26+
if(err) return console.error(err);
27+
console.log('清空数据');
28+
});
29+
2130
pageSize++;
2231
$ = cheerio.load(html);
2332

@@ -31,11 +40,34 @@ function startRequest(ipListURL) {
3140
let testip = request.defaults({ 'proxy': proxy }).get('http://ip.chinaz.com/getip.aspx', { timeout: 3000 }, function (err, response, body) {
3241
if (body && body.substring(0, 4) == '{ip:') {
3342
proxy = proxy + '\n';
34-
fs.appendFile('./data/' + 'ipList' + '.txt', proxy, 'utf-8', function (err) {
35-
if (err) {
36-
console.log(err);
37-
}
43+
44+
let ipOne = new ipModel({
45+
country: td.eq(0).find('img').attr('alt'),
46+
ip: td.eq(1).text(),
47+
port: td.eq(2).text(),
48+
area: td.eq(3).find('a').text(),
49+
types: td.eq(4).text(),
50+
protocol: td.eq(5).text(),
51+
speed: td.eq(6).find('.bar').attr('title'),
52+
time: td.eq(7).find('.bar').attr('title'),
3853
});
54+
55+
ipOne.save(function(err){
56+
if(err) return console.error(err);
57+
console.log('写入成功!');
58+
59+
ipModel.find(function(err, docs){
60+
if(err) return console.error(err);
61+
console.log(docs);
62+
});
63+
});
64+
65+
66+
// fs.appendFile('./data/' + 'ipList' + '.txt', proxy, 'utf-8', function (err) {
67+
// if (err) {
68+
// console.log(err);
69+
// }
70+
// });
3971
}
4072

4173
// console.log(body);
@@ -71,6 +103,9 @@ function startRequest(ipListURL) {
71103
});
72104
});
73105
}
74-
startRequest(ipListURL);
106+
var everyDay = schedule.scheduleJob('0 0 0 * * ?', function(){
107+
console.log('everyDay 0:00:00');
108+
startRequest(ipListURL);
109+
});
75110

76111
module.exports = ipListArray;

0 commit comments

Comments
 (0)