Commit 2b8327e5 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca
Browse files

others integrations

parent 6b6689da
Loading
Loading
Loading
Loading
+48 −0
Changes for index.js: 48 added lines, 0 removed lines.
Original line number Diff line number Diff line
const express = require('express');
const app = express();
const dirTree = require('directory-tree');
const fs = require('fs');
const http = require('http');
const request = require('request');

//console.log(JSON.stringify(tree, null, 4));

function startHere() {

    var download = function (uri, filename, callback) {
        request.head(uri, function (err, res, body) {
            console.log('content-type:', res.headers['content-type']);
            console.log('content-length:', res.headers['content-length']);

            request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
        });
    };

    var tree = dirTree('/root/.config/transmission-daemon/torrents', { extensions: /\.torrent/ });

    for (var i = 0; i < tree.children.length; i++) {
        if (tree.children[i].type == 'file') {
            var lastSegment = tree.children[i].path;
            lastSegment = lastSegment.substring(lastSegment.lastIndexOf('/'));
            console.log(lastSegment);
            var newFileName = "/mnt1/stuff/Downloads/torrentWatch" + lastSegment.substring(0, lastSegment.lastIndexOf('.'));
            console.log(newFileName);

            download('http://192.168.5.3:3000/' + lastSegment, newFileName, function () {
                console.log('done');
            });
        }
    };
}

setInterval(startHere, 60000);

/* serves all the static files */
app.get(/^(.+)$/, function (req, res) {
    console.log('static file request : ' + req.params);
    //res.sendFile(__dirname + req.params[0]);
    res.sendFile(__dirname + req.params[0]);
});

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(3000, () => console.log('Example app listening on port 3000!'))
 No newline at end of file
Loading