mirror of
https://codeberg.org/hyperreal/go-torrent-helper
synced 2024-11-01 16:53:09 +01:00
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/hekmon/transmissionrpc"
|
|
)
|
|
|
|
func GetResponse(url string) ([]byte, error) {
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
req.Header.Add("User-Agent", "go-torrent-helper")
|
|
|
|
// Add required headers for GitHub API
|
|
if strings.Contains(url, "github") {
|
|
req.Header.Add("Accept", "application/vnd.github.v3.text-match+json")
|
|
req.Header.Add("Accept", "application/vnd.github.moondragon+json")
|
|
}
|
|
|
|
// Carry out the request and receive response
|
|
client := http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error while making request: %s\n", err)
|
|
}
|
|
|
|
// Status in <200 or >299
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
return nil, fmt.Errorf("Error: %d %s\n", resp.StatusCode, resp.Status)
|
|
}
|
|
|
|
// Return response as bytes
|
|
dataInBytes, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error reading response: %s\n", err)
|
|
}
|
|
|
|
return dataInBytes, nil
|
|
}
|
|
|
|
func RemoveTorrents(nameSubstr string, relver string, transmissionbt *transmissionrpc.Client) error {
|
|
var oldTorrents []int64
|
|
torrents, err := transmissionbt.TorrentGet([]string{"id", "name"}, nil)
|
|
if err != nil {
|
|
return err
|
|
} else {
|
|
for _, torrent := range torrents {
|
|
if strings.Contains(*torrent.Name, nameSubstr) && strings.Contains(*torrent.Name, relver) {
|
|
oldTorrents = append(oldTorrents, *torrent.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
rmPayload := &transmissionrpc.TorrentRemovePayload{
|
|
IDs: oldTorrents,
|
|
DeleteLocalData: false,
|
|
}
|
|
|
|
if err := transmissionbt.TorrentRemove(rmPayload); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|