forked from liesen/spotify-api-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.c
132 lines (101 loc) · 3.74 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <assert.h>
#include <jansson.h>
#include <libspotify/api.h>
#include <string.h>
#include <stdbool.h>
#include "constants.h"
json_t *track_to_json(sp_track *track, json_t *object) {
char uri[kTrackLinkLength];
sp_link *link = sp_link_create_from_track(track, 0);
sp_link_as_string(link, uri, kTrackLinkLength);
sp_link_release(link);
json_object_set_new(object, "uri", json_string_nocheck(uri));
if (!sp_track_is_loaded(track))
return object;
const char *name = sp_track_name(track);
json_object_set_new(object, "title", json_string_nocheck(name));
return object;
}
json_t *playlist_to_json_set_collaborative(sp_playlist *playlist,
json_t *object) {
bool collaborative = sp_playlist_is_collaborative(playlist);
json_object_set_new(object, "collaborative",
collaborative ? json_true() : json_false());
return object;
}
json_t *playlist_to_json(sp_playlist *playlist, json_t *object) {
assert(sp_playlist_is_loaded(playlist));
// Owner
sp_user *owner = sp_playlist_owner(playlist);
const char *username = sp_user_display_name(owner);
sp_user_release(owner);
json_object_set_new_nocheck(object, "creator",
json_string_nocheck(username));
// URI
size_t playlist_uri_len = strlen("spotify:user:") + strlen(username) +
strlen(":playlist:") +
strlen("284on3DVWeAxWkgVuzZKGt") + 1;
char *playlist_uri = malloc(playlist_uri_len);
if (playlist_uri == NULL)
return NULL;
sp_link *playlist_link = sp_link_create_from_playlist(playlist);
if (playlist_link == NULL) // Shouldn't happen; playlist is loaded (?)
return object;
sp_link_as_string(playlist_link, playlist_uri, playlist_uri_len);
sp_link_release(playlist_link);
json_object_set_new(object, "uri",
json_string_nocheck(playlist_uri));
free(playlist_uri);
// Title
const char *title = sp_playlist_name(playlist);
json_object_set_new(object, "title",
json_string_nocheck(title));
// Collaborative
playlist_to_json_set_collaborative(playlist, object);
// Description
const char *description = sp_playlist_get_description(playlist);
if (description != NULL) {
json_object_set_new(object, "description",
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);
char track_uri[kTrackLinkLength];
for (int i = 0; i < sp_playlist_num_tracks(playlist); i++) {
sp_track *track = sp_playlist_track(playlist, i);
sp_link *track_link = sp_link_create_from_track(track, 0);
sp_link_as_string(track_link, track_uri, kTrackLinkLength);
json_array_append(tracks, json_string_nocheck(track_uri));
sp_link_release(track_link);
}
return object;
}
bool json_to_track(json_t *json, sp_track **track) {
if (!json_is_string(json))
return false;
sp_link *link = sp_link_create_from_string(json_string_value(json));
if (link == NULL)
return false;
if (sp_link_type(link) != SP_LINKTYPE_TRACK) {
sp_link_release(link);
return false;
}
*track = sp_link_as_track(link);
return *track != NULL;
}
int json_to_tracks(json_t *json, sp_track **tracks, int num_tracks) {
if (!json_is_array(json))
return 0;
int num_valid_tracks = 0;
for (int i = 0; i < num_tracks; i++) {
json_t *item = json_array_get(json, i);
if (json_to_track(item, &tracks[num_valid_tracks]))
num_valid_tracks++;
}
return num_valid_tracks;
}