-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
82 lines (75 loc) · 1.95 KB
/
test.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
var _ = require('underscore'),
couchdb = require('./couchdb.js');
var logging = require('./logger.js');
var log = logging.createDefaultLogger('log.log');
var couchoptions = {
uri : 'http://10.0.0.101:5984/',
logger : log
};
var listTorrents = function(response) {
var sorted = _.sortBy(response.torrents,function(t) {
return t.uploadRatio;
});
_.each(sorted,log,debug);
};
var initialized = function(rpc) {
var couch = couchdb(couchoptions);
var torrentEventFields = {
hashString : "hashString",
name : "name",
status : "status",
sizeWhenDone : "sizeWhenDone"
};
var getTorrentRequest = function() {
var request = {
method : 'torrent-get',
'arguments' : {fields : []}
};
_.each(torrentEventFields,function(f) {
request['arguments'].fields.push(f);
});
return request;
};
var updateDocument = function(torrent) {
var create = function() {
log.info('create');
couch.put('test/'+torrent.hashString,torrent,log.debug);
};
var update = function(b) {
var exsiting = JSON.parse(b);
torrent._rev = exsiting._rev;
torrent._id = exsiting._id;
var isEquals = _.all(torrentEventFields,function(p) {
return exsiting[p] === torrent[p];
});
if(!isEquals) {
log.info('update');
couch.put('test/'+torrent.hashString,torrent,debug);
}
};
couch.get('test/'+torrent.hashString,update,create);
};
var initDb = function() {
couch.put('test/',{},function() {
log.info('db ready');
couch.get('test',log.info);
var poll = function() {
rpc(getTorrentRequest(),function(torrents) {
_.each(torrents.torrents,updateDocument);
});
log.debug('poll');
};
poll();
});
};
initDb();
};
var transmission = require('./transmission.js');
var options = {
username : "user",
password : "pass",
url : "http://10.0.0.195:9091/transmission/rpc/",
debug : false
};
console.log("init transmission");
transmission(options,initialized);