-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmix.c
196 lines (167 loc) · 4.1 KB
/
mix.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* See LICENSE file for copyright and license details. */
#include "mix.h"
struct mix *
mix_create(json_t *root)
{
struct mix *m;
json_t *id, *name, *user_id, *description, *likes_count, *plays_count,
*tags, *liked;
size_t len;
if (root == NULL)
return NULL;
m = malloc(sizeof(struct mix));
if (m == NULL)
err(1, NULL);
/* Initialize */
m->name = NULL;
m->description = NULL;
m->tags = NULL;
m->track = NULL;
/* Set information from json object */
id = json_object_get(root, "id");
if (id != NULL)
m->id = json_integer_value(id);
name = json_object_get(root, "name");
if (name != NULL) {
len = strlen(json_string_value(name)) + 1;
m->name = malloc(len * sizeof(char));
if (m->name == NULL)
err(1, NULL);
(void)strlcpy(m->name, json_string_value(name), len);
}
user_id = json_object_get(root, "user_id");
if (user_id != NULL)
m->user_id = json_integer_value(user_id);
description = json_object_get(root, "description");
if (description != NULL) {
len = strlen(json_string_value(description)) + 1;
m->description = malloc(len * sizeof(char));
if (m->description == NULL)
err(1, NULL);
(void)strlcpy(m->description, json_string_value(description),
len);
}
likes_count = json_object_get(root, "likes_count");
if (likes_count != NULL)
m->likes_count = json_integer_value(likes_count);
plays_count = json_object_get(root, "plays_count");
if (plays_count != NULL)
m->plays_count = json_integer_value(plays_count);
tags = json_object_get(root, "tag_list_cache");
if (tags != NULL) {
len = strlen(json_string_value(tags)) + 1;
m->tags = malloc(len * sizeof(char));
if (m->tags == NULL)
err(1, NULL);
(void)strlcpy(m->tags, json_string_value(tags), len);
}
liked = json_object_get(root, "liked_by_current_user");
if (liked != NULL)
m->liked = json_is_true(liked);
/* Set default values */
m->finished = FALSE;
m->track_count = 0;
m->track = NULL;
return m;
}
void
mix_free(struct mix *m)
{
int i;
if (m == NULL)
return;
free(m->name);
free(m->description);
free(m->tags);
for (i = 0; i < m->track_count; i++)
track_free(m->track[i]);
free(m->track);
free(m);
}
struct track *
mix_nexttrack(struct info *data)
{
char *js, *url;
size_t len;
int errn;
json_t *root, *set, *status;
struct track *t;
if (data == NULL)
return NULL;
if (data->m == NULL)
return NULL;
/* Initialize variables */
js = NULL;
url = NULL;
root = NULL;
set = NULL;
status = NULL;
errn = setplaytoken(data);
if (errn == ERROR)
goto error;
/* Set up url
* A different url is needed for the first track to get
* the mix started.
*/
if (data->m->track_count == 0) {
len = strlen("http://8tracks.com/sets/") +
strlen(data->playtoken) + strlen("/play?mix_id=") +
intlen(data->m->id) + 1;
url = malloc(len * sizeof(char));
if (url == NULL)
err(1, NULL);
(void)snprintf(url, len,
"http://8tracks.com/sets/%s/play?mix_id=%d",
data->playtoken, data->m->id);
} else {
len = strlen("http://8tracks.com/sets/") +
strlen(data->playtoken) + strlen("/next?mix_id=") +
intlen(data->m->id) + 1;
url = malloc(len * sizeof(char));
if (url == NULL)
err(1, NULL);
(void)snprintf(url, len,
"http://8tracks.com/sets/%s/next?mix_id=%d",
data->playtoken, data->m->id);
}
/* Retrieve json string */
js = malloc(1);
if (js == NULL)
err(1, NULL);
errn = fetch(&js, url);
if (errn == ERROR)
goto error;
/* Parse json string */
root = json_loads(js, 0, NULL);
if (root == NULL)
goto error;
status = json_object_get(root, "status");
if (strcmp(json_string_value(status), "200 OK") != 0)
goto error;
set = json_object_get(root, "set");
if (set == NULL)
goto error;
/* Set up track */
t = track_create(set);
if (t == NULL)
goto error;
data->m->track_count++;
data->m->track = realloc(data->m->track,
data->m->track_count * sizeof(struct track *));
if (data->m->track == NULL)
goto error;
data->m->track[data->m->track_count-1] = t;
/* Cleanup */
free(url);
free(js);
json_decref(root);
return t;
error:
if (js)
free(js);
if (url)
free(url);
if (root)
json_decref(root);
return NULL;
}