mirror of
https://github.com/idanoo/GoScrobble
synced 2025-07-03 06:32:19 +00:00
0.1.2 Update API + Docker composer
This commit is contained in:
parent
73bfa838ae
commit
5217419945
16 changed files with 332 additions and 34 deletions
|
@ -128,3 +128,56 @@ func getAlbumByUUID(uuid string) (Album, error) {
|
|||
|
||||
return album, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
|
@ -164,3 +164,58 @@ func getTopArtists(userUuid string) (TopArtists, error) {
|
|||
|
||||
return topArtist, nil
|
||||
}
|
||||
|
||||
// getTopUsersForArtistUUID - Returns list of top users for a track
|
||||
func getTopUsersForArtistUUID(trackUUID string, limit int, page int) (TopUserResponse, error) {
|
||||
response := TopUserResponse{}
|
||||
|
||||
// TODO: Implement
|
||||
|
||||
// var count int
|
||||
|
||||
// total, err := getDbCount(
|
||||
// "SELECT COUNT(*) FROM `scrobbles` WHERE `track` = UUID_TO_BIN(?, true) GROUP BY `track`, `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
|
||||
}
|
||||
|
|
|
@ -227,17 +227,17 @@ func ParseSpotifyInput(ctx context.Context, userUUID string, data spotify.Recent
|
|||
}
|
||||
|
||||
// updateImageDataFromSpotify update artist/album images from spotify ;D
|
||||
func (user *User) updateImageDataFromSpotify() error {
|
||||
func (user *User) updateImageDataFromSpotify() {
|
||||
// Check that data is set before we attempt to pull
|
||||
val, _ := getConfigValue("SPOTIFY_API_SECRET")
|
||||
if val == "" {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// TO BE REWORKED TO NOT USE A DAMN USER ARGHHH
|
||||
dbToken, err := user.getSpotifyTokens()
|
||||
if err != nil {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
token := new(oauth2.Token)
|
||||
|
@ -252,7 +252,7 @@ func (user *User) updateImageDataFromSpotify() error {
|
|||
rows, err := db.Query("SELECT BIN_TO_UUID(`uuid`, true), `name` FROM `artists` WHERE IFNULL(`img`,'') NOT IN ('pending', 'complete') LIMIT 100")
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch config: %+v", err)
|
||||
return errors.New("Failed to fetch artists")
|
||||
return
|
||||
}
|
||||
|
||||
toUpdate := make(map[string]string)
|
||||
|
@ -263,7 +263,7 @@ func (user *User) updateImageDataFromSpotify() error {
|
|||
if err != nil {
|
||||
log.Printf("Failed to fetch artists: %+v", err)
|
||||
rows.Close()
|
||||
return errors.New("Failed to fetch artist")
|
||||
return
|
||||
}
|
||||
res, err := client.Search(ctx, name, spotify.SearchTypeArtist)
|
||||
if len(res.Artists.Artists) > 0 {
|
||||
|
@ -296,7 +296,7 @@ func (user *User) updateImageDataFromSpotify() error {
|
|||
rows, err = db.Query("SELECT BIN_TO_UUID(`uuid`, true), `name` FROM `albums` WHERE IFNULL(`img`,'') NOT IN ('pending', 'complete') LIMIT 100")
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch config: %+v", err)
|
||||
return errors.New("Failed to fetch artists")
|
||||
return
|
||||
}
|
||||
|
||||
toUpdate = make(map[string]string)
|
||||
|
@ -307,7 +307,7 @@ func (user *User) updateImageDataFromSpotify() error {
|
|||
if err != nil {
|
||||
log.Printf("Failed to fetch albums: %+v", err)
|
||||
rows.Close()
|
||||
return errors.New("Failed to fetch album")
|
||||
return
|
||||
}
|
||||
res, err := client.Search(ctx, name, spotify.SearchTypeAlbum)
|
||||
if len(res.Albums.Albums) > 0 {
|
||||
|
@ -331,5 +331,6 @@ func (user *User) updateImageDataFromSpotify() error {
|
|||
_ = album.updateAlbum("img", "pending", tx)
|
||||
}
|
||||
tx.Commit()
|
||||
return nil
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -76,9 +76,11 @@ func HandleRequests(port string) {
|
|||
|
||||
v1.HandleFunc("/artists/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET")
|
||||
v1.HandleFunc("/artists/{uuid}", limitMiddleware(getArtist, lightLimiter)).Methods("GET")
|
||||
v1.HandleFunc("/artists/{uuid}/top", limitMiddleware(getTopUsersForArtist, lightLimiter)).Methods("GET")
|
||||
|
||||
v1.HandleFunc("/albums/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET")
|
||||
v1.HandleFunc("/albums/{uuid}", limitMiddleware(getAlbum, lightLimiter)).Methods("GET")
|
||||
v1.HandleFunc("/albums/{uuid}/top", limitMiddleware(getTopUsersForAlbum, lightLimiter)).Methods("GET")
|
||||
|
||||
v1.HandleFunc("/tracks/top/{uuid}", limitMiddleware(getTracks, lightLimiter)).Methods("GET") // User UUID - Top Tracks
|
||||
v1.HandleFunc("/tracks/{uuid}", limitMiddleware(getTrack, lightLimiter)).Methods("GET") // Track UUID
|
||||
|
@ -417,6 +419,8 @@ func patchUser(w http.ResponseWriter, r *http.Request, claims CustomClaims, reqU
|
|||
} else if k == "token" {
|
||||
token := generateToken(32)
|
||||
userFull.updateUser("token", token, ip)
|
||||
} else if k == "active" {
|
||||
userFull.updateUser("active", "0", ip)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -685,6 +689,56 @@ func getTopUsersForTrack(w http.ResponseWriter, r *http.Request) {
|
|||
w.Write(json)
|
||||
}
|
||||
|
||||
// getTopUsersForAlbum - I suck at naming. Returns top users that have scrobbled this track.
|
||||
func getTopUsersForAlbum(w http.ResponseWriter, r *http.Request) {
|
||||
var uuid string
|
||||
for k, v := range mux.Vars(r) {
|
||||
if k == "uuid" {
|
||||
uuid = v
|
||||
}
|
||||
}
|
||||
|
||||
if uuid == "" {
|
||||
throwOkError(w, "Invalid UUID")
|
||||
return
|
||||
}
|
||||
|
||||
userList, err := getTopUsersForAlbumUUID(uuid, 10, 1)
|
||||
if err != nil {
|
||||
throwOkError(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
json, _ := json.Marshal(&userList)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(json)
|
||||
}
|
||||
|
||||
// getTopUsersForArtist - I suck at naming. Returns top users that have scrobbled this track.
|
||||
func getTopUsersForArtist(w http.ResponseWriter, r *http.Request) {
|
||||
var uuid string
|
||||
for k, v := range mux.Vars(r) {
|
||||
if k == "uuid" {
|
||||
uuid = v
|
||||
}
|
||||
}
|
||||
|
||||
if uuid == "" {
|
||||
throwOkError(w, "Invalid UUID")
|
||||
return
|
||||
}
|
||||
|
||||
userList, err := getTopUsersForArtistUUID(uuid, 10, 1)
|
||||
if err != nil {
|
||||
throwOkError(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
json, _ := json.Marshal(&userList)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(json)
|
||||
}
|
||||
|
||||
// postSpotifyResponse - Oauth Response from Spotify
|
||||
func postSpotifyReponse(w http.ResponseWriter, r *http.Request) {
|
||||
err := connectSpotifyResponse(r)
|
||||
|
@ -782,7 +836,7 @@ func getServerInfo(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
info := ServerInfo{
|
||||
Version: "0.1.1",
|
||||
Version: "0.1.2",
|
||||
RegistrationEnabled: registrationEnabled,
|
||||
}
|
||||
|
||||
|
|
|
@ -26,27 +26,11 @@ type TopTrack struct {
|
|||
Img string `json:"img"`
|
||||
Plays int `json:"plays"`
|
||||
}
|
||||
|
||||
type TopTracks struct {
|
||||
Tracks map[int]TopTrack `json:"tracks"`
|
||||
}
|
||||
|
||||
type TopUserTrackResponse struct {
|
||||
Meta TopUserTrackResponseMeta `json:"meta"`
|
||||
Items []TopUserTrackResponseItem `json:"items"`
|
||||
}
|
||||
|
||||
type TopUserTrackResponseMeta struct {
|
||||
Count int `json:"count"`
|
||||
Total int `json:"total"`
|
||||
Page int `json:"page"`
|
||||
}
|
||||
|
||||
type TopUserTrackResponseItem struct {
|
||||
UserUUID string `json:"user_uuid"`
|
||||
Count int `json:"count"`
|
||||
UserName string `json:"user_name"`
|
||||
}
|
||||
|
||||
// insertTrack - This will return if it exists or create it based on MBID > Name
|
||||
func insertTrack(name string, legnth int, mbid string, spotifyId string, album string, artists []string, tx *sql.Tx) (Track, error) {
|
||||
track := Track{}
|
||||
|
@ -313,8 +297,8 @@ func (track *Track) getAlbumsForTrack() error {
|
|||
}
|
||||
|
||||
// getTopUsersForTrackUUID - Returns list of top users for a track
|
||||
func getTopUsersForTrackUUID(trackUUID string, limit int, page int) (TopUserTrackResponse, error) {
|
||||
response := TopUserTrackResponse{}
|
||||
func getTopUsersForTrackUUID(trackUUID string, limit int, page int) (TopUserResponse, error) {
|
||||
response := TopUserResponse{}
|
||||
var count int
|
||||
|
||||
total, err := getDbCount(
|
||||
|
@ -341,7 +325,7 @@ func getTopUsersForTrackUUID(trackUUID string, limit int, page int) (TopUserTrac
|
|||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
item := TopUserTrackResponseItem{}
|
||||
item := TopUserResponseItem{}
|
||||
err := rows.Scan(&item.UserUUID, &item.UserName, &item.Count)
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||
|
|
|
@ -48,6 +48,23 @@ type UserResponse struct {
|
|||
NavidromeURL string `json:"navidrome_server"`
|
||||
}
|
||||
|
||||
type TopUserResponse struct {
|
||||
Meta TopUserResponseMeta `json:"meta"`
|
||||
Items []TopUserResponseItem `json:"items"`
|
||||
}
|
||||
|
||||
type TopUserResponseMeta struct {
|
||||
Count int `json:"count"`
|
||||
Total int `json:"total"`
|
||||
Page int `json:"page"`
|
||||
}
|
||||
|
||||
type TopUserResponseItem struct {
|
||||
UserUUID string `json:"user_uuid"`
|
||||
Count int `json:"count"`
|
||||
UserName string `json:"user_name"`
|
||||
}
|
||||
|
||||
// createUser - Called from API
|
||||
func createUser(req *RequestRequest, ip net.IP) error {
|
||||
// Check if user already exists..
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue