Files
TorrServerJellyfin/server/web/msx/msx.go
cocool97 b72c66d433 Add openapi API documentation (#294)
* Initial version

- Add OpenAPI documentation generator
- Update README.md to remove endpoint documentations

* Adds new endpoints

- Fixes build with swag
- Adds new endpoints

* Adds more endpoints documentation

- Also removes swag from Dockerfile and build script

* Finally adds all endpoints documentation

* Initial version

- Add OpenAPI documentation generator
- Update README.md to remove endpoint documentations

* Adds new endpoints

- Fixes build with swag
- Adds new endpoints

* Adds more endpoints documentation

- Also removes swag from Dockerfile and build script

* Finally adds all endpoints documentation

* Update README (#1)

* Update README

I completely redid the `README.md`. Now it's much easier to read and understand.

---------

Co-authored-by: cocool97 <34218602+cocool97@users.noreply.github.com>

* Improves documentation

* Delete server/config.db

* Update README.md

* Update README.md

* fix download in api docs

* add api docs to web

---------

Co-authored-by: Shadeov <144587546+shadeov@users.noreply.github.com>
Co-authored-by: nikk gitanes <tsynik@gmail.com>
2023-11-13 09:59:23 +03:00

139 lines
3.0 KiB
Go

package msx
import (
_ "embed"
"encoding/json"
"net/http"
"sync"
"server/version"
"github.com/gin-gonic/gin"
)
var (
//go:embed russian.json.gz
rus []byte
//go:embed torrents.js.gz
trs []byte
//go:embed torrent.js.gz
trn []byte
//go:embed ts.js.gz
its []byte
idb = new(sync.Mutex)
ids = make(map[string]string)
)
func asset(c *gin.Context, t string, d []byte) {
c.Header("Content-Encoding", "gzip")
c.Data(200, t+"; charset=UTF-8", d)
}
func SetupRoute(r *gin.RouterGroup) {
r.GET("/msx/:pth", msxPTH)
r.GET("/msx/imdb", msxIMDB)
r.GET("/msx/imdb/:id", msxIMDBID)
}
// msxPTH godoc
//
// @Summary Multi usage endpoint
// @Description Multi usage endpoint.
//
// @Tags MSX
//
// @Param link query string true "Magnet/hash/link to torrent"
//
// @Produce json
// @Success 200 "Data returned according to query"
// @Router /msx [get]
func msxPTH(c *gin.Context) {
js := []string{"http://msx.benzac.de/js/tvx-plugin.min.js"}
switch p := c.Param("pth"); p {
case "start.json":
c.JSON(200, map[string]string{
"name": "TorrServer",
"version": version.Version,
"parameter": "menu:request:interaction:init@{PREFIX}{SERVER}/msx/ts",
})
case "russian.json":
asset(c, "application/json", rus)
case "torrents.js":
asset(c, "text/javascript", trs)
case "torrent.js":
asset(c, "text/javascript", trn)
case "ts.js":
asset(c, "text/javascript", its)
case "torrents":
js = append(js, p+".js")
p = "torrent"
fallthrough
case "torrent":
if c.Query("platform") == "tizen" {
js = append(js, "http://msx.benzac.de/interaction/js/tizen-player.js")
}
fallthrough
case "ts":
b := []byte("<!DOCTYPE html>\n<html>\n<head>\n<title>TorrServer Plugin</title>\n<meta charset='UTF-8'>\n")
for _, j := range append(js, p+".js") {
b = append(b, "<script type='text/javascript' src='"+j+"'></script>\n"...)
}
c.Data(200, "text/html; charset=UTF-8", append(b, "</head>\n<body></body>\n</html>"...))
default:
c.AbortWithStatus(404)
}
}
// msxIMDB godoc
//
// @Summary Get MSX IMDB informations
// @Description Get MSX IMDB informations.
//
// @Tags MSX
//
// @Produce json
// @Success 200 "JSON MSX IMDB informations"
// @Router /msx/imdb [get]
func msxIMDB(c *gin.Context) {
idb.Lock()
defer idb.Unlock()
l := len(ids)
ids = make(map[string]string)
c.JSON(200, l)
}
// msxIMDB godoc
//
// @Summary Get MSX IMDB informations
// @Description Get MSX IMDB informations.
//
// @Tags MSX
//
// @Param id path string true "IMDB ID"
//
// @Produce json
// @Success 200 "JSON MSX IMDB informations"
// @Router /msx/imdb/:id [get]
func msxIMDBID(c *gin.Context) {
idb.Lock()
defer idb.Unlock()
p := c.Param("id")
i, o := ids[p]
if !o {
if r, e := http.Get("https://v2.sg.media-imdb.com/suggestion/h/" + p + ".json"); e == nil {
defer r.Body.Close()
if r.StatusCode == 200 {
var j struct {
D []struct{ I struct{ ImageUrl string } }
}
if e = json.NewDecoder(r.Body).Decode(&j); e == nil && len(j.D) > 0 {
i = j.D[0].I.ImageUrl
}
}
}
ids[p] = i
}
c.JSON(200, i)
}