2023-10-26 15:56:04 +02:00
|
|
|
package fedora
|
2023-06-24 04:33:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2024-03-30 06:23:56 +01:00
|
|
|
"codeberg.org/hyperreal/go-torrent-helper/common"
|
2023-07-19 01:03:01 +02:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2023-06-24 04:33:24 +02:00
|
|
|
"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)
|
2023-07-19 01:03:01 +02:00
|
|
|
|
|
|
|
// Send HTTP GET request and receive response
|
|
|
|
f.URL = "https://torrent.fedoraproject.org"
|
2023-07-18 04:54:36 +02:00
|
|
|
respBody, err := common.GetResponse(f.URL)
|
2023-06-24 04:33:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-19 01:03:01 +02:00
|
|
|
// Get a goquery doc from response body
|
|
|
|
doc, err := goquery.NewDocumentFromReader(respBody)
|
2023-07-18 04:54:36 +02:00
|
|
|
if err != nil {
|
2023-07-19 01:03:01 +02:00
|
|
|
return err
|
2023-07-18 04:54:36 +02:00
|
|
|
}
|
|
|
|
|
2023-07-19 01:03:01 +02:00
|
|
|
// Extract torrent URLs from web page source
|
2023-06-24 04:33:24 +02:00
|
|
|
var torrentURLs []string
|
2023-07-19 01:03:01 +02:00
|
|
|
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()))
|
2023-06-24 04:33:24 +02:00
|
|
|
}
|
2023-07-19 01:03:01 +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 (f Fedora) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
|
|
|
|
if err := common.RemoveTorrents(f.NameSubstr, f.Relver, transmissionbt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|