initial commit

This commit is contained in:
2026-07-11 23:10:14 -04:00
commit db7ad3eec2
8 changed files with 247 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
package api
import (
"context"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/carlmjohnson/requests"
"golang.org/x/net/publicsuffix"
)
type Client struct {
baseURL string
inner http.Client
}
func NewClient(baseURL, username, password string) (*Client, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
c := Client{
baseURL: baseURL,
inner: http.Client{
Timeout: time.Second * 10,
Jar: jar,
},
}
if err := c.login(username, password); err != nil {
return nil, err
}
return &c, nil
}
func (c *Client) reqBuilder() *requests.Builder {
return requests.
URL(c.baseURL).
Client(&c.inner).
AddValidator(requests.ValidatorHandler(requests.DefaultValidator, requests.ResponseHandler(func(r *http.Response) error {
var buf strings.Builder
_, _ = io.Copy(&buf, r.Body)
return fmt.Errorf("response body: %v", buf.String())
})))
}
func (c *Client) login(username, password string) error {
err := c.reqBuilder().
Path("/api/v2/auth/login").
Header("Referer", c.baseURL).
BodyForm(url.Values{"username": []string{username}, "password": []string{password}}).
Fetch(context.TODO())
if err != nil {
return fmt.Errorf("login: %w", err)
}
return nil
}
+57
View File
@@ -0,0 +1,57 @@
package api
import (
"context"
"net/url"
"strconv"
)
type TorrentInfo struct {
Name string `json:"name"`
Hash string `json:"hash"`
State string `json:"state"`
Category string `json:"category"`
AddedOn UnixTimestamp `json:"added_on"`
AmountLeft int64 `json:"amount_left"`
Downloaded int64 `json:"downloaded"`
Size int64 `json:"size"`
TotalSize int64 `json:"total_size"`
}
func (c *Client) LisTorrents() ([]TorrentInfo, error) {
var torrents []TorrentInfo
if err := c.reqBuilder().Path("/api/v2/torrents/info").ToJSON(&torrents).Fetch(context.TODO()); err != nil {
return nil, err
}
return torrents, nil
}
type TorrentFile struct {
Index int32 `json:"index"`
Name string `json:"name"`
Size int64 `json:"size"`
Progress float64 `json:"progress"`
Priority uint8 `json:"priority"`
IsSeed bool `json:"is_seed"`
PieceRange []int64 `json:"piece_range"`
Availability float64 `json:"availability"`
}
func (c *Client) TorrentFiles(hash string) ([]TorrentFile, error) {
var files []TorrentFile
if err := c.reqBuilder().Path("/api/v2/torrents/files").Param("hash", hash).ToJSON(&files).Fetch(context.TODO()); err != nil {
return nil, err
}
return files, nil
}
func (c *Client) DeleteTorrent(hash string, deleteFiles bool) error {
err := c.reqBuilder().
Path("/api/v2/torrents/delete").
BodyForm(url.Values{
"hashes": []string{hash},
"deleteFiles": []string{strconv.FormatBool(deleteFiles)},
}).
Fetch(context.TODO())
return err
}
+21
View File
@@ -0,0 +1,21 @@
package api
import (
"encoding/json"
"time"
)
type UnixTimestamp struct {
time.Time
}
func (ut *UnixTimestamp) UnmarshalJSON(b []byte) error {
var timestamp int64
// Unmarshal the raw JSON bytes into an int64
if err := json.Unmarshal(b, &timestamp); err != nil {
return err
}
// Convert the epoch seconds to a time.Time object
ut.Time = time.Unix(timestamp, 0)
return nil
}