2021-03-23 08:43:44 +00:00
|
|
|
package goscrobble
|
|
|
|
|
2021-03-28 08:52:34 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2021-04-02 09:24:00 +00:00
|
|
|
"time"
|
2021-03-28 08:52:34 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
// ParseJellyfinInput - Transform API data into a common struct. Uses MusicBrainzID primarily
|
2021-03-28 08:52:34 +00:00
|
|
|
func ParseJellyfinInput(userUUID string, data map[string]interface{}, ip net.IP, tx *sql.Tx) error {
|
2021-04-01 12:56:08 +00:00
|
|
|
// Debugging
|
2021-04-02 09:24:00 +00:00
|
|
|
// fmt.Printf("%+v", data)
|
2021-04-01 12:56:08 +00:00
|
|
|
|
2021-03-30 08:36:28 +00:00
|
|
|
if data["ItemType"] != "Audio" {
|
|
|
|
return errors.New("Media type not audio")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safety Checks
|
|
|
|
if data["Artist"] == nil {
|
|
|
|
return errors.New("Missing artist data")
|
|
|
|
}
|
|
|
|
|
|
|
|
if data["Album"] == nil {
|
|
|
|
return errors.New("Missing album data")
|
|
|
|
}
|
|
|
|
|
|
|
|
if data["Name"] == nil {
|
|
|
|
return errors.New("Missing track data")
|
|
|
|
}
|
|
|
|
|
2021-03-28 08:52:34 +00:00
|
|
|
// Insert artist if not exist
|
2021-04-02 09:24:00 +00:00
|
|
|
artist, err := insertArtist(fmt.Sprintf("%s", data["Artist"]), fmt.Sprintf("%s", data["Provider_musicbrainzartist"]), "", tx)
|
2021-03-28 08:52:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%+v", err)
|
|
|
|
return errors.New("Failed to map artist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert album if not exist
|
|
|
|
artists := []string{artist.Uuid}
|
2021-04-02 09:24:00 +00:00
|
|
|
album, err := insertAlbum(fmt.Sprintf("%s", data["Album"]), fmt.Sprintf("%s", data["Provider_musicbrainzalbum"]), "", artists, tx)
|
2021-03-28 08:52:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%+v", err)
|
|
|
|
return errors.New("Failed to map album")
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:24:00 +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-03-28 08:52:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%+v", err)
|
|
|
|
return errors.New("Failed to map track")
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
// Insert scrobble if not exist
|
|
|
|
timestamp := time.Now()
|
|
|
|
fmt.Println(timestamp)
|
|
|
|
err = insertScrobble(userUUID, track.Uuid, "jellyfin", timestamp, ip, tx)
|
2021-03-28 08:52:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%+v", err)
|
|
|
|
return errors.New("Failed to map track")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert track if not exist
|
|
|
|
return nil
|
2021-03-23 08:43:44 +00:00
|
|
|
}
|