adding README and torrent file bits
This commit is contained in:
parent
0eb8ce1253
commit
89e4899841
3 changed files with 185 additions and 0 deletions
59
example_loader.go
Normal file
59
example_loader.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/vbatts/go-bt/bencode"
|
||||
"github.com/vbatts/go-bt/torrent"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
flOutput = flag.String("o", "", "output the re-encoded torrent to file at this path")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if len(*flOutput) > 0 && flag.NArg() > 1 {
|
||||
fmt.Fprintf(os.Stderr, "-o and multiple input files can not be used together")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, arg := range flag.Args() {
|
||||
fh, err := os.Open(arg)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
data, err := bencode.Decode(fh)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
fh.Close()
|
||||
continue
|
||||
}
|
||||
fh.Close()
|
||||
|
||||
tf, err := torrent.DecocdeTorrentData(data)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Loaded: %s (%d files)\n", tf.Info.Name, len(tf.Info.Files))
|
||||
|
||||
if len(*flOutput) > 0 {
|
||||
fhOutput, err := os.Create(*flOutput)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
continue
|
||||
}
|
||||
err = bencode.Marshal(fhOutput, *tf)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
}
|
||||
fmt.Printf("wrote: %s\n", fhOutput.Name())
|
||||
fhOutput.Close()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue