-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoplist.c
178 lines (154 loc) · 5.87 KB
/
toplist.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
/** This file is part of SpotifyWebApi - <hugolm84@gmail.com> ===
*
* Copyright 2011,Hugo Lindström <hugolm84@gmail.com>
*
*
* Inspiration and reused functions has come from Spotifys spshell example
* and that code is:
* Copyright (c) 2006-2010 Spotify Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
#include <string.h>
#include <ctype.h>
#include "spshell.h"
#include "cmd.h"
/**
* 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();
}
/**
* 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);
}
/**
*
*/
static void toplist_usage(void)
{
cmd_sendresponse(put_error(400,"Usage: toplist / (charts) | ( (tracks | albums | artists) / (global | region / <countrycode> | user / <username>) )"), 400);
}
/**
*
*/
int cmd_toplist(int argc, char **argv)
{
sp_toplisttype type;
sp_toplistregion region;
char username[252];
if(argc == 2) {
if(!strcasecmp(argv[1], "charts"))
toplist_charts();
return -1;
}
if(argc < 3) {
toplist_usage();
return -1;
}
if(!strcasecmp(argv[1], "artists"))
type = SP_TOPLIST_TYPE_ARTISTS;
else if(!strcasecmp(argv[1], "albums"))
type = SP_TOPLIST_TYPE_ALBUMS;
else if(!strcasecmp(argv[1], "tracks"))
type = SP_TOPLIST_TYPE_TRACKS;
else {
toplist_usage();
return -1;
}
if(!strcasecmp(argv[2], "global"))
region = SP_TOPLIST_REGION_EVERYWHERE;
else if(!strcasecmp(argv[2], "user")){
region = SP_TOPLIST_REGION_USER;
if(argc != 4 || strlen(argv[3]) < 0) {
toplist_usage();
return -1;
}
strcpy(username, argv[3]);
}else if(!strcasecmp(argv[2], "region")) {
if(argc != 4 || strlen(argv[3]) != 2) {
toplist_usage();
return -1;
}
int i;
for (i = 0; argv[3][i]; i++)
argv[3][i] = toupper(argv[3][i]);
region = SP_TOPLIST_REGION(argv[3][0], argv[3][1]);
} else {
toplist_usage();
return -1;
}
#ifdef USE_MYSQL
_mysql_updateStats(g_conn, "toplist");
#endif
sp_toplistbrowse_create(g_session, type, region, username, got_toplist, NULL);
return 0;
}