add support for subsonic podcast api
This commit is contained in:
committed by
Senan Kelly
parent
ce96b9f6fa
commit
9c4286b0e2
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,7 @@ import (
|
||||
"go.senan.xyz/gonic/server/assets"
|
||||
"go.senan.xyz/gonic/server/ctrlbase"
|
||||
"go.senan.xyz/gonic/server/db"
|
||||
"go.senan.xyz/gonic/server/podcasts"
|
||||
"go.senan.xyz/gonic/version"
|
||||
)
|
||||
|
||||
@@ -82,9 +83,10 @@ type Controller struct {
|
||||
buffPool *bpool.BufferPool
|
||||
templates map[string]*template.Template
|
||||
sessDB *gormstore.Store
|
||||
Podcasts *podcasts.Podcasts
|
||||
}
|
||||
|
||||
func New(b *ctrlbase.Controller, sessDB *gormstore.Store) *Controller {
|
||||
func New(b *ctrlbase.Controller, sessDB *gormstore.Store, podcasts *podcasts.Podcasts) *Controller {
|
||||
tmplBase := template.
|
||||
New("layout").
|
||||
Funcs(sprig.FuncMap()).
|
||||
@@ -99,6 +101,7 @@ func New(b *ctrlbase.Controller, sessDB *gormstore.Store) *Controller {
|
||||
buffPool: bpool.NewBufferPool(64),
|
||||
templates: pagesFromPaths(tmplBase, prefixPages),
|
||||
sessDB: sessDB,
|
||||
Podcasts: podcasts,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +127,9 @@ type templateData struct {
|
||||
CurrentLastFMAPISecret string
|
||||
DefaultListenBrainzURL string
|
||||
SelectedUser *db.User
|
||||
//
|
||||
PodcastsEnabled bool
|
||||
Podcasts []*db.Podcast
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"go.senan.xyz/gonic/server/db"
|
||||
"go.senan.xyz/gonic/server/encode"
|
||||
"go.senan.xyz/gonic/server/scanner"
|
||||
@@ -45,6 +46,10 @@ func (c *Controller) ServeHome(r *http.Request) *Response {
|
||||
c.DB.Table("artists").Count(&data.ArtistCount)
|
||||
c.DB.Table("albums").Count(&data.AlbumCount)
|
||||
c.DB.Table("tracks").Count(&data.TrackCount)
|
||||
data.PodcastsEnabled = c.Podcasts.PodcastBasePath != ""
|
||||
if data.PodcastsEnabled {
|
||||
c.DB.Find(&data.Podcasts)
|
||||
}
|
||||
// ** begin lastfm box
|
||||
scheme := firstExisting(
|
||||
"http", // fallback
|
||||
@@ -415,3 +420,47 @@ func (c *Controller) ServeDeleteTranscodePrefDo(r *http.Request) *Response {
|
||||
redirect: "/admin/home",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ServePodcastAddDo(r *http.Request) *Response {
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
rssURL := r.FormValue("feed")
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.ParseURL(rssURL)
|
||||
if err != nil {
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashW: []string{fmt.Sprintf("could not create feed: %v", err)},
|
||||
}
|
||||
}
|
||||
_, err = c.Podcasts.AddNewPodcast(feed, user.ID)
|
||||
if err != nil {
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
flashW: []string{fmt.Sprintf("could not create feed: %v", err)},
|
||||
}
|
||||
}
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ServePodcastDeleteDo(r *http.Request) *Response {
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
id, err := strconv.Atoi(r.URL.Query().Get("id"))
|
||||
if err != nil {
|
||||
return &Response{
|
||||
err: "please provide a valid podcast id",
|
||||
code: 400,
|
||||
}
|
||||
}
|
||||
err = c.Podcasts.DeletePodcast(user.ID, id)
|
||||
if err != nil {
|
||||
return &Response{
|
||||
err: "please provide a valid podcast id",
|
||||
code: 400,
|
||||
}
|
||||
}
|
||||
return &Response{
|
||||
redirect: "/admin/home",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -79,6 +79,7 @@ func New(path string) (*DB, error) {
|
||||
migrateAddAlbumIDX(),
|
||||
migrateMultiGenre(),
|
||||
migrateListenBrainz(),
|
||||
migratePodcast(),
|
||||
))
|
||||
if err = migr.Migrate(); err != nil {
|
||||
return nil, fmt.Errorf("migrating to latest version: %w", err)
|
||||
|
||||
@@ -211,6 +211,7 @@ func migrateMultiGenre() gormigrate.Migration {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func migrateListenBrainz() gormigrate.Migration {
|
||||
return gormigrate.Migration{
|
||||
ID: "202101081149",
|
||||
@@ -225,3 +226,16 @@ func migrateListenBrainz() gormigrate.Migration {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func migratePodcast() gormigrate.Migration {
|
||||
return gormigrate.Migration{
|
||||
ID: "202101111537",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
step := tx.AutoMigrate(
|
||||
Podcast{},
|
||||
PodcastEpisode{},
|
||||
)
|
||||
return step.Error
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ package db
|
||||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -67,6 +68,15 @@ type Genre struct {
|
||||
TrackCount int `sql:"-"`
|
||||
}
|
||||
|
||||
// AudioFile is used to avoid some duplication in handlers_raw.go
|
||||
// between Track and Podcast
|
||||
type AudioFile interface {
|
||||
AudioFilename() string
|
||||
Ext() string
|
||||
MIME() string
|
||||
AudioBitrate() int
|
||||
}
|
||||
|
||||
type Track struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
@@ -109,6 +119,14 @@ func (t *Track) Ext() string {
|
||||
return longExt[1:]
|
||||
}
|
||||
|
||||
func (t *Track) AudioFilename() string {
|
||||
return t.Filename
|
||||
}
|
||||
|
||||
func (t *Track) AudioBitrate() int {
|
||||
return t.Bitrate
|
||||
}
|
||||
|
||||
func (t *Track) MIME() string {
|
||||
v, _ := mime.FromExtension(t.Ext())
|
||||
return v
|
||||
@@ -270,3 +288,68 @@ type AlbumGenre struct {
|
||||
Genre *Genre
|
||||
GenreID int `gorm:"not null; unique_index:idx_album_id_genre_id" sql:"default: null; type:int REFERENCES genres(id) ON DELETE CASCADE"`
|
||||
}
|
||||
|
||||
type Podcast struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
UpdatedAt time.Time
|
||||
ModifiedAt time.Time
|
||||
UserID int `sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
|
||||
URL string
|
||||
Title string
|
||||
Description string
|
||||
ImageURL string
|
||||
ImagePath string
|
||||
Error string
|
||||
}
|
||||
|
||||
func (p *Podcast) Fullpath(podcastPath string) string {
|
||||
return filepath.Join(podcastPath, filepath.Clean(p.Title))
|
||||
}
|
||||
|
||||
func (p *Podcast) SID() *specid.ID {
|
||||
return &specid.ID{Type: specid.Podcast, Value: p.ID}
|
||||
}
|
||||
|
||||
type PodcastEpisode struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ModifiedAt time.Time
|
||||
PodcastID int `gorm:"not null" sql:"default: null; type:int REFERENCES podcasts(id) ON DELETE CASCADE"`
|
||||
Title string
|
||||
Description string
|
||||
PublishDate *time.Time
|
||||
AudioURL string
|
||||
Bitrate int
|
||||
Length int
|
||||
Size int
|
||||
Path string
|
||||
Filename string
|
||||
Status string
|
||||
Error string
|
||||
}
|
||||
|
||||
func (pe *PodcastEpisode) SID() *specid.ID {
|
||||
return &specid.ID{Type: specid.PodcastEpisode, Value: pe.ID}
|
||||
}
|
||||
|
||||
func (pe *PodcastEpisode) AudioFilename() string {
|
||||
return pe.Filename
|
||||
}
|
||||
|
||||
func (pe *PodcastEpisode) Ext() string {
|
||||
longExt := path.Ext(pe.Filename)
|
||||
if len(longExt) < 1 {
|
||||
return ""
|
||||
}
|
||||
return longExt[1:]
|
||||
}
|
||||
|
||||
func (pe *PodcastEpisode) MIME() string {
|
||||
v, _ := mime.FromExtension(pe.Ext())
|
||||
return v
|
||||
}
|
||||
|
||||
func (pe *PodcastEpisode) AudioBitrate() int {
|
||||
return pe.Bitrate
|
||||
}
|
||||
|
||||
440
server/podcasts/podcasts.go
Normal file
440
server/podcasts/podcasts.go
Normal file
@@ -0,0 +1,440 @@
|
||||
package podcasts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/mmcdole/gofeed"
|
||||
"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/scanner/tags"
|
||||
)
|
||||
|
||||
type Podcasts struct {
|
||||
DB *db.DB
|
||||
PodcastBasePath string
|
||||
}
|
||||
|
||||
const (
|
||||
episodeDownloading = "downloading"
|
||||
episodeSkipped = "skipped"
|
||||
episodeDeleted = "deleted"
|
||||
)
|
||||
|
||||
func (p *Podcasts) GetAllPodcasts(userID int, includeEpisodes bool) (*spec.Podcasts, error) {
|
||||
podcasts := []*db.Podcast{}
|
||||
err := p.DB.Where("user_id=?", userID).Order("").Find(&podcasts).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
channels := []spec.PodcastChannel{}
|
||||
for _, c := range podcasts {
|
||||
channel := spec.PodcastChannel{
|
||||
ID: *c.SID(),
|
||||
OriginalImageURL: c.ImageURL,
|
||||
Title: c.Title,
|
||||
Description: c.Description,
|
||||
URL: c.URL,
|
||||
Status: episodeSkipped,
|
||||
}
|
||||
if includeEpisodes {
|
||||
channel.Episode, err = p.GetPodcastEpisodes(*c.SID())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
channels = append(channels, channel)
|
||||
}
|
||||
return &spec.Podcasts{List: channels}, nil
|
||||
}
|
||||
|
||||
func (p *Podcasts) GetPodcast(podcastID, userID int, includeEpisodes bool) (*spec.Podcasts, error) {
|
||||
podcasts := []*db.Podcast{}
|
||||
err := p.DB.Where("user_id=? AND id=?", userID, podcastID).
|
||||
Order("title DESC").
|
||||
Find(&podcasts).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
channels := []spec.PodcastChannel{}
|
||||
for _, c := range podcasts {
|
||||
channel := spec.PodcastChannel{
|
||||
ID: *c.SID(),
|
||||
OriginalImageURL: c.ImageURL,
|
||||
CoverArt: *c.SID(),
|
||||
Title: c.Title,
|
||||
Description: c.Description,
|
||||
URL: c.URL,
|
||||
Status: episodeSkipped,
|
||||
}
|
||||
if includeEpisodes {
|
||||
channel.Episode, err = p.GetPodcastEpisodes(*c.SID())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
channels = append(channels, channel)
|
||||
}
|
||||
return &spec.Podcasts{List: channels}, nil
|
||||
}
|
||||
|
||||
func (p *Podcasts) GetPodcastEpisodes(podcastID specid.ID) ([]spec.PodcastEpisode, error) {
|
||||
dbEpisodes := []*db.PodcastEpisode{}
|
||||
if err := p.DB.
|
||||
Where("podcast_id=?", podcastID.Value).
|
||||
Order("publish_date DESC").
|
||||
Find(&dbEpisodes).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
episodes := []spec.PodcastEpisode{}
|
||||
for _, dbe := range dbEpisodes {
|
||||
episodes = append(episodes, spec.PodcastEpisode{
|
||||
ID: *dbe.SID(),
|
||||
StreamID: *dbe.SID(),
|
||||
ContentType: dbe.MIME(),
|
||||
ChannelID: podcastID,
|
||||
Title: dbe.Title,
|
||||
Description: dbe.Description,
|
||||
Status: dbe.Status,
|
||||
CoverArt: podcastID,
|
||||
PublishDate: *dbe.PublishDate,
|
||||
Genre: "Podcast",
|
||||
Duration: dbe.Length,
|
||||
Year: dbe.PublishDate.Year(),
|
||||
Suffix: dbe.Ext(),
|
||||
BitRate: dbe.Bitrate,
|
||||
IsDir: false,
|
||||
Path: dbe.Path,
|
||||
Size: dbe.Size,
|
||||
})
|
||||
}
|
||||
|
||||
return episodes, nil
|
||||
}
|
||||
|
||||
func (p *Podcasts) AddNewPodcast(feed *gofeed.Feed, userID int) (*db.Podcast, error) {
|
||||
podcast := db.Podcast{
|
||||
Description: feed.Description,
|
||||
ImageURL: feed.Image.URL,
|
||||
UserID: userID,
|
||||
Title: feed.Title,
|
||||
URL: feed.FeedLink,
|
||||
}
|
||||
podPath := podcast.Fullpath(p.PodcastBasePath)
|
||||
err := os.Mkdir(podPath, 0755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
if err := p.DB.Save(&podcast).Error; err != nil {
|
||||
return &podcast, err
|
||||
}
|
||||
if err := p.AddNewEpisodes(userID, podcast.ID, feed.Items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go p.downloadPodcastCover(podPath, &podcast)
|
||||
|
||||
return &podcast, nil
|
||||
}
|
||||
|
||||
func getEntriesAfterDate(feed []*gofeed.Item, after time.Time) []*gofeed.Item {
|
||||
items := []*gofeed.Item{}
|
||||
for _, item := range feed {
|
||||
if item.PublishedParsed.Before(after) || item.PublishedParsed.Equal(after) {
|
||||
continue
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func (p *Podcasts) AddNewEpisodes(userID int, podcastID int, items []*gofeed.Item) error {
|
||||
podcastEpisode := db.PodcastEpisode{}
|
||||
err := p.DB.
|
||||
Where("podcast_id=?", podcastID).
|
||||
Order("publish_date DESC").
|
||||
First(&podcastEpisode).Error
|
||||
itemFound := true
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
itemFound = false
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if !itemFound {
|
||||
for _, item := range items {
|
||||
if err := p.AddEpisode(podcastID, item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, item := range getEntriesAfterDate(items, *podcastEpisode.PublishDate) {
|
||||
if err := p.AddEpisode(podcastID, item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getSecondsFromString(time string) int {
|
||||
duration, err := strconv.Atoi(time)
|
||||
if err == nil {
|
||||
return duration
|
||||
}
|
||||
splitTime := strings.Split(time, ":")
|
||||
if len(splitTime) == 3 {
|
||||
hours, _ := strconv.Atoi(splitTime[0])
|
||||
minutes, _ := strconv.Atoi(splitTime[1])
|
||||
seconds, _ := strconv.Atoi(splitTime[2])
|
||||
return (3600 * hours) + (60 * minutes) + seconds
|
||||
}
|
||||
if len(splitTime) == 2 {
|
||||
minutes, _ := strconv.Atoi(splitTime[0])
|
||||
seconds, _ := strconv.Atoi(splitTime[1])
|
||||
return (60 * minutes) + seconds
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (p *Podcasts) AddEpisode(podcastID int, item *gofeed.Item) error {
|
||||
duration := 0
|
||||
// if it has the media extension use it
|
||||
for _, content := range item.Extensions["media"]["content"] {
|
||||
durationExt := content.Attrs["duration"]
|
||||
duration = getSecondsFromString(durationExt)
|
||||
if duration != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
// if the itunes extension is available, use AddEpisode
|
||||
if duration == 0 {
|
||||
duration = getSecondsFromString(item.ITunesExt.Duration)
|
||||
}
|
||||
|
||||
for _, enc := range item.Enclosures {
|
||||
if !strings.HasPrefix(enc.Type, "audio") {
|
||||
continue
|
||||
}
|
||||
size, _ := strconv.Atoi(enc.Length)
|
||||
podcastEpisode := db.PodcastEpisode{
|
||||
PodcastID: podcastID,
|
||||
Description: item.Description,
|
||||
Title: item.Title,
|
||||
Length: duration,
|
||||
Size: size,
|
||||
PublishDate: item.PublishedParsed,
|
||||
AudioURL: enc.URL,
|
||||
Status: episodeSkipped,
|
||||
}
|
||||
if err := p.DB.Save(&podcastEpisode).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Podcasts) RefreshPodcasts(userID int, serverWide bool) error {
|
||||
podcasts := []*db.Podcast{}
|
||||
var err error
|
||||
if serverWide {
|
||||
err = p.DB.Find(&podcasts).Error
|
||||
} else {
|
||||
err = p.DB.Where("user_id=?", userID).Find(&podcasts).Error
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, podcast := range podcasts {
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.ParseURL(podcast.URL)
|
||||
if err != nil {
|
||||
log.Printf("Error refreshing podcast with url %s: %s", podcast.URL, err)
|
||||
continue
|
||||
}
|
||||
err = p.AddNewEpisodes(userID, podcast.ID, feed.Items)
|
||||
if err != nil {
|
||||
log.Printf("Error adding episodes: %s", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Podcasts) DownloadEpisode(episodeID int) error {
|
||||
podcastEpisode := db.PodcastEpisode{}
|
||||
podcast := db.Podcast{}
|
||||
err := p.DB.Where("id=?", episodeID).First(&podcastEpisode).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = p.DB.Where("id=?", podcastEpisode.PodcastID).First(&podcast).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if podcastEpisode.Status == episodeDownloading {
|
||||
log.Printf("Already downloading podcast episode with id %d", episodeID)
|
||||
return nil
|
||||
}
|
||||
podcastEpisode.Status = episodeDownloading
|
||||
p.DB.Save(&podcastEpisode)
|
||||
// nolint: bodyclose
|
||||
resp, err := http.Get(podcastEpisode.AudioURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
filename, ok := getContentDispositionFilename(resp.Header.Get("content-disposition"))
|
||||
if !ok {
|
||||
audioURL, err := url.Parse(podcastEpisode.AudioURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
filename = path.Base(audioURL.Path)
|
||||
}
|
||||
filename = p.findUniqueEpisodeName(&podcast, &podcastEpisode, filename)
|
||||
audioFile, err := os.Create(path.Join(podcast.Fullpath(p.PodcastBasePath), filename))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
podcastEpisode.Filename = filename
|
||||
podcastEpisode.Path = path.Join(filepath.Clean(podcast.Title), filename)
|
||||
p.DB.Save(&podcastEpisode)
|
||||
go p.doPodcastDownload(&podcastEpisode, audioFile, resp.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Podcasts) findUniqueEpisodeName(
|
||||
podcast *db.Podcast,
|
||||
podcastEpisode *db.PodcastEpisode,
|
||||
filename string) string {
|
||||
fp := path.Join(podcast.Fullpath(p.PodcastBasePath), filename)
|
||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||
return filename
|
||||
}
|
||||
titlePath := fmt.Sprintf("%s%s", podcastEpisode.Title,
|
||||
filepath.Ext(filename))
|
||||
fp = path.Join(podcast.Fullpath(p.PodcastBasePath), titlePath)
|
||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||
return titlePath
|
||||
}
|
||||
// try to find a filename like FILENAME (1).mp3 incrementing
|
||||
return findEpisode(podcast.Fullpath(p.PodcastBasePath), filename, 1)
|
||||
}
|
||||
|
||||
func findEpisode(base, filename string, count int) string {
|
||||
testFile := fmt.Sprintf("%s (%d)%s", filename, count, filepath.Ext(filename))
|
||||
fp := path.Join(base, testFile)
|
||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||
return testFile
|
||||
}
|
||||
return findEpisode(base, filename, count+1)
|
||||
}
|
||||
|
||||
func getContentDispositionFilename(header string) (string, bool) {
|
||||
_, params, _ := mime.ParseMediaType(header)
|
||||
filename, ok := params["filename"]
|
||||
return filename, ok
|
||||
}
|
||||
|
||||
func (p *Podcasts) downloadPodcastCover(podPath string, podcast *db.Podcast) {
|
||||
imageURL, err := url.Parse(podcast.ImageURL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ext := path.Ext(imageURL.Path)
|
||||
resp, err := http.Get(podcast.ImageURL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if ext == "" {
|
||||
filename, _ := getContentDispositionFilename(resp.Header.Get("content-disposition"))
|
||||
ext = path.Ext(filename)
|
||||
}
|
||||
coverPath := path.Join(podPath, "cover"+ext)
|
||||
coverFile, err := os.Create(coverPath)
|
||||
if err != nil {
|
||||
log.Printf("Error creating podcast cover: %s", err)
|
||||
return
|
||||
}
|
||||
if _, err := io.Copy(coverFile, resp.Body); err != nil {
|
||||
log.Printf("Error while writing cover: %s", err)
|
||||
return
|
||||
}
|
||||
podcast.ImagePath = path.Join(filepath.Clean(podcast.Title), "cover"+ext)
|
||||
p.DB.Save(podcast)
|
||||
}
|
||||
|
||||
func (p *Podcasts) doPodcastDownload(podcastEpisode *db.PodcastEpisode, pdFile *os.File, src io.Reader) {
|
||||
_, err := io.Copy(pdFile, src)
|
||||
if err != nil {
|
||||
log.Printf("Error while writing podcast episode: %s", err)
|
||||
podcastEpisode.Status = "error"
|
||||
p.DB.Save(podcastEpisode)
|
||||
return
|
||||
}
|
||||
defer pdFile.Close()
|
||||
stat, _ := pdFile.Stat()
|
||||
podTags, err := tags.New(path.Join(p.PodcastBasePath, podcastEpisode.Path))
|
||||
if err != nil {
|
||||
log.Printf("Error parsing podcast: %e", err)
|
||||
podcastEpisode.Status = "error"
|
||||
p.DB.Save(podcastEpisode)
|
||||
return
|
||||
}
|
||||
podcastEpisode.Bitrate = podTags.Bitrate()
|
||||
podcastEpisode.Status = "completed"
|
||||
podcastEpisode.Length = podTags.Length()
|
||||
podcastEpisode.Size = int(stat.Size())
|
||||
p.DB.Save(podcastEpisode)
|
||||
}
|
||||
|
||||
func (p *Podcasts) DeletePodcast(userID, podcastID int) error {
|
||||
podcast := db.Podcast{}
|
||||
err := p.DB.Where("id=? AND user_id=?", podcastID, userID).First(&podcast).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userCount := 0
|
||||
p.DB.Model(&db.Podcast{}).Where("title=?", podcast.Title).Count(&userCount)
|
||||
if userCount == 1 {
|
||||
// only delete the folder if there are not multiple listeners
|
||||
err = os.RemoveAll(podcast.Fullpath(p.PodcastBasePath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = p.DB.
|
||||
Where("id=? AND user_id=?", podcastID, userID).
|
||||
Delete(db.Podcast{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Podcasts) DeletePodcastEpisode(podcastEpisodeID int) error {
|
||||
podcastEp := db.PodcastEpisode{}
|
||||
err := p.DB.First(&podcastEp, podcastEpisodeID).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
podcastEp.Status = episodeDeleted
|
||||
p.DB.Save(&podcastEp)
|
||||
if err := os.Remove(filepath.Join(p.PodcastBasePath, podcastEp.Path)); err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
29
server/podcasts/podcasts_test.go
Normal file
29
server/podcasts/podcasts_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package podcasts
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
)
|
||||
|
||||
func TestGetMoreRecentEpisodes(t *testing.T) {
|
||||
fp := gofeed.NewParser()
|
||||
newFile, err := os.Open("testdata/rss.new")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newFeed, err := fp.Parse(newFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after, err := time.Parse(time.RFC1123, "Mon, 27 Jun 2016 06:33:43 +0000")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries := getEntriesAfterDate(newFeed.Items, after)
|
||||
if len(entries) != 2 {
|
||||
t.Errorf("Expected 2 entries, got %d", len(entries))
|
||||
}
|
||||
}
|
||||
83
server/podcasts/testdata/rss.new
vendored
Normal file
83
server/podcasts/testdata/rss.new
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:cc="http://web.resource.org/cc/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
|
||||
<channel>
|
||||
<atom:link href="https://internetbox.libsyn.com/rss" rel="self" type="application/rss+xml"/>
|
||||
<title>Internet Box</title>
|
||||
<pubDate>Mon, 01 Apr 2019 04:35:43 +0000</pubDate>
|
||||
<lastBuildDate>Sun, 10 Jan 2021 00:07:33 +0000</lastBuildDate>
|
||||
<generator>Libsyn WebEngine 2.0</generator>
|
||||
<link>https://internetboxpodcast.com</link>
|
||||
<language>en</language>
|
||||
<copyright><![CDATA[]]></copyright>
|
||||
<docs>https://internetboxpodcast.com</docs>
|
||||
<managingEditor>admin@internetboxpodcast.com (admin@internetboxpodcast.com)</managingEditor>
|
||||
<itunes:summary><![CDATA[Michael, Mike, Barbara, Ray, Andrew, Dylon, Lindsay, and Kerry from the AH community talk about various subjects.]]></itunes:summary>
|
||||
<image>
|
||||
<url>https://ssl-static.libsyn.com/p/assets/d/d/3/3/dd338b309838f617/iTunes.png</url>
|
||||
<title>Internet Box</title>
|
||||
<link><![CDATA[https://internetboxpodcast.com]]></link>
|
||||
</image>
|
||||
<itunes:author>Internet Box Crew</itunes:author>
|
||||
<itunes:keywords>achievement,box,friendship,hunter,internet,is,little,magic,mlp,my,pony,rooster,teeth</itunes:keywords>
|
||||
<itunes:category text="Leisure">
|
||||
<itunes:category text="Video Games"/>
|
||||
</itunes:category>
|
||||
<itunes:category text="Comedy"/>
|
||||
<itunes:image href="https://ssl-static.libsyn.com/p/assets/d/d/3/3/dd338b309838f617/iTunes.png" />
|
||||
<itunes:explicit>yes</itunes:explicit>
|
||||
<itunes:owner>
|
||||
<itunes:name><![CDATA[Mike]]></itunes:name>
|
||||
<itunes:email>admin@internetboxpodcast.com</itunes:email>
|
||||
</itunes:owner>
|
||||
<description><![CDATA[Michael, Mike, Barbara, Ray, Andrew, Dylon, Lindsay, and Kerry from the AH community talk about various subjects.]]></description>
|
||||
<itunes:subtitle><![CDATA[Various members from the AH community discuss games, ponies, and life in general.]]></itunes:subtitle>
|
||||
<itunes:type>episodic</itunes:type>
|
||||
|
||||
|
||||
|
||||
|
||||
<item>
|
||||
<title>Episode 128</title>
|
||||
<pubDate>Mon, 01 Apr 2019 04:35:43 +0000</pubDate>
|
||||
<guid isPermaLink="false"><![CDATA[c60f174610d44b408901d6a8d98366b4]]></guid>
|
||||
<link><![CDATA[https://internetboxpodcast.com/episode-128/]]></link>
|
||||
<itunes:image href="https://ssl-static.libsyn.com/p/assets/d/d/3/3/dd338b309838f617/iTunes.png" />
|
||||
<description><![CDATA[<p> </p> <p>The Internet Box is fooling around this week!</p> <p> </p>]]></description>
|
||||
<content:encoded><![CDATA[<p> </p> <p>The Internet Box is fooling around this week!</p> <p> </p>]]></content:encoded>
|
||||
<enclosure length="14637384" type="audio/mpeg" url="https://traffic.libsyn.com/secure/internetbox/InternetBoxEpisode128.mp3?dest-id=79492" />
|
||||
<itunes:duration>44:41</itunes:duration>
|
||||
<itunes:explicit>yes</itunes:explicit>
|
||||
<itunes:keywords>box,meme,fools,april,reddit,copypaste,inernet</itunes:keywords>
|
||||
<itunes:subtitle><![CDATA[ The Internet Box is fooling around this week! ]]></itunes:subtitle>
|
||||
<itunes:episodeType>full</itunes:episodeType>
|
||||
</item>
|
||||
<item>
|
||||
<title>Episode 127</title>
|
||||
<pubDate>Sat, 06 Aug 2016 04:46:28 +0000</pubDate>
|
||||
<guid isPermaLink="false"><![CDATA[99b830789a90b2b6a712382fe4bffc6b]]></guid>
|
||||
<link><![CDATA[http://internetboxpodcast.com/episode-127/]]></link>
|
||||
<itunes:image href="https://ssl-static.libsyn.com/p/assets/d/d/3/3/dd338b309838f617/iTunes.png" />
|
||||
<description><![CDATA[<p>The Internet Box is a national treasure this week!</p>]]></description>
|
||||
<content:encoded><![CDATA[<p>The Internet Box is a national treasure this week!</p>]]></content:encoded>
|
||||
<enclosure length="152364335" type="audio/mpeg" url="https://traffic.libsyn.com/secure/internetbox/InternetBoxEpisode127.mp3?dest-id=79492" />
|
||||
<itunes:duration>01:45:48</itunes:duration>
|
||||
<itunes:explicit>yes</itunes:explicit>
|
||||
<itunes:keywords>box,2,internet,cage,movies,national,go,season,treasure,nic,bane,pokemon</itunes:keywords>
|
||||
<itunes:subtitle><![CDATA[The Internet Box is a national treasure this week!]]></itunes:subtitle>
|
||||
</item>
|
||||
<item>
|
||||
<title>Episode 126</title>
|
||||
<pubDate>Mon, 27 Jun 2016 06:33:43 +0000</pubDate>
|
||||
<guid isPermaLink="false"><![CDATA[d5113fc98b7baf005bf66b225b166ee0]]></guid>
|
||||
<link><![CDATA[http://internetboxpodcast.com/episode-126/]]></link>
|
||||
<itunes:image href="https://ssl-static.libsyn.com/p/assets/d/d/3/3/dd338b309838f617/iTunes.png" />
|
||||
<description><![CDATA[<p>The Internet Box is clicking this week!</p>]]></description>
|
||||
<content:encoded><![CDATA[<p>The Internet Box is clicking this week!</p>]]></content:encoded>
|
||||
<enclosure length="66139165" type="audio/mpeg" url="https://traffic.libsyn.com/secure/internetbox/InternetBoxEpisode126.mp3?dest-id=79492" />
|
||||
<itunes:duration>01:20:42</itunes:duration>
|
||||
<itunes:explicit>yes</itunes:explicit>
|
||||
<itunes:keywords>box,2,factory,internet,pizza,future,robots,season,farts,cake,via,reddit,313,vorarephilia</itunes:keywords>
|
||||
<itunes:subtitle><![CDATA[The Internet Box is clicking this week!]]></itunes:subtitle>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic"
|
||||
"go.senan.xyz/gonic/server/db"
|
||||
"go.senan.xyz/gonic/server/jukebox"
|
||||
"go.senan.xyz/gonic/server/podcasts"
|
||||
"go.senan.xyz/gonic/server/scanner"
|
||||
"go.senan.xyz/gonic/server/scrobble"
|
||||
"go.senan.xyz/gonic/server/scrobble/lastfm"
|
||||
@@ -26,6 +27,7 @@ import (
|
||||
type Options struct {
|
||||
DB *db.DB
|
||||
MusicPath string
|
||||
PodcastPath string
|
||||
CachePath string
|
||||
CoverCachePath string
|
||||
ProxyPrefix string
|
||||
@@ -37,12 +39,14 @@ type Server struct {
|
||||
jukebox *jukebox.Jukebox
|
||||
router *mux.Router
|
||||
sessDB *gormstore.Store
|
||||
podcast *podcasts.Podcasts
|
||||
}
|
||||
|
||||
func New(opts Options) *Server {
|
||||
// ** begin sanitation
|
||||
opts.MusicPath = filepath.Clean(opts.MusicPath)
|
||||
opts.CachePath = filepath.Clean(opts.CachePath)
|
||||
opts.PodcastPath = filepath.Clean(opts.PodcastPath)
|
||||
// ** begin controllers
|
||||
scanner := scanner.New(opts.MusicPath, opts.DB, opts.GenreSplit)
|
||||
jukebox := jukebox.New(opts.MusicPath)
|
||||
@@ -64,7 +68,8 @@ func New(opts Options) *Server {
|
||||
sessDB.SessionOpts.HttpOnly = true
|
||||
sessDB.SessionOpts.SameSite = http.SameSiteLaxMode
|
||||
//
|
||||
ctrlAdmin := ctrladmin.New(base, sessDB)
|
||||
pcInit := &podcasts.Podcasts{DB: opts.DB, PodcastBasePath: opts.PodcastPath}
|
||||
ctrlAdmin := ctrladmin.New(base, sessDB, pcInit)
|
||||
scrobblers := []scrobble.Scrobbler{
|
||||
&lastfm.Scrobbler{DB: opts.DB},
|
||||
&listenbrainz.Scrobbler{},
|
||||
@@ -75,6 +80,7 @@ func New(opts Options) *Server {
|
||||
CoverCachePath: opts.CoverCachePath,
|
||||
Jukebox: jukebox,
|
||||
Scrobblers: scrobblers,
|
||||
Podcasts: pcInit,
|
||||
}
|
||||
setupMisc(r, base)
|
||||
setupAdmin(r.PathPrefix("/admin").Subrouter(), ctrlAdmin)
|
||||
@@ -85,6 +91,7 @@ func New(opts Options) *Server {
|
||||
jukebox: jukebox,
|
||||
router: r,
|
||||
sessDB: sessDB,
|
||||
podcast: &podcasts.Podcasts{DB: opts.DB, PodcastBasePath: opts.PodcastPath},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,6 +142,10 @@ func setupAdmin(r *mux.Router, ctrl *ctrladmin.Controller) {
|
||||
routUser.Handle("/delete_playlist_do", ctrl.H(ctrl.ServeDeletePlaylistDo))
|
||||
routUser.Handle("/create_transcode_pref_do", ctrl.H(ctrl.ServeCreateTranscodePrefDo))
|
||||
routUser.Handle("/delete_transcode_pref_do", ctrl.H(ctrl.ServeDeleteTranscodePrefDo))
|
||||
if ctrl.Podcasts.PodcastBasePath != "" {
|
||||
routUser.Handle("/add_podcast_do", ctrl.H(ctrl.ServePodcastAddDo))
|
||||
routUser.Handle("/delete_podcast_do", ctrl.H(ctrl.ServePodcastDeleteDo))
|
||||
}
|
||||
// ** begin admin routes (if session is valid, and is admin)
|
||||
routAdmin := routUser.NewRoute().Subrouter()
|
||||
routAdmin.Use(ctrl.WithAdminSession)
|
||||
@@ -198,8 +209,15 @@ func setupSubsonic(r *mux.Router, ctrl *ctrlsubsonic.Controller) {
|
||||
r.Handle("/search2{_:(?:\\.view)?}", ctrl.H(ctrl.ServeSearchTwo))
|
||||
r.Handle("/getGenres{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetGenres))
|
||||
r.Handle("/getArtistInfo{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetArtistInfo))
|
||||
// ** begin unimplemented
|
||||
r.Handle("/getPodcasts{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetPodcasts))
|
||||
// ** begin podcasts
|
||||
if ctrl.Podcasts.PodcastBasePath != "" {
|
||||
r.Handle("/getPodcasts{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetPodcasts))
|
||||
r.Handle("/downloadPodcastEpisode{_:(?:\\.view)?}", ctrl.H(ctrl.ServeDownloadPodcastEpisode))
|
||||
r.Handle("/createPodcastChannel{_:(?:\\.view)?}", ctrl.H(ctrl.ServeCreatePodcastChannel))
|
||||
r.Handle("/refreshPodcasts{_:(?:\\.view)?}", ctrl.H(ctrl.ServeRefreshPodcasts))
|
||||
r.Handle("/deletePodcastChannel{_:(?:\\.view)?}", ctrl.H(ctrl.ServeDeletePodcastChannel))
|
||||
r.Handle("/deletePodcastEpisode{_:(?:\\.view)?}", ctrl.H(ctrl.ServeDeletePodcastEpisode))
|
||||
}
|
||||
// middlewares should be run for not found handler
|
||||
// https://github.com/gorilla/mux/issues/416
|
||||
notFoundHandler := ctrl.H(ctrl.ServeNotFound)
|
||||
@@ -264,6 +282,31 @@ func (s *Server) StartJukebox() (FuncExecute, FuncInterrupt) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) StartPodcastRefresher(dur time.Duration) (FuncExecute, FuncInterrupt) {
|
||||
ticker := time.NewTicker(dur)
|
||||
done := make(chan struct{})
|
||||
waitFor := func() error {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if err := s.podcast.RefreshPodcasts(0, true); err != nil {
|
||||
log.Printf("failed to refresh some feeds: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return func() error {
|
||||
log.Printf("starting job 'podcast refresher'\n")
|
||||
return waitFor()
|
||||
}, func(_ error) {
|
||||
// stop job
|
||||
ticker.Stop()
|
||||
done <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) StartSessionClean(dur time.Duration) (FuncExecute, FuncInterrupt) {
|
||||
ticker := time.NewTicker(dur)
|
||||
done := make(chan struct{})
|
||||
|
||||
Reference in New Issue
Block a user