Skip to content

Commit

Permalink
Added subscribers. Playlist JSON now contains a field for the number …
Browse files Browse the repository at this point in the history
…of subscribers, subscriberCount. A new method, /playlist/{id}/subscribers, returns a list of the canonical usernames of all subscribers for the playlist.
  • Loading branch information
liesen committed Jun 8, 2011
1 parent 573c591 commit 686c08a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -13,6 +13,7 @@ It's a web server (listens at port 1337 by default) that talks to Spotify using

GET /playlist/{id} -> <playlist>
GET /playlist/{id}/collaborative -> {collaborative:<'true' | 'false'>}
GET /playlist/{id}/subscribers -> [<string>]

POST /playlist <- {title:<string>} -> <playlist>
POST /playlist/{id}/add?index <- [<track URI>] -> <playlist>
Expand All @@ -28,7 +29,7 @@ An extension, `patch`, accepts a (JSON) array of track URIs and replaces all tra

Make sure you have the required libraries

* subversion (`libsvn-dev`, I think) and its dependency `libapr1`
* subversion (`libsvn-dev`) and its dependency `libapr1`
* [libevent](http://monkey.org/~provos/libevent/)
* [jansson](http://www.digip.org/jansson/) > 2.0

Expand Down
6 changes: 5 additions & 1 deletion json.c
Expand Up @@ -18,7 +18,6 @@ json_t *track_to_json(sp_track *track, json_t *object) {

const char *name = sp_track_name(track);
json_object_set_new(object, "title", json_string_nocheck(name));

return object;
}

Expand Down Expand Up @@ -73,6 +72,11 @@ json_t *playlist_to_json(sp_playlist *playlist, json_t *object) {
json_string_nocheck(description));
}

// Number of subscribers
int num_subscribers = sp_playlist_num_subscribers(playlist);
json_object_set_new(object, "subscriberCount",
json_integer(num_subscribers));

// Tracks
json_t *tracks = json_array();
json_object_set_new(object, "tracks", tracks);
Expand Down
21 changes: 20 additions & 1 deletion server.c
Expand Up @@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <event2/buffer.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/keyvalq_struct.h>
#include <event2/thread.h>
#include <event2/util.h>
Expand Down Expand Up @@ -196,6 +197,21 @@ static void get_playlist_collaborative(sp_playlist *playlist,
send_reply_json(request, HTTP_OK, "OK", json);
}

static void get_playlist_subscribers(sp_playlist *playlist,
struct evhttp_request *request,
void *userdata) {
assert(sp_playlist_is_loaded(playlist));
sp_subscribers *subscribers = sp_playlist_subscribers(playlist);
json_t *array = json_array();

for (int i = 0; i < subscribers->count; i++) {
char *subscriber = subscribers->subscribers[i];
json_array_append_new(array, json_string(subscriber));
}

send_reply_json(request, HTTP_OK, "OK", array);
}

// Reads JSON from the requests body. Returns NULL on any error.
static json_t *read_request_body_json(struct evhttp_request *request,
json_error_t *error) {
Expand Down Expand Up @@ -555,6 +571,7 @@ static void put_playlist_patch(sp_playlist *playlist,
// Request dispatcher
static void handle_request(struct evhttp_request *request,
void *userdata) {
evhttp_connection_set_timeout(request->evcon, 1);
evhttp_add_header(evhttp_request_get_output_headers(request),
"Server", "johan@liesen.se/spotify-api-server");

Expand Down Expand Up @@ -607,7 +624,7 @@ static void handle_request(struct evhttp_request *request,
sp_link *playlist_link = sp_link_create_from_string(playlist_uri);

if (playlist_link == NULL) {
send_error(request, HTTP_NOTFOUND, "Link not found");
send_error(request, HTTP_NOTFOUND, "Playlist link not found");
free(uri);
return;
}
Expand Down Expand Up @@ -646,6 +663,8 @@ static void handle_request(struct evhttp_request *request,
request_callback = &get_playlist;
} else if (strncmp(action, "collaborative", 13) == 0) {
request_callback = &get_playlist_collaborative;
} else if (strncmp(action, "subscribers", 11) == 0) {
request_callback = &get_playlist_subscribers;
}
}
break;
Expand Down

0 comments on commit 686c08a

Please sign in to comment.