2021-03-28 08:52:34 +00:00
|
|
|
package goscrobble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Artist 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
|
|
|
}
|
|
|
|
|
2021-04-08 08:46:31 +00:00
|
|
|
type TopArtist struct {
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Img string `json:"img"`
|
|
|
|
Plays int `json:"plays"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TopArtists struct {
|
|
|
|
Artists map[int]TopArtist `json:"artists"`
|
|
|
|
}
|
|
|
|
|
2021-03-28 08:52:34 +00:00
|
|
|
// insertArtist - This will return if it exists or create it based on MBID > Name
|
2021-04-08 07:50:43 +00:00
|
|
|
func insertArtist(name string, mbid string, spotifyId string, img string, tx *sql.Tx) (Artist, error) {
|
2021-03-28 08:52:34 +00:00
|
|
|
artist := Artist{}
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
// Try locate our artist
|
2021-03-28 08:52:34 +00:00
|
|
|
if mbid != "" {
|
2021-04-04 09:54:53 +00:00
|
|
|
artist = getArtistByCol("mbid", mbid, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
} else if spotifyId != "" {
|
2021-04-04 09:54:53 +00:00
|
|
|
artist = getArtistByCol("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 artist.UUID == "" {
|
|
|
|
artist = getArtistByCol("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 artist.UUID == "" {
|
2021-04-08 07:50:43 +00:00
|
|
|
err := insertNewArtist(&artist, name, mbid, spotifyId, img, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error inserting artist: %+v", err)
|
|
|
|
return artist, errors.New("Failed to insert artist")
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
2021-04-02 09:24:00 +00:00
|
|
|
|
2021-04-04 09:54:53 +00:00
|
|
|
if artist.UUID == "" {
|
|
|
|
return artist, errors.New("Unable to fetch artist!")
|
|
|
|
}
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 07:50:43 +00:00
|
|
|
// Updates these values if we have the data!
|
2021-04-04 09:54:53 +00:00
|
|
|
if artist.MusicBrainzID == "" {
|
|
|
|
if artist.MusicBrainzID != mbid {
|
|
|
|
artist.MusicBrainzID = mbid
|
|
|
|
artist.updateArtist("mbid", mbid, tx)
|
|
|
|
}
|
2021-04-03 08:29:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 09:54:53 +00:00
|
|
|
if artist.SpotifyID == "" {
|
|
|
|
if artist.SpotifyID != spotifyId {
|
|
|
|
artist.SpotifyID = spotifyId
|
|
|
|
artist.updateArtist("spotify_id", spotifyId, tx)
|
|
|
|
}
|
2021-04-03 08:29:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 07:50:43 +00:00
|
|
|
if artist.Img == "" {
|
|
|
|
if img != "" {
|
|
|
|
artist.Img = img
|
|
|
|
artist.updateArtist("img", img, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 08:52:34 +00:00
|
|
|
return artist, nil
|
|
|
|
}
|
|
|
|
|
2021-04-04 09:54:53 +00:00
|
|
|
func getArtistByCol(col string, val string, tx *sql.Tx) Artist {
|
2021-03-28 08:52:34 +00:00
|
|
|
var artist Artist
|
|
|
|
err := tx.QueryRow(
|
2022-01-05 07:58:05 +00:00
|
|
|
`SELECT uuid, name, COALESCE(desc,''), COALESCE(img,''), mbid, spotify_id FROM artists WHERE "`+col+`" = $1`,
|
2021-04-04 09:54:53 +00:00
|
|
|
val).Scan(&artist.UUID, &artist.Name, &artist.Desc, &artist.Img, &artist.MusicBrainzID, &artist.SpotifyID)
|
2021-03-28 08:52:34 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.Printf("Error fetching artists: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return artist
|
|
|
|
}
|
|
|
|
|
2021-04-08 07:50:43 +00:00
|
|
|
func insertNewArtist(artist *Artist, name string, mbid string, spotifyId string, img string, tx *sql.Tx) error {
|
2021-04-04 09:54:53 +00:00
|
|
|
artist.UUID = newUUID()
|
|
|
|
artist.Name = name
|
|
|
|
artist.MusicBrainzID = mbid
|
|
|
|
artist.SpotifyID = spotifyId
|
2021-04-08 07:50:43 +00:00
|
|
|
artist.Img = img
|
2021-04-04 09:54:53 +00:00
|
|
|
|
2022-01-05 07:58:05 +00:00
|
|
|
_, err := tx.Exec(`INSERT INTO artists (uuid, name, mbid, spotify_id, img) `+
|
|
|
|
`VALUES ($1,$2,$3,$4,$5)`, artist.UUID, artist.Name, artist.MusicBrainzID, artist.SpotifyID, artist.Img)
|
2021-04-02 09:24:00 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-03 08:29:31 +00:00
|
|
|
func (artist *Artist) updateArtist(col string, val string, tx *sql.Tx) error {
|
2022-01-05 07:58:05 +00:00
|
|
|
_, err := tx.Exec(`UPDATE artists SET "`+col+`" = $1 WHERE uuid = $2`, val, artist.UUID)
|
2021-03-28 08:52:34 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-04-04 09:54:53 +00:00
|
|
|
|
|
|
|
func getArtistByUUID(uuid string) (Artist, error) {
|
|
|
|
var artist Artist
|
2022-01-05 07:58:05 +00:00
|
|
|
err := db.QueryRow(`SELECT uuid, name, COALESCE(desc, ''), COALESCE(img,''), mbid, spotify_id FROM artists WHERE uuid = $1`,
|
2021-04-04 09:54:53 +00:00
|
|
|
uuid).Scan(&artist.UUID, &artist.Name, &artist.Desc, &artist.Img, &artist.MusicBrainzID, &artist.SpotifyID)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return artist, errors.New("Invalid UUID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return artist, nil
|
|
|
|
}
|
2021-04-08 08:46:31 +00:00
|
|
|
|
|
|
|
func getTopArtists(userUuid string) (TopArtists, error) {
|
|
|
|
var topArtist TopArtists
|
|
|
|
|
2022-01-05 07:58:05 +00:00
|
|
|
log.Println(userUuid)
|
|
|
|
rows, err := db.Query(`SELECT artists.uuid, artists.name, COALESCE(artists.uuid,''), count(*) `+
|
|
|
|
`FROM scrobbles `+
|
|
|
|
`JOIN tracks ON tracks.uuid = scrobbles.track `+
|
|
|
|
`JOIN track_artist ON track_artist.track = tracks.uuid `+
|
|
|
|
`JOIN artists ON track_artist.artist = artists.uuid `+
|
|
|
|
`WHERE scrobbles."user" = $1 `+
|
|
|
|
`GROUP BY artists.uuid `+
|
|
|
|
`ORDER BY count(*) DESC `+
|
|
|
|
`LIMIT 14;`,
|
2021-04-08 08:46:31 +00:00
|
|
|
userUuid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to fetch top artist: %+v", err)
|
|
|
|
return topArtist, errors.New("Failed to fetch top artist")
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
i := 1
|
|
|
|
tempArtists := make(map[int]TopArtist)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var artist TopArtist
|
|
|
|
err := rows.Scan(&artist.UUID, &artist.Name, &artist.Img, &artist.Plays)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to fetch artist: %+v", err)
|
|
|
|
return topArtist, errors.New("Failed to fetch artist")
|
|
|
|
}
|
|
|
|
|
|
|
|
tempArtists[i] = artist
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
topArtist.Artists = tempArtists
|
|
|
|
|
|
|
|
return topArtist, nil
|
|
|
|
}
|