Skip to content

Commit

Permalink
feat(methods): add SyncMainData and GetFreeSpaceOnDisk (#26)
Browse files Browse the repository at this point in the history
* feat: GetFreeSpaceonDisk

* optimize memory usage

* optimize code further

* feat: add full maindata method incl. ServerState object

* feat(methods): add SyncMainData

---------

Co-authored-by: ze0s <[email protected]>
  • Loading branch information
martylukyy and zze0s authored Nov 5, 2024
1 parent 54dcf32 commit 3f67cde
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
40 changes: 40 additions & 0 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,43 @@ type AppPreferences struct {
WebUIUseCustomHTTPHeadersEnabled bool `json:"web_ui_use_custom_http_headers_enabled"`
WebUIUsername string `json:"web_ui_username"`
}

type MainData struct {
Rid int `json:"rid"`
FullUpdate bool `json:"full_update"`
Torrents map[string]Torrent `json:"torrents"`
TorrentsRemoved []string `json:"torrents_removed"`
Categories map[string]Category `json:"categories"`
CategoriesRemoved []string `json:"categories_removed"`
Tags []string `json:"tags"`
TagsRemoved []string `json:"tags_removed"`
Trackers map[string][]string `json:"trackers"`
ServerState ServerState `json:"server_state"`
}

type ServerState struct {
AlltimeDl int64 `json:"alltime_dl"`
AlltimeUl int64 `json:"alltime_ul"`
AverageTimeQueue int `json:"average_time_queue"`
ConnectionStatus string `json:"connection_status"`
DhtNodes int `json:"dht_nodes"`
DlInfoData int64 `json:"dl_info_data"`
DlInfoSpeed int `json:"dl_info_speed"`
DlRateLimit int `json:"dl_rate_limit"`
FreeSpaceOnDisk uint64 `json:"free_space_on_disk"`
GlobalRatio string `json:"global_ratio"`
QueuedIoJobs int `json:"queued_io_jobs"`
Queueing bool `json:"queueing"`
ReadCacheHits string `json:"read_cache_hits"`
ReadCacheOverload string `json:"read_cache_overload"`
RefreshInterval int `json:"refresh_interval"`
TotalBuffersSize int `json:"total_buffers_size"`
TotalPeerConnections int `json:"total_peer_connections"`
TotalQueuedSize int `json:"total_queued_size"`
TotalWastedSession int64 `json:"total_wasted_session"`
UpInfoData int64 `json:"up_info_data"`
UpInfoSpeed int `json:"up_info_speed"`
UpRateLimit int `json:"up_rate_limit"`
UseAltSpeedLimits bool `json:"use_alt_speed_limits"`
WriteCacheOverload string `json:"write_cache_overload"`
}
35 changes: 35 additions & 0 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ func (c *Client) GetTransferInfoCtx(ctx context.Context) (*TransferInfo, error)
return &info, nil
}

// SyncMainDataCtx Sync API implements requests for obtaining changes since the last request.
// Response ID. If not provided, rid=0 will be assumed. If the given rid is different from the one of last server reply, full_update will be true (see the server reply details for more info)
func (c *Client) SyncMainDataCtx(ctx context.Context, rid int64) (*MainData, error) {
opts := map[string]string{
"rid": strconv.FormatInt(rid, 10),
}

resp, err := c.getCtx(ctx, "/sync/maindata", opts)
if err != nil {
return nil, errors.Wrap(err, "could not get main data")
}

var info MainData
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return nil, errors.Wrap(err, "could not unmarshal body")
}

return &info, nil

}

func (c *Client) Pause(hashes []string) error {
return c.PauseCtx(context.Background(), hashes)
}
Expand Down Expand Up @@ -1267,6 +1288,20 @@ func (c *Client) GetWebAPIVersionCtx(ctx context.Context) (string, error) {
return string(body), nil
}

func (c *Client) GetFreeSpaceOnDisk() (uint64, error) {
return c.GetFreeSpaceOnDiskCtx(context.Background())
}

// GetFreeSpaceOnDiskCtx get free space on disk for default download dir. Expensive call
func (c *Client) GetFreeSpaceOnDiskCtx(ctx context.Context) (uint64, error) {
info, err := c.SyncMainDataCtx(ctx, 0)
if err != nil {
return 0, errors.Wrap(err, "could not get maindata")
}

return info.ServerState.FreeSpaceOnDisk, nil
}

const (
ReannounceMaxAttempts = 50
ReannounceInterval = 7 // interval in seconds
Expand Down

0 comments on commit 3f67cde

Please sign in to comment.