Commit 79d6a1bf authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca
Browse files

complete functions

parent ecfc52f7
Loading
Loading
Loading
Loading

*

0 → 100644
+0 −0

Empty file added.

autosync.js

0 → 100644
+91 −0
Changes for autosync.js: 91 added lines, 0 removed lines.
Original line number Diff line number Diff line
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

example.json

0 → 100644
+7 −0
Changes for example.json: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
{
  "source": "/root/.config/transmission-daemon/torrents",
  "destination": "/root/.config/transmission-daemon/torrents",
  "patterns": [
    ".torrent$"
  ]
}
 No newline at end of file
+29 −8
Changes for indexClient.js: 29 added lines, 8 removed lines.
Original line number Diff line number Diff line
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

nohup.out

0 → 100644
+20 −0
Changes for nohup.out: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
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
Loading