1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00
Files
mxpv-podsync/pkg/feed/build.go
2020-04-05 13:09:40 +02:00

124 lines
2.7 KiB
Go

package feed
import (
"context"
"fmt"
"strconv"
"time"
itunes "github.com/eduncan911/podcast"
"github.com/pkg/errors"
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/model"
)
type urlProvider interface {
URL(ctx context.Context, ns string, fileName string) (string, error)
}
func Build(ctx context.Context, feed *model.Feed, cfg *config.Feed, provider urlProvider) (*itunes.Podcast, error) {
const (
podsyncGenerator = "Podsync generator (support us at https://github.com/mxpv/podsync)"
defaultCategory = "TV & Film"
)
var (
now = time.Now().UTC()
)
p := itunes.New(feed.Title, feed.ItemURL, feed.Description, &feed.PubDate, &now)
p.Generator = podsyncGenerator
p.AddSubTitle(feed.Title)
p.IAuthor = feed.Title
p.AddSummary(feed.Description)
if cfg.Custom.CoverArt != "" {
p.AddImage(cfg.Custom.CoverArt)
} else {
p.AddImage(feed.CoverArt)
}
if cfg.Custom.Category != "" {
p.AddCategory(cfg.Custom.Category, nil)
} else {
p.AddCategory(defaultCategory, nil)
}
if cfg.Custom.Explicit {
p.IExplicit = "yes"
} else {
p.IExplicit = "no"
}
if cfg.Custom.Language != "" {
p.Language = cfg.Custom.Language
}
for i, episode := range feed.Episodes {
if episode.Status != model.EpisodeDownloaded {
// Skip episodes that are not yet downloaded
continue
}
item := itunes.Item{
GUID: episode.ID,
Link: episode.VideoURL,
Title: episode.Title,
Description: episode.Description,
ISubtitle: episode.Title,
IOrder: strconv.Itoa(i),
}
pubDate := episode.PubDate
if pubDate.IsZero() {
pubDate = now
}
item.AddPubDate(&pubDate)
item.AddSummary(episode.Description)
item.AddImage(episode.Thumbnail)
item.AddDuration(episode.Duration)
enclosureType := itunes.MP4
if feed.Format == model.FormatAudio {
enclosureType = itunes.MP3
}
episodeName := EpisodeName(cfg, episode)
downloadURL, err := provider.URL(ctx, cfg.ID, episodeName)
if err != nil {
return nil, errors.Wrapf(err, "failed to obtain download URL for: %s", episodeName)
}
item.AddEnclosure(downloadURL, enclosureType, episode.Size)
// p.AddItem requires description to be not empty, use workaround
if item.Description == "" {
item.Description = " "
}
if cfg.Custom.Explicit {
item.IExplicit = "yes"
} else {
item.IExplicit = "no"
}
if _, err := p.AddItem(item); err != nil {
return nil, errors.Wrapf(err, "failed to add item to podcast (id %q)", episode.ID)
}
}
return &p, nil
}
func EpisodeName(feedConfig *config.Feed, episode *model.Episode) string {
ext := "mp4"
if feedConfig.Format == model.FormatAudio {
ext = "mp3"
}
return fmt.Sprintf("%s.%s", episode.ID, ext)
}