Add traffic monitor

This commit is contained in:
Ben Adrian Sarmiento
2024-06-24 13:05:11 +02:00
parent 921908c890
commit 4049fa7d83
5 changed files with 113 additions and 14 deletions

View File

@@ -297,6 +297,52 @@ func (rd *RealDebrid) GetUserInformation() (*User, error) {
return &user, nil
}
// TrafficDetails represents the structure of the traffic details response
type TrafficDetails map[string]struct {
Host map[string]int64 `json:"host"`
Bytes int64 `json:"bytes"`
}
// GetTrafficDetails gets the traffic details from the Real-Debrid API
func (rd *RealDebrid) GetTrafficDetails() (map[string]int64, error) {
// Construct request URL
reqURL := "https://api.real-debrid.com/rest/1.0/traffic/details"
req, err := http.NewRequest(http.MethodGet, reqURL, nil)
if err != nil {
rd.log.Errorf("Error when creating a traffic details request: %v", err)
return nil, err
}
// Send the request
resp, err := rd.apiClient.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the traffic details request: %v", err)
return nil, err
}
defer resp.Body.Close()
// Decode the JSON response into the TrafficDetails struct
var trafficDetails TrafficDetails
err = json.NewDecoder(resp.Body).Decode(&trafficDetails)
if err != nil {
rd.log.Errorf("Error when decoding traffic details JSON: %v", err)
return nil, err
}
// Find the latest date in the traffic details
var latestDate string
for date := range trafficDetails {
if latestDate == "" || date > latestDate {
latestDate = date
}
}
// Get the traffic details for the latest date
latestTraffic := trafficDetails[latestDate].Host
return latestTraffic, nil
}
// AvailabilityCheck checks the instant availability of torrents
func (rd *RealDebrid) AvailabilityCheck(hashes []string) (AvailabilityResponse, error) {
if len(hashes) == 0 {