-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.go
188 lines (164 loc) · 4.84 KB
/
play.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package api
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/anacrolix/missinggo/perf"
"github.com/gin-gonic/gin"
"github.com/sanity-io/litter"
"github.com/projectx13/projectx/bittorrent"
"github.com/projectx13/projectx/database"
"github.com/projectx13/projectx/util"
"github.com/projectx13/projectx/xbmc"
)
// Download ...
func Download(s *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
rURL, _ := url.Parse(fmt.Sprintf("%s%s", util.GetContextHTTPHost(ctx), strings.Replace(ctx.Request.RequestURI, "/download", "/play", 1)+"&background=true"))
ctx.Redirect(302, rURL.String())
return
}
}
// Play ...
func Play(s *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
// Index is the file index to automatically select.
// Starts with 0, -1 or empty means - not defined.
// OIndex is the original file index to automatically select.
// Order is just like in the torrent file, without changes.
// NIndex is the file next to download
// NOIndex is the original torrent file next to download
index := ctx.Query("index")
oindex := ctx.Query("oindex")
nindex := ctx.Query("nindex")
noindex := ctx.Query("noindex")
uri := ctx.Query("uri")
resume := ctx.Query("resume")
doresume := ctx.DefaultQuery("doresume", "true")
query := ctx.Query("query")
contentType := ctx.Query("type")
tmdb := ctx.Query("tmdb")
show := ctx.Query("show")
season := ctx.Query("season")
episode := ctx.Query("episode")
background := ctx.DefaultQuery("background", "false")
if uri == "" && resume == "" {
return
}
fileIndex := strToInt(index, -1)
originalIndex := strToInt(oindex, -1)
nextFileIndex := strToInt(nindex, -1)
nextOriginalIndex := strToInt(noindex, -1)
tmdbID := strToInt(tmdb, 0)
showID := strToInt(show, 0)
seasonNumber := strToInt(season, 0)
episodeNumber := strToInt(episode, 0)
params := bittorrent.PlayerParams{
URI: uri,
OriginalIndex: originalIndex,
FileIndex: fileIndex,
NextOriginalIndex: nextOriginalIndex,
NextFileIndex: nextFileIndex,
ResumeHash: resume,
ResumePlayback: doresume != "false",
KodiPosition: -1,
ContentType: contentType,
TMDBId: tmdbID,
ShowID: showID,
Season: seasonNumber,
Episode: episodeNumber,
Query: query,
Background: background == "true",
}
player := bittorrent.NewPlayer(s, params)
log.Infof("Playing item: %s", litter.Sdump(params))
if t := s.GetTorrentByHash(resume); resume != "" && t != nil {
player.SetTorrent(t)
}
if player.Buffer() != nil || !player.HasChosenFile() || player.Params().Background {
player.Close()
return
}
rURL, _ := url.Parse(fmt.Sprintf("%s/files/%s", util.GetContextHTTPHost(ctx), player.PlayURL()))
ctx.Redirect(302, rURL.String())
}
}
// PlayTorrent ...
func PlayTorrent(ctx *gin.Context) {
defer perf.ScopeTimer()()
retval := xbmc.DialogInsert()
if retval["path"] == "" {
log.Errorf("No path from insert dialog: %#v", retval)
return
}
xbmc.PlayURLWithTimeout(URLQuery(URLForXBMC("/play"), "uri", retval["path"]))
ctx.String(200, "")
return
}
// PlayURI ...
func PlayURI(s *bittorrent.Service) gin.HandlerFunc {
return func(ctx *gin.Context) {
uri := ctx.Request.FormValue("uri")
file, header, fileError := ctx.Request.FormFile("file")
if file != nil && header != nil && fileError == nil {
t, err := saveTorrentFile(file, header)
if err == nil && t != "" {
uri = t
}
}
index := ctx.Query("index")
resume := ctx.Query("resume")
if uri == "" && resume == "" {
return
}
if uri != "" {
xbmc.PlayURL(URLQuery(URLForXBMC("/play"), "uri", uri, "index", index))
} else {
var (
tmdb string
show string
season string
episode string
query string
contentType string
)
t := s.GetTorrentByHash(resume)
if t != nil {
infoHash := t.InfoHash()
dbItem := database.GetStorm().GetBTItem(infoHash)
if dbItem != nil && dbItem.Type != "" {
contentType = dbItem.Type
if contentType == movieType {
tmdb = strconv.Itoa(dbItem.ID)
} else {
show = strconv.Itoa(dbItem.ShowID)
season = strconv.Itoa(dbItem.Season)
episode = strconv.Itoa(dbItem.Episode)
}
query = dbItem.Query
}
}
xbmc.PlayURL(URLQuery(URLForXBMC("/play"),
"resume", resume,
"index", index,
"tmdb", tmdb,
"show", show,
"season", season,
"episode", episode,
"query", query,
"type", contentType))
}
ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*")
ctx.String(200, "")
}
}
// strToInt parses string to int, and returning default value is no int found
func strToInt(str string, def int) int {
if str != "" {
if i, err := strconv.Atoi(str); err == nil && i >= 0 {
return i
}
}
return def
}