Skip to content

Commit

Permalink
wip: add live item tag for all shows feed
Browse files Browse the repository at this point in the history
  • Loading branch information
jj-style committed Sep 10, 2024
1 parent 45a344f commit 99b85f4
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 15 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion cmd/gobal-player-server/internal/biz/globalplayer/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync/atomic"

"github.com/google/wire"
"github.com/gorilla/feeds"
"github.com/jj-style/feeds"
"github.com/jj-style/gobal-player/pkg/globalplayer"
feeds2 "github.com/jj-style/gobal-player/pkg/globalplayer/feeds"
"github.com/jj-style/gobal-player/pkg/globalplayer/models"
Expand All @@ -24,6 +24,7 @@ type UseCase interface {
GetEpisodes(context.Context, string, string) ([]*models.Episode, error)
GetEpisodesFeed(context.Context, string, string) (*feeds.Feed, error)
GetAllShowsFeed(context.Context, string) (*feeds.Feed, error)
GetLive(context.Context) ([]*models.LiveStation, error)
}

type useCase struct {
Expand Down Expand Up @@ -135,6 +136,10 @@ func (u *useCase) GetAllShowsFeed(ctx context.Context, stationsSlug string) (*fe
return feeds2.ToFeed(u.hc, &models.Show{Name: st.Name, ImageUrl: st.ImageUrl}, episodes, st.Tagline)
}

func (u *useCase) GetLive(ctx context.Context) ([]*models.LiveStation, error) {
return u.gp.GetLive()
}

func NewUseCase(gp globalplayer.GlobalPlayer, hc resty.HttpClient) UseCase {
return &useCase{
gp: gp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"testing"

"github.com/gorilla/feeds"
"github.com/jj-style/feeds"
"github.com/jj-style/gobal-player/cmd/gobal-player-server/internal/biz/globalplayer"
gpMocks "github.com/jj-style/gobal-player/pkg/globalplayer/mocks"
"github.com/jj-style/gobal-player/pkg/globalplayer/models"
Expand Down
1 change: 1 addition & 0 deletions cmd/gobal-player-server/internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
func addRoutes(r *gin.Engine, service *service.Service) {
api := r.Group("/api")
api.GET("/stations", service.GetStations)
api.GET("/live", service.GetLive)
api.GET("/shows/:slug", service.GetShows)
api.GET("/episodes/:slug/:id", service.GetEpisodes)
api.GET("/shows/:slug/rss", service.GetAllShowsRss)
Expand Down
52 changes: 52 additions & 0 deletions cmd/gobal-player-server/internal/service/service.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package service

import (
"fmt"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/jj-style/feeds"
"github.com/jj-style/gobal-player/cmd/gobal-player-server/internal/biz/globalplayer"
"github.com/jj-style/gobal-player/pkg/globalplayer/models"
"github.com/samber/lo"
)

type Service struct {
Expand All @@ -21,6 +26,15 @@ func (s *Service) GetStations(c *gin.Context) {
c.JSONP(http.StatusOK, gin.H{"stations": stations})
}

func (s *Service) GetLive(c *gin.Context) {
live, err := s.uc.GetLive(c)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSONP(http.StatusOK, gin.H{"live": live})
}

func (s *Service) GetShows(c *gin.Context) {
type request struct {
Slug string `uri:"slug" binding:"required"`
Expand Down Expand Up @@ -107,6 +121,44 @@ func (s *Service) GetAllShowsRss(c *gin.Context) {
return
}

liveStations, err := s.uc.GetLive(c)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

// get the live shows, and if there is a corresponding live feed of the same station
// then add a podcastin2.0 live item tag to the feed
if live, found := lo.Find(liveStations, func(item *models.LiveStation) bool {
return item.Slug == req.Slug
}); found {
feed.Podcasting2LiveItem = &feeds.Podcasting2LiveItem{
Status: "live",
Start: time.Now().Add(-time.Hour).Format(time.RFC3339),
RssItem: &feeds.RssItem{
Enclosure: &feeds.RssEnclosure{Url: live.StreamUrl, Type: "audio/mpeg", Length: "312"},
Title: fmt.Sprintf("%s Live!", live.Name),
Description: live.Tagline,
Link: live.StreamUrl,
Guid: &feeds.RssGuid{Id: feeds.NewUUID().String()},
Podcasting2Item: &feeds.Podcasting2Item{
ContentLink: &feeds.Podcasting2ContentLink{
Href: live.StreamUrl,
Text: "Listen Live!",
},
AlternateEnclosure: &feeds.Podcasting2AlternateEnclosure{
Type: "audio/mpeg",
Length: "312",
Default: true,
Source: feeds.Podcasting2Source{
Url: live.StreamUrl,
},
},
},
},
}
}

rss, err := feed.ToRss()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down
3 changes: 2 additions & 1 deletion cmd/gobal-player-server/internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/gin-gonic/gin"
"github.com/gorilla/feeds"
"github.com/jj-style/feeds"
gpMocks "github.com/jj-style/gobal-player/cmd/gobal-player-server/internal/biz/globalplayer/mocks"
"github.com/jj-style/gobal-player/cmd/gobal-player-server/internal/server"
"github.com/jj-style/gobal-player/cmd/gobal-player-server/internal/service"
Expand Down Expand Up @@ -288,6 +288,7 @@ func Test_service_GetAllShowsRss(t *testing.T) {
name: "happy",
args: args{slug: "slug"},
setup: func(f fields) {
f.uc.EXPECT().GetLive(mock.Anything).Return([]*models.LiveStation{}, nil)
f.uc.EXPECT().GetAllShowsFeed(mock.Anything, "slug").
Return(&feeds.Feed{Title: "title", Description: "description", Id: "id"}, nil)
},
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/jj-style/gobal-player

go 1.22.0

replace github.com/gorilla/feeds => github.com/jj-style/feeds v0.0.0-20240828212116-0463d0c161ce

require (
github.com/adrg/libvlc-go/v3 v3.1.5
github.com/dannav/hhmmss v1.0.0
Expand All @@ -12,7 +10,7 @@ require (
github.com/gdamore/tcell/v2 v2.7.4
github.com/gin-gonic/gin v1.9.1
github.com/golang/mock v1.6.0
github.com/gorilla/feeds v1.1.2
github.com/jj-style/feeds v0.0.0-20240910202655-1572dbe35e3c
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rivo/tview v0.0.0-20240225120200-5605142ca62e
github.com/robfig/cron/v3 v3.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ github.com/influxdata/influxdb-client-go/v2 v2.13.0 h1:ioBbLmR5NMbAjP4UVA5r9b5xG
github.com/influxdata/influxdb-client-go/v2 v2.13.0/go.mod h1:k+spCbt9hcvqvUiz0sr5D8LolXHqAAOfPw9v/RIRHl4=
github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf h1:7JTmneyiNEwVBOHSjoMxiWAqB992atOeepeFYegn5RU=
github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
github.com/jj-style/feeds v0.0.0-20240828212116-0463d0c161ce h1:mTjHvxiC/7cTbXjUwLsGik0CHiRQvONVopgOHJijRTY=
github.com/jj-style/feeds v0.0.0-20240828212116-0463d0c161ce/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y=
github.com/jj-style/feeds v0.0.0-20240910202655-1572dbe35e3c h1:9prZaUOmEvxknMJVbkXVHUGsno24kRkDfiqGV3wyHm8=
github.com/jj-style/feeds v0.0.0-20240910202655-1572dbe35e3c/go.mod h1:XKLvMqWwbKDipqAZ7nbCbiGcGGR8En4yVZI5mrU0C6g=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
Expand Down
13 changes: 13 additions & 0 deletions pkg/globalplayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type GlobalPlayer interface {
GetStations() ([]*models.Station, error)
GetShows(stationSlug string) ([]*models.Show, error)
GetEpisodes(stationSlug, showId string) ([]*models.Episode, error)
GetLive() ([]*models.LiveStation, error)
}

type gpClient struct {
Expand Down Expand Up @@ -105,6 +106,18 @@ func (c *gpClient) GetEpisodes(stationSlug, showId string) ([]*models.Episode, e
}), nil
}

func (c *gpClient) GetLive() ([]*models.LiveStation, error) {
u, _ := url.JoinPath("live", "lbc", "uk.json")
got, err := resty.Get[nextjs.LiveResponse](c.rc, u)
if err != nil {
return nil, err
}

return lo.Map(got.PageProps.Brands, func(item nextjs.Brand, _ int) *models.LiveStation {
return models.LiveStationFromApiModel(&item)
}), nil
}

// Login logs in through the the global player API returning the authorisation response, or errors.
func Login(email, password, apiKey string) (nextjs.LoginResponse, error) {
return resty.Post[url.Values, nextjs.LoginResponse](
Expand Down
2 changes: 1 addition & 1 deletion pkg/globalplayer/feeds/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/dannav/hhmmss"
"github.com/gorilla/feeds"
"github.com/jj-style/feeds"
"github.com/jj-style/gobal-player/pkg/globalplayer/models"
"github.com/jj-style/gobal-player/pkg/resty"
"github.com/samber/lo"
Expand Down
59 changes: 58 additions & 1 deletion pkg/globalplayer/mocks/mock_GlobalPlayer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions pkg/globalplayer/models/live.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package models

import "github.com/jj-style/gobal-player/pkg/globalplayer/models/nextjs"

type LiveStation struct {
Name string `json:"name"`
Slug string `json:"slug"`
StreamUrl string `json:"streamUrl"`
Id string `json:"id"`
Tagline string `json:"tagline"`
ImageUrl string `json:"imageUrl"`
}

func LiveStationFromApiModel(b *nextjs.Brand) *LiveStation {
return &LiveStation{
Id: b.BrandID,
Name: b.BrandName,
ImageUrl: b.BrandLogo,
StreamUrl: b.StreamURL,
Tagline: b.Tagline,
Slug: b.BrandSlug,
}
}
2 changes: 1 addition & 1 deletion pkg/resty/mocks/mock_Cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/resty/mocks/mock_Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/resty/mocks/mock_HttpClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 99b85f4

Please sign in to comment.