Refactor Fedora AddNewTorrents

This commit is contained in:
Jeffrey Serio 2023-07-18 18:03:01 -05:00
parent cf928cf05b
commit 7818fe5ab8

View File

@ -2,11 +2,10 @@ package fedora
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
@ -18,30 +17,28 @@ type Fedora struct {
}
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)
// Send HTTP GET request and receive response
f.URL = "https://torrent.fedoraproject.org"
respBody, err := common.GetResponse(f.URL)
if err != nil {
return err
}
dataInBytes, err := ioutil.ReadAll(respBody)
// Get a goquery doc from response body
doc, err := goquery.NewDocumentFromReader(respBody)
if err != nil {
return fmt.Errorf("Error reading response body: %s\n", err)
return err
}
bodyText := string(dataInBytes)
// Extract torrent URLs from web page contents
// Extract torrent URLs from web page source
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)
}
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()))
}
})
// Add torrents to Transmission instance
for _, torrentURL := range torrentURLs {