torrents_controller.js - 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
       ---
       torrents_controller.js (3614B)
       ---
            1 'use strict';
            2 
            3 const Torrent = require('../models/torrent_model');
            4 const async = require('async');
            5 
            6 function getCount(callback) { 
            7 
            8   Torrent.aggregate([
            9       { $group: {
           10           _id: null,
           11           count: { $sum: 1 },
           12           total_seeders: { $sum: "$seeders"  },
           13           total_leechers: { $sum: "$leechers"  }
           14       }}
           15   ], function (err, result) {
           16       if (err) {
           17           callback(err,null);
           18       } else {
           19         callback(null, result[0]);
           20       }
           21   });
           22   
           23 }
           24 
           25 function getTorrent(query, callback) {
           26   Torrent.find(query, function(err, data) {
           27     if (err) {
           28       callback(err, null);
           29     } else {
           30       callback(null, data[0]);
           31     }
           32   });
           33 }
           34 
           35 function saveTorrent(query, data, callback) {
           36   Torrent.findOneAndUpdate(query, data, function(err, data) {
           37     if (err) {
           38       callback(err, null);
           39     } else {
           40       callback(null, data[0]);
           41     }
           42   });
           43 }
           44 
           45 function updateTorrent(torrent, callback) {
           46 
           47   const webtorrentHealth = require('webtorrent-health');
           48 
           49   webtorrentHealth(torrent.magneturi, {trackers: torrent.announce}, function (err, data) {
           50     if (err) {
           51       callback(err, null);
           52     } else {
           53       let newData = { 
           54         seeders: data.seeds,
           55         leechers: data.peers,
           56         ratio: (Math.round((data.peers > 0 ? data.seeds / data.peers : data.seeds) +'e+2') + 'e-2')
           57       };
           58       callback(null, newData);
           59     }
           60   });
           61 }
           62 
           63 exports.download = function(req, res) {
           64 
           65   const torrentFolder = './data/torrents/';
           66 
           67   async.waterfall([
           68     function(callback){
           69       getTorrent({hash: req.params.id }, function(err, torrent) {
           70         if (err) callback(true);
           71 
           72         if (!torrent)
           73           callback("404");
           74         else
           75           callback(null, torrent);
           76       });
           77     }], function (err, data) {
           78 
           79       if (err === "404")
           80         res.sendStatus(404);
           81       else if (err)
           82         res.sendStatus(500);
           83       else
           84         res.download(torrentFolder + data.name + '.torrent');
           85     }
           86   );
           87 };
           88 
           89 exports.index = function(req, res) {
           90   async.parallel(
           91     {
           92       torrent_data: function(callback) {
           93         Torrent.find({},'hash name seeders leechers comment magneturi ', callback);
           94       }, 
           95       torrent_seeds: function(callback) {
           96         getCount(callback);
           97       }
           98     },
           99     function(err, results) {
          100       res.render('torrent', { title: 'Index Page', data: results });
          101     }
          102   );
          103 };
          104 
          105 exports.detail = function(req, res) {
          106   async.parallel(
          107     {
          108       torrent_data: function(callback) {
          109         Torrent.find({ hash: req.params.id }, callback);
          110       },
          111       torrent_seeds: function(callback) {
          112         getCount(callback);
          113       }
          114     },
          115     function(err, results) {
          116       if (results.torrent_data === '') {
          117         res.sendStatus(404);
          118       } else {
          119         res.render('details', { title: 'Torrent Details', data: results });
          120       }
          121     }
          122   );
          123 };
          124 
          125 exports.update = function(req, res) {
          126 
          127   async.waterfall([
          128     function(callback){
          129       getTorrent({hash: req.params.id }, function(err, torrent) {
          130         if (err) callback(true);
          131 
          132         if (!torrent)
          133           callback("404");
          134         else
          135           callback(null, torrent);
          136       });
          137     },
          138     function(arg1, callback){
          139       updateTorrent(arg1, function(err, data) { 
          140         if (err) callback(true);
          141 
          142         callback(null, data);
          143 
          144       });
          145     },
          146     function(arg1, callback){
          147       saveTorrent({hash: req.params.id }, arg1, function(err, data) { 
          148         if (err) callback(true);
          149 
          150         callback(null, data);
          151 
          152       });
          153     }], function (err) {
          154 
          155       if (err === "404")
          156         res.sendStatus(404);
          157       else if (err)
          158         res.sendStatus(500);
          159       else
          160         res.redirect('/details/' + req.params.id);
          161     }
          162   );
          163