From 7818fe5ab851f0de9ae4e806eb57817f1e1c112d Mon Sep 17 00:00:00 2001 From: Jeffrey Serio <23226432+hyperreal64@users.noreply.github.com> Date: Tue, 18 Jul 2023 18:03:01 -0500 Subject: [PATCH] Refactor Fedora AddNewTorrents --- fedora/main.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/fedora/main.go b/fedora/main.go index fdf1bd3..b29ddea 100644 --- a/fedora/main.go +++ b/fedora/main.go @@ -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 {