go-torrent-helper/rocky/main.go
2023-07-16 21:28:25 -05:00

65 lines
1.5 KiB
Go

package rocky
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
type Rocky struct {
NameSubstr string
Relver string
URL string
}
func (r Rocky) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Rocky Linux mirror
archs := []string{"aarch64", "ppc64le", "s390x", "x86_64"}
var torrentURLs []string
for _, arch := range archs {
r.URL = fmt.Sprintf("https://download.rockylinux.org/pub/rocky/%s/isos/%s/", r.Relver, arch)
dataInBytes, err := common.GetResponse(r.URL)
if err != nil {
return err
}
bodyText := string(dataInBytes)
// Extract torrent URLs from web page contents
re := regexp.MustCompile(`(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])`)
match := re.FindAllString(bodyText, 1000)
for _, v := range match {
if strings.Contains(v, ".torrent") {
torrentURLs = append(torrentURLs, v)
}
}
}
// Add torrents to Transmission instance
for _, torrentURL := range torrentURLs {
torrent, err := transmissionbt.TorrentAdd(&transmissionrpc.TorrentAddPayload{
Filename: &torrentURL,
})
if err != nil {
return err
}
log.Printf("%s added\n", *torrent.Name)
}
return nil
}
func (r Rocky) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
if err := common.RemoveTorrents(r.NameSubstr, r.Relver, transmissionbt); err != nil {
return err
}
return nil
}