go-torrent-helper/rocky/main.go

68 lines
1.6 KiB
Go
Raw Normal View History

2023-10-26 15:56:04 +02:00
package rocky
2023-06-24 04:33:24 +02:00
import (
"fmt"
"log"
2023-07-17 04:28:25 +02:00
"strings"
2023-06-24 04:33:24 +02:00
2024-03-30 01:59:29 +01:00
"git.hyperreal.coffee/go-torrent-helper/common"
2023-07-18 04:54:36 +02:00
"github.com/PuerkitoBio/goquery"
2023-06-24 04:33:24 +02:00
"github.com/hekmon/transmissionrpc"
)
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
2023-07-18 04:54:36 +02:00
// Iterate over architectures and extract torrent URLs for each
2023-07-17 04:28:25 +02:00
for _, arch := range archs {
r.URL = fmt.Sprintf("https://download.rockylinux.org/pub/rocky/%s/isos/%s/", r.Relver, arch)
2023-07-18 04:54:36 +02:00
respBody, err := common.GetResponse(r.URL)
2023-07-17 04:28:25 +02:00
if err != nil {
return err
}
2023-07-18 04:54:36 +02:00
// Get a goquery doc from response body
doc, err := goquery.NewDocumentFromReader(respBody)
if err != nil {
return err
}
2023-07-17 04:28:25 +02:00
// Extract torrent URLs from web page contents
2023-07-18 04:54:36 +02:00
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), "torrent") {
torrentURLs = append(torrentURLs, fmt.Sprintf("%s/%s", r.URL, s.Text()))
2023-07-17 04:28:25 +02:00
}
2023-07-18 04:54:36 +02:00
})
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
}