Skip to content

Commit

Permalink
refactor(feed): improve dependency injection for item methods (#34)
Browse files Browse the repository at this point in the history
* refactor(feed): improve dependency injection for item methods

* chore: cleanup unnecessary logs

* test(feed): enrich item
  • Loading branch information
psmarcin authored Jul 14, 2020
1 parent d1dad77 commit 88baaed
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 400 deletions.
3 changes: 1 addition & 2 deletions pkg/api/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
func videoHandler() func(ctx *fiber.Ctx) {
return func(ctx *fiber.Ctx) {
videoID := ctx.Params("videoId")
v := video.New(videoID)

details, err := v.GetFileInformation(ytdl.DefaultClient, ytdl.DefaultClient)
details, err := video.GetFileInformation(videoID, ytdl.DefaultClient, ytdl.DefaultClient)
if err != nil {
l.WithError(err).Errorf("getting video url: %s", videoID)
ctx.SendStatus(http.StatusNotFound)
Expand Down
51 changes: 49 additions & 2 deletions pkg/feed/feed.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package feed

import (
"fmt"
"github.com/eduncan911/podcast"
"github.com/psmarcin/youtubegoespodcast/pkg/youtube"
"github.com/sirupsen/logrus"
"os"
"sort"
"time"
)

const (
Expand Down Expand Up @@ -44,8 +47,7 @@ func Create(channelID string, dependencies Dependencies) (Feed, error) {
return f, err
}

f.SetItems(videos)
err = f.EnrichItems()
err = f.SetItems(videos)
if err != nil {
l.WithError(err).Errorf("can't enrich videos")
return f, err
Expand All @@ -71,6 +73,51 @@ func (f *Feed) sortVideos() {
f.Content.Items = sortByOrder(f.Content.Items)
}

func (f *Feed) SetVideos() error {
// set channel last updated at field as latest item publishing date
if len(f.Items) != 0 {
lastItem := f.Items[0]
f.Content.LastBuildDate = lastItem.PubDate.Format(time.RFC1123Z)
f.Content.PubDate = f.Content.LastBuildDate
}

for _, item := range f.Items {
videoURL := os.Getenv("API_URL") + "video/" + item.ID + "/track.mp3"

err := f.AddItem(podcast.Item{
GUID: item.GUID,
Title: item.Title,
Link: item.Link.String(),
Description: item.Description,
PubDate: &item.PubDate,
Enclosure: &podcast.Enclosure{
URL: videoURL,
Length: item.FileLength,
Type: podcast.MP3,
},
IDuration: fmt.Sprintf("%f", item.Duration.Seconds()),
IExplicit: "no",
})

if err != nil {
l.WithError(err).Errorf("can't add new video to feed")
return err
}
}

return nil
}

func (f *Feed) SetItems(videos []youtube.Video) error {
items, err := NewMap(videos)
if err != nil {
return err
}
f.Items = items

return nil
}

func sortByOrder(items []*podcast.Item) []*podcast.Item {
sort.Slice(items, func(i, j int) bool {
return items[i].PubDate.After(*items[j].PubDate)
Expand Down
101 changes: 101 additions & 0 deletions pkg/feed/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package feed

import (
"github.com/psmarcin/youtubegoespodcast/pkg/video"
"github.com/psmarcin/youtubegoespodcast/pkg/youtube"
"github.com/rylio/ytdl"
"net/url"
"time"
)

type Item struct {
ID string
GUID string
Title string
Link *url.URL
Description string
PubDate time.Time
FileURL *url.URL
FileLength int64
FileType string
Duration time.Duration
IsExplicit bool
}

type fileInformationGetter func(string, video.FileInformationGetter, video.FileUrlGetter) (video.Video, error)

func NewMap(videos []youtube.Video) ([]Item, error) {
stream := make(chan Item, len(videos))
for _, v := range videos {
go func(video youtube.Video) {
item, err := New(video)
if err != nil {
l.WithError(err).WithField("video", video).Infof("can't create new item from video")
}
stream <- item
}(v)
}

var items []Item
counter := 0
for {
if counter >= len(videos) {
break
}

item := <-stream
if item.IsValid() {
items = append(items, item)
}

counter++
}

return items, nil
}

func New(v youtube.Video) (Item, error) {
u, err := url.Parse(v.Url)
if err != nil {
return Item{}, err
}

item := Item{
ID: v.ID,
GUID: v.Url,
Title: v.Title,
Link: u,
Description: v.Description,
PubDate: v.PublishedAt,
FileType: "mp3",
IsExplicit: false,
}

err = item.enrich(video.GetFileInformation)
if err != nil {
return item, err
}

return item, nil
}

func (item *Item) enrich(fileGetter fileInformationGetter) error {
details, err := fileGetter(item.ID, ytdl.DefaultClient, ytdl.DefaultClient)
if err != nil {
return err
}

item.FileURL = details.FileUrl
item.Duration = details.Duration
item.FileLength = details.ContentLength

return nil
}

func (item Item) IsValid() bool {
if item.Title == "" || item.Description == "" {
return false
}

return true
}
Loading

0 comments on commit 88baaed

Please sign in to comment.