feat: save torrent link to history

This commit is contained in:
Simon Ding
2024-10-09 23:56:04 +08:00
parent 6527f843d8
commit 7e4d907ef6
14 changed files with 838 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package utils
import (
"encoding/json"
"net/http"
"os"
"os/exec"
"regexp"
@@ -10,6 +11,7 @@ import (
"strings"
"unicode"
"github.com/anacrolix/torrent/metainfo"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"golang.org/x/exp/rand"
@@ -218,3 +220,34 @@ func isWSL() bool {
}
return strings.Contains(strings.ToLower(string(releaseData)), "microsoft")
}
func Link2Magnet(link string) (string, error) {
if strings.HasPrefix(strings.ToLower(link), "magnet:") {
return link, nil
}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse //do not follow redirects
},
}
resp, err := client.Get(link)
if err != nil {
return "", errors.Wrap(err, "get link")
}
defer resp.Body.Close()
if resp.StatusCode >= 300 && resp.StatusCode < 400 {
//redirects
tourl := resp.Header.Get("Location")
return Link2Magnet(tourl)
}
info, err := metainfo.Load(resp.Body)
if err != nil {
return "", errors.Wrap(err, "parse response")
}
mg, err := info.MagnetV2()
if err != nil {
return "", errors.Wrap(err, "convert magnet")
}
return mg.String(), nil
}