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"
|
|
|
|
|
2023-10-26 02:45:21 +02:00
|
|
|
"git.sr.ht/~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
|
2023-06-25 13:29:13 +02:00
|
|
|
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 {
|
2023-06-25 13:29:13 +02:00
|
|
|
if err := common.RemoveTorrents(n.NameSubstr, n.Relver, transmissionbt); err != nil {
|
2023-06-24 04:33:24 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|