本文整理汇总了Golang中github.com/anacrolix/torrent/metainfo.LoadFromFile函数的典型用法代码示例。如果您正苦于以下问题:Golang LoadFromFile函数的具体用法?Golang LoadFromFile怎么用?Golang LoadFromFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LoadFromFile函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
flag.Parse()
metaInfo, err := metainfo.LoadFromFile(*torrentPath)
if err != nil {
log.Fatal(err)
}
mMapSpan := &mmap_span.MMapSpan{}
if len(metaInfo.Info.Files) > 0 {
for _, file := range metaInfo.Info.Files {
filename := filepath.Join(append([]string{*dataPath, metaInfo.Info.Name}, file.Path...)...)
goMMap := fileToMmap(filename, file.Length)
mMapSpan.Append(goMMap)
}
log.Println(len(metaInfo.Info.Files))
} else {
goMMap := fileToMmap(*dataPath, metaInfo.Info.Length)
mMapSpan.Append(goMMap)
}
log.Println(mMapSpan.Size())
log.Println(len(metaInfo.Info.Pieces))
info := metaInfo.Info
for i := range iter.N(metaInfo.Info.NumPieces()) {
p := info.Piece(i)
hash := sha1.New()
_, err := io.Copy(hash, io.NewSectionReader(mMapSpan, p.Offset(), p.Length()))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d: %x: %v\n", i, p.Hash(), bytes.Equal(hash.Sum(nil), p.Hash().Bytes()))
}
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:32,代码来源:main.go
示例2: AddTorrentFromFile
func (cl *Client) AddTorrentFromFile(filename string) (T *Torrent, err error) {
mi, err := metainfo.LoadFromFile(filename)
if err != nil {
return
}
return cl.AddTorrent(mi)
}
开发者ID:jakop345,项目名称:torrent,代码行数:7,代码来源:client.go
示例3: main
func main() {
tagflag.Parse(&flags)
for _, filename := range flags.TorrentFiles {
metainfo, err := metainfo.LoadFromFile(filename)
if err != nil {
log.Print(err)
continue
}
info := &metainfo.Info.Info
if flags.JustName {
fmt.Printf("%s\n", metainfo.Info.Name)
continue
}
d := map[string]interface{}{
"Name": info.Name,
"NumPieces": info.NumPieces(),
"PieceLength": info.PieceLength,
}
if flags.PieceHashes {
d["PieceHashes"] = func() (ret []string) {
for i := range iter.N(info.NumPieces()) {
ret = append(ret, hex.EncodeToString(info.Pieces[i*20:(i+1)*20]))
}
return
}()
}
b, _ := json.MarshalIndent(d, "", " ")
os.Stdout.Write(b)
}
if !flags.JustName {
os.Stdout.WriteString("\n")
}
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:33,代码来源:main.go
示例4: torrentFileInfoHash
func torrentFileInfoHash(fileName string) (ih torrent.InfoHash, ok bool) {
mi, _ := metainfo.LoadFromFile(fileName)
if mi == nil {
return
}
util.CopyExact(ih[:], mi.Info.Hash)
ok = true
return
}
开发者ID:gbjk,项目名称:torrent,代码行数:9,代码来源:dirwatch.go
示例5: main
func main() {
flag.Parse()
for _, arg := range flag.Args() {
mi, err := metainfo.LoadFromFile(arg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%x: %s\n", mi.Info.Hash, arg)
}
}
开发者ID:pmwoodward3,项目名称:torrentsaga,代码行数:10,代码来源:main.go
示例6: argSpec
func argSpec(arg string) (ts *torrent.TorrentSpec, err error) {
if strings.HasPrefix(arg, "magnet:") {
return torrent.TorrentSpecFromMagnetURI(arg)
}
mi, err := metainfo.LoadFromFile(arg)
if err != nil {
return
}
ts = torrent.TorrentSpecFromMetaInfo(mi)
return
}
开发者ID:jaswinder-singh,项目名称:torrentsaga,代码行数:11,代码来源:main.go
示例7: addTorrents
func addTorrents(client *torrent.Client) {
for _, arg := range opts.Torrent {
t := func() *torrent.Torrent {
if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg)
if err != nil {
log.Fatalf("error adding magnet: %s", err)
}
return t
} else {
metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
os.Exit(1)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
}
return t
}
}()
torrentBar(t)
err := t.AddPeers(func() (ret []torrent.Peer) {
for _, ta := range opts.TestPeer {
ret = append(ret, torrent.Peer{
IP: ta.IP,
Port: ta.Port,
})
}
return
}())
if err != nil {
log.Fatal(err)
}
go func() {
<-t.GotInfo()
t.DownloadAll()
}()
}
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:41,代码来源:main.go
示例8: main
func main() {
name := flag.Bool("name", false, "print name")
flag.Parse()
for _, filename := range flag.Args() {
metainfo, err := metainfo.LoadFromFile(filename)
if err != nil {
log.Print(err)
continue
}
if *name {
fmt.Printf("%s\n", metainfo.Info.Name)
continue
}
d := map[string]interface{}{
"Name": metainfo.Info.Name,
"NumPieces": metainfo.Info.NumPieces(),
}
b, _ := json.MarshalIndent(d, "", " ")
os.Stdout.Write(b)
}
if !*name {
os.Stdout.WriteString("\n")
}
}
开发者ID:jaswinder-singh,项目名称:torrentsaga,代码行数:24,代码来源:main.go
示例9: main
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
var rootGroup struct {
Client torrent.Config `group:"Client Options"`
TestPeers []string `long:"test-peer" description:"address of peer to inject to every torrent"`
MMap bool `long:"mmap" description:"memory-map the torrent files"`
}
// Don't pass flags.PrintError because it's inconsistent with printing.
// https://github.com/jessevdk/go-flags/issues/132
parser := flags.NewParser(&rootGroup, flags.HelpFlag|flags.PassDoubleDash)
parser.Usage = "[OPTIONS] (magnet URI or .torrent file path)..."
posArgs, err := parser.Parse()
if err != nil {
fmt.Fprintln(os.Stderr, "Download from the BitTorrent network.")
fmt.Println(err)
os.Exit(2)
}
testPeers, err := resolvedPeerAddrs(rootGroup.TestPeers)
if err != nil {
log.Fatal(err)
}
if rootGroup.MMap {
rootGroup.Client.TorrentDataOpener = func(info *metainfo.Info) torrent.Data {
ret, err := mmap.TorrentData(info, "")
if err != nil {
log.Fatalf("error opening torrent data for %q: %s", info.Name, err)
}
return ret
}
}
if len(posArgs) == 0 {
fmt.Fprintln(os.Stderr, "no torrents specified")
return
}
client, err := torrent.NewClient(&rootGroup.Client)
if err != nil {
log.Fatalf("error creating client: %s", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
client.WriteStatus(w)
})
defer client.Close()
for _, arg := range posArgs {
t := func() torrent.Torrent {
if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg)
if err != nil {
log.Fatalf("error adding magnet: %s", err)
}
return t
} else {
metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil {
log.Fatal(err)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
}
return t
}
}()
err := t.AddPeers(testPeers)
if err != nil {
log.Fatal(err)
}
go func() {
<-t.GotInfo()
t.DownloadAll()
}()
}
done := make(chan struct{})
go func() {
defer close(done)
if client.WaitAll() {
log.Print("downloaded ALL the torrents")
} else {
log.Fatal("y u no complete torrents?!")
}
}()
ticker := time.NewTicker(time.Second)
waitDone:
for {
select {
case <-done:
break waitDone
case <-ticker.C:
os.Stdout.WriteString(progressLine(client))
}
}
if rootGroup.Client.Seed {
select {}
}
}
开发者ID:soul9,项目名称:torrent,代码行数:95,代码来源:main.go
示例10: main
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
var opts struct {
torrent.Config `name:"Client"`
Mmap bool `help:"memory-map torrent data"`
TestPeer []*net.TCPAddr `short:"p" help:"addresses of some starting peers"`
Torrent []string `type:"pos" arity:"+" help:"torrent file path or magnet uri"`
}
tagflag.Parse(&opts, tagflag.SkipBadTypes())
clientConfig := opts.Config
if opts.Mmap {
clientConfig.TorrentDataOpener = func(info *metainfo.Info) torrent.Data {
ret, err := mmap.TorrentData(info, "")
if err != nil {
log.Fatalf("error opening torrent data for %q: %s", info.Name, err)
}
return ret
}
}
torrents := opts.Torrent
if len(torrents) == 0 {
fmt.Fprintf(os.Stderr, "no torrents specified\n")
return
}
client, err := torrent.NewClient(&clientConfig)
if err != nil {
log.Fatalf("error creating client: %s", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
client.WriteStatus(w)
})
defer client.Close()
for _, arg := range torrents {
t := func() torrent.Torrent {
if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg)
if err != nil {
log.Fatalf("error adding magnet: %s", err)
}
return t
} else {
metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
os.Exit(1)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
}
return t
}
}()
err := t.AddPeers(func() (ret []torrent.Peer) {
for _, ta := range opts.TestPeer {
ret = append(ret, torrent.Peer{
IP: ta.IP,
Port: ta.Port,
})
}
return
}())
if err != nil {
log.Fatal(err)
}
go func() {
<-t.GotInfo()
t.DownloadAll()
}()
}
done := make(chan struct{})
go func() {
defer close(done)
if client.WaitAll() {
log.Print("downloaded ALL the torrents")
} else {
log.Fatal("y u no complete torrents?!")
}
}()
ticker := time.NewTicker(time.Second)
waitDone:
for {
select {
case <-done:
break waitDone
case <-ticker.C:
os.Stdout.WriteString(progressLine(client))
}
}
if opts.Seed {
select {}
}
}
开发者ID:ssi379,项目名称:torrent,代码行数:94,代码来源:main.go
注:本文中的github.com/anacrolix/torrent/metainfo.LoadFromFile函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论