Refactor Fedora AddNewTorrents

This commit is contained in:
Jeffrey Serio 2023-07-18 18:03:01 -05:00
parent cf928cf05b
commit 7818fe5ab8
1 changed files with 12 additions and 15 deletions

View File

@ -2,11 +2,10 @@ package fedora
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"regexp"
"strings" "strings"
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc" "github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common" "tildegit.org/hyperreal/go-torrent-helper/common"
) )
@ -18,30 +17,28 @@ type Fedora struct {
} }
func (f Fedora) AddNewTorrents(transmissionbt *transmissionrpc.Client) error { 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) 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) respBody, err := common.GetResponse(f.URL)
if err != nil { if err != nil {
return err return err
} }
dataInBytes, err := ioutil.ReadAll(respBody) // Get a goquery doc from response body
doc, err := goquery.NewDocumentFromReader(respBody)
if err != nil { if err != nil {
return fmt.Errorf("Error reading response body: %s\n", err) return err
} }
bodyText := string(dataInBytes) // Extract torrent URLs from web page source
// Extract torrent URLs from web page contents
var torrentURLs []string var torrentURLs []string
re := regexp.MustCompile(`(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])`) doc.Find("a").Each(func(i int, s *goquery.Selection) {
match := re.FindAllString(bodyText, 1000) if strings.Contains(s.Text(), torrentSubstr) {
for _, v := range match { torrentURLs = append(torrentURLs, fmt.Sprintf("%s/torrents/%s", f.URL, s.Text()))
if strings.Contains(v, torrentSubstr) {
torrentURLs = append(torrentURLs, v)
} }
} })
// Add torrents to Transmission instance // Add torrents to Transmission instance
for _, torrentURL := range torrentURLs { for _, torrentURL := range torrentURLs {