go-torrent-helper/qubes/main.go
2023-07-17 21:54:36 -05:00

63 lines
1.4 KiB
Go

package qubes
import (
"fmt"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
type Qubes struct {
NameSubstr string
Relver string
URL string
}
func (q Qubes) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Qubes OS
q.URL = "https://mirrors.edge.kernel.org/qubes/iso/"
respBody, err := common.GetResponse(q.URL)
if err != nil {
return err
}
// Get a goquery doc from response body
doc, err := goquery.NewDocumentFromReader(respBody)
if err != nil {
return err
}
// Extract torrent URLs from web page contents
var torrentURLs []string
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), q.Relver) && strings.Contains(s.Text(), "torrent") {
torrentURLs = append(torrentURLs, fmt.Sprintf("%s/%s", q.URL, s.Text()))
}
})
// 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 (q Qubes) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
if err := common.RemoveTorrents(q.NameSubstr, q.Relver, transmissionbt); err != nil {
return err
}
return nil
}