-
Notifications
You must be signed in to change notification settings - Fork 28
/
offline.go
174 lines (156 loc) · 4.06 KB
/
offline.go
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
167
168
169
170
171
172
173
174
package elevengo
import (
"context"
"iter"
"github.com/deadblue/elevengo/internal/util"
"github.com/deadblue/elevengo/lowlevel/api"
"github.com/deadblue/elevengo/lowlevel/client"
"github.com/deadblue/elevengo/lowlevel/types"
"github.com/deadblue/elevengo/option"
)
type OfflineClearFlag int
const (
OfflineClearDone OfflineClearFlag = iota
OfflineClearAll
OfflineClearFailed
OfflineClearRunning
OfflineClearDoneAndDelete
OfflineClearAllAndDelete
offlineClearFlagMin = OfflineClearDone
offlineClearFlagMax = OfflineClearAllAndDelete
)
// OfflineTask describe an offline downloading task.
type OfflineTask struct {
InfoHash string
Name string
Size int64
Status int
Percent float64
Url string
FileId string
}
func (t *OfflineTask) IsRunning() bool {
return t.Status == 1
}
func (t *OfflineTask) IsDone() bool {
return t.Status == 2
}
func (t *OfflineTask) IsFailed() bool {
return t.Status == -1
}
func (t *OfflineTask) from(ti *types.TaskInfo) *OfflineTask {
t.InfoHash = ti.InfoHash
t.Name = ti.Name
t.Size = ti.Size
t.Status = ti.Status
t.Percent = ti.Percent
t.Url = ti.Url
t.FileId = ti.FileId
return t
}
type offlineIterator struct {
llc client.Client
page int
result *types.OfflineListResult
}
func (i *offlineIterator) update() (err error) {
if i.result != nil && i.page > i.result.PageCount {
return errNoMoreItems
}
spec := (&api.OfflineListSpec{}).Init(i.page)
if err = i.llc.CallApi(spec, context.Background()); err == nil {
i.result = &spec.Result
i.page += 1
}
return
}
func (i *offlineIterator) Count() int {
if i.result == nil {
return 0
}
return i.result.TaskCount
}
func (i *offlineIterator) Items() iter.Seq2[int, *OfflineTask] {
return func(yield func(int, *OfflineTask) bool) {
for index := 0; ; {
for _, ti := range i.result.Tasks {
if stop := !yield(index, (&OfflineTask{}).from(ti)); stop {
return
}
index += 1
}
if err := i.update(); err != nil {
break
}
}
}
}
// OfflineIterate returns an iterator to access all offline tasks.
func (a *Agent) OfflineIterate() (it Iterator[OfflineTask], err error) {
oi := &offlineIterator{
llc: a.llc,
page: 1,
}
if err = oi.update(); err == nil {
it = oi
}
return
}
// OfflineDelete deletes tasks.
func (a *Agent) OfflineDelete(hashes []string, options ...*option.OfflineDeleteOptions) (err error) {
if len(hashes) == 0 {
return
}
// Apply options
deleteFiles := false
if opts := util.NotNull(options...); opts != nil {
deleteFiles = opts.DeleteFiles
}
// Call API
spec := (&api.OfflineDeleteSpec{}).Init(hashes, deleteFiles)
return a.llc.CallApi(spec, context.Background())
}
// OfflineClear clears tasks which is in specific status.
func (a *Agent) OfflineClear(flag OfflineClearFlag) (err error) {
if flag < offlineClearFlagMin || flag > offlineClearFlagMax {
flag = OfflineClearDone
}
spec := (&api.OfflineClearSpec{}).Init(int(flag))
return a.llc.CallApi(spec, context.Background())
}
// OfflineAddUrl adds offline tasks by download URLs.
// It returns an info hash list related to the given urls, the info hash will
// be empty if the related URL is invalid.
//
// You can use options to change the download directory:
//
// agent := Default()
// agent.CredentialImport(&Credential{UID: "", CID: "", SEID: ""})
// hashes, err := agent.OfflineAddUrl([]string{
// "https://foo.bar/file.zip",
// "magent:?xt=urn:btih:111222",
// "ed2k://|file|name|size|md4|",
// }, option.OfflineSaveDownloadedFileTo("dirId"))
func (a *Agent) OfflineAddUrl(urls []string, options ...*option.OfflineAddOptions) (hashes []string, err error) {
// Prepare results buffer
if urlCount := len(urls); urlCount == 0 {
return
} else {
hashes = make([]string, urlCount)
}
// Apply options
saveDirId := ""
if opts := util.NotNull(options...); opts != nil {
saveDirId = opts.SaveDirId
}
// Call API
spec := (&api.OfflineAddUrlsSpec{}).Init(urls, saveDirId, &a.common)
if err = a.llc.CallApi(spec, context.Background()); err == nil {
for i, task := range spec.Result {
if task != nil {
hashes[i] = task.InfoHash
}
}
}
return
}