-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtask.coffee
More file actions
82 lines (64 loc) · 1.89 KB
/
task.coffee
File metadata and controls
82 lines (64 loc) · 1.89 KB
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
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 = (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 and 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}"
# console.log cmd
exec cmd,
maxBuffer: 1024 * 1024
env:
'PHANTOMJS_EXECUTABLE': path.resolve(__dirname, './node_modules/casperjs/node_modules/.bin/phantomjs')
,(error, stdout, stderr) ->
unless error?
try
json = JSON.parse stdout
catch e
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 = (url, options = {})->
if typeof url == 'string'
options.url = url
return casperTask(options)
else
return false
exports.delete = (id, options = {})->
if typeof id == 'string'
options.delete = id
return casperTask(options)
else
return false
exports.fetch = (options)->
return casperTask(options)
exports.login = (username, password, options = {})->
if typeof username == 'string' and typeof password == 'string'
options.username = username
options.password = password
return casperTask(options)
else
return false