2021-03-28 08:52:34 +00:00
|
|
|
package goscrobble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Album struct {
|
2021-04-04 09:54:53 +00:00
|
|
|
UUID string `json:"uuid"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Desc string `json:"desc"`
|
|
|
|
Img string `json:"img"`
|
|
|
|
MusicBrainzID string `json:"mbid"`
|
|
|
|
SpotifyID string `json:"spotify_id"`
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertAlbum - This will return if it exists or create it based on MBID > Name
|
2021-04-08 07:50:43 +00:00
|
|
|
func insertAlbum(name string, mbid string, spotifyId string, artists []string, img string, tx *sql.Tx) (Album, error) {
|
2021-03-28 08:52:34 +00:00
|
|
|
album := Album{}
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
// Try locate our album
|
2021-03-28 08:52:34 +00:00
|
|
|
if mbid != "" {
|
2021-04-04 09:54:53 +00:00
|
|
|
album = getAlbumByCol("mbid", mbid, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
} else if spotifyId != "" {
|
2021-04-04 09:54:53 +00:00
|
|
|
album = getAlbumByCol("spotify_id", spotifyId, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it didn't match above, lookup by name
|
2021-04-04 09:54:53 +00:00
|
|
|
if album.UUID == "" {
|
|
|
|
album = getAlbumByCol("name", name, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't find it. Lets add it!
|
2021-04-04 09:54:53 +00:00
|
|
|
if album.UUID == "" {
|
2021-04-08 07:50:43 +00:00
|
|
|
err := insertNewAlbum(&album, name, mbid, spotifyId, img, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return album, errors.New("Failed to insert album")
|
|
|
|
}
|
2021-03-28 08:52:34 +00:00
|
|
|
|
2021-04-04 09:54:53 +00:00
|
|
|
if album.UUID == "" {
|
|
|
|
return album, errors.New("Failed to fetch album")
|
2021-04-02 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try linkem up
|
|
|
|
err = album.linkAlbumToArtists(artists, tx)
|
|
|
|
if err != nil {
|
|
|
|
return album, errors.New("Unable to link albums!")
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 09:54:53 +00:00
|
|
|
// Updates these values if we match earlier!
|
2021-04-03 08:29:31 +00:00
|
|
|
if album.MusicBrainzID != mbid {
|
|
|
|
album.MusicBrainzID = mbid
|
|
|
|
album.updateAlbum("mbid", mbid, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if album.SpotifyID != spotifyId {
|
|
|
|
album.SpotifyID = spotifyId
|
|
|
|
album.updateAlbum("spotify_id", spotifyId, tx)
|
|
|
|
}
|
|
|
|
|
2021-04-08 07:50:43 +00:00
|
|
|
if album.Img == "" {
|
|
|
|
if img != "" {
|
|
|
|
album.Img = img
|
|
|
|
album.updateAlbum("img", img, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 08:52:34 +00:00
|
|
|
return album, nil
|
|
|
|
}
|
|
|
|
|
2021-04-04 09:54:53 +00:00
|
|
|
func getAlbumByCol(col string, val string, tx *sql.Tx) Album {
|
2021-03-28 08:52:34 +00:00
|
|
|
var album Album
|
|
|
|
err := tx.QueryRow(
|
2021-04-04 09:54:53 +00:00
|
|
|
"SELECT BIN_TO_UUID(`uuid`, true), `name`, IFNULL(`desc`, ''), IFNULL(`img`,''), `mbid`, `spotify_id` FROM `albums` WHERE `"+col+"` = ?",
|
|
|
|
val).Scan(&album.UUID, &album.Name, &album.Desc, &album.Img, &album.MusicBrainzID, &album.SpotifyID)
|
2021-03-28 08:52:34 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.Printf("Error fetching albums: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return album
|
|
|
|
}
|
|
|
|
|
2021-04-08 07:50:43 +00:00
|
|
|
func insertNewAlbum(album *Album, name string, mbid string, spotifyId string, img string, tx *sql.Tx) error {
|
2021-04-04 09:54:53 +00:00
|
|
|
album.UUID = newUUID()
|
|
|
|
album.Name = name
|
|
|
|
album.MusicBrainzID = mbid
|
|
|
|
album.SpotifyID = spotifyId
|
2021-04-08 07:50:43 +00:00
|
|
|
album.Img = img
|
2021-04-04 09:54:53 +00:00
|
|
|
|
2021-04-08 07:50:43 +00:00
|
|
|
_, err := tx.Exec("INSERT INTO `albums` (`uuid`, `name`, `mbid`, `spotify_id`, `img`) "+
|
|
|
|
"VALUES (UUID_TO_BIN(?, true),?,?,?,?)", album.UUID, album.Name, album.MusicBrainzID, album.SpotifyID, album.Img)
|
2021-03-28 08:52:34 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (album *Album) linkAlbumToArtists(artists []string, tx *sql.Tx) error {
|
|
|
|
var err error
|
|
|
|
for _, artist := range artists {
|
2021-03-28 09:24:18 +00:00
|
|
|
_, err = tx.Exec("INSERT INTO `album_artist` (`album`, `artist`) "+
|
2021-04-04 09:54:53 +00:00
|
|
|
"VALUES (UUID_TO_BIN(?, true), UUID_TO_BIN(?, true))", album.UUID, artist)
|
2021-03-28 08:52:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-04-02 09:24:00 +00:00
|
|
|
|
2021-04-03 08:29:31 +00:00
|
|
|
func (album *Album) updateAlbum(col string, val string, tx *sql.Tx) error {
|
2021-04-04 09:54:53 +00:00
|
|
|
_, err := tx.Exec("UPDATE `albums` SET `"+col+"` = ? WHERE `uuid` = UUID_TO_BIN(?,true)", val, album.UUID)
|
2021-04-02 09:24:00 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-04-04 09:54:53 +00:00
|
|
|
|
|
|
|
func getAlbumByUUID(uuid string) (Album, error) {
|
|
|
|
var album Album
|
|
|
|
err := db.QueryRow("SELECT BIN_TO_UUID(`uuid`, true), `name`, IFNULL(`desc`,''), IFNULL(`img`,''), `mbid`, `spotify_id` FROM `albums` WHERE `uuid` = UUID_TO_BIN(?, true)",
|
|
|
|
uuid).Scan(&album.UUID, &album.Name, &album.Desc, &album.Img, &album.MusicBrainzID, &album.SpotifyID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return album, errors.New("Invalid UUID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return album, nil
|
|
|
|
}
|
2022-01-05 21:20:30 +00:00
|
|
|
|
|
|
|
// getTopUsersForAlbumUUID - Returns list of top users for a track
|
|
|
|
func getTopUsersForAlbumUUID(trackUUID string, limit int, page int) (TopUserResponse, error) {
|
|
|
|
response := TopUserResponse{}
|
|
|
|
// TODO: Implement this
|
|
|
|
// var count int
|
|
|
|
|
|
|
|
// total, err := getDbCount(
|
|
|
|
// "SELECT COUNT(*) FROM `scrobbles` WHERE `album` = UUID_TO_BIN(?, true) GROUP BY `album`, `user`", trackUUID)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("Failed to fetch scrobble count: %+v", err)
|
|
|
|
// return response, errors.New("Failed to fetch combined scrobbles")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// rows, err := db.Query(
|
|
|
|
// "SELECT BIN_TO_UUID(`scrobbles`.`user`, true), `users`.`username`, COUNT(*) "+
|
|
|
|
// "FROM `scrobbles` "+
|
|
|
|
// "JOIN `users` ON `scrobbles`.`user` = `users`.`uuid` "+
|
|
|
|
// "WHERE `track` = UUID_TO_BIN(?, true) "+
|
|
|
|
// "GROUP BY `scrobbles`.`user` "+
|
|
|
|
// "ORDER BY COUNT(*) DESC LIMIT ?",
|
|
|
|
// trackUUID, limit)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
|
|
|
// return response, errors.New("Failed to fetch combined scrobbles")
|
|
|
|
// }
|
|
|
|
// defer rows.Close()
|
|
|
|
|
|
|
|
// for rows.Next() {
|
|
|
|
// item := TopUserResponseItem{}
|
|
|
|
// err := rows.Scan(&item.UserUUID, &item.UserName, &item.Count)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
|
|
|
// return response, errors.New("Failed to fetch combined scrobbles")
|
|
|
|
// }
|
|
|
|
// count++
|
|
|
|
// response.Items = append(response.Items, item)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// err = rows.Err()
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
|
|
|
// return response, errors.New("Failed to fetch scrobbles")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// response.Meta.Count = count
|
|
|
|
// response.Meta.Total = total
|
|
|
|
// response.Meta.Page = page
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|