refactor, update peer info script - seedlinux - Torrent indexing tool opensource torrents with share ratio's etc.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit a2167567291134b9a3c1a776b8b02fc1fc7c8f3a
(DIR) parent bba945255f1eebcd6686db701a5c00fca5e9879e
(HTM) Author: Jay Scott <jay@jayscott.co.uk>
Date: Sun, 2 Jul 2017 21:40:10 +0100
refactor, update peer info script
I have done a bit of refactoring after reading up on the best
wants to handle async callbacks. I have also updated the script
that will run hourly to get the latest data. It can still be
refactored more to use the functions from the torrents controller
for example.
Diffstat:
M app.js | 1 -
A bin/update | 102 +++++++++++++++++++++++++++++++
M controllers/torrents_controller.js | 79 +++++++++++++++++++++----------
M package-lock.json | 173 ++++++++++++++++++++++++++++++-
M package.json | 5 +++--
5 files changed, 328 insertions(+), 32 deletions(-)
---
(DIR) diff --git a/app.js b/app.js
@@ -2,7 +2,6 @@
const express = require('express');
const path = require('path');
-const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
(DIR) diff --git a/bin/update b/bin/update
@@ -0,0 +1,102 @@
+#!/usr/bin/env node
+'use strict';
+
+console.log('Updating Seeder and Leecher information');
+
+const async = require('async');
+const mongoose = require('mongoose');
+const webtorrentHealth = require('webtorrent-health');
+const mongoDB = process.env.DB_URI;
+const Torrent = require('../models/torrent_model');
+
+mongoose.connect(mongoDB);
+
+var db = mongoose.connection;
+
+db.on('error', console.error.bind(console, 'MongoDB connection error:'));
+
+function updateTorrent(torrent, callback) {
+ webtorrentHealth(torrent.magneturi, {trackers: torrent.announce}, function (err, data) {
+ if (err) {
+ callback(err, null);
+ } else {
+ let newData = {
+ seeders: data.seeds,
+ leechers: data.peers,
+ ratio: (Math.round((data.peers > 0 ? data.seeds / data.peers : data.seeds) +'e+2') + 'e-2')
+ };
+ callback(null, newData);
+ }
+ });
+}
+
+function getTorrent(query, callback) {
+ Torrent.find(query, function(err, data) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, data[0]);
+ }
+ });
+}
+
+function saveTorrent(query, data, callback) {
+ Torrent.findOneAndUpdate(query, data, function(err, data) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, data[0]);
+ }
+ });
+}
+
+Torrent.find({}, function(err, data) {
+ if (err) console.log(err);
+
+ async.eachSeries(data, function(file, callback) {
+ async.waterfall([
+ function(callback){
+ getTorrent({hash: file.hash }, function(err, torrent) {
+ if (err) callback(true);
+
+ if (!torrent)
+ callback(true);
+ else
+ callback(null, torrent);
+ });
+ },
+ function(arg1, callback){
+ updateTorrent(arg1, function(err, data) {
+ if (err) callback(true);
+
+ callback(null, data);
+
+ });
+ },
+ function(arg1, callback){
+ saveTorrent({hash: file.hash }, arg1, function(err, data) {
+ if (err) callback(true);
+
+ console.log("Saved - " + file.hash);
+ callback(null, data);
+
+ });
+ }], function (err) {
+
+ if (err)
+ throw err;
+
+ callback();
+ }
+ );
+ }, function(err) {
+ if( err ) {
+ console.log('A torrent failed to process');
+ process.exit(1);
+ } else {
+ console.log('All torrents have been processed successfully');
+ process.exit();
+ }
+ });
+});
+
(DIR) diff --git a/controllers/torrents_controller.js b/controllers/torrents_controller.js
@@ -11,9 +11,9 @@ function getTorrent(query, callback) {
callback(null, data[0]);
}
});
-};
+}
-function updateTorrent(query, data, callback) {
+function saveTorrent(query, data, callback) {
Torrent.findOneAndUpdate(query, data, function(err, data) {
if (err) {
callback(err, null);
@@ -21,7 +21,25 @@ function updateTorrent(query, data, callback) {
callback(null, data[0]);
}
});
-};
+}
+
+function updateTorrent(torrent, callback) {
+
+ const webtorrentHealth = require('webtorrent-health');
+
+ webtorrentHealth(torrent.magneturi, {trackers: torrent.announce}, function (err, data) {
+ if (err) {
+ callback(err, null);
+ } else {
+ let newData = {
+ seeders: data.seeds,
+ leechers: data.peers,
+ ratio: (Math.round((data.peers > 0 ? data.seeds / data.peers : data.seeds) +'e+2') + 'e-2')
+ };
+ callback(null, newData);
+ }
+ });
+}
exports.index = function(req, res) {
async.parallel(
@@ -50,7 +68,7 @@ exports.torrent_detail = function(req, res) {
}
},
function(err, results) {
- if (results.torrent_data == '') {
+ if (results.torrent_data === '') {
res.sendStatus(404);
} else {
res.render('details', { title: 'Torrent Details', data: results });
@@ -61,29 +79,41 @@ exports.torrent_detail = function(req, res) {
exports.torrent_update = function(req, res) {
- const webtorrentHealth = require('webtorrent-health');
+ async.waterfall([
+ function(callback){
+ getTorrent({hash: req.params.id }, function(err, torrent) {
+ if (err) callback(true);
- getTorrent({hash: req.params.id }, function(err, torrent) {
- if (err) res.sendStatus(404);
+ if (!torrent)
+ callback("404");
+ else
+ callback(null, torrent);
+ });
+ },
+ function(arg1, callback){
+ updateTorrent(arg1, function(err, data) {
+ if (err) callback(true);
+
+ callback(null, data);
- if (torrent == null) {
- res.sendStatus(404);
- } else {
- webtorrentHealth(torrent.magneturi, {trackers: torrent.announce}, function (err, data) {
- if (err) res.sendStatus(500);
+ });
+ },
+ function(arg1, callback){
+ saveTorrent({hash: req.params.id }, arg1, function(err, data) {
+ if (err) callback(true);
- let newData = {
- seeders: data.seeds,
- leechers: data.peers,
- ratio: Math.round((data.peers > 0 ? data.seeds / data.peers : data.seeds) +'e+2') + 'e-2'
- }
-
- updateTorrent({hash: req.params.id}, newData, function(err, user) {
- if (err) res.sendStatus(500);
+ callback(null, data);
+ });
+ }], function (err, result) {
+
+ if (err === "404")
+ res.sendStatus(404);
+ else if (err)
+ res.sendStatus(500);
+ else
res.redirect('/torrent/details/' + req.params.id);
- });
- })
+
}
- });
-}
+ );
+};
+\ No newline at end of file
(DIR) diff --git a/package-lock.json b/package-lock.json
@@ -77,6 +77,12 @@
"resolved": "https://registry.npmjs.org/async/-/async-2.5.0.tgz",
"integrity": "sha512-e+lJAJeNWuPCNyxZKOBdaJGyLGHugXVQtrAwtuAe2vhxTYxFTKE73p8JuTmdH0qdQZtDvI4dhJwjZc5zsfIsYw=="
},
+ "balanced-match": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
+ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
+ "dev": true
+ },
"basic-auth": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-1.1.0.tgz",
@@ -143,6 +149,12 @@
}
}
},
+ "brace-expansion": {
+ "version": "1.1.8",
+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz",
+ "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=",
+ "dev": true
+ },
"bson": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/bson/-/bson-1.0.4.tgz",
@@ -190,6 +202,12 @@
"resolved": "https://registry.npmjs.org/clean-css/-/clean-css-3.4.27.tgz",
"integrity": "sha1-re91sxwWD/pdcvTeZ5ZuJmDBolU="
},
+ "cli": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/cli/-/cli-1.0.1.tgz",
+ "integrity": "sha1-IoF1NPJL+klQw01TLUjsvGIbjBQ=",
+ "dev": true
+ },
"cliui": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-2.1.0.tgz",
@@ -210,6 +228,18 @@
"resolved": "https://registry.npmjs.org/compact2string/-/compact2string-1.4.0.tgz",
"integrity": "sha1-qZzZbqAAUlaEsmloOuIiLW7qe0k="
},
+ "concat-map": {
+ "version": "0.0.1",
+ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
+ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
+ "dev": true
+ },
+ "console-browserify": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz",
+ "integrity": "sha1-8CQcRXMKn8YyOyBtvzjtx0HQuxA=",
+ "dev": true
+ },
"console-control-strings": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz",
@@ -250,6 +280,12 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
+ "date-now": {
+ "version": "0.1.4",
+ "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz",
+ "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=",
+ "dev": true
+ },
"debug": {
"version": "2.6.8",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz",
@@ -287,6 +323,44 @@
"resolved": "https://registry.npmjs.org/doctypes/-/doctypes-1.1.0.tgz",
"integrity": "sha1-6oCxBqh1OHdOijpKWv4pPeSJ4Kk="
},
+ "dom-serializer": {
+ "version": "0.1.0",
+ "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.1.0.tgz",
+ "integrity": "sha1-BzxpdUbOB4DOI75KKOKT5AvDDII=",
+ "dev": true,
+ "dependencies": {
+ "domelementtype": {
+ "version": "1.1.3",
+ "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.1.3.tgz",
+ "integrity": "sha1-vSh3PiZCiBrsUVRJJCmcXNgiGFs=",
+ "dev": true
+ },
+ "entities": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.1.tgz",
+ "integrity": "sha1-blwtClYhtdra7O+AuQ7ftc13cvA=",
+ "dev": true
+ }
+ }
+ },
+ "domelementtype": {
+ "version": "1.3.0",
+ "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.0.tgz",
+ "integrity": "sha1-sXrtguirWeUt2cGbF1bg/BhyBMI=",
+ "dev": true
+ },
+ "domhandler": {
+ "version": "2.3.0",
+ "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.3.0.tgz",
+ "integrity": "sha1-LeWaCCLVAn+r/28DLCsloqir5zg=",
+ "dev": true
+ },
+ "domutils": {
+ "version": "1.5.1",
+ "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.5.1.tgz",
+ "integrity": "sha1-3NhIiib1Y9YQeeSMn3t+Mjc2gs8=",
+ "dev": true
+ },
"ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -302,6 +376,12 @@
"resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.0.tgz",
"integrity": "sha1-epDYM+/abPpurA9JSduw+tOmMgY="
},
+ "entities": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-1.0.0.tgz",
+ "integrity": "sha1-sph6o4ITR/zeZCsk/fyeT7cSvyY=",
+ "dev": true
+ },
"es6-promise": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.2.1.tgz",
@@ -317,6 +397,12 @@
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.0.tgz",
"integrity": "sha1-b2Ma7zNtbEY2K1F2QETOIWvjwFE="
},
+ "exit": {
+ "version": "0.1.2",
+ "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz",
+ "integrity": "sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=",
+ "dev": true
+ },
"expand-template": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/expand-template/-/expand-template-1.0.3.tgz",
@@ -362,6 +448,12 @@
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.0.tgz",
"integrity": "sha1-9HTKXmqSRtb9jglTz6m5yAWvp44="
},
+ "fs.realpath": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
+ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
+ "dev": true
+ },
"function-bind": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.0.tgz",
@@ -389,6 +481,12 @@
"integrity": "sha1-l/tdlr/eiXMxPyDoKI75oWf6ZM4=",
"optional": true
},
+ "glob": {
+ "version": "7.1.2",
+ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
+ "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
+ "dev": true
+ },
"graceful-readlink": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz",
@@ -410,6 +508,32 @@
"resolved": "https://registry.npmjs.org/hooks-fixed/-/hooks-fixed-2.0.0.tgz",
"integrity": "sha1-oB2JTVKsf2WZu7H2PfycQR33DLo="
},
+ "htmlparser2": {
+ "version": "3.8.3",
+ "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.8.3.tgz",
+ "integrity": "sha1-mWwosZFRaovoZQGn15dX5ccMEGg=",
+ "dev": true,
+ "dependencies": {
+ "isarray": {
+ "version": "0.0.1",
+ "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
+ "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=",
+ "dev": true
+ },
+ "readable-stream": {
+ "version": "1.1.14",
+ "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
+ "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=",
+ "dev": true
+ },
+ "string_decoder": {
+ "version": "0.10.31",
+ "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
+ "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=",
+ "dev": true
+ }
+ }
+ },
"http-errors": {
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.1.tgz",
@@ -420,6 +544,12 @@
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.15.tgz",
"integrity": "sha1-/iZaIYrGpXz+hUkn6dBMGYJe3es="
},
+ "inflight": {
+ "version": "1.0.6",
+ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
+ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
+ "dev": true
+ },
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
@@ -476,6 +606,26 @@
"resolved": "https://registry.npmjs.org/js-stringify/-/js-stringify-1.0.2.tgz",
"integrity": "sha1-Fzb939lyTyijaCrcYjCufk6Weds="
},
+ "jshint": {
+ "version": "2.9.5",
+ "resolved": "https://registry.npmjs.org/jshint/-/jshint-2.9.5.tgz",
+ "integrity": "sha1-HnJSkVzmgbQIJ+4UJIxG006apiw=",
+ "dev": true,
+ "dependencies": {
+ "lodash": {
+ "version": "3.7.0",
+ "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.7.0.tgz",
+ "integrity": "sha1-Nni9irmVBXwHreg27S7wh9qBHUU=",
+ "dev": true
+ },
+ "strip-json-comments": {
+ "version": "1.0.4",
+ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-1.0.4.tgz",
+ "integrity": "sha1-HhX7ysl9Pumb8tc7TGVrCCu6+5E=",
+ "dev": true
+ }
+ }
+ },
"jstransformer": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/jstransformer/-/jstransformer-1.0.0.tgz",
@@ -551,6 +701,12 @@
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.15.tgz",
"integrity": "sha1-pOv1BkCUVpI3uM9wBGd20J/JKu0="
},
+ "minimatch": {
+ "version": "3.0.4",
+ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
+ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
+ "dev": true
+ },
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
@@ -702,6 +858,12 @@
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.1.tgz",
"integrity": "sha1-yKuMkiO6NIiKpkopeyiFO+wY2lY="
},
+ "path-is-absolute": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
+ "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
+ "dev": true
+ },
"path-parse": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz",
@@ -928,11 +1090,6 @@
}
}
},
- "serve-favicon": {
- "version": "2.4.3",
- "resolved": "https://registry.npmjs.org/serve-favicon/-/serve-favicon-2.4.3.tgz",
- "integrity": "sha1-WYaxewUCZCtkHCH4GLGszjICXSM="
- },
"serve-static": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.12.3.tgz",
@@ -949,6 +1106,12 @@
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.0.3.tgz",
"integrity": "sha1-ZlZ+NwQ+608E2RvWWMDL77VbjgQ="
},
+ "shelljs": {
+ "version": "0.3.0",
+ "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.3.0.tgz",
+ "integrity": "sha1-NZbmMHp4FUT1kfN9phg2DzHbV7E=",
+ "dev": true
+ },
"signal-exit": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz",
(DIR) diff --git a/package.json b/package.json
@@ -9,7 +9,6 @@
"dependencies": {
"app-root-path": "^2.0.1",
"async": "^2.5.0",
- "bittorrent-tracker": "^9.2.3",
"body-parser": "~1.17.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
@@ -20,7 +19,9 @@
"morgan": "~1.8.1",
"parse-torrent": "^5.8.3",
"pug": "~2.0.0-beta11",
- "serve-favicon": "~2.4.2",
"webtorrent-health": "^1.1.1"
+ },
+ "devDependencies": {
+ "jshint": "^2.9.5"
}
}