add support for subsonic podcast api
This commit is contained in:
committed by
Senan Kelly
parent
ce96b9f6fa
commit
9c4286b0e2
@@ -14,6 +14,7 @@ import (
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/jukebox"
|
||||
"go.senan.xyz/gonic/server/scrobble"
|
||||
"go.senan.xyz/gonic/server/podcasts"
|
||||
)
|
||||
|
||||
type CtxKey int
|
||||
@@ -30,6 +31,7 @@ type Controller struct {
|
||||
CoverCachePath string
|
||||
Jukebox *jukebox.Jukebox
|
||||
Scrobblers []scrobble.Scrobbler
|
||||
Podcasts *podcasts.Podcasts
|
||||
}
|
||||
|
||||
type metaResponse struct {
|
||||
|
||||
@@ -41,6 +41,9 @@ func (c *Controller) ServeScrobble(r *http.Request) *spec.Response {
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` parameter")
|
||||
}
|
||||
if id.Type == specid.Podcast || id.Type == specid.PodcastEpisode {
|
||||
return spec.NewError(10, "please provide a valid track id")
|
||||
}
|
||||
// fetch user to get lastfm session
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
// fetch track for getting info to send to last.fm function
|
||||
@@ -107,6 +110,7 @@ func (c *Controller) ServeGetUser(r *http.Request) *spec.Response {
|
||||
AdminRole: user.IsAdmin,
|
||||
JukeboxRole: true,
|
||||
ScrobblingEnabled: hasLastFM || hasListenBrainz,
|
||||
PodcastRole: c.Podcasts.PodcastBasePath != "",
|
||||
Folder: []int{1},
|
||||
}
|
||||
return sub
|
||||
|
||||
95
server/ctrlsubsonic/handlers_podcast.go
Normal file
95
server/ctrlsubsonic/handlers_podcast.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package ctrlsubsonic
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||
"go.senan.xyz/gonic/server/db"
|
||||
)
|
||||
|
||||
func (c *Controller) ServeGetPodcasts(r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
isIncludeEpisodes := true
|
||||
if ie, err := params.GetBool("includeEpisodes"); !ie && err == nil {
|
||||
isIncludeEpisodes = false
|
||||
}
|
||||
sub := spec.NewResponse()
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
id, err := params.GetID("id")
|
||||
if err != nil {
|
||||
sub.Podcasts, err = c.Podcasts.GetAllPodcasts(user.ID, isIncludeEpisodes)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "Failed to retrieve podcasts: %s", err)
|
||||
}
|
||||
return sub
|
||||
}
|
||||
sub.Podcasts, _ = c.Podcasts.GetPodcast(id.Value, user.ID, isIncludeEpisodes)
|
||||
return sub
|
||||
}
|
||||
|
||||
func (c *Controller) ServeDownloadPodcastEpisode(r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
id, err := params.GetID("id")
|
||||
if err != nil || id.Type != specid.PodcastEpisode {
|
||||
return spec.NewError(10, "Please provide a valid podcast episode id")
|
||||
}
|
||||
if err := c.Podcasts.DownloadEpisode(id.Value); err != nil {
|
||||
return spec.NewError(10, "Failed to download episode: %s", err)
|
||||
}
|
||||
return spec.NewResponse()
|
||||
}
|
||||
|
||||
func (c *Controller) ServeCreatePodcastChannel(r *http.Request) *spec.Response {
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
rssURL, _ := params.Get("url")
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.ParseURL(rssURL)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "Failed to parse feed: %s", err)
|
||||
}
|
||||
_, err = c.Podcasts.AddNewPodcast(feed, user.ID)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "Failed to add feed: %s", err)
|
||||
}
|
||||
return spec.NewResponse()
|
||||
}
|
||||
|
||||
func (c *Controller) ServeRefreshPodcasts(r *http.Request) *spec.Response {
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
err := c.Podcasts.RefreshPodcasts(user.ID, false)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "Failed to refresh feeds: %s", err)
|
||||
}
|
||||
return spec.NewResponse()
|
||||
}
|
||||
|
||||
func (c *Controller) ServeDeletePodcastChannel(r *http.Request) *spec.Response {
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
id, err := params.GetID("id")
|
||||
if err != nil || id.Type != specid.Podcast {
|
||||
return spec.NewError(10, "Please provide a valid podcast ID")
|
||||
}
|
||||
err = c.Podcasts.DeletePodcast(user.ID, id.Value)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "Failed to delete podcast: %s", err)
|
||||
}
|
||||
return spec.NewResponse()
|
||||
}
|
||||
|
||||
func (c *Controller) ServeDeletePodcastEpisode(r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
id, err := params.GetID("id")
|
||||
if err != nil || id.Type != specid.PodcastEpisode {
|
||||
return spec.NewError(10, "Please provide a valid podcast episode ID")
|
||||
}
|
||||
err = c.Podcasts.DeletePodcastEpisode(id.Value)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "Failed to delete podcast: %s", err)
|
||||
}
|
||||
return spec.NewResponse()
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||
"go.senan.xyz/gonic/server/db"
|
||||
"go.senan.xyz/gonic/server/encode"
|
||||
)
|
||||
@@ -44,6 +45,12 @@ func streamGetTrack(dbc *db.DB, trackID int) (*db.Track, error) {
|
||||
return &track, err
|
||||
}
|
||||
|
||||
func streamGetPodcast(dbc *db.DB, podcastID int) (*db.PodcastEpisode, error) {
|
||||
podcast := db.PodcastEpisode{}
|
||||
err := dbc.First(&podcast, podcastID).Error
|
||||
return &podcast, err
|
||||
}
|
||||
|
||||
func streamUpdateStats(dbc *db.DB, userID, albumID int) {
|
||||
play := db.Play{
|
||||
AlbumID: albumID,
|
||||
@@ -67,24 +74,51 @@ var (
|
||||
errCoverEmpty = errors.New("no cover found for that folder")
|
||||
)
|
||||
|
||||
func coverGetPath(dbc *db.DB, musicPath string, id int) (string, error) {
|
||||
folder := &db.Album{}
|
||||
err := dbc.DB.
|
||||
Select("id, left_path, right_path, cover").
|
||||
First(folder, id).
|
||||
Error
|
||||
func coverGetPath(dbc *db.DB, musicPath, podcastPath string, id specid.ID) (string, error) {
|
||||
var err error
|
||||
coverPath := ""
|
||||
switch id.Type {
|
||||
case specid.Album:
|
||||
folder := &db.Album{}
|
||||
err = dbc.DB.
|
||||
Select("id, left_path, right_path, cover").
|
||||
First(folder, id.Value).
|
||||
Error
|
||||
coverPath = path.Join(
|
||||
musicPath,
|
||||
folder.LeftPath,
|
||||
folder.RightPath,
|
||||
folder.Cover,
|
||||
)
|
||||
if folder.Cover == "" {
|
||||
return "", errCoverEmpty
|
||||
}
|
||||
case specid.Podcast:
|
||||
podcast := &db.Podcast{}
|
||||
err = dbc.First(podcast, id.Value).Error
|
||||
|
||||
if podcast.ImagePath == "" {
|
||||
return "", errCoverEmpty
|
||||
}
|
||||
coverPath = path.Join(podcastPath, podcast.ImagePath)
|
||||
case specid.PodcastEpisode:
|
||||
podcastEp := &db.PodcastEpisode{}
|
||||
err = dbc.First(podcastEp, id.Value).Error
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
return "", errCoverNotFound
|
||||
}
|
||||
podcast := &db.Podcast{}
|
||||
err = dbc.First(podcast, podcastEp.PodcastID).Error
|
||||
if podcast.ImagePath == "" {
|
||||
return "", errCoverEmpty
|
||||
}
|
||||
coverPath = path.Join(podcastPath, podcast.ImagePath)
|
||||
default:
|
||||
}
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
return "", errCoverNotFound
|
||||
}
|
||||
if folder.Cover == "" {
|
||||
return "", errCoverEmpty
|
||||
}
|
||||
return path.Join(
|
||||
musicPath,
|
||||
folder.LeftPath,
|
||||
folder.RightPath,
|
||||
folder.Cover,
|
||||
), nil
|
||||
return coverPath, nil
|
||||
}
|
||||
|
||||
func coverScaleAndSave(absPath, cachePath string, size int) error {
|
||||
@@ -118,7 +152,7 @@ func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *s
|
||||
_, err = os.Stat(cachePath)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
coverPath, err := coverGetPath(c.DB, c.MusicPath, id.Value)
|
||||
coverPath, err := coverGetPath(c.DB, c.MusicPath, c.Podcasts.PodcastBasePath, id)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "couldn't find cover `%s`: %v", id, err)
|
||||
}
|
||||
@@ -140,35 +174,53 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` parameter")
|
||||
}
|
||||
track, err := streamGetTrack(c.DB, id.Value)
|
||||
if err != nil {
|
||||
var audioFile db.AudioFile
|
||||
var audioPath string
|
||||
if id.Type == specid.Track {
|
||||
track, _ := streamGetTrack(c.DB, id.Value)
|
||||
audioFile = track
|
||||
audioPath = path.Join(c.MusicPath, track.RelPath())
|
||||
if err != nil {
|
||||
return spec.NewError(70, "track with id `%s` was not found", id)
|
||||
}
|
||||
} else if id.Type == specid.PodcastEpisode {
|
||||
podcast, err := streamGetPodcast(c.DB, id.Value)
|
||||
audioFile = podcast
|
||||
audioPath = path.Join(c.Podcasts.PodcastBasePath, podcast.Path)
|
||||
if err != nil {
|
||||
return spec.NewError(70, "track with id `%s` was not found", id)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil && id.Type != specid.Podcast {
|
||||
return spec.NewError(70, "media with id `%d` was not found", id.Value)
|
||||
}
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
defer streamUpdateStats(c.DB, user.ID, track.Album.ID)
|
||||
if id.Type == specid.Track {
|
||||
defer streamUpdateStats(c.DB, user.ID, audioFile.(*db.Track).Album.ID)
|
||||
}
|
||||
pref := streamGetTransPref(c.DB, user.ID, params.GetOr("c", ""))
|
||||
trackPath := path.Join(c.MusicPath, track.RelPath())
|
||||
//
|
||||
onInvalidProfile := func() error {
|
||||
log.Printf("serving raw `%s`\n", track.Filename)
|
||||
w.Header().Set("Content-Type", track.MIME())
|
||||
http.ServeFile(w, r, trackPath)
|
||||
log.Printf("serving raw `%s`\n", audioFile.AudioFilename())
|
||||
w.Header().Set("Content-Type", audioFile.MIME())
|
||||
http.ServeFile(w, r, audioPath)
|
||||
return nil
|
||||
}
|
||||
onCacheHit := func(profile encode.Profile, path string) error {
|
||||
log.Printf("serving transcode `%s`: cache [%s/%dk] hit!\n",
|
||||
track.Filename, profile.Format, profile.Bitrate)
|
||||
audioFile.AudioFilename(), profile.Format, profile.Bitrate)
|
||||
http.ServeFile(w, r, path)
|
||||
return nil
|
||||
}
|
||||
onCacheMiss := func(profile encode.Profile) (io.Writer, error) {
|
||||
log.Printf("serving transcode `%s`: cache [%s/%dk] miss!\n",
|
||||
track.Filename, profile.Format, profile.Bitrate)
|
||||
audioFile.AudioFilename(), profile.Format, profile.Bitrate)
|
||||
return w, nil
|
||||
}
|
||||
encodeOptions := encode.Options{
|
||||
TrackPath: trackPath,
|
||||
TrackBitrate: track.Bitrate,
|
||||
TrackPath: audioPath,
|
||||
TrackBitrate: audioFile.AudioBitrate(),
|
||||
CachePath: c.CachePath,
|
||||
ProfileName: pref.Profile,
|
||||
PreferredBitrate: params.GetOrInt("maxBitRate", 0),
|
||||
@@ -177,7 +229,7 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
|
||||
OnCacheMiss: onCacheMiss,
|
||||
}
|
||||
if err := encode.Encode(encodeOptions); err != nil {
|
||||
log.Printf("serving transcode `%s`: error: %v\n", track.Filename, err)
|
||||
log.Printf("serving transcode `%s`: error: %v\n", audioFile.AudioFilename(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -188,13 +240,25 @@ func (c *Controller) ServeDownload(w http.ResponseWriter, r *http.Request) *spec
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` parameter")
|
||||
}
|
||||
track, err := streamGetTrack(c.DB, id.Value)
|
||||
if err != nil {
|
||||
return spec.NewError(70, "media with id `%s` was not found", id)
|
||||
var filePath string
|
||||
var audioFile db.AudioFile
|
||||
if id.Type == specid.Track {
|
||||
track, _ := streamGetTrack(c.DB, id.Value)
|
||||
audioFile = track
|
||||
filePath = path.Join(c.MusicPath, track.RelPath())
|
||||
if err != nil {
|
||||
return spec.NewError(70, "track with id `%s` was not found", id)
|
||||
}
|
||||
} else if id.Type == specid.PodcastEpisode {
|
||||
podcast, err := streamGetPodcast(c.DB, id.Value)
|
||||
audioFile = podcast
|
||||
filePath = path.Join(c.Podcasts.PodcastBasePath, podcast.Path)
|
||||
if err != nil {
|
||||
return spec.NewError(70, "podcast with id `%s` was not found", id)
|
||||
}
|
||||
}
|
||||
log.Printf("serving raw `%s`\n", track.Filename)
|
||||
w.Header().Set("Content-Type", track.MIME())
|
||||
trackPath := path.Join(c.MusicPath, track.RelPath())
|
||||
http.ServeFile(w, r, trackPath)
|
||||
log.Printf("serving raw `%s`\n", audioFile.AudioFilename())
|
||||
w.Header().Set("Content-Type", audioFile.MIME())
|
||||
http.ServeFile(w, r, filePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package ctrlsubsonic
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
)
|
||||
|
||||
func (c *Controller) ServeGetPodcasts(r *http.Request) *spec.Response {
|
||||
sub := spec.NewResponse()
|
||||
sub.Podcasts = &spec.Podcasts{
|
||||
List: []struct{}{},
|
||||
}
|
||||
return sub
|
||||
}
|
||||
@@ -288,5 +288,37 @@ type JukeboxPlaylist struct {
|
||||
}
|
||||
|
||||
type Podcasts struct {
|
||||
List []struct{} `xml:"channel" json:"channel"`
|
||||
List []PodcastChannel `xml:"channel" json:"channel"`
|
||||
}
|
||||
|
||||
type PodcastChannel struct {
|
||||
ID specid.ID `xml:"id,attr" json:"id"`
|
||||
URL string `xml:"url,attr" json:"url"`
|
||||
Title string `xml:"title,attr" json:"title"`
|
||||
Description string `xml:"description,attr" json:"description"`
|
||||
CoverArt specid.ID `xml:"coverArt,attr" json:"coverArt,omitempty"`
|
||||
OriginalImageURL string `xml:"originalImageUrl,attr" json:"originalImageUrl,omitempty"`
|
||||
Status string `xml:"status,attr" json:"status"`
|
||||
Episode []PodcastEpisode `xml:"episode" json:"episode,omitempty"`
|
||||
}
|
||||
|
||||
type PodcastEpisode struct {
|
||||
ID specid.ID `xml:"id,attr" json:"id"`
|
||||
StreamID specid.ID `xml:"streamId,attr" json:"streamId"`
|
||||
ChannelID specid.ID `xml:"channelId,attr" json:"channelId"`
|
||||
Title string `xml:"title,attr" json:"title"`
|
||||
Description string `xml:"description,attr" json:"description"`
|
||||
PublishDate time.Time `xml:"publishDate,attr" json:"publishDate"`
|
||||
Status string `xml:"status,attr" json:"status"`
|
||||
Parent string `xml:"parent,attr" json:"parent"`
|
||||
IsDir bool `xml:"isDir,attr" json:"isDir"`
|
||||
Year int `xml:"year,attr" json:"year"`
|
||||
Genre string `xml:"genre,attr" json:"genre"`
|
||||
CoverArt specid.ID `xml:"coverArt,attr" json:"coverArt"`
|
||||
Size int `xml:"size,attr" json:"size"`
|
||||
ContentType string `xml:"contentType,attr" json:"contentType"`
|
||||
Suffix string `xml:"suffix,attr" json:"suffix"`
|
||||
Duration int `xml:"duration,attr" json:"duration"`
|
||||
BitRate int `xml:"bitRate,attr" json:"bitrate"`
|
||||
Path string `xml:"path,attr" json:"path"`
|
||||
}
|
||||
|
||||
@@ -20,10 +20,12 @@ var (
|
||||
type IDT string
|
||||
|
||||
const (
|
||||
Artist IDT = "ar"
|
||||
Album IDT = "al"
|
||||
Track IDT = "tr"
|
||||
separator = "-"
|
||||
Artist IDT = "ar"
|
||||
Album IDT = "al"
|
||||
Track IDT = "tr"
|
||||
Podcast IDT = "pd"
|
||||
PodcastEpisode IDT = "pe"
|
||||
separator = "-"
|
||||
)
|
||||
|
||||
type ID struct {
|
||||
@@ -49,6 +51,10 @@ func New(in string) (ID, error) {
|
||||
return ID{Type: Album, Value: val}, nil
|
||||
case Track:
|
||||
return ID{Type: Track, Value: val}, nil
|
||||
case Podcast:
|
||||
return ID{Type: Podcast, Value: val}, nil
|
||||
case PodcastEpisode:
|
||||
return ID{Type: PodcastEpisode, Value: val}, nil
|
||||
default:
|
||||
return ID{}, fmt.Errorf("%q: %w", partType, ErrBadPrefix)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user