Browse Source

initial commit

main
Unbewohnte 3 years ago
commit
b895c47c0b
  1. 9
      LICENSE
  2. 25
      README.md
  3. 3
      go.mod
  4. 98
      magnetInfo.go
  5. 22
      magnetInfo_test.go

9
LICENSE

@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright © 2021 Unbewohne | Nikolay Kasyanov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

25
README.md

@ -0,0 +1,25 @@
# magnet-info
## Parse a magnet link into a struct
# Installation
```
go get github.com/Unbewohnte/magnet-info
```
# Usage
```
package main
import (
"fmt"
magnetinfo "magnet-info"
)
func main() {
info, _ := magnetinfo.Parse("magnet:?xt=urn:btih:83918ea4bb488cefd3d8b8b8762597d32aebb4fa&tr=http%3A%2F%2Facademictorrents.com%2Fannounce.php&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969")
fmt.Printf("%+v", info)
// ...
}
```

3
go.mod

@ -0,0 +1,3 @@
module github.com/Unbewohnte/magnetInfo
go 1.17

98
magnetInfo.go

@ -0,0 +1,98 @@
package magnetinfo
import (
"fmt"
"net/url"
"strconv"
"strings"
)
// Contains all magnet link parameters
type MagnetInfo struct {
DisplayNames []string // DN
ExactLength uint64 // XL
ExactTopics []string // XT
WebSeeds []string // WS
AcceptableSources []string // AS
ExactSources []string // XS
KeywordTopics []string // KT
ManifestTopics []string // MT
AddressTrackers []string // TR
}
// Parses a magnet link and returns its info
func Parse(magnetLink string) (*MagnetInfo, error) {
// split a magnet link identifier and key-value pairs
parts := strings.Split(magnetLink, ":?")
if len(parts) == 1 || parts[0] != "magnet" {
return nil, fmt.Errorf("given string does not contain a \"magnet\" identifier")
}
// get all key-value pairs
pairs := strings.Split(parts[1], "&")
var info MagnetInfo
for _, pair := range pairs {
// get key and its value
keyval := strings.Split(pair, "=")
key := keyval[0]
value := keyval[1]
switch strings.ToLower(key) {
case "dn":
value, err := url.QueryUnescape(value)
if err != nil {
return nil, err
}
info.DisplayNames = append(info.DisplayNames, value)
case "xl":
length, err := strconv.ParseUint(value, 10, 64)
if err != nil {
fmt.Println(err)
}
info.ExactLength = length
case "xt":
info.ExactTopics = append(info.ExactTopics, value)
case "ws":
info.WebSeeds = append(info.WebSeeds, value)
case "as":
value, err := url.QueryUnescape(value)
if err != nil {
return nil, err
}
info.AcceptableSources = append(info.AcceptableSources, value)
case "xs":
info.ExactSources = append(info.ExactSources, value)
case "kt":
value, err := url.QueryUnescape(value)
if err != nil {
return nil, err
}
info.KeywordTopics = append(info.KeywordTopics, value)
case "mt":
value, err := url.QueryUnescape(value)
if err != nil {
return nil, err
}
info.ManifestTopics = append(info.ManifestTopics, value)
case "tr":
value, err := url.QueryUnescape(value)
if err != nil {
return nil, err
}
info.AddressTrackers = append(info.AddressTrackers, value)
}
}
return &info, nil
}

22
magnetInfo_test.go

@ -0,0 +1,22 @@
package magnetinfo
import (
"testing"
)
func Test_Parse(t *testing.T) {
magnets := []string{
"magnet:?xt=urn:btih:OVFYDTCS55I7T5JLQFZA2NH6LKZI5XDM",
"magnet:?xt=urn:btih:83918ea4bb488cefd3d8b8b8762597d32aebb4fa&tr=http%3A%2F%2Facademictorrents.com%2Fannounce.php&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969",
"magnet:?xt=urn:btih:e8b1f9c5bf555fe58bc73addb83457dd6da69630&tr=http%3A%2F%2Facademictorrents.com%2Fannounce.php&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969",
}
for _, magnet := range magnets {
_, err := Parse(magnet)
if err != nil {
t.Errorf("Parse failed: %s", err)
}
}
}
Loading…
Cancel
Save