go-torrent-helper/fedora/main.go

68 lines
1.5 KiB
Go
Raw Normal View History

2023-06-24 04:33:24 +02:00
package fedora
import (
"fmt"
2023-07-18 04:54:36 +02:00
"io/ioutil"
2023-06-24 04:33:24 +02:00
"log"
"regexp"
"strings"
"github.com/hekmon/transmissionrpc"
2023-07-18 04:54:36 +02:00
"tildegit.org/hyperreal/go-torrent-helper/common"
2023-06-24 04:33:24 +02:00
)
type Fedora struct {
NameSubstr string
Relver string
URL string
}
func (f Fedora) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Send HTTP GET request and receive response
f.URL = "https://torrent.fedoraproject.org/"
torrentSubstr := fmt.Sprintf("%s.torrent", f.Relver)
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-18 04:54:36 +02:00
dataInBytes, err := ioutil.ReadAll(respBody)
if err != nil {
return fmt.Errorf("Error reading response body: %s\n", err)
}
2023-06-24 04:33:24 +02:00
bodyText := string(dataInBytes)
// Extract torrent URLs from web page contents
var torrentURLs []string
re := regexp.MustCompile(`(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])`)
match := re.FindAllString(bodyText, 1000)
for _, v := range match {
if strings.Contains(v, torrentSubstr) {
torrentURLs = append(torrentURLs, v)
}
}
// 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
}