func (v Video) createTorrent() error { log.Println("creating torrent") f, err := os.Create(v.torrentFilename()) defer f.Close() if err != nil { return err } b := torrent.Builder{} b.AddFile(v.OutputFilename) for _, group := range builtinAnnounceList { b.AddAnnounceGroup(group) } batch, err := b.Submit() if err != nil { log.Fatal(err) } errs, status := batch.Start(f, runtime.NumCPU()) lastProgress := int64(-1) for { select { case err, ok := <-errs: if !ok || err == nil { return err } log.Print(err) case bytesDone := <-status: progress := 100 * bytesDone / batch.TotalSize() if progress != lastProgress { log.Printf("%d%%", progress) lastProgress = progress } } } }
// Writes to w, a metainfo containing the file at name. func CreateMetaInfo(name string, w io.Writer) { builder := metainfo.Builder{} builder.AddFile(name) builder.AddAnnounceGroup([]string{"lol://cheezburger"}) builder.SetPieceLength(5) batch, err := builder.Submit() if err != nil { panic(err) } errs, _ := batch.Start(w, 1) <-errs }
func main() { b := torrent.Builder{} for _, filename := range flag.Args() { if err := filepath.Walk(filename, func(path string, info os.FileInfo, err error) error { if _, err := os.Stat(path); os.IsNotExist(err) { return err } log.Print(path) if info.IsDir() { return nil } b.AddFile(path) return nil }); err != nil { log.Print(err) } } for _, group := range builtinAnnounceList { b.AddAnnounceGroup(group) } batch, err := b.Submit() if err != nil { log.Fatal(err) } errs, status := batch.Start(os.Stdout, runtime.NumCPU()) lastProgress := int64(-1) for { select { case err, ok := <-errs: if !ok || err == nil { return } log.Print(err) case bytesDone := <-status: progress := 100 * bytesDone / batch.TotalSize() if progress != lastProgress { log.Printf("%d%%", progress) lastProgress = progress } } } }
func (b *Builtin) CreateTorrent(path, torrentPath string, trackers []string) (err error) { builder := metainfo.Builder{} builder.AddFile(path) builder.AddAnnounceGroup(trackers) batch, err := builder.Submit() if err != nil { return } torrentFile, err := os.Create(torrentPath) if err != nil { return } defer torrentFile.Close() errs, _ := batch.Start(torrentFile, runtime.NumCPU()) err = <-errs if err != nil { return } return }