-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathclient.go
76 lines (62 loc) · 1.77 KB
/
client.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
package client
import (
"context"
"fmt"
q "github.com/bitmagnet-io/bitmagnet/internal/database/query"
"github.com/bitmagnet-io/bitmagnet/internal/database/search"
"github.com/bitmagnet-io/bitmagnet/internal/gql/gqlmodel/gen"
"github.com/bitmagnet-io/bitmagnet/internal/model"
"github.com/bitmagnet-io/bitmagnet/internal/protocol"
)
type AddInfoHashesRequest struct {
ClientID gen.ClientID
InfoHashes []protocol.ID
}
type content = []search.TorrentContentResultItem
type clientWorker interface {
AddInfoHashes(ctx context.Context, req AddInfoHashesRequest) error
download(ctx context.Context, content *content) error
}
type commonClient struct {
config *Config
search search.Search
client clientWorker
}
func New(cfg *Config, search search.Search) commonClient {
cc := commonClient{
config: cfg,
search: search,
}
return cc
}
func (c commonClient) downloadCategory(contentType model.ContentType) string {
category := c.config.Categories[contentType]
if category == "" {
category = c.config.DefaultCategory
}
return category
}
func (c commonClient) AddInfoHashes(ctx context.Context, req AddInfoHashesRequest) error {
switch c.config.DownloadClient {
case gen.ClientIDTransmission:
c.client = transmissionClient{commonClient: c}
case gen.ClientIDQBittorrent:
c.client = qBitClient{commonClient: c}
default:
return fmt.Errorf("not implemented %s", c.config.DownloadClient)
}
options := []q.Option{
q.Where(
search.TorrentContentInfoHashCriteria(req.InfoHashes...),
),
search.TorrentContentCoreJoins(),
search.HydrateTorrentContentContent(),
search.HydrateTorrentContentTorrent(),
q.Limit(uint(len(req.InfoHashes))),
}
sr, err := c.search.TorrentContent(ctx, options...)
if err != nil {
return err
}
return c.client.download(ctx, &sr.Items)
}