-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtask.js
110 lines (97 loc) · 2.49 KB
/
task.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
// Generated by CoffeeScript 1.7.1
var Q, casperTask, casperjsCmdPath, configPath, cookieConfigPath, exec, homePath, indexPath, path;
path = require("path");
exec = require("child_process").exec;
Q = require('q');
casperjsCmdPath = path.resolve(__dirname, "./node_modules/.bin/casperjs");
indexPath = path.resolve(__dirname, "./xunlei/index.coffee");
homePath = process.env['HOME'];
configPath = path.join(homePath, ".lixian-cli");
cookieConfigPath = path.join(configPath, "cookies.txt");
casperTask = function(options) {
var cmd, q;
if (options == null) {
options = {};
}
q = Q.defer();
cmd = casperjsCmdPath + (" --cookies-file=" + cookieConfigPath);
if (options.page) {
cmd += " --page=" + options.page;
}
if (options.tasknum) {
cmd += " --tasknum=" + options.tasknum;
}
if (options.username && options.password) {
cmd += " --username=" + options.username + " --password='" + options.password + "'";
}
if (options.url) {
cmd += " --url='" + options.url + "'";
}
if (options["delete"]) {
cmd += " --delete='" + options["delete"] + "'";
}
cmd += " " + indexPath;
exec(cmd, {
maxBuffer: 1024 * 1024,
env: {
'PHANTOMJS_EXECUTABLE': path.resolve(__dirname, './node_modules/casperjs/node_modules/.bin/phantomjs')
}
}, function(error, stdout, stderr) {
var e, json;
if (error == null) {
try {
json = JSON.parse(stdout);
} catch (_error) {
e = _error;
q.reject({
error: 'Task succeed, but got json with wrong format.'
});
}
q.resolve(json);
} else {
q.reject({
error: error,
stdout: stdout,
stderr: stderr
});
}
});
return q.promise;
};
exports.add = function(url, options) {
if (options == null) {
options = {};
}
if (typeof url === 'string') {
options.url = url;
return casperTask(options);
} else {
return false;
}
};
exports["delete"] = function(id, options) {
if (options == null) {
options = {};
}
if (typeof id === 'string') {
options["delete"] = id;
return casperTask(options);
} else {
return false;
}
};
exports.fetch = function(options) {
return casperTask(options);
};
exports.login = function(username, password, options) {
if (options == null) {
options = {};
}
if (typeof username === 'string' && typeof password === 'string') {
options.username = username;
options.password = password;
return casperTask(options);
} else {
return false;
}
};