pcgamedb/utils/magnet.go
nite07 f8e3265a76 fix CleanOrphanGamesInGameInfos
mod SearchGameInfo
fix trigger body miss data
disable goggames crawler
2024-11-18 20:21:08 +08:00

67 lines
1.5 KiB
Go

package utils
import (
"bytes"
"strconv"
"strings"
"github.com/anacrolix/torrent/metainfo"
)
func ConvertTorrentToMagnet(torrent []byte) (string, string, error) {
minfo, err := metainfo.Load(bytes.NewReader(torrent))
if err != nil {
return "", "", err
}
info, err := minfo.UnmarshalInfo()
if err != nil {
return "", "", err
}
var size uint64 = uint64(info.Length)
if size == 0 {
for _, file := range info.Files {
size += uint64(file.Length)
}
}
magnet, err := minfo.MagnetV2()
if err != nil {
return "", "", err
}
return magnet.String(), BytesToSize(size), nil
}
func SubSizeStrings(sizes []string) (string, error) {
size := uint64(0)
for _, sizeStr := range sizes {
sizeStr := strings.ToLower(sizeStr)
if strings.Contains(sizeStr, "gb") {
sizeStr = strings.ReplaceAll(sizeStr, "gb", "")
sizeStr = strings.TrimSpace(sizeStr)
addSize, err := strconv.ParseFloat(sizeStr, 64)
if err != nil {
return "", err
}
size += uint64(addSize * 1024 * 1024 * 1024)
}
if strings.Contains(sizeStr, "mb") {
sizeStr = strings.ReplaceAll(sizeStr, "mb", "")
sizeStr = strings.TrimSpace(sizeStr)
addSize, err := strconv.ParseFloat(sizeStr, 64)
if err != nil {
return "", err
}
size += uint64(addSize * 1024 * 1024)
}
if strings.Contains(sizeStr, "kb") {
sizeStr = strings.ReplaceAll(sizeStr, "kb", "")
sizeStr = strings.TrimSpace(sizeStr)
addSize, err := strconv.ParseFloat(sizeStr, 64)
if err != nil {
return "", err
}
size += uint64(addSize * 1024)
}
}
return BytesToSize(size), nil
}