go-torrent-helper/nixos/main.go

59 lines
1.4 KiB
Go
Raw Normal View History

2023-06-24 04:33:24 +02:00
package nixos
import (
"encoding/json"
"log"
2023-07-02 22:12:46 +02:00
"tildegit.org/hyperreal/go-torrent-helper/common"
2023-06-24 04:33:24 +02:00
"github.com/hekmon/transmissionrpc"
)
type Nixos struct {
NameSubstr string
Relver string
2023-06-24 04:33:24 +02:00
URL string
}
func (n Nixos) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Send GET request and receive HTTP response
n.URL = "https://api.github.com/repos/AnimMouse/NixOS-ISO-Torrents/releases/latest"
dataInBytes, err := common.GetResponse(n.URL)
if err != nil {
return err
}
result := make(map[string]interface{})
if err := json.Unmarshal(dataInBytes, &result); err != nil {
return err
}
// Get torrent URLs from HTTP response result
var torrentURLs []interface{}
for _, asset := range result["assets"].([]interface{}) {
torrentURLs = append(torrentURLs, asset.(map[string]interface{})["browser_download_url"])
}
// Add URLs to Transmission
for _, torrentURL := range torrentURLs {
strTorrentURL := torrentURL.(string)
torrent, err := transmissionbt.TorrentAdd(&transmissionrpc.TorrentAddPayload{
Filename: &strTorrentURL,
})
if err != nil {
return err
}
log.Printf("%s added\n", *torrent.Name)
}
return nil
}
func (n Nixos) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
if err := common.RemoveTorrents(n.NameSubstr, n.Relver, transmissionbt); err != nil {
2023-06-24 04:33:24 +02:00
return err
}
return nil
}