-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharia2go.go
366 lines (311 loc) · 7.86 KB
/
aria2go.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package aria2go
// #include "aria2go.h"
// #include <stdlib.h>
import "C"
import (
"time"
"unsafe"
)
//ENUMS
type DownloadEvent int
const (
EVENT_ON_DOWNLOAD_START DownloadEvent = C.EVENT_ON_DOWNLOAD_START
EVENT_ON_DOWNLOAD_PAUSE DownloadEvent = C.EVENT_ON_DOWNLOAD_PAUSE
EVENT_ON_DOWNLOAD_STOP DownloadEvent = C.EVENT_ON_DOWNLOAD_STOP
EVENT_ON_DOWNLOAD_COMPLETE DownloadEvent = C.EVENT_ON_DOWNLOAD_COMPLETE
EVENT_ON_DOWNLOAD_ERROR DownloadEvent = C.EVENT_ON_DOWNLOAD_ERROR
EVENT_ON_BT_DOWNLOAD_COMPLETE DownloadEvent = C.EVENT_ON_BT_DOWNLOAD_COMPLETE
)
//ENUMS
type DownloadStatus int
const (
DOWNLOAD_ACTIVE DownloadStatus = C.DOWNLOAD_ACTIVE
DOWNLOAD_WAITING DownloadStatus = C.DOWNLOAD_WAITING
DOWNLOAD_PAUSED DownloadStatus = C.DOWNLOAD_PAUSED
DOWNLOAD_COMPLETE DownloadStatus = C.DOWNLOAD_COMPLETE
DOWNLOAD_ERROR DownloadStatus = C.DOWNLOAD_ERROR
DOWNLOAD_REMOVED DownloadStatus = C.DOWNLOAD_REMOVED
)
func (ds DownloadStatus) String() string {
names := [...]string{
"active",
"waiting",
"paused",
"completed",
"has an error",
"removed"}
return names[ds]
}
type Aria2go struct {
}
type Gid struct {
ptr unsafe.Pointer
}
type FileData struct {
Index int
Path string
Length int64
CompletedLength int64
Selected bool
//uris []string
}
type GlobalStat struct {
DownloadSpeed int
UploadSpeed int
NumActive int
NumWaiting int
NumStopped int
}
type BtMetaInfoData struct {
//announce list not implemented
Comment string
CreationDate time.Time
MultiMod bool
SingleMod bool
Name string
Valid bool
}
func New() Aria2go {
var ret Aria2go
C.init_aria2go()
return ret
}
func (d Aria2go) Init_aria2go_session(keepRunning bool) {
C.finalize_aria2go()
if keepRunning {
C.init_aria2go_session(C.int(1))
} else {
C.init_aria2go_session(C.int(0))
}
}
func (d Aria2go) Run() int {
return int(C.run_aria2go(C.int(0)))
}
func (d Aria2go) RunUntillFinished() {
//Returns when all downloads finisged
C.keepruning_aria2go()
}
func (d Aria2go) KeepRunning() {
C.finalize_aria2go()
C.init_aria2go_session(C.int(1))
go func() {
C.keepruning_aria2go()
}()
}
func (d Aria2go) RunOnce() int {
return int(C.run_aria2go(C.int(1)))
}
func (d Aria2go) GidToHex(gid Gid) string {
p := C.gidToHex_aria2go(gid.ptr)
s := C.GoString(p)
C.free(unsafe.Pointer(p))
return s
}
func (d Aria2go) HexToGid(s string) Gid {
var ret Gid
ret.ptr = C.hexToGid_aria2go(C.CString(s))
return ret
}
func (d Aria2go) IsNull(g Gid) bool {
if C.isNull_aria2go(g.ptr) == 0 {
return false
}
return true
}
func (d Aria2go) AddUriInPosition(uri string, position int) Gid {
var ret Gid
ret.ptr = C.addUri_aria2go(C.CString(uri), C.int(position))
return ret
}
func (d Aria2go) AddUri(uri string) Gid {
var ret Gid
ret.ptr = C.addUri_aria2go(C.CString(uri), C.int(-1))
return ret
}
func (d Aria2go) AddMetalinkInPosition(file_location string, position int) []Gid {
var gids []Gid
var gid Gid
l := int(C.addMetalink_aria2go(C.CString(file_location), C.int(position)))
for i := 0; i < l; i++ {
gid.ptr = C.get_element_gid(C.int(i))
gids = append(gids, gid)
}
return gids
}
func (d Aria2go) AddMetaLink(file_location string) []Gid {
return d.AddMetalinkInPosition(file_location, -1)
}
func (d Aria2go) AddUriToCache(uri string) {
//You can use this one even if you didnt start session yet
C.add_uri(C.CString(uri))
}
func (d Aria2go) ClearUriCache() {
C.clear_uris()
}
func (d Aria2go) AddAllFromCacheWithPosition(p int) Gid {
var ret Gid
ret.ptr = C.add_all_from_cache(C.int(p))
return ret
}
func (d Aria2go) AddAllFromCache() Gid {
var ret Gid
ret.ptr = C.add_all_from_cache(C.int(-1))
return ret
}
func (d Aria2go) GetActiveDownload() []Gid {
var gids []Gid
l := int(C.getActiveDownload_aria2go())
for i := 0; i < l; i++ {
var g Gid
g.ptr = C.get_element_gid(C.int(i))
gids = append(gids, g)
}
return gids
}
func (d Aria2go) RemoveDownload(g Gid) {
C.removeDownload_aria2go(g.ptr, C.int(0))
//Second variable is bool for forcing
}
func (d Aria2go) ForceRemoveDownload(g Gid) {
C.removeDownload_aria2go(g.ptr, C.int(1))
}
func (d Aria2go) PauseDownload(g Gid) {
C.pauseDownload_aria2go(g.ptr, C.int(0))
//Second variable is bool for forcing
}
func (d Aria2go) ForcePauseDownload(g Gid) {
C.pauseDownload_aria2go(g.ptr, C.int(1))
}
func (d Aria2go) UnpauseDownload(g Gid) {
C.unpauseDownload_aria2go(g.ptr)
}
//Event Callback
type EventCallback func(DownloadEvent, Gid)
var callback EventCallback //Have to be global due to garbage collector
func (d Aria2go) SetEventCallback(eventCallback EventCallback) {
callback = eventCallback
}
//export runGoCallBack
func runGoCallBack(event C.enum_DownloadEvent, g unsafe.Pointer) {
if callback == nil {
return
}
var gid Gid
gid.ptr = g
callback(DownloadEvent(event), gid)
}
func (d Aria2go) Finalize() {
C.finalize_aria2go()
C.deinit_aria2go()
}
func (g Gid) GetStatus() DownloadStatus {
return DownloadStatus(C.getStatus_gid(g.ptr))
}
func (g Gid) GetTotalLength() int64 {
return int64(C.getTotalLength_gid(g.ptr))
}
func (g Gid) GetCompletedLength() int64 {
return int64(C.getCompletedLength_gid(g.ptr))
}
func (g Gid) GetUploadLength() int64 {
return int64(C.getUploadLength_gid(g.ptr))
}
func (g Gid) GetBitfield() string {
p := C.getBitfield_gid(g.ptr)
s := C.GoString(p)
C.free(unsafe.Pointer(p))
return s
}
func (g Gid) GetDownloadSpeed() int {
return int(C.getDownloadSpeed_gid(g.ptr))
}
func (g Gid) GetUploadSpeed() int {
return int(C.getUploadSpeed_gid(g.ptr))
}
func (g Gid) GetInfoHash() string {
p := C.getInfoHash_gid(g.ptr)
s := C.GoString(p)
C.free(unsafe.Pointer(p))
return s
}
func (g Gid) GetNumPieces() int {
return int(C.getNumPieces_gid(g.ptr))
}
func (g Gid) GetConnections() int {
return int(C.getConnections_gid(g.ptr))
}
func (g Gid) GetErrorCode() int {
return int(C.getErrorCode_gid(g.ptr))
}
func (g Gid) GetNumFiles() int {
return int(C.getNumFiles_gid(g.ptr))
}
func (g Gid) GetDir() string {
p := C.getDir_gid(g.ptr)
s := C.GoString(p)
C.free(unsafe.Pointer(p))
return s
}
func (g Gid) GetBtMetaInfo() BtMetaInfoData {
var btMetaInfo BtMetaInfoData
btmi := C.getBtMetaInfo_gid(g.ptr)
if btmi == nil {
btMetaInfo.Valid = false
} else {
//Comment
c := C.get_comment_BtMetaInfo(btmi)
btMetaInfo.Comment = C.GoString(c)
C.free(unsafe.Pointer(c))
//Creation Time
btMetaInfo.CreationDate = time.Unix(int64(C.get_creationDate_BtMetaInfo(btmi)), 0)
//Mode
mode := int(C.get_mode_BtMetaInfo(btmi))
btMetaInfo.SingleMod = false
btMetaInfo.MultiMod = false
if mode == 0 {
btMetaInfo.SingleMod = true
} else if mode == 1 {
btMetaInfo.MultiMod = true
}
//Name
n := C.get_name_BtMetaInfo(btmi)
btMetaInfo.Name = C.GoString(n)
C.free(unsafe.Pointer(n))
C.free(btmi)
}
return btMetaInfo
}
func (g Gid) GetFiles() []FileData {
var files []FileData
var ptr unsafe.Pointer
l := int(C.getFiles_gid(g.ptr))
for i := 0; i < l; i++ {
var f FileData
ptr = C.get_element_fileData(C.int(i))
f.Index = int(C.get_index_fileData(ptr))
p := C.get_path_fileData(ptr)
f.Path = C.GoString(p)
C.free(unsafe.Pointer(p))
f.Length = int64(C.get_length_fileData(ptr))
f.CompletedLength = int64(C.get_completedLength_fileData(ptr))
if int(C.get_selected_fileData(ptr)) == 0 {
f.Selected = true
} else {
f.Selected = false
}
//Uris not added due to high cost with loops
files = append(files, f)
}
return files
}
func (d Aria2go) GetGlobalStat() GlobalStat {
var globalStat GlobalStat
gs := C.getGlobalStat_aria2go()
globalStat.DownloadSpeed = int(C.get_downloadSpeed_globalStat(gs))
globalStat.UploadSpeed = int(C.get_uploadSpeed_globalStat(gs))
globalStat.NumActive = int(C.get_numActive_globalStat(gs))
globalStat.NumWaiting = int(C.get_numWaiting_globalStat(gs))
globalStat.NumStopped = int(C.get_numStopped_globalStat(gs))
C.free(gs)
return globalStat
}