/** Dispatch checks args and then trys to match against commands */ void cmd_dispatch(int argc, char **argv) { int i; if(argc < 1) { cmd_sendresponse(put_error(501,"Bad Request"), 404); cmd_done(); return; } for(i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) { if(!strcmp(commands[i].name, argv[0])) { if(commands[i].fn(argc, argv)) cmd_done(); return; }else if(!strcmp("playlist", argv[0])) { /// @note: this is soon to be deprecated, a backwardcomp for prev version #ifdef USE_MYSQL _mysql_updateStats(g_conn, "init"); cmd_sendresponse(put_error(200,"Pong"), 200); cmd_done(); return; #endif } } cmd_sendresponse(put_error(501,"Not implemented"), 404); cmd_done(); }
/** Cmd pong Used when you need to ping the service, and see if its up Responds with "pong" if available. */ int cmd_pong(int arc, char **argv){ #ifdef USE_MYSQL _mysql_updateStats(g_conn, "init"); #endif cmd_sendresponse(put_error(200,"Pong"), 200); cmd_done(); return 1; }
static void search_complete(sp_search *search, void *userdata) { if (sp_search_error(search) == SP_ERROR_OK){ get_search(search); } else cmd_sendresponse(put_error(400, sp_error_message(sp_search_error(search))), 400); sp_search_release(search); cmd_done(); }
/** * @param search The search result */ static void get_search(sp_search *search) { int i; json_t *json = json_object(); json_t *tracks = json_array(); json_object_set_new(json, "tracks", tracks); for (i = 0; i < sp_search_num_tracks(search); ++i){ json_array_append_new(tracks, get_track(sp_search_track(search, i))); } json_object_set_new_nocheck(json, "query", json_string_nocheck(sp_search_query(search))); cmd_sendresponse(json, 200); }
/** Help outputs the cmds, and its args and howtoos */ static int cmd_help(int argc, char **argv) { int i; json_t *json = json_object(); json_t *help = json_object(); json_object_set_new(json, "help", help); json_t *result = json_array(); json_object_set_new(help, "commands", result); for(i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) json_array_append_new(result, json_pack("{s:s, s:s, s:s}", commands[i].name,commands[i].help, "usage", commands[i].usage, "notes", commands[i].notes)); cmd_sendresponse(json, 200); json_decref(json); return -1; }
/** * toplist_charts * * returnes json result for possible charts * the types are taken from spotify client, there may be more types * available for other users in other countries then Sweden. */ static void toplist_charts(void) { json_t *obj = json_pack("{s:[{s:[{s:s,s:s},{s:s,s:s},{s:s,s:s}]},{s:[{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},{s:s,s:s},]}]}", "Charts", "types", "name", "Top Artists", "id", "artists", "name", "Top Albums", "id", "albums", "name", "Top Tracks", "id", "tracks", "geo", "name", "Everywhere", "id", "global", "name", "For me", "id", "user/<username>", "name", "DK", "id", "region/DK", "name", "ES", "id", "region/ES", "name", "FI", "id", "region/FI", "name", "FR", "id", "region/FR", "name", "GB", "id", "region/GB", "name", "NO", "id", "region/NO", "name", "US", "id", "region/US", "name", "SE", "id", "region/SE", "name", "NL", "id", "region/NL" ); cmd_sendresponse(obj, 200); }
/** * Callback for libspotify * * @param result The toplist result object that is now done * @param userdata The opaque pointer given to sp_toplistbrowse_create() */ static void got_toplist(sp_toplistbrowse *result, void *userdata) { int i; json_t *json = json_object(); json_t *toplist = json_object(); json_object_set_new(json, "toplist", toplist); json_t *results = json_array(); json_object_set_new(toplist, "result", results); // We print from all types. Only one of the loops will acually yield anything. for(i = 0; i < sp_toplistbrowse_num_artists(result); i++) json_array_append_new(results, get_artist(sp_toplistbrowse_artist(result, i))); for(i = 0; i < sp_toplistbrowse_num_albums(result); i++) json_array_append_new(results, get_album(sp_toplistbrowse_album(result, i))); for(i = 0; i < sp_toplistbrowse_num_tracks(result); i++) json_array_append_new(results, get_track(sp_toplistbrowse_track(result, i))); cmd_sendresponse(json, 200); sp_toplistbrowse_release(result); cmd_done(); }
static void search_usage(void) { cmd_sendresponse(put_error(400, "Usage: search <query> Example: /search/artist:Madonna track:Like a prayer"), 400); }
static void toplist_usage(void) { cmd_sendresponse(put_error(400,"Usage: toplist / (charts) | ( (tracks | albums | artists) / (global | region / <countrycode> | user / <username>) )"), 400); }