torrential

package module
v0.0.0-...-341a761 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2017 License: GPL-3.0 Imports: 18 Imported by: 0

README

torrential

GoDoc Build Status Sourcegraph

The joelanford/torrential package implements a conventient service and an HTTP handler for bittorrent downloading and monitoring.

torrential.Service has methods for adding torrents from an io.Reader of the torrent file format, HTTP URLs to torrent files, and magnet links. It also has methods for retrieving all active torrents (or individual torrents by their info hash) and channels of events. It can also be configured to invoke a webhook on torrent events.

torrential.Handler wraps torrential.Service to expose the service methods via RESTful HTTP endpoints.

Installation

Assuming a correctly configured go environment, one can run the following command to install joelanford/torrential in $GOPATH

go get -u github.com/joelanford/torrential

Example

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"

	"github.com/anacrolix/torrent"
	"github.com/gorilla/mux"
	"github.com/joelanford/torrential"
	"github.com/joelanford/torrential/cache"
)

func main() {
	svc, err := torrential.NewService(&torrential.Config{
		ClientConfig: &torrent.Config{
			DataDir: "torrential-data/downloads",
		},
		Cache:      cache.NewDirectory("torrential-data/cache"),
		SeedRatio:  1.0,
		WebhookURL: "http://localhost:8080/webhook",
	})
	if err != nil {
		log.Fatal(err)
	}

	router := mux.NewRouter()
	router.PathPrefix("/webhook").HandlerFunc(dumpRequest)
	router.PathPrefix("/torrential").Handler(torrential.Handler("/torrential", svc))

	log.Fatal(http.ListenAndServe(":8080", router))
}

func dumpRequest(w http.ResponseWriter, r *http.Request) {
	output, err := httputil.DumpRequest(r, true)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		return
	}
	fmt.Println(string(output))
}

Special Thanks

Thanks to the maintainers and all of the contributors of the anacrolix/torrent project! This project is heavily dependent on it and wouldn't exist without it.

License

GPLv3 licensed. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(basePath string, svc *Service) http.Handler

Types

type Config

type Config struct {
	ClientConfig *torrent.Config
	Cache        cache.Cache
	WebhookURL   string
	SeedRatio    float64
	DropWhenDone bool
}

type Event

type Event struct {
	Type    EventType `json:"type"`
	Torrent Torrent   `json:"torrent"`
	File    *File     `json:"file,omitempty"`
	Piece   *int      `json:"piece,omitempty"`
}

type EventType

type EventType int
const (
	Added EventType = iota
	GotInfo
	PieceDone
	FileDone
	DownloadDone
	SeedingDone
	Closed
)

func (EventType) MarshalJSON

func (t EventType) MarshalJSON() (data []byte, err error)

func (EventType) String

func (t EventType) String() string

type Eventer

type Eventer interface {
	Events(done <-chan struct{}) <-chan Event
}

type EventerOptionFunc

type EventerOptionFunc func(e *TorrentEventer)

func SeedRatio

func SeedRatio(seedRatio float64) EventerOptionFunc

SeedRatio returns an OptionFunc that sets the given seed ratio when the Torrent is initialized.

type File

type File struct {
	*torrent.File
}

func (File) MarshalJSON

func (f File) MarshalJSON() ([]byte, error)

type MultiEventer

type MultiEventer struct {
	// contains filtered or unexported fields
}

func (*MultiEventer) Events

func (e *MultiEventer) Events(done <-chan struct{}) <-chan Event

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(conf *Config) (*Service, error)

func (*Service) AddMagnetURI

func (svc *Service) AddMagnetURI(magnetURI string) (*Torrent, error)

func (*Service) AddTorrentReader

func (svc *Service) AddTorrentReader(torrentReader io.Reader) (*Torrent, error)

func (*Service) AddTorrentURL

func (svc *Service) AddTorrentURL(torrentURL string) (*Torrent, error)

func (*Service) Drop

func (svc *Service) Drop(infoHash string, deleteFiles bool) error

func (*Service) Eventer

func (svc *Service) Eventer(infoHash string) (*TorrentEventer, error)

func (*Service) MultiEventer

func (svc *Service) MultiEventer() *MultiEventer

func (*Service) Torrent

func (svc *Service) Torrent(infoHash string) (*Torrent, error)

func (*Service) Torrents

func (svc *Service) Torrents() (torrents []Torrent)

type Torrent

type Torrent struct {
	*torrent.Torrent
}

func (Torrent) MarshalJSON

func (t Torrent) MarshalJSON() ([]byte, error)

type TorrentEventer

type TorrentEventer struct {
	// contains filtered or unexported fields
}

func (*TorrentEventer) Added

func (e *TorrentEventer) Added() <-chan struct{}

Added returns a channel that will be closed when the torrent is added.

func (*TorrentEventer) Closed

func (e *TorrentEventer) Closed() <-chan struct{}

Closed returns a channel that will be closed when the torrent is closed.

func (*TorrentEventer) DownloadDone

func (e *TorrentEventer) DownloadDone() <-chan struct{}

DownloadDone returns a channel that will be closed when the torrent download is complete.

func (*TorrentEventer) Events

func (e *TorrentEventer) Events(done <-chan struct{}) <-chan Event

Events returns a channel on which all of the events will be sent. The channel will be closed after the closed event is sent.

func (*TorrentEventer) FileDone

func (e *TorrentEventer) FileDone(filePath string) (<-chan struct{}, bool)

FileDone returns a channel that will be closed when the file at the given path has completed downloading.

func (*TorrentEventer) GotInfo

func (e *TorrentEventer) GotInfo() <-chan struct{}

GotInfo returns a channel that will be closed when the torrent info is ready.

func (*TorrentEventer) PieceDone

func (e *TorrentEventer) PieceDone(index int) (<-chan struct{}, bool)

func (*TorrentEventer) SeedingDone

func (e *TorrentEventer) SeedingDone() <-chan struct{}

SeedingDone returns a channel that will be closed when the torrent seeding is complete, based on the Torrent's configured seed ratio. Changes to the seed ratio after the returned channel is closed will have no effect.

func (*TorrentEventer) SetSeedRatio

func (e *TorrentEventer) SetSeedRatio(seedRatio float64)

SetSeedRatio sets the monitored seed ratio for the torrent. If the channel returned by SeedingDone() has already been closed, this will have no effect.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL