Skip to content

Commit 8b33e1c

Browse files
committed
Merge branch '0azis-episode-support'
2 parents a24c041 + 3360114 commit 8b33e1c

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

cmd/toru/stream.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
7+
"strconv"
68
"strings"
79
"time"
810

@@ -51,15 +53,28 @@ func Progress(t *torrent.Torrent) {
5153
// takes any type of torrent file/url/magnet, adds it to the client and streams it
5254
func StreamTorrent(torfile string, cl *libtorrent.Client) (string, error) {
5355
success, _ := pterm.DefaultSpinner.Start("getting torrent info")
54-
5556
t, err := cl.AddTorrent(torfile)
5657
if err != nil {
5758
return "", err
5859
}
59-
6060
success.Success("Success!")
6161

62-
link := cl.ServeTorrent(t)
62+
63+
torrentFiles := len(t.Files())
64+
var episode int
65+
66+
if torrentFiles != 1 {
67+
ep, _ := Prompt("Choose an episode")
68+
episode, err = strconv.Atoi(ep)
69+
if err != nil {
70+
return "", errors.New("episode must be numeric")
71+
}
72+
if episode > torrentFiles {
73+
return "", errors.New("episode doesn't exist")
74+
}
75+
}
76+
77+
link := cl.ServeTorrent(t, episode)
6378

6479
// consider deleting this as it sometimes conflicts with the fzf user interface
6580
go func() {
@@ -68,7 +83,6 @@ func StreamTorrent(torfile string, cl *libtorrent.Client) (string, error) {
6883
}
6984
}()
7085

71-
fmt.Println(link)
7286
return link, nil
7387
}
7488

pkg/libtorrent/client.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package libtorrent
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"log"
89
"net/http"
910
"os"
1011
"path"
12+
"strconv"
1113
"strings"
1214
"time"
1315

@@ -85,43 +87,60 @@ func (c *Client) DownloadTorrent(torrent string) error {
8587

8688
func (c *Client) ServeTorrents(ctx context.Context, torrents []*torrent.Torrent) {
8789
for _, t := range torrents {
88-
link := c.ServeTorrent(t)
90+
// it doesn't matter what episode included here, so it's just 0
91+
link := c.ServeTorrent(t, 0)
8992
fmt.Println(link)
9093
}
9194
}
9295

93-
func GetVideoFile(t *torrent.Torrent) *torrent.File {
94-
for _, f := range t.Files() {
95-
ext := path.Ext(f.Path())
96-
switch ext {
97-
case ".mp4", ".mkv", ".avi", ".avif", ".av1", ".mov", ".flv", ".f4v", ".webm", ".wmv", ".mpeg", ".mpg", ".mlv", ".hevc", ".flac", ".flic":
98-
return f
99-
default:
100-
continue
101-
}
96+
func GetVideoFile(t *torrent.Torrent, episode int) (*torrent.File, error) {
97+
f := t.Files()[episode]
98+
ext := path.Ext(f.Path())
99+
switch ext {
100+
case ".mp4", ".mkv", ".avi", ".avif", ".av1", ".mov", ".flv", ".f4v", ".webm", ".wmv", ".mpeg", ".mpg", ".mlv", ".hevc", ".flac", ".flic":
101+
return f, nil
102+
default:
103+
return f, errors.New("server handler: Not supported extension")
102104
}
103-
return nil
104105
}
105106

106107
// handler for ServeTorrent
107108
func (c *Client) handler(w http.ResponseWriter, r *http.Request) {
108109
ts := c.TorrentClient.Torrents()
109-
ep := r.URL.Query().Get("ep")
110+
queries := r.URL.Query()
111+
// get hash of torrent
112+
hash := queries.Get("hash")
113+
// get episode
114+
ep, err := strconv.Atoi(queries.Get("ep"))
115+
if err != nil {
116+
http.Error(w, http.StatusText(400), http.StatusBadRequest)
117+
return
118+
}
119+
110120
// idk why but this is always mangled af
111-
ep = strings.TrimSpace(ep)
112-
ep = strings.ReplaceAll(ep, "\n", "")
121+
hash = strings.TrimSpace(hash)
122+
hash = strings.ReplaceAll(hash, "\n", "")
113123

114-
if ep == "" {
115-
log.Println("server handler: Episode query is empty")
124+
if hash == "" {
125+
log.Println("server handler: Hash query is empty")
116126
return
117127
}
118128

119129
for _, ff := range ts {
120130
<-ff.GotInfo()
121131
ih := ff.InfoHash().String()
122132

123-
if ih == ep {
124-
f := GetVideoFile(ff)
133+
if ih == hash {
134+
if ep == 0 {
135+
ep = 1
136+
}
137+
138+
f, err := GetVideoFile(ff, ep-1)
139+
if err != nil {
140+
log.Println(err)
141+
return
142+
}
143+
125144
w.Header().Set("Content-Type", "video/mp4")
126145
http.ServeContent(w, r, f.DisplayPath(), time.Unix(f.Torrent().Metainfo().CreationDate, 0), f.NewReader())
127146
}
@@ -148,16 +167,17 @@ func (c *Client) StartServer() {
148167

149168
// Generate a link that can be used with the default clients server to play a torrent
150169
// that is already loaded into the client
151-
func (c *Client) ServeTorrent(t *torrent.Torrent) string {
170+
func (c *Client) ServeTorrent(t *torrent.Torrent, episode int) string {
152171
mh := t.InfoHash().String()
153-
return fmt.Sprintf("http://localhost:%s/stream?ep=%s", c.Port, mh)
172+
return fmt.Sprintf("http://localhost:%s/stream?hash=%s&ep=%d", c.Port, mh, episode)
154173
}
155174

156175
// old version of servetorrent, only works once lol. DOnt use, delete soon
157176
// WARN do not use this | do not use this | do not fucking use this
158177
func (c *Client) ServeSingleTorrent(ctx context.Context, t *torrent.Torrent) string {
159178
var link string
160-
f := GetVideoFile(t)
179+
// if you tell don't use, well, I include 0 here as episode argument :)
180+
f, _ := GetVideoFile(t, 0)
161181
if f == nil {
162182
log.Fatal("Could not find video file")
163183
}

0 commit comments

Comments
 (0)