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

complete functions

parent ecfc52f7
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
{
"source": "/root/.config/transmission-daemon/torrents",
"destination": "/root/.config/transmission-daemon/torrents",
"patterns": [
".torrent$"
]
}
\ No newline at end of file
var Client = require('node-rest-client').Client; 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; var obj;
function clientGet() function clientGet() {
{
// direct way // direct way
client.get("http://10.8.0.1:3000/getFiles", function (data, response) { client.get("http://10.8.0.1:3000/getFiles", function (data, response) {
// parsed response body as js object // parsed response body as js object
// raw response // raw response
data = data.toString('utf8'); data = data.toString('utf8');
obj = JSON.parse(data); obj = JSON.parse(data);
console.log(obj.list); 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'); });
obj.list.forEach(function (item) {
console.log(item.name);
}); });
}); });
} }
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(); clientGet();
setInterval(clientGet, 60000); setInterval(clientGet, 60000);
\ No newline at end of file
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
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
"dependencies": { "dependencies": {
"directory-tree": "2.0.0", "directory-tree": "2.0.0",
"express": "^4.16.2", "express": "^4.16.2",
"node-rest-client": "^3.1.0", "node-rest-client": "3.1.0",
"request": "2.83.0", "request": "2.83.0",
"scp2": "^0.5.0" "scp2": "^0.5.0"
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment