go-torrent-helper/parrot/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

2023-10-26 15:56:04 +02:00
package parrot
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 Parrot struct {
NameSubstr string
Relver string
URL string
}
func (p Parrot) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
2023-07-25 03:00:14 +02:00
p.URL = fmt.Sprintf("https://deb.parrot.sh/direct///parrot/iso/%s/", p.Relver)
2023-07-18 04:54:36 +02:00
respBody, err := common.GetResponse(p.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
var torrentURLs []string
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", p.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 (p Parrot) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
if err := common.RemoveTorrents(p.NameSubstr, p.Relver, transmissionbt); err != nil {
return err
}
return nil
}