Merge pull request #955 from chme/jsonapi-track-playlists

Add JSON API endpoint to fetch playlists for a track
This commit is contained in:
Christian Meffert 2020-04-19 07:21:58 +02:00 committed by GitHub
commit a1cf32172a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 124 additions and 0 deletions

View File

@ -758,6 +758,7 @@ curl -X PUT "http://localhost:3689/api/queue/items/2"
| GET | [/api/library/albums/{id}](#get-an-album) | Get an album |
| GET | [/api/library/albums/{id}/tracks](#list-album-tracks) | Get list of tracks for an album |
| GET | [/api/library/tracks/{id}](#get-a-track) | Get a track |
| GET | [/api/library/tracks/{id}/playlists](#list-playlists-for-a-track) | Get list of playlists for a track |
| PUT | [/api/library/tracks/{id}](#update-track-properties) | Update a tracks properties (rating, play_count) |
| GET | [/api/library/genres](#list-genres) | Get list of genres |
| GET | [/api/library/count](#get-count-of-tracks-artists-and-albums) | Get count of tracks, artists and albums |
@ -1508,6 +1509,63 @@ curl -X GET "http://localhost:3689/api/library/track/1"
```
### List playlists for a track
Get the list of playlists that contain a track (does not return smart playlists)
**Endpoint**
```http
GET /api/library/tracks/{id}/playlists
```
**Path parameters**
| Parameter | Value |
| --------------- | -------------------- |
| id | Track id |
**Query parameters**
| Parameter | Value |
| --------------- | ----------------------------------------------------------- |
| offset | *(Optional)* Offset of the first playlist to return |
| limit | *(Optional)* Maximum number of playlist to return |
**Response**
| Key | Type | Value |
| --------------- | -------- | ----------------------------------------- |
| items | array | Array of [`playlist`](#playlist-object) objects |
| total | integer | Total number of playlists |
| offset | integer | Requested offset of the first playlist |
| limit | integer | Requested maximum number of playlists |
**Example**
```shell
curl -X GET "http://localhost:3689/api/library/tracks/27/playlists"
```
```json
{
"items": [
{
"id": 1,
"name": "playlist",
"path": "/music/srv/playlist.m3u",
"smart_playlist": false,
"uri": "library:playlist:1"
},
...
],
"total": 2,
"offset": 0,
"limit": -1
}
```
### Update track properties
Change properties of a specific track (supported properties are "rating" and "play_count")

View File

@ -3176,6 +3176,71 @@ jsonapi_reply_library_tracks_put_byid(struct httpd_request *hreq)
return HTTP_OK;
}
static int
jsonapi_reply_library_track_playlists(struct httpd_request *hreq)
{
struct query_params query_params;
json_object *reply;
json_object *items;
char *path;
const char *track_id;
int id;
int total;
int ret = 0;
if (!is_modified(hreq->req, DB_ADMIN_DB_MODIFIED))
return HTTP_NOTMODIFIED;
track_id = hreq->uri_parsed->path_parts[3];
if (safe_atoi32(track_id, &id) < 0)
{
DPRINTF(E_LOG, L_WEB, "Error converting track id '%s' to int.\n", track_id);
return HTTP_INTERNAL;
}
path = db_file_path_byid(id);
if (!path)
{
DPRINTF(E_WARN, L_WEB, "No file path found for track with id '%s' not found.\n", track_id);
return HTTP_BADREQUEST;
}
reply = json_object_new_object();
items = json_object_new_array();
json_object_object_add(reply, "items", items);
memset(&query_params, 0, sizeof(struct query_params));
ret = query_params_limit_set(&query_params, hreq);
if (ret < 0)
goto error;
query_params.type = Q_FIND_PL;
query_params.filter = db_mprintf("filepath = '%q'", path);
ret = fetch_playlists(&query_params, items, &total);
if (ret < 0)
goto error;
json_object_object_add(reply, "total", json_object_new_int(total));
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
if (ret < 0)
DPRINTF(E_LOG, L_WEB, "track playlists: Couldn't add playlists to response buffer.\n");
error:
free_query_params(&query_params, 1);
jparse_free(reply);
free(path);
if (ret < 0)
return HTTP_INTERNAL;
return HTTP_OK;
}
static int
jsonapi_reply_library_playlists(struct httpd_request *hreq)
{
@ -4159,6 +4224,7 @@ static struct httpd_uri_map adm_handlers[] =
{ EVHTTP_REQ_PUT, "^/api/library/albums/[[:digit:]]+/tracks$", jsonapi_reply_library_album_tracks_put_byid },
{ EVHTTP_REQ_GET, "^/api/library/tracks/[[:digit:]]+$", jsonapi_reply_library_tracks_get_byid },
{ EVHTTP_REQ_PUT, "^/api/library/tracks/[[:digit:]]+$", jsonapi_reply_library_tracks_put_byid },
{ EVHTTP_REQ_GET, "^/api/library/tracks/[[:digit:]]+/playlists$", jsonapi_reply_library_track_playlists },
{ EVHTTP_REQ_GET, "^/api/library/genres$", jsonapi_reply_library_genres},
{ EVHTTP_REQ_GET, "^/api/library/count$", jsonapi_reply_library_count },
{ EVHTTP_REQ_GET, "^/api/library/files$", jsonapi_reply_library_files },