-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsync.js
167 lines (142 loc) · 4.76 KB
/
sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var PutIO = require('put.io-v2');
var argv = require( 'argv' );
var _ = require('underscore');
var fs = require('fs');
var execSync = require('execSync');
var Pushover = require('node-pushover');
var request = require('request');
var TVShowMatcher = require('./tvshowdir');
var config = require('./config');
require('longjohn');
var push = null;
if (config.pushpin.enabled) {
push = new Pushover({
token: config.pushpin.appkey,
user: config.pushpin.userkey
});
} else {
push = {
send: function() {}
};
}
var api = new PutIO(config.putIo.oauth2key);
var args = argv.option([{
name: 'directory-id',
short: 'd',
type: 'int',
description: 'id of the directory to sync'
}, {
name: 'local-path',
short: 'l',
type: 'path',
description: 'local dir to sync to'
}, {
name: 'tvshow-dir',
short: 's',
type: 'path',
description: '(optional) local filepath of your TV show dir'
}]).run();
var directoryId = args.options['directory-id'] || 0;
var localPath = args.options['local-path'];
var tvShowDir = args.options['tvshow-dir'];
var matcher = null;
if (tvShowDir) {
matcher = TVShowMatcher(tvShowDir);
} else {
matcher = function() {};
}
function deleteShowIfCompleted(api, fileNode, stat) {
if (stat && stat.size == fileNode.size) {
// this file was allready downloaded - so we might delete it
console.log('deleting ' + fileNode.name + ' from put.io');
api.files.delete(fileNode.id);
return true;
};
return false;
}
function sendRPCRequest(methodName, params) {
if (!params) params = [];
request.post({
url: 'http://' + config.aria2c.rpcHost + '/jsonrpc',
json: {
"jsonrpc":"2.0",
"method":methodName,
"params": params,
"id":"1",
"timeout": 5000
}
}, function(error, response, body) {
if (error && error.code == 'ECONNREFUSED') {
console.error('connection refused to aria2c rpc at ' + config.aria2c.rpcHost);
console.error('could it be you forgot to start aria2c --enable-rpc ?');
}
if (body && body.error) {
console.error('aria2c response: ' + body.error.message);
}
}
);
}
function listDir(directoryId, localPath, isChildDir) {
api.files.list(directoryId, function gotPutIoListing(data) {
if (data.files.length == 0) {
if (isChildDir) {
console.log('deleting empty directory from put.io');
api.files.delete(directoryId);
}
} else {
fs.mkdir(localPath, 0766, function dirCreated() {
_.each(data.files, function eachFile(fileNode) {
var localFilePath = localPath + '/' + fileNode.name;
if (fileNode.content_type == 'application/x-directory') {
listDir(fileNode.id, localFilePath, true);
} else {
var fileDir = localPath;
var tvshow = matcher(fileNode.name);
if (tvshow) fileDir = tvshow.path;
var finalPath = fileDir + '/' + fileNode.name;
fs.stat(finalPath, function gotFileStat(err, stat) {
if (deleteShowIfCompleted(api, fileNode, stat)) {
return;
}
if (config.aria2c.rpcHost && config.aria2c.useRPC) {
console.log('adding ' + localFilePath + ' to the download queue...');
sendRPCRequest('aria2.addUri', [ [ api.files.download(fileNode.id) ], { dir: fileDir } ]);
if (tvshow) {
push.send('put.io sync', 'Began download of an episode of ' + tvshow.name);
} else {
push.send('put.io sync', 'Began download of ' + fileNode.name);
}
} else {
var shellCommand = config.aria2c.path + ' -d "' + fileDir + '" "' + api.files.download(fileNode.id) + '"';
console.log('downloading ' + localFilePath + '...');
console.log(shellCommand);
var result = execSync.stdout(shellCommand);
var afterStat = fs.statSync(finalPath);
deleteShowIfCompleted(api, fileNode, afterStat);
if (fileNode.size > 20 * 1024 * 1024) {
if (tvshow) {
push.send('put.io sync', 'Downloaded an episode of ' + tvshow.name);
} else {
push.send('put.io sync', 'Downloaded ' + fileNode.name);
}
}
}
});
}
});
});
}
});
}
var lockFile = '/tmp/putiosync-' + directoryId + '.lock';
if (fs.existsSync(lockFile)) {
console.log('Process already running. If it is not the delete ' + lockFile);
} else {
process.on('exit', function() {
fs.unlinkSync(lockFile);
});
fs.open(lockFile, 'w', 0666, function(err, fd) {
fs.closeSync(fd);
});
listDir(directoryId, localPath, false);
}