-
Notifications
You must be signed in to change notification settings - Fork 0
/
artist.go
94 lines (80 loc) · 2.58 KB
/
artist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package catalog
import (
"fmt"
catalog_ "github.com/mlctrez/lexstream/smapiv0/catalog"
"time"
)
// ArtistCatalog
// https://developer.amazon.com/en-US/docs/alexa/music-skills/catalog-reference.html#artist
type ArtistCatalog struct {
Header
Entities []*ArtistEntity `json:"entities"`
}
type ArtistEntity struct {
Id string `json:"id"`
Names []Name `json:"names,omitempty"`
Popularity *Popularity `json:"popularity,omitempty"`
LastUpdatedTime JSONTime `json:"lastUpdatedTime"`
Locales []Locale `json:"locales,omitempty"`
AlternateNames []AlternateName `json:"alternateNames,omitempty"`
LanguageOfContent []string `json:"languageOfContent,omitempty"`
// Deleted must be nil ( not deleted ) or a pointer to true.
// deleted=false is not valid according to the catalog upload api
Deleted *bool `json:"deleted,omitempty"`
}
func CreateArtistCatalog() *ArtistCatalog {
return &ArtistCatalog{Header: buildHeader(MusicGroup), Entities: []*ArtistEntity{}}
}
var _ MetaDataReceiver = (*ArtistCatalog)(nil)
func (ac *ArtistCatalog) AddMetaData(md MetaData, lastUpdate time.Time) {
if md.ArtistID() == "" {
return
}
lexArtistId := fmt.Sprintf("artist.%s", md.ArtistID())
if md.Deleted() {
deleteFlag := true
ae := &ArtistEntity{Id: lexArtistId, LastUpdatedTime: JSONTime(lastUpdate), Deleted: &deleteFlag}
for i, entity := range ac.Entities {
if entity.Id == lexArtistId {
ac.Entities[i] = ae
return
}
}
ac.Entities = append(ac.Entities, ae)
return
}
matched := false
for _, entity := range ac.Entities {
if entity.Id == lexArtistId {
matched = true
if lastUpdate.After(time.Time(entity.LastUpdatedTime)) {
entity.LastUpdatedTime = JSONTime(lastUpdate)
}
}
}
if !matched {
ne := &ArtistEntity{
Id: lexArtistId,
Names: []Name{{Language: "en", Value: md.Artist()}},
Popularity: &Popularity{Default: 100},
LastUpdatedTime: JSONTime(lastUpdate),
Locales: DefaultLocales(),
}
ac.Entities = append(ac.Entities, ne)
}
}
var _ StagingCatalog = (*ArtistCatalog)(nil)
func (ac *ArtistCatalog) CatalogType() catalog_.CatalogType {
return catalog_.CatalogType_AMAZONMusicGroup()
}
func (ac *ArtistCatalog) Validate() error {
expectedCatalog := CreateArtistCatalog()
if !expectedCatalog.TypeMatches(ac.Header) {
return fmt.Errorf("staged catalog type does not match %s", expectedCatalog.Type)
}
if len(ac.Entities) == 0 {
return fmt.Errorf("catalog has no entities")
}
// TODO: validate full catalog spec
return nil
}