本文整理汇总了Golang中github.com/anacrolix/torrent.Torrent.Info方法的典型用法代码示例。如果您正苦于以下问题:Golang Torrent.Info方法的具体用法?Golang Torrent.Info怎么用?Golang Torrent.Info使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/anacrolix/torrent.Torrent
的用法示例。
在下文中一共展示了Torrent.Info方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: torrentBar
func torrentBar(t *torrent.Torrent) {
bar := uiprogress.AddBar(1)
bar.AppendCompleted()
bar.AppendFunc(func(*uiprogress.Bar) (ret string) {
select {
case <-t.GotInfo():
default:
return "getting info"
}
if t.Seeding() {
return "seeding"
} else if t.BytesCompleted() == t.Info().TotalLength() {
return "completed"
} else {
return fmt.Sprintf("downloading (%s/%s)", humanize.Bytes(uint64(t.BytesCompleted())), humanize.Bytes(uint64(t.Info().TotalLength())))
}
})
bar.PrependFunc(func(*uiprogress.Bar) string {
return t.Name()
})
go func() {
<-t.GotInfo()
bar.Total = int(t.Info().TotalLength())
for {
bc := t.BytesCompleted()
bar.Set(int(bc))
time.Sleep(time.Second)
}
}()
}
示例2: Update
func (torrent *Torrent) Update(t *torrent.Torrent) {
torrent.Name = t.Name()
torrent.Loaded = t.Info() != nil
if torrent.Loaded {
torrent.Size = t.Length()
}
totalChunks := 0
totalCompleted := 0
tfiles := t.Files()
if len(tfiles) > 0 && torrent.Files == nil {
torrent.Files = make([]*File, len(tfiles))
}
//merge in files
for i, f := range tfiles {
path := f.Path()
file := torrent.Files[i]
if file == nil {
file = &File{Path: path}
torrent.Files[i] = file
}
chunks := f.State()
file.Size = f.Length()
file.Chunks = len(chunks)
completed := 0
for _, p := range chunks {
if p.Complete {
completed++
}
}
file.Completed = completed
file.Percent = percent(int64(file.Completed), int64(file.Chunks))
file.f = f
totalChunks += file.Chunks
totalCompleted += file.Completed
}
//cacluate rate
now := time.Now()
bytes := t.BytesCompleted()
torrent.Percent = percent(bytes, torrent.Size)
if !torrent.updatedAt.IsZero() {
dt := float32(now.Sub(torrent.updatedAt))
db := float32(bytes - torrent.Downloaded)
rate := db * (float32(time.Second) / dt)
if rate >= 0 {
torrent.DownloadRate = rate
}
}
torrent.Downloaded = bytes
torrent.updatedAt = now
torrent.t = t
}