Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/offline_download/qbit/qbit.go
1562 views
1
package qbit
2
3
import (
4
"github.com/alist-org/alist/v3/internal/conf"
5
"github.com/alist-org/alist/v3/internal/errs"
6
"github.com/alist-org/alist/v3/internal/model"
7
"github.com/alist-org/alist/v3/internal/offline_download/tool"
8
"github.com/alist-org/alist/v3/internal/setting"
9
"github.com/alist-org/alist/v3/pkg/qbittorrent"
10
"github.com/pkg/errors"
11
)
12
13
type QBittorrent struct {
14
client qbittorrent.Client
15
}
16
17
func (a *QBittorrent) Run(task *tool.DownloadTask) error {
18
return errs.NotSupport
19
}
20
21
func (a *QBittorrent) Name() string {
22
return "qBittorrent"
23
}
24
25
func (a *QBittorrent) Items() []model.SettingItem {
26
// qBittorrent settings
27
return []model.SettingItem{
28
{Key: conf.QbittorrentUrl, Value: "http://admin:adminadmin@localhost:8080/", Type: conf.TypeString, Group: model.OFFLINE_DOWNLOAD, Flag: model.PRIVATE},
29
{Key: conf.QbittorrentSeedtime, Value: "0", Type: conf.TypeNumber, Group: model.OFFLINE_DOWNLOAD, Flag: model.PRIVATE},
30
}
31
}
32
33
func (a *QBittorrent) Init() (string, error) {
34
a.client = nil
35
url := setting.GetStr(conf.QbittorrentUrl)
36
qbClient, err := qbittorrent.New(url)
37
if err != nil {
38
return "", err
39
}
40
a.client = qbClient
41
return "ok", nil
42
}
43
44
func (a *QBittorrent) IsReady() bool {
45
return a.client != nil
46
}
47
48
func (a *QBittorrent) AddURL(args *tool.AddUrlArgs) (string, error) {
49
err := a.client.AddFromLink(args.Url, args.TempDir, args.UID)
50
if err != nil {
51
return "", err
52
}
53
return args.UID, nil
54
}
55
56
func (a *QBittorrent) Remove(task *tool.DownloadTask) error {
57
err := a.client.Delete(task.GID, false)
58
return err
59
}
60
61
func (a *QBittorrent) Status(task *tool.DownloadTask) (*tool.Status, error) {
62
info, err := a.client.GetInfo(task.GID)
63
if err != nil {
64
return nil, err
65
}
66
s := &tool.Status{}
67
s.TotalBytes = info.Size
68
s.Progress = float64(info.Completed) / float64(info.Size) * 100
69
switch info.State {
70
case qbittorrent.UPLOADING, qbittorrent.PAUSEDUP, qbittorrent.QUEUEDUP, qbittorrent.STALLEDUP, qbittorrent.FORCEDUP, qbittorrent.CHECKINGUP:
71
s.Completed = true
72
case qbittorrent.ALLOCATING, qbittorrent.DOWNLOADING, qbittorrent.METADL, qbittorrent.PAUSEDDL, qbittorrent.QUEUEDDL, qbittorrent.STALLEDDL, qbittorrent.CHECKINGDL, qbittorrent.FORCEDDL, qbittorrent.CHECKINGRESUMEDATA, qbittorrent.MOVING:
73
s.Status = "[qBittorrent] downloading"
74
case qbittorrent.ERROR, qbittorrent.MISSINGFILES, qbittorrent.UNKNOWN:
75
s.Err = errors.Errorf("[qBittorrent] failed to download %s, error: %s", task.GID, info.State)
76
default:
77
s.Err = errors.Errorf("[qBittorrent] unknown error occurred downloading %s", task.GID)
78
}
79
return s, nil
80
}
81
82
var _ tool.Tool = (*QBittorrent)(nil)
83
84
func init() {
85
tool.Tools.Add(&QBittorrent{})
86
}
87
88