Fix torrent URL extraction

This commit is contained in:
Jeffrey Serio 2023-07-16 21:28:25 -05:00
parent eedd50a88f
commit 9822eaedc4
3 changed files with 70 additions and 29 deletions

View File

@ -3,9 +3,11 @@ package parrot
import ( import (
"fmt" "fmt"
"log" "log"
"regexp"
"strings"
"tildegit.org/hyperreal/go-torrent-helper/common"
"github.com/hekmon/transmissionrpc" "github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
) )
type Parrot struct { type Parrot struct {
@ -16,17 +18,22 @@ type Parrot struct {
func (p Parrot) AddNewTorrents(transmissionbt *transmissionrpc.Client) error { func (p Parrot) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Parrot Security // Set torrentURLs for Parrot Security
p.URL = "https://deb.parrot.sh/parrot/" p.URL = fmt.Sprintf("https://deb.parrot.sh/parrot/iso/%s/", p.Relver)
torrentURLs := []string{ dataInBytes, err := common.GetResponse(p.URL)
fmt.Sprintf("%s/iso/%s/Parrot-security-%s_amd64.iso.torrent", p.URL, p.Relver, p.Relver), if err != nil {
fmt.Sprintf("%s/iso/%s/Parrot-security-%s_amd64.ova.torrent", p.URL, p.Relver, p.Relver), return err
fmt.Sprintf("%s/iso/%s/Parrot-security-%s_arm64.utm.torrent", p.URL, p.Relver, p.Relver), }
fmt.Sprintf("%s/iso/%s/Parrot-home-%s_amd64.iso.torrent", p.URL, p.Relver, p.Relver),
fmt.Sprintf("%s/iso/%s/Parrot-home-%s_amd64.ova.torrent", p.URL, p.Relver, p.Relver), bodyText := string(dataInBytes)
fmt.Sprintf("%s/iso/%s/Parrot-home-%s_arm64.utm.torrent", p.URL, p.Relver, p.Relver),
fmt.Sprintf("%s/iso/%s/Parrot-htb-%s_amd64.iso.torrent", p.URL, p.Relver, p.Relver), // Extract torrent URLs from web page contents
fmt.Sprintf("%s/iso/%s/Parrot-architect-%s_arm64.iso.torrent", p.URL, p.Relver, p.Relver), var torrentURLs []string
fmt.Sprintf("%s/iso/%s/Parrot-architect-%s_amd64.iso.torrent", p.URL, p.Relver, p.Relver), re := regexp.MustCompile(`(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])`)
match := re.FindAllString(bodyText, 1000)
for _, v := range match {
if strings.Contains(v, ".torrent") {
torrentURLs = append(torrentURLs, v)
}
} }
// Add torrents to Transmission instance // Add torrents to Transmission instance

View File

@ -1,11 +1,12 @@
package qubes package qubes
import ( import (
"fmt"
"log" "log"
"regexp"
"strings"
"tildegit.org/hyperreal/go-torrent-helper/common"
"github.com/hekmon/transmissionrpc" "github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
) )
type Qubes struct { type Qubes struct {
@ -16,18 +17,35 @@ type Qubes struct {
func (q Qubes) AddNewTorrents(transmissionbt *transmissionrpc.Client) error { func (q Qubes) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Qubes OS // Set torrentURLs for Qubes OS
q.URL = "https://mirrors.edge.kernel.org/qubes/iso/Qubes" q.URL = "https://mirrors.edge.kernel.org/qubes/iso/Qubes/iso/"
torrentURL := fmt.Sprintf("%s-%s-x86_64.torrent", q.URL, q.Relver) dataInBytes, err := common.GetResponse(q.URL)
// Add torrent to Transmission instance
torrent, err := transmissionbt.TorrentAdd(&transmissionrpc.TorrentAddPayload{
Filename: &torrentURL,
})
if err != nil { if err != nil {
return err return err
} }
log.Printf("%s added\n", *torrent.Name) 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, ".torrent") && strings.Contains(v, q.Relver) {
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 return nil
} }

View File

@ -3,9 +3,11 @@ package rocky
import ( import (
"fmt" "fmt"
"log" "log"
"regexp"
"strings"
"tildegit.org/hyperreal/go-torrent-helper/common"
"github.com/hekmon/transmissionrpc" "github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
) )
type Rocky struct { type Rocky struct {
@ -16,12 +18,26 @@ type Rocky struct {
func (r Rocky) AddNewTorrents(transmissionbt *transmissionrpc.Client) error { func (r Rocky) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Rocky Linux mirror // Set torrentURLs for Rocky Linux mirror
r.URL = "https://download.rockylinux.org/pub/rocky/" archs := []string{"aarch64", "ppc64le", "s390x", "x86_64"}
torrentURLs := []string{ var torrentURLs []string
fmt.Sprintf("%s/%s/isos/x86_64/Rocky-%s-x86_64-dvd.torrent", r.URL, r.Relver, r.Relver),
fmt.Sprintf("%s/%s/isos/aarch64/Rocky-%s-aarch64-dvd.torrent", r.URL, r.Relver, r.Relver), for _, arch := range archs {
fmt.Sprintf("%s/%s/isos/ppc64le/Rocky-%s-ppc64le-dvd.torrent", r.URL, r.Relver, r.Relver), r.URL = fmt.Sprintf("https://download.rockylinux.org/pub/rocky/%s/isos/%s/", r.Relver, arch)
fmt.Sprintf("%s/%s/isos/s390x/Rocky-%s-s390x-dvd.torrent", r.URL, r.Relver, r.Relver), dataInBytes, err := common.GetResponse(r.URL)
if err != nil {
return err
}
bodyText := string(dataInBytes)
// Extract torrent URLs from web page contents
re := regexp.MustCompile(`(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])`)
match := re.FindAllString(bodyText, 1000)
for _, v := range match {
if strings.Contains(v, ".torrent") {
torrentURLs = append(torrentURLs, v)
}
}
} }
// Add torrents to Transmission instance // Add torrents to Transmission instance