Fix torrent URL extraction

This commit is contained in:
Jeffrey Serio 2023-07-17 21:54:36 -05:00
parent 9822eaedc4
commit cf928cf05b
11 changed files with 98 additions and 45 deletions

View File

@ -4,8 +4,8 @@ import (
"fmt"
"log"
"tildegit.org/hyperreal/go-torrent-helper/common"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
type AlmaLinux struct {

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/almalinux"
"tildegit.org/hyperreal/go-torrent-helper/debian"
"tildegit.org/hyperreal/go-torrent-helper/fedora"
@ -13,7 +14,6 @@ import (
"tildegit.org/hyperreal/go-torrent-helper/parrot"
"tildegit.org/hyperreal/go-torrent-helper/qubes"
"tildegit.org/hyperreal/go-torrent-helper/rocky"
"github.com/hekmon/transmissionrpc"
)
type Distro interface {

View File

@ -2,7 +2,7 @@ package common
import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"strings"
@ -10,7 +10,7 @@ import (
"github.com/hekmon/transmissionrpc"
)
func GetResponse(url string) ([]byte, error) {
func GetResponse(url string) (io.ReadCloser, error) {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("User-Agent", "go-torrent-helper")
@ -33,13 +33,8 @@ func GetResponse(url string) ([]byte, error) {
return nil, fmt.Errorf("Error: %d %s\n", resp.StatusCode, resp.Status)
}
// Return response as bytes
dataInBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error reading response: %s\n", err)
}
return dataInBytes, nil
// Return response body
return resp.Body, nil
}
func values[M ~map[K]V, K comparable, V any](m M) []V {

2
debian/main.go vendored
View File

@ -4,8 +4,8 @@ import (
"fmt"
"log"
"tildegit.org/hyperreal/go-torrent-helper/common"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
type Debian struct {

View File

@ -2,12 +2,13 @@ package fedora
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
"tildegit.org/hyperreal/go-torrent-helper/common"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
type Fedora struct {
@ -20,11 +21,16 @@ 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)
dataInBytes, err := common.GetResponse(f.URL)
respBody, err := common.GetResponse(f.URL)
if err != nil {
return err
}
dataInBytes, err := ioutil.ReadAll(respBody)
if err != nil {
return fmt.Errorf("Error reading response body: %s\n", err)
}
bodyText := string(dataInBytes)
// Extract torrent URLs from web page contents

3
go.mod
View File

@ -5,6 +5,9 @@ go 1.20
require github.com/hekmon/transmissionrpc v1.1.0
require (
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hekmon/cunits/v2 v2.0.2 // indirect
golang.org/x/net v0.7.0 // indirect
)

35
go.sum
View File

@ -1,6 +1,41 @@
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hekmon/cunits/v2 v2.0.2 h1:qnZMfVxBiTgtAXVSMLKxzV1nihS6KzdjDHgh+nS4RgI=
github.com/hekmon/cunits/v2 v2.0.2/go.mod h1:9r1TycXYXaTmEWlAIfFV8JT+Xo59U96yUJAYHxzii2M=
github.com/hekmon/transmissionrpc v1.1.0 h1:58xY27x2JYxaMlIj7ycKnxqgCm3IjvTxfB7cHPLxOfs=
github.com/hekmon/transmissionrpc v1.1.0/go.mod h1:qkwhsyD/MQSlWvOE1AC92xajwEveAuGsOvTuOBZEuHc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -2,10 +2,12 @@ package nixos
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"tildegit.org/hyperreal/go-torrent-helper/common"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
type Nixos struct {
@ -17,11 +19,16 @@ type Nixos struct {
func (n Nixos) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Send GET request and receive HTTP response
n.URL = "https://api.github.com/repos/AnimMouse/NixOS-ISO-Torrents/releases/latest"
dataInBytes, err := common.GetResponse(n.URL)
respBody, err := common.GetResponse(n.URL)
if err != nil {
return err
}
dataInBytes, err := ioutil.ReadAll(respBody)
if err != nil {
return fmt.Errorf("Error reading response body: %s\n", err)
}
result := make(map[string]interface{})
if err := json.Unmarshal(dataInBytes, &result); err != nil {
return err

View File

@ -3,9 +3,9 @@ package parrot
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
@ -17,24 +17,25 @@ type Parrot struct {
}
func (p Parrot) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Parrot Security
p.URL = fmt.Sprintf("https://deb.parrot.sh/parrot/iso/%s/", p.Relver)
dataInBytes, err := common.GetResponse(p.URL)
respBody, err := common.GetResponse(p.URL)
if err != nil {
return err
}
bodyText := string(dataInBytes)
// Get a goquery doc from response body
doc, err := goquery.NewDocumentFromReader(respBody)
if err != nil {
return err
}
// 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") {
torrentURLs = append(torrentURLs, v)
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), "torrent") {
torrentURLs = append(torrentURLs, fmt.Sprintf("%s/%s", p.URL, s.Text()))
}
}
})
// Add torrents to Transmission instance
for _, torrentURL := range torrentURLs {

View File

@ -1,10 +1,11 @@
package qubes
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
@ -17,23 +18,25 @@ type Qubes struct {
func (q Qubes) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
// Set torrentURLs for Qubes OS
q.URL = "https://mirrors.edge.kernel.org/qubes/iso/Qubes/iso/"
dataInBytes, err := common.GetResponse(q.URL)
q.URL = "https://mirrors.edge.kernel.org/qubes/iso/"
respBody, err := common.GetResponse(q.URL)
if err != nil {
return err
}
bodyText := string(dataInBytes)
// Get a goquery doc from response body
doc, err := goquery.NewDocumentFromReader(respBody)
if err != nil {
return err
}
// 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)
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), q.Relver) && strings.Contains(s.Text(), "torrent") {
torrentURLs = append(torrentURLs, fmt.Sprintf("%s/%s", q.URL, s.Text()))
}
}
})
// Add torrents to Transmission instance
for _, torrentURL := range torrentURLs {

View File

@ -3,9 +3,9 @@ package rocky
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/hekmon/transmissionrpc"
"tildegit.org/hyperreal/go-torrent-helper/common"
)
@ -21,23 +21,26 @@ func (r Rocky) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
archs := []string{"aarch64", "ppc64le", "s390x", "x86_64"}
var torrentURLs []string
// Iterate over architectures and extract torrent URLs for each
for _, arch := range archs {
r.URL = fmt.Sprintf("https://download.rockylinux.org/pub/rocky/%s/isos/%s/", r.Relver, arch)
dataInBytes, err := common.GetResponse(r.URL)
respBody, err := common.GetResponse(r.URL)
if err != nil {
return err
}
bodyText := string(dataInBytes)
// Get a goquery doc from response body
doc, err := goquery.NewDocumentFromReader(respBody)
if err != nil {
return err
}
// 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)
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), "torrent") {
torrentURLs = append(torrentURLs, fmt.Sprintf("%s/%s", r.URL, s.Text()))
}
}
})
}
// Add torrents to Transmission instance