go-torrent-helper/rocky/main.go

65 lines
1.5 KiB
Go
Raw Normal View History

2023-06-24 04:33:24 +02:00
package rocky
import (
"fmt"
"log"
2023-07-17 04:28:25 +02:00
"regexp"
"strings"
2023-06-24 04:33:24 +02:00
"github.com/hekmon/transmissionrpc"
2023-07-17 04:28:25 +02:00
"tildegit.org/hyperreal/go-torrent-helper/common"
2023-06-24 04:33:24 +02:00
)
type Rocky struct {
NameSubstr string
Relver string
URL string
}
func (r Rocky) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Rocky Linux mirror
2023-07-17 04:28:25 +02:00
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)
}
}
2023-06-24 04:33:24 +02:00
}
// 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
}