-
Notifications
You must be signed in to change notification settings - Fork 39
/
playlist.c
299 lines (266 loc) · 8.67 KB
/
playlist.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "playlist.h"
#include "util.h"
#include <json/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char data[8192];
size_t length;
} buffer_t;
static void fm_song_free(fm_song_t *song)
{
free(song->title);
free(song->artist);
free(song->album);
free(song->cover);
free(song->url);
free(song->audio);
free(song);
}
static fm_song_t* fm_song_parse_json(struct json_object *obj)
{
fm_song_t *song = (fm_song_t*) malloc(sizeof(fm_song_t));
const char json_escape = '\"';
song->title = escape(json_object_get_string(json_object_object_get(obj, "title")), json_escape);
song->artist = escape(json_object_get_string(json_object_object_get(obj, "artist")), json_escape);
song->album = escape(json_object_get_string(json_object_object_get(obj, "albumtitle")), json_escape);
song->pubdate = json_object_get_int(json_object_object_get(obj, "public_time"));
song->cover = escape(json_object_get_string(json_object_object_get(obj, "picture")), json_escape);
song->url = escape(json_object_get_string(json_object_object_get(obj, "album")), json_escape);
song->audio = escape(json_object_get_string(json_object_object_get(obj, "url")), json_escape);
song->sid = json_object_get_int(json_object_object_get(obj, "sid"));
song->like = json_object_get_int(json_object_object_get(obj, "like"));
if (song->sid == 0) {
fm_song_free(song);
song = NULL;
}
return song;
}
static void fm_playlist_history_add(fm_playlist_t *pl, fm_song_t *song, char state)
{
const static int max_hist = 10;
int len = 0;
fm_history_t *h = pl->history;
fm_history_t *last = NULL;
fm_history_t *penult = NULL;
while (h) {
len++;
penult = last;
last = h;
h = h->next;
}
if (len < max_hist) { // append new history item
h = (fm_history_t*) malloc(sizeof(fm_history_t));
h->sid = song->sid;
h->state = state;
h->next = pl->history;
pl->history = h;
}
else { // reuse the last history item and move it to the head
last->sid = song->sid;
last->state = state;
penult->next = NULL;// make penult to new last item
last->next = pl->history;
pl->history = last;
}
}
static void fm_playlist_hisotry_clear(fm_playlist_t *pl)
{
fm_history_t *h = pl->history;
fm_history_t *next;
while (h) {
next = h->next;
free(h);
h = next;
}
}
static const char* fm_playlist_history_str(fm_playlist_t *pl)
{
static char buffer[1024];
memset(buffer, 0, sizeof(buffer));
char* p = buffer;
fm_history_t *hist = pl->history;
while (hist) {
sprintf(p, "|%d:%c", hist->sid, hist->state);
p += strlen(p);
hist = hist->next;
}
return buffer;
}
static void fm_playlist_push_front(fm_playlist_t *pl, fm_song_t *song)
{
song->next = pl->playlist;
pl->playlist = song;
}
static fm_song_t* fm_playlist_pop_front(fm_playlist_t *pl)
{
fm_song_t *ret = NULL;
if (pl->playlist) {
ret = pl->playlist;
pl->playlist = pl->playlist->next;
}
return ret;
}
static void fm_playlist_clear(fm_playlist_t *pl)
{
fm_song_t *s = pl->playlist;
fm_song_t *next;
while (s) {
next = s->next;
fm_song_free(s);
s = next;
}
pl->playlist = NULL;
}
static void fm_playlist_parse_json(fm_playlist_t *pl, struct json_object *obj)
{
int i;
int ret = json_object_get_int(json_object_object_get(obj, "r"));
if (ret != 0) {
fprintf(stderr, "API error: %s\n", json_object_get_string(json_object_object_get(obj, "err")));
exit(ret);
}
printf("Playlist parsing new API response\n");
array_list *songs = json_object_get_array(json_object_object_get(obj, "song"));
for (i = songs->length - 1; i >= 0; i--) {
struct json_object *o = (struct json_object*) array_list_get_idx(songs, i);
fm_song_t *song = fm_song_parse_json(o);
if (song) {
fm_playlist_push_front(pl, song);
printf("Playlist add song %d to the top\n", song->sid);
}
}
json_object_put(obj);
}
void fm_playlist_init(fm_playlist_t *pl, fm_playlist_config_t *config)
{
pl->history = NULL;
pl->playlist = NULL;
pl->api = "http://www.douban.com/j/app/radio/people";
pl->channel_api = "http://www.douban.com/j/app/radio/channels";
pl->app_name = "radio_desktop_win";
pl->version = "100";
pl->config = *config;
pl->curl = curl_easy_init();
}
void fm_playlist_cleanup(fm_playlist_t *pl)
{
curl_easy_cleanup(pl->curl);
fm_playlist_hisotry_clear(pl);
fm_playlist_clear(pl);
}
static size_t append_to_buffer(char *ptr, size_t size, size_t nmemb, void *userp)
{
buffer_t *buffer = (buffer_t*) userp;
size_t bytes = size * nmemb;
memcpy(buffer->data + buffer->length, ptr, bytes);
buffer->length += bytes;
return bytes;
}
static size_t drop_buffer(char *ptr, size_t size, size_t nmemb, void *userp)
{
size_t bytes = size * nmemb;
return bytes;
}
static struct json_object* fm_playlist_send_long_report(fm_playlist_t *pl, int sid, char act)
{
static buffer_t curl_buffer;
char url[1024];
printf("Playlist send long report: %d:%c\n", sid, act);
sprintf(url, "%s?app_name=%s&version=%s&user_id=%d&expire=%d&token=%s&channel=%d&sid=%d&type=%c&h=%s&kbps=%s",
pl->api, pl->app_name, pl->version, pl->config.uid, pl->config.expire, pl->config.token, pl->config.channel,
sid, act, fm_playlist_history_str(pl), pl->config.kbps);
printf("Playlist request: %s\n", url);
memset(curl_buffer.data, 0, sizeof(curl_buffer.data));
curl_buffer.length = 0;
curl_easy_setopt(pl->curl, CURLOPT_URL, url);
curl_easy_setopt(pl->curl, CURLOPT_WRITEFUNCTION, append_to_buffer);
curl_easy_setopt(pl->curl, CURLOPT_WRITEDATA, &curl_buffer);
curl_easy_perform(pl->curl);
return json_tokener_parse(curl_buffer.data);
}
static void fm_playlist_send_short_report(fm_playlist_t *pl, int sid, char act)
{
char url[1024];
printf("Playlist send short report: %d:%c\n", sid, act);
sprintf(url, "%s?app_name=%s&version=%s&user_id=%d&expire=%d&token=%s&channel=%d&sid=%d&type=%c&kbps=%s",
pl->api, pl->app_name, pl->version, pl->config.uid, pl->config.expire, pl->config.token, pl->config.channel,
sid, act, pl->config.kbps);
printf("Playlist request: %s\n", url);
curl_easy_setopt(pl->curl, CURLOPT_URL, url);
curl_easy_setopt(pl->curl, CURLOPT_WRITEFUNCTION, drop_buffer);
curl_easy_setopt(pl->curl, CURLOPT_WRITEDATA, NULL);
curl_easy_perform(pl->curl);
}
fm_song_t* fm_playlist_current(fm_playlist_t *pl)
{
if (pl->playlist == NULL)
return fm_playlist_next(pl);
else
return pl->playlist;
}
fm_song_t* fm_playlist_next(fm_playlist_t *pl)
{
int sid = 0;
printf("Playlist next song\n");
if (pl->playlist) {
sid = pl->playlist->sid;
fm_playlist_history_add(pl, pl->playlist, 'e');
fm_playlist_send_short_report(pl, sid, 'e');
fm_song_free(fm_playlist_pop_front(pl));
if (pl->playlist == NULL) {
printf("Playlist empty, request more\n");
fm_playlist_parse_json(pl, fm_playlist_send_long_report(pl, sid, 'p'));
}
}
else {
printf("Playlist init empty, request new\n");
fm_playlist_parse_json(pl, fm_playlist_send_long_report(pl, sid, 'n'));
}
return pl->playlist;
}
fm_song_t* fm_playlist_skip(fm_playlist_t *pl)
{
int sid = 0;
printf("Playlist skip song\n");
if (pl->playlist) {
sid = pl->playlist->sid;
fm_playlist_history_add(pl, pl->playlist, 's');
fm_playlist_clear(pl);
fm_playlist_parse_json(pl, fm_playlist_send_long_report(pl, sid, 's'));
return pl->playlist;
}
else
return fm_playlist_next(pl);
}
fm_song_t* fm_playlist_ban(fm_playlist_t *pl)
{
int sid = 0;
printf("Playlist ban song\n");
if (pl->playlist) {
sid = pl->playlist->sid;
fm_playlist_history_add(pl, pl->playlist, 'b');
fm_playlist_clear(pl);
fm_playlist_parse_json(pl, fm_playlist_send_long_report(pl, sid, 'b'));
return pl->playlist;
}
else
return fm_playlist_next(pl);
}
void fm_playlist_rate(fm_playlist_t *pl)
{
printf("Playlist rate song\n");
if (pl->playlist) {
pl->playlist->like = 1;
fm_playlist_send_short_report(pl, pl->playlist->sid, 'r');
}
}
void fm_playlist_unrate(fm_playlist_t *pl)
{
printf("Playlist unrate song\n");
if (pl->playlist) {
pl->playlist->like = 0;
fm_playlist_send_short_report(pl, pl->playlist->sid, 'u');
}
}