mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-22 00:21:55 +00:00
Merge branch 'dev-0.1.2' into 'master'
0.1.2 See merge request goscrobble/goscrobble-api!5
This commit is contained in:
commit
1298bf9a7a
@ -1,6 +1,7 @@
|
|||||||
# 0.1.2
|
# 0.1.2
|
||||||
- Add docker-compose file for local dev
|
- Add docker-compose file for local dev
|
||||||
- Implemented top listeners for artist/album endpoints to match track
|
- Implemented top listeners for artist/album endpoints to match track
|
||||||
|
- Add recent endpoint
|
||||||
|
|
||||||
# 0.1.1
|
# 0.1.1
|
||||||
- Cached all config values
|
- Cached all config values
|
||||||
|
@ -130,54 +130,56 @@ func getAlbumByUUID(uuid string) (Album, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getTopUsersForAlbumUUID - Returns list of top users for a track
|
// getTopUsersForAlbumUUID - Returns list of top users for a track
|
||||||
func getTopUsersForAlbumUUID(trackUUID string, limit int, page int) (TopUserResponse, error) {
|
func getTopUsersForAlbumUUID(albumUUID string, limit int, page int) (TopUserResponse, error) {
|
||||||
response := TopUserResponse{}
|
response := TopUserResponse{}
|
||||||
// TODO: Implement this
|
var count int
|
||||||
// var count int
|
|
||||||
|
|
||||||
// total, err := getDbCount(
|
total, err := getDbCount(
|
||||||
// "SELECT COUNT(*) FROM `scrobbles` WHERE `album` = UUID_TO_BIN(?, true) GROUP BY `album`, `user`", trackUUID)
|
"SELECT COUNT(*) FROM `scrobbles` "+
|
||||||
|
"JOIN `track_album` ON `track_album`.`track` = `scrobbles`.`track` "+
|
||||||
|
"WHERE `track_album`.`album` = UUID_TO_BIN(?, true);", albumUUID)
|
||||||
|
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Printf("Failed to fetch scrobble count: %+v", err)
|
log.Printf("Failed to fetch scrobble count: %+v", err)
|
||||||
// return response, errors.New("Failed to fetch combined scrobbles")
|
return response, errors.New("Failed to fetch combined scrobbles")
|
||||||
// }
|
}
|
||||||
|
|
||||||
// rows, err := db.Query(
|
rows, err := db.Query(
|
||||||
// "SELECT BIN_TO_UUID(`scrobbles`.`user`, true), `users`.`username`, COUNT(*) "+
|
"SELECT BIN_TO_UUID(`scrobbles`.`user`, true), `users`.`username`, COUNT(*) "+
|
||||||
// "FROM `scrobbles` "+
|
"FROM `scrobbles` "+
|
||||||
// "JOIN `users` ON `scrobbles`.`user` = `users`.`uuid` "+
|
"JOIN `users` ON `scrobbles`.`user` = `users`.`uuid` "+
|
||||||
// "WHERE `track` = UUID_TO_BIN(?, true) "+
|
"JOIN `track_album` ON `track_album`.`track` = `scrobbles`.`track` "+
|
||||||
// "GROUP BY `scrobbles`.`user` "+
|
"WHERE `track_album`.`album` = UUID_TO_BIN(?, true) "+
|
||||||
// "ORDER BY COUNT(*) DESC LIMIT ?",
|
"GROUP BY `scrobbles`.`user` "+
|
||||||
// trackUUID, limit)
|
"ORDER BY COUNT(*) DESC LIMIT ?",
|
||||||
|
albumUUID, limit)
|
||||||
|
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
// return response, errors.New("Failed to fetch combined scrobbles")
|
return response, errors.New("Failed to fetch combined scrobbles")
|
||||||
// }
|
}
|
||||||
// defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
// for rows.Next() {
|
for rows.Next() {
|
||||||
// item := TopUserResponseItem{}
|
item := TopUserResponseItem{}
|
||||||
// err := rows.Scan(&item.UserUUID, &item.UserName, &item.Count)
|
err := rows.Scan(&item.UserUUID, &item.UserName, &item.Count)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
// return response, errors.New("Failed to fetch combined scrobbles")
|
return response, errors.New("Failed to fetch combined scrobbles")
|
||||||
// }
|
}
|
||||||
// count++
|
count++
|
||||||
// response.Items = append(response.Items, item)
|
response.Items = append(response.Items, item)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// err = rows.Err()
|
err = rows.Err()
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
// return response, errors.New("Failed to fetch scrobbles")
|
return response, errors.New("Failed to fetch scrobbles")
|
||||||
// }
|
}
|
||||||
|
|
||||||
// response.Meta.Count = count
|
response.Meta.Count = count
|
||||||
// response.Meta.Total = total
|
response.Meta.Total = total
|
||||||
// response.Meta.Page = page
|
response.Meta.Page = page
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
@ -166,56 +166,57 @@ func getTopArtists(userUuid string) (TopArtists, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getTopUsersForArtistUUID - Returns list of top users for a track
|
// getTopUsersForArtistUUID - Returns list of top users for a track
|
||||||
func getTopUsersForArtistUUID(trackUUID string, limit int, page int) (TopUserResponse, error) {
|
func getTopUsersForArtistUUID(artistUUID string, limit int, page int) (TopUserResponse, error) {
|
||||||
response := TopUserResponse{}
|
response := TopUserResponse{}
|
||||||
|
|
||||||
// TODO: Implement
|
var count int
|
||||||
|
|
||||||
// var count int
|
total, err := getDbCount(
|
||||||
|
"SELECT COUNT(*) FROM `scrobbles` "+
|
||||||
|
"JOIN `track_artist` ON `track_artist`.`track` = `scrobbles`.`track` "+
|
||||||
|
"WHERE `track_artist`.`artist` = UUID_TO_BIN(?, true);", artistUUID)
|
||||||
|
|
||||||
// total, err := getDbCount(
|
if err != nil {
|
||||||
// "SELECT COUNT(*) FROM `scrobbles` WHERE `track` = UUID_TO_BIN(?, true) GROUP BY `track`, `user`", trackUUID)
|
log.Printf("Failed to fetch scrobble count: %+v", err)
|
||||||
|
return response, errors.New("Failed to fetch combined scrobbles")
|
||||||
|
}
|
||||||
|
|
||||||
// if err != nil {
|
rows, err := db.Query(
|
||||||
// log.Printf("Failed to fetch scrobble count: %+v", err)
|
"SELECT BIN_TO_UUID(`scrobbles`.`user`, true), `users`.`username`, COUNT(*) "+
|
||||||
// return response, errors.New("Failed to fetch combined scrobbles")
|
"FROM `scrobbles` "+
|
||||||
// }
|
"JOIN `users` ON `scrobbles`.`user` = `users`.`uuid` "+
|
||||||
|
"JOIN `track_artist` ON `track_artist`.`track` = `scrobbles`.`track` "+
|
||||||
|
"WHERE `track_artist`.`artist` = UUID_TO_BIN(?, true) "+
|
||||||
|
"GROUP BY `scrobbles`.`user` "+
|
||||||
|
"ORDER BY COUNT(*) DESC LIMIT ?",
|
||||||
|
artistUUID, limit)
|
||||||
|
|
||||||
// rows, err := db.Query(
|
if err != nil {
|
||||||
// "SELECT BIN_TO_UUID(`scrobbles`.`user`, true), `users`.`username`, COUNT(*) "+
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
// "FROM `scrobbles` "+
|
return response, errors.New("Failed to fetch combined scrobbles")
|
||||||
// "JOIN `users` ON `scrobbles`.`user` = `users`.`uuid` "+
|
}
|
||||||
// "WHERE `track` = UUID_TO_BIN(?, true) "+
|
defer rows.Close()
|
||||||
// "GROUP BY `scrobbles`.`user` "+
|
|
||||||
// "ORDER BY COUNT(*) DESC LIMIT ?",
|
|
||||||
// trackUUID, limit)
|
|
||||||
|
|
||||||
// if err != nil {
|
for rows.Next() {
|
||||||
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
item := TopUserResponseItem{}
|
||||||
// return response, errors.New("Failed to fetch combined scrobbles")
|
err := rows.Scan(&item.UserUUID, &item.UserName, &item.Count)
|
||||||
// }
|
if err != nil {
|
||||||
// defer rows.Close()
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
|
return response, errors.New("Failed to fetch combined scrobbles")
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
response.Items = append(response.Items, item)
|
||||||
|
}
|
||||||
|
|
||||||
// for rows.Next() {
|
err = rows.Err()
|
||||||
// item := TopUserResponseItem{}
|
if err != nil {
|
||||||
// err := rows.Scan(&item.UserUUID, &item.UserName, &item.Count)
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
// if err != nil {
|
return response, errors.New("Failed to fetch scrobbles")
|
||||||
// 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()
|
response.Meta.Count = count
|
||||||
// if err != nil {
|
response.Meta.Total = total
|
||||||
// log.Printf("Failed to fetch scrobbles: %+v", err)
|
response.Meta.Page = page
|
||||||
// return response, errors.New("Failed to fetch scrobbles")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// response.Meta.Count = count
|
|
||||||
// response.Meta.Total = total
|
|
||||||
// response.Meta.Page = page
|
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ type ScrobbleResponseItem struct {
|
|||||||
Album string `json:"album"`
|
Album string `json:"album"`
|
||||||
Track ScrobbleTrackItem `json:"track"`
|
Track ScrobbleTrackItem `json:"track"`
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
|
User ScrobbleTrackItem `json:"user"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ScrobbleTrackItem struct {
|
type ScrobbleTrackItem struct {
|
||||||
@ -127,3 +128,49 @@ func checkIfScrobbleExists(userUuid string, timestamp time.Time, source string)
|
|||||||
|
|
||||||
return count != 0
|
return count != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRecentScrobbles() (ScrobbleResponse, error) {
|
||||||
|
scrobbleReq := ScrobbleResponse{}
|
||||||
|
var count int
|
||||||
|
limit := 50
|
||||||
|
|
||||||
|
rows, err := db.Query(
|
||||||
|
"SELECT BIN_TO_UUID(`scrobbles`.`uuid`, true), `scrobbles`.`created_at`, BIN_TO_UUID(`artists`.`uuid`, true), `artists`.`name`, `albums`.`name`, BIN_TO_UUID(`tracks`.`uuid`, true), `tracks`.`name`, `scrobbles`.`source`, BIN_TO_UUID(`scrobbles`.`user`, true), `users`.`username` FROM `scrobbles` "+
|
||||||
|
"JOIN tracks ON scrobbles.track = tracks.uuid "+
|
||||||
|
"JOIN track_artist ON track_artist.track = tracks.uuid "+
|
||||||
|
"JOIN track_album ON track_album.track = tracks.uuid "+
|
||||||
|
"JOIN artists ON track_artist.artist = artists.uuid "+
|
||||||
|
"JOIN albums ON track_album.album = albums.uuid "+
|
||||||
|
"JOIN users ON scrobbles.user = users.uuid "+
|
||||||
|
"GROUP BY scrobbles.uuid, albums.uuid "+
|
||||||
|
"ORDER BY scrobbles.created_at DESC LIMIT ?", limit)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
|
return scrobbleReq, errors.New("Failed to fetch scrobbles")
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
item := ScrobbleResponseItem{}
|
||||||
|
err := rows.Scan(&item.UUID, &item.Timestamp, &item.Artist.UUID, &item.Artist.Name, &item.Album, &item.Track.UUID, &item.Track.Name, &item.Source, &item.User.UUID, &item.User.Name)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
|
return scrobbleReq, errors.New("Failed to fetch scrobbles")
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
scrobbleReq.Items = append(scrobbleReq.Items, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rows.Err()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch scrobbles: %+v", err)
|
||||||
|
return scrobbleReq, errors.New("Failed to fetch scrobbles")
|
||||||
|
}
|
||||||
|
|
||||||
|
scrobbleReq.Meta.Count = count
|
||||||
|
scrobbleReq.Meta.Total = 50
|
||||||
|
scrobbleReq.Meta.Page = 1
|
||||||
|
|
||||||
|
return scrobbleReq, nil
|
||||||
|
}
|
||||||
|
@ -72,6 +72,8 @@ func HandleRequests(port string) {
|
|||||||
|
|
||||||
// No Auth
|
// No Auth
|
||||||
v1.HandleFunc("/stats", limitMiddleware(handleStats, lightLimiter)).Methods("GET")
|
v1.HandleFunc("/stats", limitMiddleware(handleStats, lightLimiter)).Methods("GET")
|
||||||
|
v1.HandleFunc("/recent", limitMiddleware(handleRecentScrobbles, lightLimiter)).Methods("GET")
|
||||||
|
|
||||||
v1.HandleFunc("/profile/{username}", limitMiddleware(getProfile, lightLimiter)).Methods("GET")
|
v1.HandleFunc("/profile/{username}", limitMiddleware(getProfile, lightLimiter)).Methods("GET")
|
||||||
|
|
||||||
v1.HandleFunc("/artists/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET")
|
v1.HandleFunc("/artists/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET")
|
||||||
@ -844,3 +846,15 @@ func getServerInfo(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(js)
|
w.Write(js)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleRecentScrobbles(w http.ResponseWriter, r *http.Request) {
|
||||||
|
scrobbleList, err := getRecentScrobbles()
|
||||||
|
if err != nil {
|
||||||
|
throwOkError(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
json, _ := json.Marshal(&scrobbleList)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(json)
|
||||||
|
}
|
||||||
|
@ -302,7 +302,7 @@ func getTopUsersForTrackUUID(trackUUID string, limit int, page int) (TopUserResp
|
|||||||
var count int
|
var count int
|
||||||
|
|
||||||
total, err := getDbCount(
|
total, err := getDbCount(
|
||||||
"SELECT COUNT(*) FROM `scrobbles` WHERE `track` = UUID_TO_BIN(?, true) GROUP BY `track`, `user`", trackUUID)
|
"SELECT COUNT(*) FROM `scrobbles` WHERE `track` = UUID_TO_BIN(?, true)", trackUUID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to fetch scrobble count: %+v", err)
|
log.Printf("Failed to fetch scrobble count: %+v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user