Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(subsonic): add getNewestPodcasts
  • Loading branch information
brian-doherty committed Apr 21, 2022
1 parent dc4d9e4 commit f6687df
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions db/model.go
Expand Up @@ -371,6 +371,10 @@ func (pe *PodcastEpisode) SID() *specid.ID {
return &specid.ID{Type: specid.PodcastEpisode, Value: pe.ID}
}

func (pe *PodcastEpisode) PodcastSID() *specid.ID {
return &specid.ID{Type: specid.Podcast, Value: pe.PodcastID}
}

func (pe *PodcastEpisode) AudioFilename() string {
return pe.Filename
}
Expand Down
13 changes: 13 additions & 0 deletions podcasts/podcasts.go
Expand Up @@ -75,6 +75,19 @@ func (p *Podcasts) GetPodcastEpisodes(podcastID int) ([]*db.PodcastEpisode, erro
return episodes, nil
}

func (p *Podcasts) GetNewestPodcastEpisodes(count int) ([]*db.PodcastEpisode, error) {
episodes := []*db.PodcastEpisode{}
err := p.db.
Order("publish_date DESC").
Limit(count).
Find(&episodes).
Error
if err != nil {
return nil, fmt.Errorf("find newest podcast episodes: %w", err)
}
return episodes, nil
}

func (p *Podcasts) AddNewPodcast(rssURL string, feed *gofeed.Feed,
userID int) (*db.Podcast, error) {
podcast := db.Podcast{
Expand Down
15 changes: 15 additions & 0 deletions server/ctrlsubsonic/handlers_podcast.go
Expand Up @@ -29,6 +29,21 @@ func (c *Controller) ServeGetPodcasts(r *http.Request) *spec.Response {
return sub
}

func (c *Controller) ServeGetNewestPodcasts(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
count := params.GetOrInt("count", 10)
episodes, err := c.Podcasts.GetNewestPodcastEpisodes(count)
if err != nil {
return spec.NewError(10, "failed get podcast(s): %s", err)
}
sub := spec.NewResponse()
sub.NewestPodcasts = &spec.NewestPodcasts{}
for _, episode := range episodes {
sub.NewestPodcasts.List = append(sub.NewestPodcasts.List, spec.NewPodcastEpisode(episode))
}
return sub
}

func (c *Controller) ServeDownloadPodcastEpisode(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
Expand Down
8 changes: 4 additions & 4 deletions server/ctrlsubsonic/spec/construct_podcast.go
Expand Up @@ -13,25 +13,25 @@ func NewPodcastChannel(p *db.Podcast) *PodcastChannel {
Status: "skipped",
}
for _, episode := range p.Episodes {
specEpisode := NewPodcastEpisode(p, episode)
specEpisode := NewPodcastEpisode(episode)
ret.Episode = append(ret.Episode, specEpisode)
}
return ret
}

func NewPodcastEpisode(p *db.Podcast, e *db.PodcastEpisode) *PodcastEpisode {
func NewPodcastEpisode(e *db.PodcastEpisode) *PodcastEpisode {
if e == nil {
return nil
}
return &PodcastEpisode{
ID: e.SID(),
StreamID: e.SID(),
ContentType: e.MIME(),
ChannelID: p.SID(),
ChannelID: e.PodcastSID(),
Title: e.Title,
Description: e.Description,
Status: string(e.Status),
CoverArt: p.SID(),
CoverArt: e.PodcastSID(),
PublishDate: *e.PublishDate,
Genre: "Podcast",
Duration: e.Length,
Expand Down
5 changes: 5 additions & 0 deletions server/ctrlsubsonic/spec/spec.go
Expand Up @@ -44,6 +44,7 @@ type Response struct {
JukeboxStatus *JukeboxStatus `xml:"jukeboxStatus" json:"jukeboxStatus,omitempty"`
JukeboxPlaylist *JukeboxPlaylist `xml:"jukeboxPlaylist" json:"jukeboxPlaylist,omitempty"`
Podcasts *Podcasts `xml:"podcasts" json:"podcasts,omitempty"`
NewestPodcasts *NewestPodcasts `xml:"newestPodcasts" json:"newestPodcasts,omitempty"`
Bookmarks *Bookmarks `xml:"bookmarks" json:"bookmarks,omitempty"`
Starred *Starred `xml:"starred" json:"starred,omitempty"`
StarredTwo *StarredTwo `xml:"starred2" json:"starred2,omitempty"`
Expand Down Expand Up @@ -294,6 +295,10 @@ type Podcasts struct {
List []*PodcastChannel `xml:"channel" json:"channel"`
}

type NewestPodcasts struct {
List []*PodcastEpisode `xml:"episode" json:"episode"`
}

type PodcastChannel struct {
ID *specid.ID `xml:"id,attr" json:"id"`
URL string `xml:"url,attr" json:"url"`
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Expand Up @@ -253,6 +253,7 @@ func setupSubsonic(r *mux.Router, ctrl *ctrlsubsonic.Controller) {

// podcasts
r.Handle("/getPodcasts{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetPodcasts))
r.Handle("/getNewestPodcasts{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetNewestPodcasts))
r.Handle("/downloadPodcastEpisode{_:(?:\\.view)?}", ctrl.H(ctrl.ServeDownloadPodcastEpisode))
r.Handle("/createPodcastChannel{_:(?:\\.view)?}", ctrl.H(ctrl.ServeCreatePodcastChannel))
r.Handle("/refreshPodcasts{_:(?:\\.view)?}", ctrl.H(ctrl.ServeRefreshPodcasts))
Expand Down

0 comments on commit f6687df

Please sign in to comment.