update - seedlinux - Torrent indexing tool opensource torrents with share ratio's etc.
 (HTM) git clone git://jay.scot/seedlinux
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       update (2441B)
       ---
            1 #!/usr/bin/env node
            2 'use strict';
            3 
            4 console.log('Updating Seeder and Leecher information');
            5 
            6 const async = require('async');
            7 const mongoose = require('mongoose');
            8 const webtorrentHealth = require('webtorrent-health');
            9 const config = require('../app/config');
           10 
           11 const mongoDB = config.dbURI;
           12 const Torrent = require('../models/torrent_model');
           13 
           14 mongoose.connect(mongoDB);
           15 
           16 var db = mongoose.connection;
           17 
           18 db.on('error', console.error.bind(console, 'MongoDB connection error:'));
           19 
           20 function updateTorrent(torrent, callback) {
           21   webtorrentHealth(torrent.magneturi, {trackers: torrent.announce}, function (err, data) {
           22     if (err) {
           23       callback(err, null);
           24     } else {
           25       let newData = { 
           26         seeders: data.seeds,
           27         leechers: data.peers,
           28         ratio: (Math.round((data.peers > 0 ? data.seeds / data.peers : data.seeds) +'e+2') + 'e-2')
           29       };
           30       callback(null, newData);
           31     }
           32   });
           33 }
           34 
           35 function getTorrent(query, callback) {
           36   Torrent.find(query, function(err, data) {
           37     if (err) {
           38       callback(err, null);
           39     } else {
           40       callback(null, data[0]);
           41     }
           42   });
           43 }
           44 
           45 function saveTorrent(query, data, callback) {
           46   Torrent.findOneAndUpdate(query, data, function(err, data) {
           47     if (err) {
           48       callback(err, null);
           49     } else {
           50       callback(null, data[0]);
           51     }
           52   });
           53 }
           54 
           55 Torrent.find({}, function(err, data) { 
           56   if (err) console.log(err); 
           57 
           58   async.eachSeries(data, function(file, callback) {
           59     async.waterfall([
           60       function(callback){
           61         getTorrent({hash: file.hash }, function(err, torrent) {
           62           if (err) callback(true);
           63 
           64           if (!torrent)
           65             callback(true);
           66           else
           67             callback(null, torrent);
           68         });
           69       },
           70       function(arg1, callback){
           71         updateTorrent(arg1, function(err, data) { 
           72           if (err) callback(true);
           73 
           74           callback(null, data);
           75 
           76         });
           77       },
           78       function(arg1, callback){
           79         saveTorrent({hash: file.hash }, arg1, function(err, data) { 
           80           if (err) callback(true);
           81 
           82           console.log("Saved - " + file.hash);
           83           callback(null, data);
           84 
           85         });
           86       }], function (err) {
           87 
           88         if (err)
           89           throw err; 
           90 
           91         callback();
           92       }
           93     );
           94   }, function(err) {
           95       if( err ) {
           96         console.log('A torrent failed to process');
           97         process.exit(1);
           98       } else {
           99         console.log('All torrents have been processed successfully');
          100         process.exit();
          101       }
          102   });
          103 });
          104