diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b750fe23..083c8a2d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ stages: - bundle variables: - VERSION: 0.0.10 + VERSION: 0.0.13 build-go: image: golang:1.16.2 diff --git a/docs/changelog.md b/docs/changelog.md index 17f6527c..b8f55822 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,3 +1,6 @@ +# 0.0.13 +- Fix multiscrobbler support + # 0.0.12 - Add client TZ support + Selectable on user page - Move token auth to GET ?key=XYZ for wider webhook support diff --git a/internal/goscrobble/ingress_multiscrobbler.go b/internal/goscrobble/ingress_multiscrobbler.go index 4e103725..84fb1c40 100644 --- a/internal/goscrobble/ingress_multiscrobbler.go +++ b/internal/goscrobble/ingress_multiscrobbler.go @@ -2,7 +2,8 @@ package goscrobble import ( "database/sql" - "fmt" + "errors" + "log" "net" "time" ) @@ -12,62 +13,46 @@ type MultiScrobblerInput struct { Album string `json:"album"` Track string `json:"track"` PlayedAt time.Time `json:"playDate"` - Duration string `json:"duration"` + Duration int `json:"duration"` } // ParseMultiScrobblerInput - Transform API data -func ParseMultiScrobblerInput(userUUID string, data map[string]interface{}, ip net.IP, tx *sql.Tx) error { +func ParseMultiScrobblerInput(userUUID string, data MultiScrobblerInput, ip net.IP, tx *sql.Tx) error { // Debugging - fmt.Printf("%+v", data) + artists := make([]string, 0) + albumartists := make([]string, 0) - // // Safety Checks - // if data["artists"] == nil { - // return errors.New("Missing artist data") - // } + // Insert track artists + for _, artist := range data.Artists { + artist, err := insertArtist(artist, "", "", tx) - // if data["album"] == nil { - // return errors.New("Missing album data") - // } + if err != nil { + log.Printf("%+v", err) + return errors.New("Failed to map artist: " + artist.Name) + } + artists = append(artists, artist.Uuid) + } - // if data["track"] == nil { - // return errors.New("Missing track data") - // } - - // // Insert track artists - // for _, artist := range data["artists"] { - // artist, err := insertArtist(artist.Name, "", artist.ID.String(), tx) - - // if err != nil { - // log.Printf("%+v", err) - // return errors.New("Failed to map artist: " + artist.Name) - // } - // artists = append(artists, artist.Uuid) - // } - // // Insert album if not exist - // artists := []string{artist.Uuid} - // album, err := insertAlbum(fmt.Sprintf("%s", data["Album"]), fmt.Sprintf("%s", data["Provider_musicbrainzalbum"]), "", artists, tx) - // if err != nil { - // log.Printf("%+v", err) - // return errors.New("Failed to map album") - // } - - // // 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) - // if err != nil { - // log.Printf("%+v", err) - // return errors.New("Failed to map track") - // } - - // // Insert scrobble if not exist - // timestamp := time.Now() - // fmt.Println(timestamp) - // err = insertScrobble(userUUID, track.Uuid, "jellyfin", timestamp, ip, tx) - // if err != nil { - // log.Printf("%+v", err) - // return errors.New("Failed to map track") - // } + // Insert album if not exist + album, err := insertAlbum(data.Album, "", "", albumartists, tx) + if err != nil { + log.Printf("%+v", err) + return errors.New("Failed to map album") + } // Insert track if not exist + track, err := insertTrack(data.Track, data.Duration, "", "", album.Uuid, artists, tx) + if err != nil { + log.Printf("%+v", err) + return errors.New("Failed to map track") + } + + // Insert scrobble if not exist + err = insertScrobble(userUUID, track.Uuid, "multiscrobbler", data.PlayedAt, ip, tx) + if err != nil { + log.Printf("%+v", err) + return errors.New("Failed to map track") + } + return nil } diff --git a/internal/goscrobble/server.go b/internal/goscrobble/server.go index decc0347..1fc777b9 100644 --- a/internal/goscrobble/server.go +++ b/internal/goscrobble/server.go @@ -220,13 +220,7 @@ func handleResetPassword(w http.ResponseWriter, r *http.Request) { // serveEndpoint - API stuffs func handleIngress(w http.ResponseWriter, r *http.Request, userUuid string) { - bodyJson, err := decodeJson(r.Body) - fmt.Println(err) - if err != nil { - throwInvalidJson(w) - return - } - + var err error ip := getUserIp(r) tx, _ := db.Begin() @@ -234,7 +228,14 @@ func handleIngress(w http.ResponseWriter, r *http.Request, userUuid string) { switch ingressType { case "jellyfin": - err := ParseJellyfinInput(userUuid, bodyJson, ip, tx) + bodyJson, err := decodeJson(r.Body) + fmt.Println(err) + if err != nil { + throwInvalidJson(w) + return + } + + err = ParseJellyfinInput(userUuid, bodyJson, ip, tx) if err != nil { fmt.Printf("Err? %+v", err) tx.Rollback() @@ -242,8 +243,18 @@ func handleIngress(w http.ResponseWriter, r *http.Request, userUuid string) { return } case "multiscrobbler": - err := ParseMultiScrobblerInput(userUuid, bodyJson, ip, tx) + msInput := MultiScrobblerInput{} + err := json.NewDecoder(r.Body).Decode(&msInput) if err != nil { + fmt.Println(err) + tx.Rollback() + throwOkError(w, err.Error()) + return + } + + err = ParseMultiScrobblerInput(userUuid, msInput, ip, tx) + if err != nil { + fmt.Println(err) tx.Rollback() throwOkError(w, err.Error()) return @@ -437,7 +448,7 @@ func deleteSpotifyLink(w http.ResponseWriter, r *http.Request, u string, v strin func fetchServerInfo(w http.ResponseWriter, r *http.Request) { info := ServerInfo{ - Version: "0.0.11", + Version: "0.0.13", } js, _ := json.Marshal(&info)