mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-17 13:53:41 +00:00
feat: integration torrent (#30)
This commit is contained in:
1 parent
43378c7dc8
commit
034137322f
20 files changed
+925
-139
No files matched your search
@@ -0,0 +1,115 @@
|
||||
package bt
|
||||
|
||||
import (
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/monkeyWie/gopeed-core/internal/controller"
|
||||
"github.com/monkeyWie/gopeed-core/internal/fetcher"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/base"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/util"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var client *torrent.Client
|
||||
|
||||
type Fetcher struct {
|
||||
Ctl controller.Controller
|
||||
|
||||
torrent *torrent.Torrent
|
||||
opts *base.Options
|
||||
}
|
||||
|
||||
func (f *Fetcher) Setup(ctl controller.Controller) (err error) {
|
||||
f.Ctl = ctl
|
||||
var once sync.Once
|
||||
once.Do(func() {
|
||||
client, err = torrent.NewClient(nil)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Fetcher) Wait() (err error) {
|
||||
client.WaitAll()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fetcher) Resolve(req *base.Request) (res *base.Resource, err error) {
|
||||
schema := util.ParseSchema(req.URL)
|
||||
if schema == "MAGNET" {
|
||||
f.torrent, err = client.AddMagnet(req.URL)
|
||||
} else {
|
||||
f.torrent, err = client.AddTorrentFromFile(req.URL)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
<-f.torrent.GotInfo()
|
||||
res = &base.Resource{
|
||||
Req: req,
|
||||
Length: f.torrent.Length(),
|
||||
Range: true,
|
||||
Files: make([]*base.FileInfo, len(f.torrent.Files())),
|
||||
}
|
||||
for i, file := range f.torrent.Files() {
|
||||
res.Files[i] = &base.FileInfo{
|
||||
Name: filepath.Base(file.DisplayPath()),
|
||||
Path: filepath.Dir(file.Path()),
|
||||
Size: file.Length(),
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Fetcher) Create(res *base.Resource, opts *base.Options) (err error) {
|
||||
f.opts = opts
|
||||
if len(f.opts.SelectFiles) == 0 {
|
||||
f.opts.SelectFiles = make([]int, len(res.Files))
|
||||
for i := range f.opts.SelectFiles {
|
||||
f.opts.SelectFiles[i] = i
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fetcher) Start() (err error) {
|
||||
if len(f.opts.SelectFiles) == 0 {
|
||||
f.torrent.DownloadAll()
|
||||
} else {
|
||||
for _, index := range f.opts.SelectFiles {
|
||||
f.torrent.Files()[index].Download()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Fetcher) Pause() (err error) {
|
||||
f.torrent.DisallowDataDownload()
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Fetcher) Continue() (err error) {
|
||||
f.torrent.AllowDataDownload()
|
||||
return
|
||||
}
|
||||
|
||||
func (f *Fetcher) Progress() fetcher.Progress {
|
||||
p := make(fetcher.Progress, len(f.opts.SelectFiles))
|
||||
for i := range p {
|
||||
file := f.torrent.Files()[i]
|
||||
p[i] = file.BytesCompleted()
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
type FetcherBuilder struct {
|
||||
}
|
||||
|
||||
var schemes = []string{"FILE", "MAGNET"}
|
||||
|
||||
func (fb *FetcherBuilder) Schemes() []string {
|
||||
return schemes
|
||||
}
|
||||
|
||||
func (fb *FetcherBuilder) Build() fetcher.Fetcher {
|
||||
return &Fetcher{}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package bt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/monkeyWie/gopeed-core/internal/controller"
|
||||
"github.com/monkeyWie/gopeed-core/internal/fetcher"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/base"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFetcher_Resolve_Magnet(t *testing.T) {
|
||||
fetcher := buildFetcher()
|
||||
res, err := fetcher.Resolve(&base.Request{
|
||||
URL: "magnet:?xt=urn:btih:A213E40628B27B58E70F0DADAC5350CA05403AC4&dn=Microsoft%20Office%20PRO%20Plus%202021%20Retail%20Version%202108%20Build%2014326.2&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2780%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2730%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=http%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
func TestFetcher_Resolve_Torrent(t *testing.T) {
|
||||
fetcher := buildFetcher()
|
||||
res, err := fetcher.Resolve(&base.Request{
|
||||
URL: "d:/test/test.torrent",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
func TestFetcher_Create(t *testing.T) {
|
||||
fetcher := buildFetcher()
|
||||
res, err := fetcher.Resolve(&base.Request{
|
||||
URL: "magnet:?xt=urn:btih:A213E40628B27B58E70F0DADAC5350CA05403AC4&dn=Microsoft%20Office%20PRO%20Plus%202021%20Retail%20Version%202108%20Build%2014326.2&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2780%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2730%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=http%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := fetcher.Create(res, &base.Options{}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := fetcher.Start(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := fetcher.Wait(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func buildFetcher() fetcher.Fetcher {
|
||||
fetcher := new(FetcherBuilder).Build()
|
||||
fetcher.Setup(controller.NewController())
|
||||
return fetcher
|
||||
}
|
||||
Reference in new issue
Block a user