Skip to content

Commit

Permalink
progress, engine is getting quite limiting, will abandon the engine i…
Browse files Browse the repository at this point in the history
…nterface and remote engines after this commit
  • Loading branch information
jpillora committed Jul 11, 2015
1 parent 7f698e3 commit 98526bc
Show file tree
Hide file tree
Showing 25 changed files with 502 additions and 284 deletions.
12 changes: 9 additions & 3 deletions ct/engines/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ type Engine interface {
Name() string //name (lower(name)->id)
NewConfig() interface{} //*Config object
SetConfig(interface{}) error
Magnet(uri string) error
Torrents() <-chan *shared.Torrent
NewTorrent(magnetURI string) error
StartTorrent(infohash string) error
StopTorrent(infohash string) error
DeleteTorrent(infohash string) error
StartFile(infohash, filepath string) error
StopFile(infohash, filepath string) error
GetTorrents() <-chan *shared.Torrent
}

//TODO engines which require polling
type EnginePoller interface {
//Polls the status of all torrents and all files, passes updated torrents
//down the Torrents channel
Poll() error
PollTorrent(*shared.Torrent) error
}

//insert each of the cloud-torrent bundled engines
Expand Down
91 changes: 86 additions & 5 deletions ct/engines/native/native.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package native

import (
"encoding/hex"
"fmt"
"log"
"time"

"github.com/anacrolix/torrent"
Expand Down Expand Up @@ -43,22 +45,90 @@ func (n *Native) SetConfig(obj interface{}) error {
return nil
}

func (n *Native) Magnet(uri string) error {
_, err := n.client.AddMagnet(uri)
func (n *Native) NewTorrent(magnetURI string) error {
_, err := n.client.AddMagnet(magnetURI)
if err != nil {
return err
}
return nil
}

func (n *Native) Torrents() <-chan *shared.Torrent {
func (n *Native) getTorrent(infohash string) (torrent.Torrent, error) {
var t torrent.Torrent
ih, err := str2ih(infohash)
if err != nil {
return t, err
}
t, ok := n.client.Torrent(ih)
if !ok {
return t, fmt.Errorf("Missing torrent %x", ih)
}
return t, nil
}

func (n *Native) StartTorrent(infohash string) error {
log.Printf("start %s", infohash)
t, err := n.getTorrent(infohash)
if err != nil {
return err
}
t.DownloadAll()
return nil
}

func (n *Native) StopTorrent(infohash string) error {
return fmt.Errorf("Unsupported")
}

func (n *Native) DeleteTorrent(infohash string) error {
t, err := n.getTorrent(infohash)
if err != nil {
return err
}
t.Drop()
return nil
}

func (n *Native) getFile(infohash, filepath string) (file torrent.File, err error) {
t, err := n.getTorrent(infohash)
if err != nil {
return
}
for _, f := range t.Files() {
if filepath == f.Path() {
file = f
return
}
}
err = fmt.Errorf("File not found")
return
}

func (n *Native) StartFile(infohash, filepath string) error {
f, err := n.getFile(infohash, filepath)
if err != nil {
return err
}
f.PrioritizeRegion(0, f.Length())
return nil
}

func (n *Native) StopFile(infohash, filepath string) error {
return fmt.Errorf("Unsupported")
}

func (n *Native) GetTorrents() <-chan *shared.Torrent {
n.queue = make(chan *shared.Torrent)
go n.pollTorrents()
return n.queue
}

func (n *Native) pollTorrents() {
for {
time.Sleep(time.Second)
if n.client == nil {
continue
}
for _, t := range n.client.Torrents() {
//copy torrent info
st := &shared.Torrent{
Expand Down Expand Up @@ -89,11 +159,22 @@ func (n *Native) pollTorrents() {
//enqueue update
n.queue <- st
}
time.Sleep(time.Second)
}
}

//mask over TorrentDataOpener to allow torrent.Config to be parsed
func str2ih(str string) (torrent.InfoHash, error) {
var ih torrent.InfoHash
n, err := hex.Decode(ih[:], []byte(str))
if err != nil {
return ih, fmt.Errorf("Invalid hex string")
}
if n != 20 {
return ih, fmt.Errorf("Invalid length")
}
return ih, nil
}

//mask over TorrentDataOpener to allow torrent.Config to be marshalled
type config struct {
torrent.Config
TorrentDataOpener string `json:",omitempty"` //masks func
Expand Down
11 changes: 2 additions & 9 deletions ct/search-handlers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package ct

//see github.com/jpillora/scraper for config specification
//cloud-torrent uses "<id>-item" handlers
var defaultSearchConfig = []byte(`{
"google": {
"name": "Google Search",
"url": "https://www.google.com/search?q={{q}}",
"list": "#search ol > li",
"result": {
"title": "li > h3 a",
"url": ["li > h3 a", "@href", "/^\\/url\\?q=([^&]+)/"]
}
},
"kat": {
"name": "Kickass Torrents",
"url": "https://kat.cr/usearch/{{query}}/{{page:1}}/?field=seeders&sorder=desc",
Expand Down
67 changes: 46 additions & 21 deletions ct/server-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"

"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
Expand Down Expand Up @@ -64,7 +66,7 @@ func (s *Server) api(r *http.Request) error {
}

//convert torrent bytes into magnet
if action == "torrent" {
if action == "torrentfile" {
reader := bytes.NewBuffer(data)
info, err := metainfo.Load(reader)
if err != nil {
Expand Down Expand Up @@ -94,28 +96,51 @@ func (s *Server) api(r *http.Request) error {
}
case "magnet":
uri := string(data)
if err := e.Magnet(uri); err != nil {
if err := e.NewTorrent(uri); err != nil {
return fmt.Errorf("Magnet error: %s", err)
}
// case "list":
// torrents, err := e.List()
// if err != nil {
// return fmt.Errorf("List error: %s", err)
// }
// for _, t := range torrents {
// s.state.Torrents[eid][t.InfoHash] = t
// }
// s.rt.Update() //state change
// case "fetch":
// ih := string(data)
// t, ok := s.state.Torrents[eid][ih]
// if !ok {
// return fmt.Errorf("Invalid torrent: %s", ih)
// }
// if err := e.Fetch(t); err != nil {
// return fmt.Errorf("Fetch error: %s", err)
// }
// s.rt.Update() //state change
case "torrent":
cmd := strings.SplitN(string(data), ":", 2)
if len(cmd) != 2 {
return fmt.Errorf("Invalid request")
}
state := cmd[0]
infohash := cmd[1]
log.Printf("torrent api: %s -> %s", state, infohash)
if state == "start" {
if err := e.StartTorrent(infohash); err != nil {
return err
}
} else if state == "stop" {
if err := e.StopTorrent(infohash); err != nil {
return err
}
} else if state == "delete" {
if err := e.DeleteTorrent(infohash); err != nil {
return err
}
} else {
return fmt.Errorf("Invalid state: %s", state)
}
case "file":
cmd := strings.SplitN(string(data), ":", 3)
if len(cmd) != 3 {
return fmt.Errorf("Invalid request")
}
state := cmd[0]
infohash := cmd[1]
filepath := cmd[2]
if state == "start" {
if err := e.StartFile(infohash, filepath); err != nil {
return err
}
} else if state == "stop" {
if err := e.StopFile(infohash, filepath); err != nil {
return err
}
} else {
return fmt.Errorf("Invalid state: %s", state)
}
default:
return fmt.Errorf("Invalid action: %s", action)
}
Expand Down
2 changes: 1 addition & 1 deletion ct/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *Server) AddEngine(e engine.Engine) error {
Config: e.NewConfig(),
Torrents: torrents,
}
go s.torrentsWatch(torrents, e.Torrents())
go s.torrentsWatch(torrents, e.GetTorrents())
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion ct/shared/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package shared
//cloud torrent specific torrent structs

type Torrent struct {
InfoHash string //hash of torrent
Name string
Loaded bool
Progress int64
Size int64
InfoHash string //hash of torrent
Files []*File
}

Expand Down
170 changes: 122 additions & 48 deletions static/files.go

Large diffs are not rendered by default.

Binary file removed static/files/css/Lato/Lato-0.woff
Binary file not shown.
Binary file modified static/files/css/Lato/Lato-1.woff
Binary file not shown.
Binary file modified static/files/css/Lato/Lato-2.woff
Binary file not shown.
Binary file modified static/files/css/Lato/Lato-3.woff
Binary file not shown.
Binary file added static/files/css/Lato/Lato-4.woff
Binary file not shown.
8 changes: 4 additions & 4 deletions static/files/css/Lato/Lato.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
font-family: 'Lato';
font-style: normal;
font-weight: 400;
src: local('Lato Regular'), local('Lato-Regular'), url(./Lato-0.woff) format('woff');
src: local('Lato Regular'), local('Lato-Regular'), url(./Lato-1.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 700;
src: local('Lato Bold'), local('Lato-Bold'), url(./Lato-1.woff) format('woff');
src: local('Lato Bold'), local('Lato-Bold'), url(./Lato-2.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: italic;
font-weight: 400;
src: local('Lato Italic'), local('Lato-Italic'), url(./Lato-2.woff) format('woff');
src: local('Lato Italic'), local('Lato-Italic'), url(./Lato-3.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: italic;
font-weight: 700;
src: local('Lato Bold Italic'), local('Lato-BoldItalic'), url(./Lato-3.woff) format('woff');
src: local('Lato Bold Italic'), local('Lato-BoldItalic'), url(./Lato-4.woff) format('woff');
}
Loading

0 comments on commit 98526bc

Please sign in to comment.