Permalink
Browse files

Added subscribers. Playlist JSON now contains a field for the number …

…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...
1 parent eb15267 commit 14ed12c854ae8024103ef1c8a5b4a301dc9a104f @liesen committed Jun 8, 2011
Showing with 27 additions and 3 deletions.
  1. +2 −1 README.md
  2. +5 −1 json.c
  3. +20 −1 server.c
View
@@ -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>
@@ -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
View
6 json.c
@@ -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;
}
@@ -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);
View
@@ -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>
@@ -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) {
@@ -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");
@@ -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;
}
@@ -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;

0 comments on commit 14ed12c

Please sign in to comment.