@@ -2,12 +2,14 @@ package libtorrent
22
33import (
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
8688func (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
107108func (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
158177func (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