-
Notifications
You must be signed in to change notification settings - Fork 18
/
api.go
87 lines (77 loc) · 2.56 KB
/
api.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
package status
import (
"fmt"
"sort"
"github.com/sagan/ptool/client"
"github.com/sagan/ptool/cmd/brush/strategy"
"github.com/sagan/ptool/site"
"github.com/sagan/ptool/util"
)
type StatusResponse struct {
Name string
Kind int64
ClientStatus *client.Status
ClientTorrents []client.Torrent
SiteStatus *site.Status
SiteTorrents []site.Torrent // latest site torrents
SiteTorrentScores map[string]float64
Error error
}
func fetchClientStatus(clientInstance client.Client, showTorrents bool, showAllTorrents bool,
category string, ch chan *StatusResponse) {
response := &StatusResponse{Name: clientInstance.GetName(), Kind: 1}
clientStatus, err := clientInstance.GetStatus()
response.ClientStatus = clientStatus
if err != nil {
response.Error = fmt.Errorf("cann't get client %s status: error=%v", clientInstance.GetName(), err)
ch <- response
return
}
if showTorrents {
clientTorrents, err := clientInstance.GetTorrents("", category, showAllTorrents)
if showAllTorrents {
sort.Slice(clientTorrents, func(i, j int) bool {
if clientTorrents[i].Name != clientTorrents[j].Name {
return clientTorrents[i].Name < clientTorrents[j].Name
}
return clientTorrents[i].InfoHash < clientTorrents[j].InfoHash
})
} else {
sort.Slice(clientTorrents, func(i, j int) bool {
return clientTorrents[i].Atime > clientTorrents[j].Atime
})
}
response.ClientTorrents = clientTorrents
if err != nil {
response.Error = fmt.Errorf("cann't get client %s torrents: %v", clientInstance.GetName(), err)
}
}
ch <- response
}
func fetchSiteStatus(siteInstance site.Site, showTorrents bool, full bool, showScore bool, ch chan *StatusResponse) {
response := &StatusResponse{Name: siteInstance.GetName(), Kind: 2}
SiteStatus, err := siteInstance.GetStatus()
response.SiteStatus = SiteStatus
if err != nil {
response.Error = fmt.Errorf("cann't get site %s status: error=%v", siteInstance.GetName(), err)
ch <- response
return
}
if showTorrents {
siteTorrents, err := siteInstance.GetLatestTorrents(full)
if err != nil {
response.Error = fmt.Errorf("cann't get site %s torrents: %v", siteInstance.GetName(), err)
} else {
if showScore {
brushSiteOption := strategy.GetBrushSiteOptions(siteInstance, util.Now())
scores := map[string]float64{}
for _, torrent := range siteTorrents {
scores[torrent.Id], _, _ = strategy.RateSiteTorrent(&torrent, brushSiteOption)
}
response.SiteTorrentScores = scores
}
response.SiteTorrents = siteTorrents
}
}
ch <- response
}