2023-10-01 05:42:55 +04:00
|
|
|
package radio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-10-02 03:18:30 +04:00
|
|
|
type Song struct {
|
2024-05-11 04:52:40 +04:00
|
|
|
Artist string
|
|
|
|
Title string
|
|
|
|
StartAt time.Time
|
|
|
|
Duration time.Duration
|
|
|
|
Listeners int64
|
|
|
|
PeakListeners int64
|
2024-05-09 20:30:17 +04:00
|
|
|
}
|
|
|
|
|
2023-10-04 18:10:17 +04:00
|
|
|
// DurationString returns song's duration as a string formatted as [H:]M:SS.
|
2023-10-02 03:52:26 +04:00
|
|
|
func (s *Song) DurationString() string {
|
2023-10-02 03:55:17 +04:00
|
|
|
if s.Duration.Hours() >= 1 {
|
2023-10-02 18:08:57 +04:00
|
|
|
return time.UnixMilli(s.Duration.Milliseconds()).Format("3:4:05")
|
2023-10-02 03:52:26 +04:00
|
|
|
}
|
2023-10-02 18:08:57 +04:00
|
|
|
return time.UnixMilli(s.Duration.Milliseconds()).Format("4:05")
|
2023-10-02 03:52:26 +04:00
|
|
|
}
|
|
|
|
|
2023-10-02 03:18:30 +04:00
|
|
|
func (s *Song) MarshalJSON() ([]byte, error) {
|
2023-10-01 05:42:55 +04:00
|
|
|
return json.Marshal(&struct {
|
2024-05-11 04:52:40 +04:00
|
|
|
Artist string `json:"artist"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
DurationMill int64 `json:"duration_msec,omitempty"`
|
|
|
|
Listeners int64 `json:"listeners"`
|
|
|
|
PeakListeners int64 `json:"max_listeners"`
|
|
|
|
StartAt string `json:"start_at"`
|
2023-10-01 05:42:55 +04:00
|
|
|
}{
|
2024-05-11 04:52:40 +04:00
|
|
|
Artist: s.Artist,
|
|
|
|
Title: s.Title,
|
|
|
|
DurationMill: s.Duration.Milliseconds(),
|
|
|
|
Listeners: s.Listeners,
|
|
|
|
PeakListeners: s.PeakListeners,
|
|
|
|
StartAt: s.StartAt.UTC().Format(time.RFC3339)})
|
2023-10-01 05:42:55 +04:00
|
|
|
}
|