2021-04-16 20:44:56 +00:00
|
|
|
package api
|
|
|
|
|
2021-04-20 19:17:34 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-16 20:38:28 +00:00
|
|
|
"openmediacenter/apiGo/api/api"
|
2021-09-09 21:33:04 +00:00
|
|
|
"openmediacenter/apiGo/config"
|
2021-04-20 19:17:34 +00:00
|
|
|
"openmediacenter/apiGo/database"
|
|
|
|
)
|
2021-04-16 20:44:56 +00:00
|
|
|
|
2021-09-21 08:45:52 +00:00
|
|
|
func addTvshowHandlers() {
|
2021-06-08 19:55:54 +00:00
|
|
|
// do not add handlers if tvshows not enabled
|
2021-09-09 21:33:04 +00:00
|
|
|
if config.GetConfig().Features.DisableTVSupport {
|
2021-06-08 19:55:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/tvshow [getTVShows]
|
|
|
|
* @apiDescription get all available tv shows
|
|
|
|
* @apiName getTVShows
|
|
|
|
* @apiGroup TVshow
|
|
|
|
*
|
|
|
|
* @apiSuccess {Object[]} .
|
|
|
|
* @apiSuccess {uint32} .Id tvshow id
|
|
|
|
* @apiSuccess {string} .Name tvshow name
|
|
|
|
*/
|
2021-09-16 20:38:28 +00:00
|
|
|
api.AddHandler("getTVShows", api.TVShowNode, api.PermUser, func(context api.Context) {
|
2021-04-16 20:44:56 +00:00
|
|
|
query := "SELECT id, name FROM tvshow"
|
|
|
|
rows := database.Query(query)
|
2021-09-16 20:38:28 +00:00
|
|
|
context.Json(readTVshowsFromResultset(rows))
|
2021-04-16 20:44:56 +00:00
|
|
|
})
|
2021-04-20 19:17:34 +00:00
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/tvshow [getEpisodes]
|
|
|
|
* @apiDescription get all Episodes of a TVShow
|
|
|
|
* @apiName getEpisodes
|
|
|
|
* @apiGroup TVshow
|
|
|
|
*
|
|
|
|
* @apiParam {uint32} ShowID id of tvshow to get episodes from
|
|
|
|
*
|
|
|
|
* @apiSuccess {Object[]} .
|
|
|
|
* @apiSuccess {uint32} .ID episode id
|
|
|
|
* @apiSuccess {string} .Name episode name
|
|
|
|
* @apiSuccess {uint8} .Season Season number
|
|
|
|
* @apiSuccess {uint8} .Episode Episode number
|
|
|
|
*/
|
2021-09-16 20:38:28 +00:00
|
|
|
api.AddHandler("getEpisodes", api.TVShowNode, api.PermUser, func(context api.Context) {
|
2021-05-22 19:33:32 +00:00
|
|
|
var args struct {
|
|
|
|
ShowID uint32
|
|
|
|
}
|
2021-09-16 20:38:28 +00:00
|
|
|
err := api.DecodeRequest(context.GetRequest(), &args)
|
|
|
|
if err != nil {
|
|
|
|
context.Text("unable to decode request")
|
|
|
|
return
|
2021-05-22 19:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf("SELECT id, name, season, episode FROM tvshow_episodes WHERE tvshow_id=%d", args.ShowID)
|
2021-04-20 19:17:34 +00:00
|
|
|
rows := database.Query(query)
|
|
|
|
|
|
|
|
type Episode struct {
|
|
|
|
ID uint32
|
|
|
|
Name string
|
|
|
|
Season uint8
|
|
|
|
Episode uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
episodes := []Episode{}
|
|
|
|
for rows.Next() {
|
|
|
|
var ep Episode
|
|
|
|
err := rows.Scan(&ep.ID, &ep.Name, &ep.Season, &ep.Episode)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
episodes = append(episodes, ep)
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:38:28 +00:00
|
|
|
context.Json(episodes)
|
2021-04-20 19:17:34 +00:00
|
|
|
})
|
2021-04-22 18:31:36 +00:00
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/tvshow [loadEpisode]
|
|
|
|
* @apiDescription load all info of episode
|
|
|
|
* @apiName loadEpisode
|
|
|
|
* @apiGroup TVshow
|
|
|
|
*
|
|
|
|
* @apiParam {uint32} ID id of episode
|
|
|
|
*
|
|
|
|
* @apiSuccess {uint32} TVShowID episode id
|
|
|
|
* @apiSuccess {string} Name episode name
|
|
|
|
* @apiSuccess {uint8} Season Season number
|
|
|
|
* @apiSuccess {uint8} Episode Episode number
|
|
|
|
* @apiSuccess {string} Path webserver path of video file
|
|
|
|
*/
|
2021-09-16 20:38:28 +00:00
|
|
|
api.AddHandler("loadEpisode", api.TVShowNode, api.PermUser, func(context api.Context) {
|
2021-05-22 19:33:32 +00:00
|
|
|
var args struct {
|
|
|
|
ID uint32
|
|
|
|
}
|
2021-09-16 20:38:28 +00:00
|
|
|
err := api.DecodeRequest(context.GetRequest(), &args)
|
|
|
|
if err != nil {
|
|
|
|
context.Text("unable to decode argument")
|
|
|
|
return
|
2021-05-22 19:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 18:31:36 +00:00
|
|
|
query := fmt.Sprintf(`
|
|
|
|
SELECT tvshow_episodes.name, season, tvshow_id, episode, filename, t.foldername
|
|
|
|
FROM tvshow_episodes
|
|
|
|
JOIN tvshow t on t.id = tvshow_episodes.tvshow_id
|
2021-05-22 19:33:32 +00:00
|
|
|
WHERE tvshow_episodes.id=%d`, args.ID)
|
2021-04-22 18:31:36 +00:00
|
|
|
row := database.QueryRow(query)
|
|
|
|
|
|
|
|
var ret struct {
|
|
|
|
Name string
|
|
|
|
Season uint8
|
|
|
|
Episode uint8
|
|
|
|
TVShowID uint32
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
var filename string
|
|
|
|
var foldername string
|
|
|
|
|
2021-09-16 20:38:28 +00:00
|
|
|
err = row.Scan(&ret.Name, &ret.Season, &ret.TVShowID, &ret.Episode, &filename, &foldername)
|
2021-04-22 18:31:36 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2021-09-16 20:38:28 +00:00
|
|
|
context.Error(err.Error())
|
|
|
|
return
|
2021-04-22 18:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.Path = foldername + "/" + filename
|
|
|
|
|
2021-09-16 20:38:28 +00:00
|
|
|
context.Json(ret)
|
2021-04-22 18:31:36 +00:00
|
|
|
})
|
2021-04-23 19:23:51 +00:00
|
|
|
|
2021-05-23 12:21:44 +00:00
|
|
|
/**
|
|
|
|
* @api {post} /api/tvshow [readThumbnail]
|
|
|
|
* @apiDescription Load Thubnail of specific episode
|
|
|
|
* @apiName readThumbnail
|
|
|
|
* @apiGroup TVshow
|
|
|
|
*
|
|
|
|
* @apiParam {int} Id id of episode to load thumbnail
|
|
|
|
*
|
|
|
|
* @apiSuccess {string} . Base64 encoded Thubnail
|
|
|
|
*/
|
2021-09-16 20:38:28 +00:00
|
|
|
api.AddHandler("readThumbnail", api.TVShowNode, api.PermUser, func(context api.Context) {
|
2021-05-22 19:33:32 +00:00
|
|
|
var args struct {
|
|
|
|
Id int
|
|
|
|
}
|
2021-09-16 20:38:28 +00:00
|
|
|
err := api.DecodeRequest(context.GetRequest(), &args)
|
|
|
|
if err != nil {
|
|
|
|
context.Text("unable to decode request")
|
|
|
|
return
|
2021-05-22 19:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 19:23:51 +00:00
|
|
|
var pic []byte
|
|
|
|
|
2021-05-22 19:33:32 +00:00
|
|
|
query := fmt.Sprintf("SELECT thumbnail FROM tvshow WHERE id=%d", args.Id)
|
2021-04-23 19:23:51 +00:00
|
|
|
|
2021-09-16 20:38:28 +00:00
|
|
|
err = database.QueryRow(query).Scan(&pic)
|
2021-04-23 19:23:51 +00:00
|
|
|
if err != nil {
|
2021-05-22 19:33:32 +00:00
|
|
|
fmt.Printf("the thumbnail of movie id %d couldn't be found", args.Id)
|
2021-09-16 20:38:28 +00:00
|
|
|
return
|
2021-04-23 19:23:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:38:28 +00:00
|
|
|
context.Text(string(pic))
|
2021-04-23 19:23:51 +00:00
|
|
|
})
|
2021-04-16 20:44:56 +00:00
|
|
|
}
|