2013-09-19 15:00:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-17 14:20:02 +08:00
|
|
|
"bufio"
|
2016-04-03 14:37:17 +08:00
|
|
|
"encoding/hex"
|
2015-06-22 17:50:29 +08:00
|
|
|
"encoding/json"
|
2013-09-19 15:00:51 +08:00
|
|
|
"fmt"
|
2017-11-08 16:33:05 +08:00
|
|
|
"io"
|
2013-09-19 15:00:51 +08:00
|
|
|
"log"
|
2015-06-22 17:50:29 +08:00
|
|
|
"os"
|
2014-04-08 17:39:34 +08:00
|
|
|
|
2018-06-17 14:20:02 +08:00
|
|
|
"github.com/anacrolix/envpprof"
|
2016-04-03 14:37:17 +08:00
|
|
|
"github.com/anacrolix/tagflag"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2013-09-19 15:00:51 +08:00
|
|
|
)
|
|
|
|
|
2016-04-03 14:37:17 +08:00
|
|
|
var flags struct {
|
|
|
|
JustName bool
|
|
|
|
PieceHashes bool
|
2016-05-03 10:51:31 +08:00
|
|
|
Files bool
|
2016-04-03 14:37:17 +08:00
|
|
|
tagflag.StartPos
|
2017-11-08 16:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func processReader(r io.Reader) error {
|
|
|
|
metainfo, err := metainfo.Load(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
info, err := metainfo.UnmarshalInfo()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error unmarshalling info: %s", err)
|
|
|
|
}
|
|
|
|
if flags.JustName {
|
|
|
|
fmt.Printf("%s\n", info.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
d := map[string]interface{}{
|
|
|
|
"Name": info.Name,
|
|
|
|
"NumPieces": info.NumPieces(),
|
|
|
|
"PieceLength": info.PieceLength,
|
|
|
|
"InfoHash": metainfo.HashInfoBytes().HexString(),
|
|
|
|
"NumFiles": len(info.UpvertedFiles()),
|
|
|
|
"TotalLength": info.TotalLength(),
|
|
|
|
"Announce": metainfo.Announce,
|
|
|
|
"AnnounceList": metainfo.AnnounceList,
|
|
|
|
"UrlList": metainfo.UrlList,
|
|
|
|
}
|
2021-01-06 17:27:17 +08:00
|
|
|
if len(metainfo.Nodes) > 0 {
|
2020-05-21 00:18:00 +08:00
|
|
|
d["Nodes"] = metainfo.Nodes
|
|
|
|
}
|
2017-11-08 16:33:05 +08:00
|
|
|
if flags.Files {
|
2019-07-17 13:28:49 +08:00
|
|
|
d["Files"] = info.UpvertedFiles()
|
2017-11-08 16:33:05 +08:00
|
|
|
}
|
|
|
|
if flags.PieceHashes {
|
|
|
|
d["PieceHashes"] = func() (ret []string) {
|
2021-09-14 11:46:50 +08:00
|
|
|
for i, numPieces := 0, info.NumPieces(); i < numPieces; i += 1 {
|
2017-11-08 16:33:05 +08:00
|
|
|
ret = append(ret, hex.EncodeToString(info.Pieces[i*20:(i+1)*20]))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
b, _ := json.MarshalIndent(d, "", " ")
|
|
|
|
_, err = os.Stdout.Write(b)
|
|
|
|
return err
|
2016-04-03 14:37:17 +08:00
|
|
|
}
|
|
|
|
|
2013-09-19 15:00:51 +08:00
|
|
|
func main() {
|
2018-06-17 14:20:02 +08:00
|
|
|
defer envpprof.Stop()
|
2016-04-03 14:37:17 +08:00
|
|
|
tagflag.Parse(&flags)
|
2018-06-17 14:20:02 +08:00
|
|
|
err := processReader(bufio.NewReader(os.Stdin))
|
2017-11-08 16:33:05 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2013-09-19 15:00:51 +08:00
|
|
|
}
|
2016-04-03 14:37:17 +08:00
|
|
|
if !flags.JustName {
|
2015-07-04 11:30:26 +08:00
|
|
|
os.Stdout.WriteString("\n")
|
|
|
|
}
|
2013-09-19 15:00:51 +08:00
|
|
|
}
|