2021-04-01 12:56:08 +00:00
|
|
|
package goscrobble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-02 12:11:05 +00:00
|
|
|
"time"
|
2021-04-01 12:56:08 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
type MultiScrobblerInput struct {
|
|
|
|
Artists []string `json:"artists"`
|
|
|
|
Album string `json:"album"`
|
|
|
|
Track string `json:"track"`
|
|
|
|
PlayedAt time.Time `json:"playDate"`
|
|
|
|
Duration string `json:"duration"`
|
|
|
|
}
|
|
|
|
|
2021-04-01 12:56:08 +00:00
|
|
|
// ParseMultiScrobblerInput - Transform API data
|
|
|
|
func ParseMultiScrobblerInput(userUUID string, data map[string]interface{}, ip net.IP, tx *sql.Tx) error {
|
|
|
|
// Debugging
|
|
|
|
fmt.Printf("%+v", data)
|
|
|
|
|
|
|
|
// // Safety Checks
|
2021-04-02 12:11:05 +00:00
|
|
|
// if data["artists"] == nil {
|
2021-04-01 12:56:08 +00:00
|
|
|
// return errors.New("Missing artist data")
|
|
|
|
// }
|
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
// if data["album"] == nil {
|
2021-04-01 12:56:08 +00:00
|
|
|
// return errors.New("Missing album data")
|
|
|
|
// }
|
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
// if data["track"] == nil {
|
2021-04-01 12:56:08 +00:00
|
|
|
// return errors.New("Missing track data")
|
|
|
|
// }
|
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
// // Insert track artists
|
|
|
|
// for _, artist := range data["artists"] {
|
|
|
|
// artist, err := insertArtist(artist.Name, "", artist.ID.String(), tx)
|
2021-04-01 12:56:08 +00:00
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("%+v", err)
|
|
|
|
// return errors.New("Failed to map artist: " + artist.Name)
|
|
|
|
// }
|
|
|
|
// artists = append(artists, artist.Uuid)
|
|
|
|
// }
|
2021-04-01 12:56:08 +00:00
|
|
|
// // Insert album if not exist
|
|
|
|
// artists := []string{artist.Uuid}
|
2021-04-02 12:11:05 +00:00
|
|
|
// album, err := insertAlbum(fmt.Sprintf("%s", data["Album"]), fmt.Sprintf("%s", data["Provider_musicbrainzalbum"]), "", artists, tx)
|
2021-04-01 12:56:08 +00:00
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("%+v", err)
|
|
|
|
// return errors.New("Failed to map album")
|
|
|
|
// }
|
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
// // Insert track if not exist
|
|
|
|
// length := timestampToSeconds(fmt.Sprintf("%s", data["RunTime"]))
|
|
|
|
// track, err := insertTrack(fmt.Sprintf("%s", data["Name"]), length, fmt.Sprintf("%s", data["Provider_musicbrainztrack"]), "", album.Uuid, artists, tx)
|
2021-04-01 12:56:08 +00:00
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("%+v", err)
|
|
|
|
// return errors.New("Failed to map track")
|
|
|
|
// }
|
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
// // Insert scrobble if not exist
|
|
|
|
// timestamp := time.Now()
|
|
|
|
// fmt.Println(timestamp)
|
|
|
|
// err = insertScrobble(userUUID, track.Uuid, "jellyfin", timestamp, ip, tx)
|
2021-04-01 12:56:08 +00:00
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("%+v", err)
|
|
|
|
// return errors.New("Failed to map track")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Insert track if not exist
|
|
|
|
return nil
|
|
|
|
}
|