Skip to content

Instantly share code, notes, and snippets.

@smartass08
Created September 11, 2020 10:31
Show Gist options
  • Save smartass08/c16a02f73fd5cdc239348e476ee0655f to your computer and use it in GitHub Desktop.
Save smartass08/c16a02f73fd5cdc239348e476ee0655f to your computer and use it in GitHub Desktop.
Torrent downloader written in go using rain library (Toy experiment)
package main
import (
"github.com/cenkalti/rain/torrent"
"github.com/cheggaaa/pb/v3"
"os"
"sync"
"time"
)
var wg sync.WaitGroup
func progressbar(tor *torrent.Torrent){
defer wg.Done()
s := tor.Stats()
bar := pb.StartNew(int(s.Bytes.Total))
bar.Set(pb.Bytes, true)
bar.Set(pb.SIBytesPrefix, true)
for range time.Tick(time.Second) {
s := tor.Stats()
bar.SetCurrent(s.Bytes.Total-s.Bytes.Incomplete)
if s.Bytes.Downloaded == s.Bytes.Total{
bar.Finish()
}
for s.Status.String() == "Stopped" {
return
}
}
}
func GetConfig()torrent.Config{
_ = os.Remove("data.db")
config := torrent.DefaultConfig
config.DataDir = "0"
config.Database = "data.db"
config.RPCEnabled = false
config.ResumeOnStartup = false
config.SpeedLimitUpload = 1
return config
}
func main() {
magnet := os.Args[1]
ses, _ := torrent.NewSession(GetConfig())
lol := torrent.AddTorrentOptions{}
lol.StopAfterDownload = true
tor, _ := ses.AddURI(magnet, &lol)
wg.Add(1)
go progressbar(tor)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment