go-torrent-helper/fedora/main.go
2023-10-26 08:56:04 -05:00

65 lines
1.4 KiB
Go

package fedora
import (
"fmt"
"log"
"strings"
"git.sr.ht/~hyperreal/go-torrent-helper/common"
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc"
)
type Fedora struct {
NameSubstr string
Relver string
URL string
}
func (f Fedora) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
torrentSubstr := fmt.Sprintf("%s.torrent", f.Relver)
// Send HTTP GET request and receive response
f.URL = "https://torrent.fedoraproject.org"
respBody, err := common.GetResponse(f.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 source
var torrentURLs []string
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), torrentSubstr) {
torrentURLs = append(torrentURLs, fmt.Sprintf("%s/torrents/%s", f.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 (f Fedora) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
if err := common.RemoveTorrents(f.NameSubstr, f.Relver, transmissionbt); err != nil {
return err
}
return nil
}