meta.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package ncm
  2. import (
  3. "go.uber.org/zap"
  4. "strings"
  5. "unlock-music.dev/cli/algo/common"
  6. )
  7. type ncmMeta interface {
  8. common.AudioMeta
  9. // GetFormat return the audio format, e.g. mp3, flac
  10. GetFormat() string
  11. // GetAlbumImageURL return the album image url
  12. GetAlbumImageURL() string
  13. }
  14. type ncmMetaMusic struct {
  15. logger *zap.Logger
  16. Format string `json:"format"`
  17. MusicName string `json:"musicName"`
  18. Artist interface{} `json:"artist"`
  19. Album string `json:"album"`
  20. AlbumPicDocID interface{} `json:"albumPicDocId"`
  21. AlbumPic string `json:"albumPic"`
  22. Flag int `json:"flag"`
  23. Bitrate int `json:"bitrate"`
  24. Duration int `json:"duration"`
  25. Alias []interface{} `json:"alias"`
  26. TransNames []interface{} `json:"transNames"`
  27. }
  28. func newNcmMetaMusic(logger *zap.Logger) *ncmMetaMusic {
  29. ncm := new(ncmMetaMusic)
  30. ncm.logger = logger.With(zap.String("module", "ncmMetaMusic"))
  31. return ncm
  32. }
  33. func (m *ncmMetaMusic) GetAlbumImageURL() string {
  34. return m.AlbumPic
  35. }
  36. func (m *ncmMetaMusic) GetArtists() []string {
  37. m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
  38. var artists []string = nil
  39. if jsonArtists, ok := m.Artist.([][]string); ok {
  40. for _, artist := range jsonArtists {
  41. for _, name := range artist {
  42. artists = append(artists, name)
  43. }
  44. }
  45. } else if artist, ok := m.Artist.(string); ok {
  46. // #78: artist is a string type.
  47. // https://git.unlock-music.dev/um/cli/issues/78
  48. artists = []string{artist}
  49. } else {
  50. m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
  51. }
  52. return artists
  53. }
  54. func (m *ncmMetaMusic) GetTitle() string {
  55. return m.MusicName
  56. }
  57. func (m *ncmMetaMusic) GetAlbum() string {
  58. return m.Album
  59. }
  60. func (m *ncmMetaMusic) GetFormat() string {
  61. return m.Format
  62. }
  63. //goland:noinspection SpellCheckingInspection
  64. type ncmMetaDJ struct {
  65. ProgramID int `json:"programId"`
  66. ProgramName string `json:"programName"`
  67. MainMusic ncmMetaMusic `json:"mainMusic"`
  68. DjID int `json:"djId"`
  69. DjName string `json:"djName"`
  70. DjAvatarURL string `json:"djAvatarUrl"`
  71. CreateTime int64 `json:"createTime"`
  72. Brand string `json:"brand"`
  73. Serial int `json:"serial"`
  74. ProgramDesc string `json:"programDesc"`
  75. ProgramFeeType int `json:"programFeeType"`
  76. ProgramBuyed bool `json:"programBuyed"`
  77. RadioID int `json:"radioId"`
  78. RadioName string `json:"radioName"`
  79. RadioCategory string `json:"radioCategory"`
  80. RadioCategoryID int `json:"radioCategoryId"`
  81. RadioDesc string `json:"radioDesc"`
  82. RadioFeeType int `json:"radioFeeType"`
  83. RadioFeeScope int `json:"radioFeeScope"`
  84. RadioBuyed bool `json:"radioBuyed"`
  85. RadioPrice int `json:"radioPrice"`
  86. RadioPurchaseCount int `json:"radioPurchaseCount"`
  87. }
  88. func (m *ncmMetaDJ) GetArtists() []string {
  89. if m.DjName != "" {
  90. return []string{m.DjName}
  91. }
  92. return m.MainMusic.GetArtists()
  93. }
  94. func (m *ncmMetaDJ) GetTitle() string {
  95. if m.ProgramName != "" {
  96. return m.ProgramName
  97. }
  98. return m.MainMusic.GetTitle()
  99. }
  100. func (m *ncmMetaDJ) GetAlbum() string {
  101. if m.Brand != "" {
  102. return m.Brand
  103. }
  104. return m.MainMusic.GetAlbum()
  105. }
  106. func (m *ncmMetaDJ) GetFormat() string {
  107. return m.MainMusic.GetFormat()
  108. }
  109. func (m *ncmMetaDJ) GetAlbumImageURL() string {
  110. if strings.HasPrefix(m.MainMusic.GetAlbumImageURL(), "http") {
  111. return m.MainMusic.GetAlbumImageURL()
  112. }
  113. return m.DjAvatarURL
  114. }