From 3f67cdeeaf8bc2685013129fbab26c29254007b6 Mon Sep 17 00:00:00 2001 From: martylukyy <35452459+martylukyy@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:05:00 +0100 Subject: [PATCH] feat(methods): add SyncMainData and GetFreeSpaceOnDisk (#26) * feat: GetFreeSpaceonDisk * optimize memory usage * optimize code further * feat: add full maindata method incl. ServerState object * feat(methods): add SyncMainData --------- Co-authored-by: ze0s --- domain.go | 40 ++++++++++++++++++++++++++++++++++++++++ methods.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/domain.go b/domain.go index fe2e25d..a70866c 100644 --- a/domain.go +++ b/domain.go @@ -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"` +} diff --git a/methods.go b/methods.go index 7850b41..c3ce628 100644 --- a/methods.go +++ b/methods.go @@ -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) } @@ -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