From 728d253e1fc1f261b9ab155c73d909520e9114e9 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Thu, 21 Jan 2021 22:46:19 +0100 Subject: [PATCH] [jsonapi] Make updating of currently playing track metadata easier Update to commit #aaffa4a that makes it easier to update currently playing track, user can use "now_playing" instead of getting the queue item id. --- README_JSON_API.md | 14 ++++++++++++-- README_RADIO_STREAMS.md | 14 ++++++++++++-- src/httpd_jsonapi.c | 24 +++++++++++++++--------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/README_JSON_API.md b/README_JSON_API.md index aa847007..ebb82893 100644 --- a/README_JSON_API.md +++ b/README_JSON_API.md @@ -529,8 +529,8 @@ curl -X PUT "http://localhost:3689/api/outputs/0/toggle" | GET | [/api/queue](#list-queue-items) | Get a list of queue items | | PUT | [/api/queue/clear](#clearing-the-queue) | Remove all items from the queue | | POST | [/api/queue/items/add](#adding-items-to-the-queue) | Add items to the queue | -| PUT | [/api/queue/items/{id}](#updating-a-queue-item) | Updating a queue item in the queue | -| DELETE | [/api/queue/items/{id}](#removing-a-queue-item) | Remove a queue item form the queue | +| PUT | [/api/queue/items/{id}|now_playing](#updating-a-queue-item) | Updating a queue item in the queue | +| DELETE | [/api/queue/items/{id}](#removing-a-queue-item) | Remove a queue item from the queue | @@ -699,6 +699,10 @@ Update or move a queue item in the current queue ```http PUT /api/queue/items/{id} ``` +or +```http +PUT /api/queue/items/now_playing +``` **Path parameters** @@ -706,6 +710,8 @@ PUT /api/queue/items/{id} | --------------- | -------------------- | | id | Queue item id | +(or use now_playing to update the currenly playing track) + **Query parameters** | Parameter | Value | @@ -733,6 +739,10 @@ curl -X PUT "http://localhost:3689/api/queue/items/3?new_position=0" curl -X PUT "http://localhost:3689/api/queue/items/3?title=Awesome%20title&artwork_url=http%3A%2F%2Fgyfgafguf.dk%2Fimages%2Fpige3.jpg" ``` +```shell +curl -X PUT "http://localhost:3689/api/queue/items/now_playing?title=Awesome%20title&artwork_url=http%3A%2F%2Fgyfgafguf.dk%2Fimages%2Fpige3.jpg" +``` + ### Removing a queue item Remove a queue item from the current queue diff --git a/README_RADIO_STREAMS.md b/README_RADIO_STREAMS.md index 43e52eef..d1c7a182 100644 --- a/README_RADIO_STREAMS.md +++ b/README_RADIO_STREAMS.md @@ -43,7 +43,8 @@ Metadata: ``` In the above, first fix is the blank name, second is the image artwork. -### 1) Stream Name/Title + +### 1) Set stream name/title via the M3U file Set the name with an EXTINF tag in the m3u playlist file: ``` @@ -57,7 +58,7 @@ Length is -1 since it's a stream, `` was left blank since `StreamTitle` is accurate in the Metadata but `` was set to `My Radio Stream Name` since `icy-name` was blank. -### 2) Artwork (and track duration) +### 2) StreamUrl is a JSON file with metadata If `StreamUrl` does not point directly to an artwork file then the link may be to a json file that contains an artwork link. If so, you can make forked-daapd download the file automatically and search for an artwork link, and also track @@ -91,6 +92,15 @@ curl -X PUT "http://localhost:3689/api/settings/misc/streamurl_keywords_artwork_ If you want multiple search phrases then comma separate, e.g. "duration,length". +### 3) Set metadata with a custom script +If your radio station publishes metadata via another method than the above, e.g. +just on their web site, then you will have to write a script that pulls the +metadata and then pushes it to forked-daapd. To update metadata for the +currently playing radio station use something like this JSON API request: + +```shell +curl -X PUT "http://localhost:3689/api/queue/items/now_playing?title=Awesome%20title&artwork_url=http%3A%2F%2Fgyfgafguf.dk%2Fimages%2Fpige3.jpg" +``` If your radio station is not returning any artwork links, you can also just make a static artwork by placing a png/jpg in the same directory as the m3u, and with diff --git a/src/httpd_jsonapi.c b/src/httpd_jsonapi.c index be1d1984..4f81d7fa 100644 --- a/src/httpd_jsonapi.c +++ b/src/httpd_jsonapi.c @@ -2533,10 +2533,9 @@ jsonapi_reply_queue_tracks_add(struct httpd_request *hreq) } static int -update_pos(uint32_t item_id, const char *new) +update_pos(uint32_t item_id, const char *new, char shuffle) { uint32_t new_position; - struct player_status status; int ret; if (safe_atou32(new, &new_position) < 0) @@ -2545,8 +2544,7 @@ update_pos(uint32_t item_id, const char *new) return HTTP_BADREQUEST; } - player_get_status(&status); - ret = db_queue_move_byitemid(item_id, new_position, status.shuffle); + ret = db_queue_move_byitemid(item_id, new_position, shuffle); if (ret < 0) { DPRINTF(E_LOG, L_WEB, "Moving item '%d' to new position %d failed\n", item_id, new_position); @@ -2567,22 +2565,29 @@ static int jsonapi_reply_queue_tracks_update(struct httpd_request *hreq) { struct db_queue_item *queue_item; - uint32_t item_id; + struct player_status status; + uint32_t item_id = 0; const char *param; bool is_changed; int ret; - ret = safe_atou32(hreq->uri_parsed->path_parts[3], &item_id); - if (ret < 0 || !(queue_item = db_queue_fetch_byitemid(item_id))) + player_get_status(&status); + + if (strcmp(hreq->uri_parsed->path_parts[3], "now_playing") != 0) + safe_atou32(hreq->uri_parsed->path_parts[3], &item_id); + else + item_id = status.item_id; + + if (!item_id || !(queue_item = db_queue_fetch_byitemid(item_id))) { - DPRINTF(E_LOG, L_WEB, "No valid item id given '%s'\n", hreq->uri_parsed->path); + DPRINTF(E_LOG, L_WEB, "No valid item id given, or now_playing given but not playing: '%s'\n", hreq->uri_parsed->path); return HTTP_BADREQUEST; } ret = HTTP_OK; is_changed = false; if ((param = evhttp_find_header(hreq->query, "new_position"))) - ret = update_pos(item_id, param); + ret = update_pos(item_id, param, status.shuffle); if ((param = evhttp_find_header(hreq->query, "title")) && (is_changed = true)) update_str(&queue_item->title, param); if ((param = evhttp_find_header(hreq->query, "album")) && (is_changed = true)) @@ -4378,6 +4383,7 @@ static struct httpd_uri_map adm_handlers[] = { EVHTTP_REQ_PUT, "^/api/queue/clear$", jsonapi_reply_queue_clear }, { EVHTTP_REQ_POST, "^/api/queue/items/add$", jsonapi_reply_queue_tracks_add }, { EVHTTP_REQ_PUT, "^/api/queue/items/[[:digit:]]+$", jsonapi_reply_queue_tracks_update }, + { EVHTTP_REQ_PUT, "^/api/queue/items/now_playing$", jsonapi_reply_queue_tracks_update }, { EVHTTP_REQ_DELETE, "^/api/queue/items/[[:digit:]]+$", jsonapi_reply_queue_tracks_delete }, { EVHTTP_REQ_POST, "^/api/queue/save$", jsonapi_reply_queue_save},