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 06:23:56 +01:00
|
|
|
"codeberg.org/hyperreal/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
|
|
|
|
}
|