Skip to content

Commit

Permalink
Initial nzbget client
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Murray committed Jul 2, 2019
1 parent 9d10fd2 commit 19230b3
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 0 deletions.
32 changes: 32 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"os"

"github.com/chaosaffe/dolphin/pkg/nzbget"
)

func main() {

// TODO: read address from config file

c := nzbget.NewClient("http://10.0.60.101:30411")

v, err := c.Version()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Printf("NZBGet Version: %s\n", v)

nzbs, err := c.ListNZBs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Printf("%d Active NZB's\n", len(nzbs))

}
30 changes: 30 additions & 0 deletions pkg/nzbget/control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nzbget

import "github.com/pkg/errors"

type versionRequest struct {
Method string `json:"method"`
}

type versionResponse struct {
APIVersion string `json:"version"`
ID string `json:"id"`
Result string `json:"result"`
}

func (c *Nzbget) Version() (string, error) {

request := versionRequest{
Method: "version",
}

response := versionResponse{}

err := c.doCall(request, &response)
if err != nil {
return "", errors.Wrap(err, "unable to get version")
}

return response.Result, nil

}
49 changes: 49 additions & 0 deletions pkg/nzbget/nzbget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package nzbget

import (
"encoding/json"
"net/http"
"strings"

"github.com/pkg/errors"
)

// Nzbget represents a client for an Nzbget server
type Nzbget struct {
endpoint string
httpClient *http.Client
customHeaders map[string]string
}

// TODO: build url with host, port, username, password,
// TODO: category for download
// TODO: priority for download

func NewClient(address string) Nzbget {

return Nzbget{
endpoint: address + "/jsonrpc",
}

}

func (c *Nzbget) doCall(request interface{}, response interface{}) error {
data, err := json.Marshal(request)
if err != nil {
return errors.Wrap(err, "unable to marshal request")
}
resp, err := http.Post(c.endpoint,
"application/json", strings.NewReader(string(data)))
if err != nil {
return errors.Wrap(err, "posting request failed")
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
//decoder.DisallowUnknownFields()
decoder.UseNumber()
err = decoder.Decode(&response)
if err != nil {
return errors.Wrap(err, "unable to unmarshal response")
}
return nil
}
112 changes: 112 additions & 0 deletions pkg/nzbget/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package nzbget

import (
"github.com/pkg/errors"
)

type listGroupsRequest struct {
Method string `json:"method"`
ID interface{} `json:"id,omitempty"`
Parameters listGroupsParameters `json:"param"`
}

type listGroupsResponse struct {
APIVersion string `json:"version"`
ID string `json:"id"`
Result []NZB `json:"result"`
}

type listGroupsParameters struct {
ItemCount int `json:"NumberOfLogEntries"`
}

// TODO: combine hi/low's

type NZB struct {
RemainingSizeLo int64 `json:"RemainingSizeLo"`
RemainingSizeHi int `json:"RemainingSizeHi"`
RemainingSizeMB int `json:"RemainingSizeMB"`
PausedSizeLo int `json:"PausedSizeLo"`
PausedSizeHi int `json:"PausedSizeHi"`
PausedSizeMB int `json:"PausedSizeMB"`
RemainingFileCount int `json:"RemainingFileCount"`
RemainingParCount int `json:"RemainingParCount"`
MinPriority int `json:"MinPriority"`
MaxPriority int `json:"MaxPriority"`
ActiveDownloads int `json:"ActiveDownloads"`
Status string `json:"Status"`
ID int `json:"NZBID"`
Name string `json:"NZBName"`
Kind string `json:"Kind"`
URL string `json:"URL"`
Filename string `json:"NZBFilename"`
DestDir string `json:"DestDir"`
FinalDir string `json:"FinalDir"`
Category string `json:"Category"`
ParStatus string `json:"ParStatus"`
ExParStatus string `json:"ExParStatus"`
UnpackStatus string `json:"UnpackStatus"`
MoveStatus string `json:"MoveStatus"`
ScriptStatus string `json:"ScriptStatus"`
DeleteStatus string `json:"DeleteStatus"`
MarkStatus string `json:"MarkStatus"`
URLStatus string `json:"UrlStatus"`
FileSizeLo int64 `json:"FileSizeLo"`
FileSizeHi int `json:"FileSizeHi"`
FileSizeMB int `json:"FileSizeMB"`
FileCount int `json:"FileCount"`
MinPostTime int `json:"MinPostTime"`
MaxPostTime int `json:"MaxPostTime"`
TotalArticles int `json:"TotalArticles"`
SuccessArticles int `json:"SuccessArticles"`
FailedArticles int `json:"FailedArticles"`
Health int `json:"Health"`
CriticalHealth int `json:"CriticalHealth"`
DuplicateKey string `json:"DupeKey"`
DuplicateScore int `json:"DupeScore"`
DuplicateMode string `json:"DupeMode"`
DownloadedSizeLo int `json:"DownloadedSizeLo"`
DownloadedSizeHi int `json:"DownloadedSizeHi"`
DownloadedSizeMB int `json:"DownloadedSizeMB"`
DownloadTimeSec int `json:"DownloadTimeSec"`
PostTotalTimeSec int `json:"PostTotalTimeSec"`
ParTimeSec int `json:"ParTimeSec"`
RepairTimeSec int `json:"RepairTimeSec"`
UnpackTimeSec int `json:"UnpackTimeSec"`
MessageCount int `json:"MessageCount"`
ExtraParBlocks int `json:"ExtraParBlocks"`
Parameters []struct {
Name string `json:"Name"`
Value string `json:"Value"`
} `json:"Parameters"`
ScriptStatuses []interface{} `json:"ScriptStatuses"`
ServerStats []struct {
ServerID int `json:"ServerID"`
SuccessArticles int `json:"SuccessArticles"`
FailedArticles int `json:"FailedArticles"`
} `json:"ServerStats"`
PostInfoText string `json:"PostInfoText"`
PostStageProgress int `json:"PostStageProgress"`
PostStageTimeSec int `json:"PostStageTimeSec"`
}

func (c *Nzbget) ListNZBs() ([]NZB, error) {

request := listGroupsRequest{
Method: "listgroups",
Parameters: listGroupsParameters{
// ItemCount is deprecated, must be zero (https://nzbget.net/api/listgroups)
ItemCount: 0,
},
}

response := listGroupsResponse{}

err := c.doCall(request, &response)
if err != nil {
return nil, errors.Wrap(err, "unable to list groups")
}

return response.Result, nil

}

0 comments on commit 19230b3

Please sign in to comment.