| adding script to parse torrent folder and update db - seedlinux - Torrent index… | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 1a5521dad4623d081a795545b7aec954d4f30904 | |
| parent 4212fe0f4b92c5c56ee4be03844479e06558d9e4 | |
| Author: Jay Scott <[email protected]> | |
| Date: Tue, 4 Jul 2017 15:10:45 +0100 | |
| adding script to parse torrent folder and update db | |
| Diffstat: | |
| A bin/parse | 81 ++++++++++++++++++++++++++++++ | |
| M package.json | 1 - | |
| M views/layout.pug | 1 - | |
| 3 files changed, 81 insertions(+), 2 deletions(-) | |
| --- | |
| diff --git a/bin/parse b/bin/parse | |
| @@ -0,0 +1,80 @@ | |
| +#!/usr/bin/env node | |
| +'use strict'; | |
| + | |
| +console.log('Parsing torrent folder'); | |
| + | |
| +const async = require('async'); | |
| +const mongoose = require('mongoose'); | |
| +const mongoDB = process.env.DB_URI; | |
| +const parseTorrent = require('parse-torrent'); | |
| +const Torrent = require('../models/torrent_model'); | |
| +const fs = require('fs'); | |
| + | |
| +const torrentFolder = './data/torrents'; | |
| + | |
| +mongoose.connect(mongoDB); | |
| +var db = mongoose.connection; | |
| +db.on('error', console.error.bind(console, 'MongoDB connection error:')); | |
| + | |
| +function saveTorrent(query, data, callback) { | |
| + Torrent.findOneAndUpdate(query, data, {upsert: true},function(err) { | |
| + if (err) { | |
| + callback(err, null); | |
| + } else { | |
| + callback(null); | |
| + } | |
| + }); | |
| +} | |
| + | |
| +fs.readdir(torrentFolder, function (err, files) { | |
| + if (err) console.log(err); | |
| + | |
| + async.eachSeries(files, function(file, callback) { | |
| + async.waterfall([ | |
| + function(callback){ | |
| + | |
| + console.log("Parsing - " + file); | |
| + | |
| + let tFile = fs.readFileSync(torrentFolder + '/' + file); | |
| + let tData = parseTorrent(tFile); | |
| + | |
| + let tMagnet = parseTorrent.toMagnetURI({ | |
| + infoHash: tData.infoHash | |
| + }); | |
| + | |
| + let newData = { | |
| + name: tData.name, | |
| + hash: tData.infoHash, | |
| + created: tData.created, | |
| + comment: tData.comment, | |
| + announce: tData.announce, | |
| + files: tData.files, | |
| + magneturi: tMagnet, | |
| + }; | |
| + | |
| + callback(null, newData); | |
| + }, | |
| + function(arg1, callback){ | |
| + saveTorrent({hash: arg1.hash }, arg1, function(err, data) { | |
| + if (err) callback(true); | |
| + | |
| + 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(); | |
| + } | |
| + }); | |
| +}); | |
| +\ No newline at end of file | |
| diff --git a/package.json b/package.json | |
| @@ -9,7 +9,6 @@ | |
| "start": "DEBUG=seedlinux:* node ./bin/www" | |
| }, | |
| "dependencies": { | |
| - "app-root-path": "^2.0.1", | |
| "async": "^2.5.0", | |
| "body-parser": "~1.17.1", | |
| "cookie-parser": "~1.4.3", | |
| diff --git a/views/layout.pug b/views/layout.pug | |
| @@ -12,7 +12,6 @@ html | |
| img.ui.centered.medium.image(src='/images/logo.png') | |
| .sub.ui.centered.header= "Start Contributing to Open Source!" | |
| - | |
| .ui.hidden.divider | |
| .ui.container |