go-torrent-helper/nixos/main.go

66 lines
1.5 KiB
Go
Raw Normal View History

2023-10-26 15:56:04 +02:00
package nixos
2023-06-24 04:33:24 +02:00
import (
"encoding/json"
2023-07-18 04:54:36 +02:00
"fmt"
"io/ioutil"
2023-06-24 04:33:24 +02:00
"log"
2024-03-30 06:23:56 +01:00
"codeberg.org/hyperreal/go-torrent-helper/common"
2023-10-26 15:56:04 +02:00
"github.com/hekmon/transmissionrpc"
2023-06-24 04:33:24 +02:00
)
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"
2023-07-18 04:54:36 +02:00
respBody, err := common.GetResponse(n.URL)
2023-06-24 04:33:24 +02:00
if err != nil {
return err
}
2023-07-18 04:54:36 +02:00
dataInBytes, err := ioutil.ReadAll(respBody)
if err != nil {
return fmt.Errorf("Error reading response body: %s\n", err)
}
2023-06-24 04:33:24 +02:00
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
}