001/*
002 * Copyright (c) 2012, the Last.fm Java Project and Committers
003 * All rights reserved.
004 *
005 * Redistribution and use of this software in source and binary forms, with or without modification, are
006 * permitted provided that the following conditions are met:
007 *
008 * - Redistributions of source code must retain the above
009 *   copyright notice, this list of conditions and the
010 *   following disclaimer.
011 *
012 * - Redistributions in binary form must reproduce the above
013 *   copyright notice, this list of conditions and the
014 *   following disclaimer in the documentation and/or other
015 *   materials provided with the distribution.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
019 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
020 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
021 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
023 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026
027package de.umass.lastfm;
028
029import java.util.ArrayList;
030import java.util.Collection;
031import java.util.HashMap;
032import java.util.Map;
033
034/**
035 * Contains bindings for all methods in the "library" namespace.
036 *
037 * @author Martin Chorley
038 * @author Janni Kovacs
039 */
040public class Library {
041
042        private Library() {
043        }
044
045        /**
046         * Retrieves a paginated list of all the artists in a user's library.
047         *
048         * @param user The user whose library you want to fetch.
049         * @param apiKey A Last.fm API key.
050         * @return a {@link PaginatedResult} of the artists
051         */
052        public static PaginatedResult<Artist> getArtists(String user, String apiKey) {
053                return getArtists(user, 1, 0, apiKey);
054        }
055
056        /**
057         * Retrieves a paginated list of all the artists in a user's library.
058         *
059         * @param user The user whose library you want to fetch.
060         * @param page The page number you wish to scan to.
061         * @param apiKey A Last.fm API key.
062         * @return a {@link PaginatedResult} of the artists
063         */
064        public static PaginatedResult<Artist> getArtists(String user, int page, String apiKey) {
065                return getArtists(user, page, 0, apiKey);
066        }
067
068        /**
069         * Retrieves a paginated list of all the artists in a user's library.
070         *
071         * @param user The user whose library you want to fetch.
072         * @param page The page number you wish to scan to.
073         * @param limit Limit the number of artists returned (maximum/default is 50).
074         * @param apiKey A Last.fm API key.
075         * @return a {@link PaginatedResult} of the artists
076         */
077        public static PaginatedResult<Artist> getArtists(String user, int page, int limit, String apiKey) {
078                Map<String, String> params = new HashMap<String, String>();
079                params.put("user", user);
080                params.put("page", String.valueOf(page));
081                if (limit > 0)
082                        params.put("limit", String.valueOf(limit));
083                Result result = Caller.getInstance().call("library.getArtists", apiKey, params);
084                return ResponseBuilder.buildPaginatedResult(result, Artist.class);
085        }
086
087        /**
088         * Retrieves all artists in a user's library. Pay attention if you use this method as it may produce
089         * a lot of network traffic and therefore may consume a long time.
090         *
091         * @param user The user whose library you want to fetch.
092         * @param apiKey A Last.fm API key.
093         * @return all artists in a user's library
094         */
095        public static Collection<Artist> getAllArtists(String user, String apiKey) {
096                Collection<Artist> artists = null;
097                int page = 1, total;
098                do {
099                        PaginatedResult<Artist> result = getArtists(user, page, apiKey);
100                        total = result.getTotalPages();
101                        Collection<Artist> pageResults = result.getPageResults();
102                        if (artists == null) {
103                                // artists is initialized here to initialize it with the right size and avoid array copying later on
104                                artists = new ArrayList<Artist>(total * pageResults.size());
105                        }
106                        artists.addAll(pageResults);
107                        page++;
108                } while (page <= total);
109                return artists;
110        }
111
112
113        /**
114         * Retrieves a paginated list of all the albums in a user's library.
115         *
116         * @param user The user whose library you want to fetch.
117         * @param apiKey A Last.fm API key.
118         * @return a {@link PaginatedResult} of the albums
119         */
120        public static PaginatedResult<Album> getAlbums(String user, String apiKey) {
121                return getAlbums(user, 1, 0, apiKey);
122        }
123
124        /**
125         * Retrieves a paginated list of all the albums in a user's library.
126         *
127         * @param user The user whose library you want to fetch.
128         * @param page The page number you wish to scan to.
129         * @param apiKey A Last.fm API key.
130         * @return a {@link PaginatedResult} of the albums
131         */
132        public static PaginatedResult<Album> getAlbums(String user, int page, String apiKey) {
133                return getAlbums(user, page, 0, apiKey);
134        }
135
136        /**
137         * Retrieves a paginated list of all the albums in a user's library.
138         *
139         * @param user The user whose library you want to fetch.
140         * @param page The page number you wish to scan to.
141         * @param limit Limit the number of albums returned (maximum/default is 50).
142         * @param apiKey A Last.fm API key.
143         * @return a {@link PaginatedResult} of the albums
144         */
145        public static PaginatedResult<Album> getAlbums(String user, int page, int limit, String apiKey) {
146                Map<String, String> params = new HashMap<String, String>();
147                params.put("user", user);
148                params.put("page", String.valueOf(page));
149                if (limit > 0)
150                        params.put("limit", String.valueOf(limit));
151                Result result = Caller.getInstance().call("library.getAlbums", apiKey, params);
152                return ResponseBuilder.buildPaginatedResult(result, Album.class);
153        }
154
155        /**
156         * Retrieves all albums in a user's library. Pay attention if you use this method as it may produce
157         * a lot of network traffic and therefore may consume a long time.
158         *
159         * @param user The user whose library you want to fetch.
160         * @param apiKey A Last.fm API key.
161         * @return all albums in a user's library
162         */
163        public static Collection<Album> getAllAlbums(String user, String apiKey) {
164                Collection<Album> albums = null;
165                int page = 1, total;
166                do {
167                        PaginatedResult<Album> result = getAlbums(user, page, apiKey);
168                        total = result.getTotalPages();
169                        Collection<Album> pageResults = result.getPageResults();
170                        if (albums == null) {
171                                // albums is initialized here to initialize it with the right size and avoid array copying later on
172                                albums = new ArrayList<Album>(total * pageResults.size());
173                        }
174                        albums.addAll(pageResults);
175                        page++;
176                } while (page <= total);
177                return albums;
178        }
179
180
181        /**
182         * Retrieves a paginated list of all the tracks in a user's library.
183         *
184         * @param user The user whose library you want to fetch.
185         * @param apiKey A Last.fm API key.
186         * @return a {@link PaginatedResult} of the tracks
187         */
188        public static PaginatedResult<Track> getTracks(String user, String apiKey) {
189                return getTracks(user, 1, 0, apiKey);
190        }
191
192        /**
193         * Retrieves a paginated list of all the tracks in a user's library.
194         *
195         * @param user The user whose library you want to fetch.
196         * @param page The page number you wish to scan to.
197         * @param apiKey A Last.fm API key.
198         * @return a {@link PaginatedResult} of the tracks
199         */
200        public static PaginatedResult<Track> getTracks(String user, int page, String apiKey) {
201                return getTracks(user, page, 0, apiKey);
202        }
203
204        /**
205         * Retrieves a paginated list of all the tracks in a user's library.
206         *
207         * @param user The user whose library you want to fetch.
208         * @param page The page number you wish to scan to.
209         * @param limit Limit the number of albums returned (maximum/default is 50).
210         * @param apiKey A Last.fm API key.
211         * @return a {@link PaginatedResult} of the tracks
212         */
213        public static PaginatedResult<Track> getTracks(String user, int page, int limit, String apiKey) {
214                Map<String, String> params = new HashMap<String, String>();
215                params.put("user", user);
216                params.put("page", String.valueOf(page));
217                if (limit > 0)
218                        params.put("limit", String.valueOf(limit));
219                Result result = Caller.getInstance().call("library.getTracks", apiKey, params);
220                return ResponseBuilder.buildPaginatedResult(result, Track.class);
221        }
222
223        /**
224         * Retrieves all tracks in a user's library. Pay attention if you use this method as it may produce
225         * a lot of network traffic and therefore may consume a long time.
226         *
227         * @param user The user whose library you want to fetch.
228         * @param apiKey A Last.fm API key.
229         * @return all tracks in a user's library
230         */
231        public static Collection<Track> getAllTracks(String user, String apiKey) {
232                Collection<Track> tracks = null;
233                int page = 1, total;
234                do {
235                        PaginatedResult<Track> result = getTracks(user, page, apiKey);
236                        total = result.getTotalPages();
237                        Collection<Track> pageResults = result.getPageResults();
238                        if (tracks == null) {
239                                // tracks is initialized here to initialize it with the right size and avoid array copying later on
240                                tracks = new ArrayList<Track>(total * pageResults.size());
241                        }
242                        tracks.addAll(pageResults);
243                        page++;
244                } while (page <= total);
245                return tracks;
246        }
247
248        /**
249         * Add an artist to a user's Last.fm library
250         *
251         * @param artist The artist name you wish to add
252         * @param session A Session instance
253         * @return the result of the operation
254         */
255        public static Result addArtist(String artist, Session session) {
256                return Caller.getInstance().call("library.addArtist", session, "artist", artist);
257        }
258
259        /**
260         * Add an album to a user's Last.fm library
261         *
262         * @param artist The artist that composed the track
263         * @param album The album name you wish to add
264         * @param session A Session instance
265         * @return the result of the operation
266         */
267        public static Result addAlbum(String artist, String album, Session session) {
268                return Caller.getInstance().call("library.addAlbum", session, "artist", artist, "album", album);
269        }
270
271        /**
272         * Add a track to a user's Last.fm library
273         *
274         * @param artist The artist that composed the track
275         * @param track The track name you wish to add
276         * @param session A Session instance
277         * @return the result of the operation
278         */
279        public static Result addTrack(String artist, String track, Session session) {
280                return Caller.getInstance().call("library.addTrack", session, "artist", artist, "track", track);
281        }
282
283        /**
284         * Remove an artist from a user's Last.fm library
285         *
286         * @param artist The artist name you wish to remove
287         * @param session A Session instance
288         * @return the result of the operation
289         */
290        public static Result removeArtist(String artist, Session session) {
291                return Caller.getInstance().call("library.removeArtist", session, "artist", artist);
292        }
293
294        /**
295         * Remove an album from a user's Last.fm library
296         *
297         * @param artist The artist that composed the album
298         * @param album The name of the album you wish to remove
299         * @param session A Session instance
300         * @return the result of the operation
301         */
302        public static Result removeAlbum(String artist, String album, Session session) {
303                return Caller.getInstance().call("library.removeAlbum", session, "artist", artist, "album", album);
304        }
305
306        /**
307         * Remove a track from a user's Last.fm library
308         *
309         * @param artist The artist that composed the track
310         * @param track The name of the track that you wish to remove
311         * @param session A Session instance
312         * @return the result of the operation
313         */
314        public static Result removeTrack(String artist, String track, Session session) {
315                return Caller.getInstance().call("library.removeTrack", session, "artist", artist, "track", track);
316        }
317
318        /**
319         * Remove a scrobble from a user's Last.fm library
320         *
321         * @param artist The artist that composed the track
322         * @param track The name of the track
323         * @param timestamp The unix timestamp of the scrobble that you wish to remove
324         * @param session A Session instance
325         * @return the result of the operation
326         */
327        public static Result removeScrobble(String artist, String track, long timestamp, Session session) {
328                return Caller.getInstance().call("library.removeScrobble", session, "artist", artist, "track", track, "timestamp",
329                                String.valueOf(timestamp));
330        }
331}