DHT搜索
本文地址:http://tongxinmao.com/Article/Detail/id/196
'use strict';
var fs = require('fs');
var path = require('path');
var bencode = require('bencode');
var P2PSpider = require('./lib');
var db = require('./lib/db');
var p2p = P2PSpider({
nodesMaxSize: 400,
maxConnections: 800,
timeout: 10000
});
p2p.ignore(function (infohash, rinfo, callback) {
var torrentFilePathSaveTo = path.join(__dirname, "torrents", infohash + ".torrent");
fs.exists(torrentFilePathSaveTo, function(exists) {
callback(exists); //if is not exists, download the metadata.
});
});
p2p.on('metadata', function (metadata) {
//
//
var record={};
if(metadata.info.hasOwnProperty("name.utf-8"))
{
console.log(metadata);
record.name=metadata.info['name.utf-8'];
}else
{
record.name=metadata.info.name
}
if(metadata.info.hasOwnProperty('files'))
{
record.filenum=metadata.info.files.length;
}else
{
record.filenum=1;
}
record.infohash=metadata.infohash;
record.address=metadata.address;
if(metadata.info.length)//.hasOwnProperty("key") // true
{
record.length=metadata.info.length;
}
else
{
record.length=metadata.info['piece length']*(metadata.info.pieces.length/20);
}
var userAddSql = 'INSERT INTO bt2(name,keywords,filenum,length,infohash,address) VALUES(?,?,?,?,?,?)';
var userAddSql_Params = [record.name, '', record.filenum,record.length,record.infohash,record.address];
db.query(userAddSql,userAddSql_Params,function (err, rows, fields) {
if(err){
console.log('[INSERT ERROR] - ',err.message);
return;
}
console.log('-------INSERT----------');
});
var torrentFilePathSaveTo = path.join(__dirname, "torrents", metadata.infohash + ".torrent");
fs.writeFile(torrentFilePathSaveTo, bencode.encode({'info': metadata.info}), function(err) {
if (err) {
return console.error(err);
}
console.log(metadata.info.name + ".torrent has saved.\n");
});
});
p2p.listen(6881, '0.0.0.0');
var db = {};
var mysql = require('mysql');
var conn = mysql.createConnection({
host : 'localhost',
user : 'xx',
password : 'xx',
database : 'xx'
});
conn.connect();
db.query = function(sql,para, callback){
if (!sql) {
callback();
return;
}
conn.query(sql,para, function(err, rows, fields) {
if (err) {
console.log(err);
callback(err, null);
return;
};
callback(null, rows, fields);
});
}
module.exports = db;
上一篇:txm2017产品规划
下一篇:mingw32 mingw64 msys cygwin TDM-GCC