go-torrent-helper/kali/main.go

63 lines
1.4 KiB
Go
Raw Normal View History

2024-03-30 01:59:29 +01:00
package kali
import (
"fmt"
"log"
"strings"
2024-03-30 06:23:56 +01:00
"codeberg.org/hyperreal/go-torrent-helper/common"
2024-03-30 01:59:29 +01:00
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc"
)
type Kali struct {
NameSubstr string
Relver string
URL string
}
func (k Kali) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Send HTTP GET request and receive response
2024-03-30 13:58:02 +01:00
k.URL = fmt.Sprintf("https://cdimage.kali.org/kali-%s", k.Relver)
2024-03-30 01:59:29 +01:00
respBody, err := common.GetResponse(k.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(), ".torrent") {
2024-03-30 13:58:02 +01:00
torrentURLs = append(torrentURLs, fmt.Sprintf("%s/%s", k.URL, s.Text()))
2024-03-30 01:59:29 +01: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 (k Kali) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
if err := common.RemoveTorrents(k.NameSubstr, k.Relver, transmissionbt); err != nil {
return err
}
return nil
}