2021-04-16 20:44:56 +00:00
|
|
|
package api
|
|
|
|
|
2021-04-20 19:17:34 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"openmediacenter/apiGo/database"
|
|
|
|
)
|
2021-04-16 20:44:56 +00:00
|
|
|
|
|
|
|
func AddTvshowHandlers() {
|
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-05-22 19:33:32 +00:00
|
|
|
AddHandler("getTVShows", TVShowNode, func(info *HandlerInfo) []byte {
|
2021-04-16 20:44:56 +00:00
|
|
|
query := "SELECT id, name FROM tvshow"
|
|
|
|
rows := database.Query(query)
|
|
|
|
return jsonify(readTVshowsFromResultset(rows))
|
|
|
|
})
|
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-05-22 19:33:32 +00:00
|
|
|
AddHandler("getEpisodes", TVShowNode, func(info *HandlerInfo) []byte {
|
|
|
|
var args struct {
|
|
|
|
ShowID uint32
|
|
|
|
}
|
|
|
|
if err := FillStruct(&args, info.Data); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonify(episodes)
|
|
|
|
})
|
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-05-22 19:33:32 +00:00
|
|
|
AddHandler("loadEpisode", TVShowNode, func(info *HandlerInfo) []byte {
|
|
|
|
var args struct {
|
|
|
|
ID uint32
|
|
|
|
}
|
|
|
|
if err := FillStruct(&args, info.Data); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
err := row.Scan(&ret.Name, &ret.Season, &ret.TVShowID, &ret.Episode, &filename, &foldername)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Path = foldername + "/" + filename
|
|
|
|
|
|
|
|
return jsonify(ret)
|
|
|
|
})
|
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-05-22 19:33:32 +00:00
|
|
|
AddHandler("readThumbnail", TVShowNode, func(info *HandlerInfo) []byte {
|
|
|
|
var args struct {
|
|
|
|
Id int
|
|
|
|
}
|
|
|
|
if err := FillStruct(&args, info.Data); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
err := database.QueryRow(query).Scan(&pic)
|
|
|
|
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-04-23 19:23:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic
|
|
|
|
})
|
2021-04-16 20:44:56 +00:00
|
|
|
}
|