diff --git a/* b/* new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/autosync.js b/autosync.js new file mode 100644 index 0000000000000000000000000000000000000000..bb0c3432b8b038f0fa6755b683b2127c23c0961f --- /dev/null +++ b/autosync.js @@ -0,0 +1,91 @@ +var fs = require("fs"); +var path = require("path"); + + +var configFile = "example.json"; +if (process.argv.length == 3) { + configFile = process.argv[2]; +} +var config = JSON.parse(fs.readFileSync(configFile, "UTF-8")); + +config.repatterns = []; +for (var i = 0; i < config.patterns.length; i++) { + config.repatterns.push(new RegExp(config.patterns[i], "i")); +} + + +console.log("------------------------------------------"); +console.log("Autosync - Revision 0"); +console.log("------------------------------------------"); +console.log("Source: " + config.source); +console.log("Destination: " + config.destination); +console.log("Patterns: " + config.repatterns); +console.log("------------------------------------------"); + + +function dups(a, b) { + var i = 0; + var j = 0; + var r = []; + + a.sort(); + b.sort(); + + while (i < a.length && j < b.length) { + if (a[i] == b[j]) { + r.push(a[i]); + i++; + j++; + } + else { + if (a[i] < b[j]) + i++; + else + j++; + } + } + + return r; +} + +function copy(source, dest, cb) { + var rs = fs.createReadStream(source); + var ws = fs.createWriteStream(dest); + + rs.pipe(ws); + ws.on("end", cb); +} + +function watchFile(source, dest) { + var file = path.basename(source); + console.log("watching " + file); + fs.watchFile(source, function (curr, prev) { + copy(source, dest, function (err) { + if (err) { + console.log("failed to copy " + file); + // TODO retry? + } + else { + console.log("copied " + file); + } + }); + }); +} + +fs.readdir(config.source, function (err, sourceFiles) { + if (err) throw err; + fs.readdir(config.destination, function (err, destFiles) { + if (err) throw err; + var files = dups(sourceFiles, destFiles); + + for (var i = 0; i < files.length; i++) { + for (var j = 0; j < config.repatterns.length; j++) { + if (files[i].match(config.repatterns[j])) { + var sourceFile = path.join(config.source, files[i]); + var destFile = path.join(config.destination, files[i]); + watchFile(sourceFile, destFile); + } + } + } + }); +}); \ No newline at end of file diff --git a/example.json b/example.json new file mode 100644 index 0000000000000000000000000000000000000000..006b97ad007e7788400810b89cc5561a3aaf6d27 --- /dev/null +++ b/example.json @@ -0,0 +1,7 @@ +{ + "source": "/root/.config/transmission-daemon/torrents", + "destination": "/root/.config/transmission-daemon/torrents", + "patterns": [ + ".torrent$" + ] +} \ No newline at end of file diff --git a/indexClient.js b/indexClient.js index 08844a2eaf52d8a0d2cdf5216819f114a18de632..256106bba8d2bd733d73f32387fd4cb120b1c4f0 100644 --- a/indexClient.js +++ b/indexClient.js @@ -1,25 +1,46 @@ var Client = require('node-rest-client').Client; +var http = require('http'); +var fs = require('fs'); + +var options = { + // customize mime types for json or xml connections + //(in your case with a space between type and charset) + mimetypes: { + json: ["application/json", "application/json; charset=utf-8"], + xml: ["application/xml", "application/xml; charset=utf-8"] + } +}; + +var client = new Client(options); -var client = new Client(); var obj; -function clientGet() -{ +function clientGet() { // direct way client.get("http://10.8.0.1:3000/getFiles", function (data, response) { // parsed response body as js object // raw response data = data.toString('utf8'); obj = JSON.parse(data); - console.log(obj.list); - - obj.list.forEach(function (item) { - console.log(item.name); + obj.res.forEach(function (item, index) { + download('http://10.8.0.1:3000/' + item.name, '/mnt1/stuff/Downloads/torrentWatch/' + item.name, function () { console.log(item.name+'\n\r'); }); }); }); - } +var download = function (url, dest, cb) { + var file = fs.createWriteStream(dest); + var request = http.get(url, function (response) { + response.pipe(file); + file.on('finish', function () { + file.close(cb); // close() is async, call cb after close completes. + }); + }).on('error', function (err) { // Handle errors + fs.unlink(dest); // Delete the file async. (But we don't check the result) + if (cb) cb(err.message); + }); +}; + clientGet(); setInterval(clientGet, 60000); \ No newline at end of file diff --git a/nohup.out b/nohup.out new file mode 100644 index 0000000000000000000000000000000000000000..a631de47624251988dc3fbe6fe38cc1f80eab57c --- /dev/null +++ b/nohup.out @@ -0,0 +1,20 @@ +Mad Max Fury Road 2015 4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.a9ef06bf5361c7d7.torrent + +Miss Peregrines Home For Peculiar Children 2016 .4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.7ac07b397c109d32.torrent + +Mad Max Fury Road 2015 4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.a9ef06bf5361c7d7.torrent + +Miss Peregrines Home For Peculiar Children 2016 .4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.7ac07b397c109d32.torrent + +Mad Max Fury Road 2015 4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.a9ef06bf5361c7d7.torrent + +Miss Peregrines Home For Peculiar Children 2016 .4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.7ac07b397c109d32.torrent + +The Salt of the Earth 2014 BDRip 1080p HEVC ITA ENG AC3-NAHOM.53ec7624a5088c70.torrent + +Mad Max Fury Road 2015 4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.a9ef06bf5361c7d7.torrent + +Miss Peregrines Home For Peculiar Children 2016 .4K.UHD.2160p.BDRip Ita Eng x265-NAHOM.7ac07b397c109d32.torrent + +The Salt of the Earth 2014 BDRip 1080p HEVC ITA ENG AC3-NAHOM.53ec7624a5088c70.torrent + diff --git a/package.json b/package.json index 97d03a77aaa57854b18e083cb92c11ee7c8a7a1c..b58fd78921c0f0d67bfddfc053f2026e673e517d 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "dependencies": { "directory-tree": "2.0.0", "express": "^4.16.2", - "node-rest-client": "^3.1.0", + "node-rest-client": "3.1.0", "request": "2.83.0", "scp2": "^0.5.0" }, diff --git a/torrents b/torrents new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391