parse - 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
---
parse (1986B)
---
1 #!/usr/bin/env node
2 'use strict';
3
4 console.log('Parsing torrent folder');
5
6 const async = require('async');
7 const mongoose = require('mongoose');
8 const parseTorrent = require('parse-torrent');
9 const Torrent = require('../models/torrent_model');
10 const fs = require('fs');
11 const config = require('../app/config');
12 const mongoDB = config.dbURI;
13
14 const torrentFolder = './data/torrents';
15
16 mongoose.connect(mongoDB);
17 var db = mongoose.connection;
18 db.on('error', console.error.bind(console, 'MongoDB connection error:'));
19
20 function saveTorrent(query, data, callback) {
21 Torrent.findOneAndUpdate(query, data, {upsert: true},function(err) {
22 if (err) {
23 callback(err, null);
24 } else {
25 callback(null);
26 }
27 });
28 }
29
30 fs.readdir(torrentFolder, function (err, files) {
31 if (err) console.log(err);
32
33 async.eachSeries(files, function(file, callback) {
34 async.waterfall([
35 function(callback){
36
37 console.log("Parsing - " + file);
38
39 let tFile = fs.readFileSync(torrentFolder + '/' + file);
40 let tData = parseTorrent(tFile);
41
42 let tMagnet = parseTorrent.toMagnetURI({
43 infoHash: tData.infoHash
44 });
45
46 let newData = {
47 name: tData.name,
48 hash: tData.infoHash,
49 created: tData.created,
50 comment: tData.comment,
51 announce: tData.announce,
52 files: tData.files,
53 magneturi: tMagnet,
54 };
55
56 callback(null, newData);
57 },
58 function(arg1, callback){
59 saveTorrent({hash: arg1.hash }, arg1, function(err, data) {
60 if (err) callback(true);
61
62 callback(null, data);
63 });
64 }], function (err) {
65
66 if (err)
67 throw err;
68
69 callback();
70 }
71 );
72 }, function(err) {
73 if( err ) {
74 console.log('A torrent failed to process');
75 process.exit(1);
76 } else {
77 console.log('All torrents have been processed successfully');
78 process.exit();
79 }
80 });
81