2 Copyright (C) 2006 by Jonas Kramer
3 Published under the terms of the GNU General Public License (GPL).
18 char ** neighbors(const char * user
) {
19 char * encoded
= NULL
, feed
[128], ** names
= NULL
;
24 encode(user
, & encoded
);
26 memset(feed
, (char) 0, sizeof(feed
));
29 "http://ws.audioscrobbler.com/1.0/user/%s/neighbours.txt",
35 names
= cache(feed
, "neighbors", 0);
38 for(i
= 0; names
[i
] != NULL
; ++i
) {
39 char * ptr
= strchr(names
[i
], ',');
41 unsigned length
= strlen(ptr
+ 1);
42 memmove(names
[i
], ptr
+ 1, length
);
44 names
[i
] = realloc(names
[i
], sizeof(char) * (length
+ 1));
45 assert(names
[i
] != NULL
);
53 char ** topartists(const char * user
) {
54 char * encoded
= NULL
, feed
[128], ** names
= NULL
;
59 encode(user
, & encoded
);
61 memset(feed
, (char) 0, sizeof(feed
));
64 "http://ws.audioscrobbler.com/1.0/user/%s/topartists.txt",
70 names
= cache(feed
, "top-artists", 0);
73 for(i
= 0; names
[i
] != NULL
; ++i
) {
74 char * ptr
= strchr(names
[i
], ',');
76 ptr
= strchr(ptr
+ 1, ',');
78 unsigned length
= strlen(ptr
+ 1);
79 memmove(names
[i
], ptr
+ 1, length
);
81 names
[i
] = realloc(names
[i
], sizeof(char) * (length
+ 1));
82 assert(names
[i
] != NULL
);
90 char ** friends(const char * user
) {
91 char * encoded
= NULL
, feed
[128];
94 encode(user
, & encoded
);
96 memset(feed
, (char) 0, sizeof(feed
));
99 "http://ws.audioscrobbler.com/1.0/user/%s/friends.txt",
105 return cache(feed
, "friends", 0);
109 char ** toptags(char key
, struct hash track
) {
110 unsigned length
, x
, count
, idx
;
111 char ** tags
= NULL
, url
[256], * type
= NULL
, * artist
= NULL
,
112 ** resp
, cachename
[512];
114 memset(cachename
, (char) 0, sizeof(cache
));
116 /* Get artist, album or track tags? */
117 type
= strchr("al", key
) ? "artist" : "track";
119 /* Prepare artist name for use in URL. */
120 encode(value(& track
, "creator"), & artist
);
121 stripslashes(artist
);
123 /* Prepare URL for album / artist tags. */
124 memset(url
, 0, sizeof(url
));
127 "http://ws.audioscrobbler.com/1.0/%s/%s/",
133 /* Append title if we want track tags. */
136 encode(value(& track
, "title"), & title
);
137 snprintf(cachename
, sizeof(cachename
), "tags-t-%s--%s", artist
, title
);
139 length
+= snprintf(url
+ length
, sizeof(url
) - length
, "%s/", title
);
142 snprintf(cachename
, sizeof(cachename
), "tags-a-%s", artist
);
145 strncpy(url
+ length
, "toptags.xml", sizeof(url
) - length
- 1);
147 /* Fetch XML document. */
148 if((resp
= cache(url
, cachename
, 0)) == NULL
)
151 /* Count tags in XML. */
152 for(count
= x
= 0; resp
[x
]; ++x
)
153 if(strstr(resp
[x
], "<name>") != NULL
)
156 tags
= calloc(count
+ 1, sizeof(char *));
158 assert(tags
!= NULL
);
162 /* Search tag names in XML document and copy them into our list. */
163 for(x
= 0, idx
= 0; resp
[x
] && idx
< count
; ++x
) {
164 char * pbeg
= strstr(resp
[x
], "<name>");
166 char * pend
= strstr(pbeg
+= 6, "</name>");
169 tags
[idx
++] = strndup(pbeg
, pend
- pbeg
);
170 assert(tags
[idx
- 1] != NULL
);
183 char ** overalltags(void) {
184 unsigned x
, count
= 0, idx
;
185 const char * url
= "http://ws.audioscrobbler.com/1.0/tag/toptags.xml";
186 char ** tags
= NULL
, ** resp
;
188 if((resp
= cache(url
, "overall-tags", 0)) == NULL
)
191 for(x
= 0; resp
[x
]; ++x
)
192 if(strstr(resp
[x
], "<tag name=\"") != NULL
)
195 tags
= calloc(count
+ 1, sizeof(char *));
197 assert(tags
!= NULL
);
201 for(x
= 0, idx
= 0; resp
[x
]; ++x
) {
202 char * pbeg
= strstr(resp
[x
], "<tag name=\""), * pend
;
204 pend
= strstr(pbeg
+= 11, "\"");
207 tags
[idx
++] = strndup(pbeg
, pend
- pbeg
);
208 assert(tags
[idx
- 1] != NULL
);
220 char ** usertags(const char * user
) {
221 char ** tags
= NULL
, ** resp
, * encoded
= NULL
, url
[256], cachename
[64];
222 const char * fmt
= "http://ws.audioscrobbler.com/1.0/user/%s/tags.txt";
225 memset(url
, (char) 0, sizeof(url
));
226 memset(cachename
, (char) 0, sizeof(cachename
));
228 encode(user
, & encoded
);
230 snprintf(url
, sizeof(url
), fmt
, encoded
);
231 snprintf(cachename
, sizeof(cachename
), "usertags-%s", encoded
);
236 if((resp
= cache(url
, cachename
, 0)) != NULL
) {
238 while(resp
[n
] != NULL
) {
239 char * begin
= strchr(resp
[n
], ',');
241 char * end
= strchr(begin
, ',');
245 tags
= realloc(tags
, sizeof(char *) * (ntag
+ 2));
247 assert(tags
!= NULL
);
249 tags
[ntag
++] = strdup(begin
);
251 assert(tags
[ntag
- 1] != NULL
);