PageRenderTime 40ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/joggplayer-1.1.4s/src/net/roarsoftware/lastfm/Library.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 257 lines | 132 code | 20 blank | 105 comment | 18 complexity | bd9356652bd1ab5d56c9109a337bd24a MD5 | raw file
  1. package net.roarsoftware.lastfm;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import net.roarsoftware.xml.DomElement;
  9. /**
  10. * Contains bindings for all methods in the "library" namespace.
  11. *
  12. * @author Martin Chorley
  13. * @author Janni Kovacs
  14. */
  15. public class Library {
  16. private Library() {
  17. }
  18. /**
  19. * Retrieves a paginated list of all the artists in a user's library.
  20. *
  21. * @param user The user whose library you want to fetch.
  22. * @param apiKey A Last.fm API key.
  23. * @return a {@link PaginatedResult} of the artists
  24. */
  25. public static PaginatedResult<Artist> getArtists(String user, String apiKey) {
  26. return getArtists(user, 1, 0, apiKey);
  27. }
  28. /**
  29. * Retrieves a paginated list of all the artists in a user's library.
  30. *
  31. * @param user The user whose library you want to fetch.
  32. * @param page The page number you wish to scan to.
  33. * @param apiKey A Last.fm API key.
  34. * @return a {@link PaginatedResult} of the artists
  35. */
  36. public static PaginatedResult<Artist> getArtists(String user, int page, String apiKey) {
  37. return getArtists(user, page, 0, apiKey);
  38. }
  39. /**
  40. * Retrieves a paginated list of all the artists in a user's library.
  41. *
  42. * @param user The user whose library you want to fetch.
  43. * @param page The page number you wish to scan to.
  44. * @param limit Limit the amount of artists returned (maximum/default is 50).
  45. * @param apiKey A Last.fm API key.
  46. * @return a {@link PaginatedResult} of the artists
  47. */
  48. public static PaginatedResult<Artist> getArtists(String user, int page, int limit, String apiKey) {
  49. Map<String, String> params = new HashMap<String, String>();
  50. params.put("user", user);
  51. params.put("page", String.valueOf(page));
  52. params.put("limit", String.valueOf(limit));
  53. Result result = Caller.getInstance().call("library.getArtists", apiKey, params);
  54. if (!result.isSuccessful())
  55. return new PaginatedResult<Artist>(0, 0, Collections.<Artist>emptyList());
  56. DomElement element = result.getContentElement();
  57. List<Artist> artists = new ArrayList<Artist>();
  58. for (DomElement domElement : element.getChildren("artist")) {
  59. artists.add(Artist.artistFromElement(domElement));
  60. }
  61. int currentPage = Integer.valueOf(element.getAttribute("page"));
  62. int totalPages = Integer.valueOf(element.getAttribute("totalPages"));
  63. return new PaginatedResult<Artist>(currentPage, totalPages, artists);
  64. }
  65. /**
  66. * Retrieves all artists in a user's library. Pay attention if you use this method as it may produce
  67. * a lot of network traffic and therefore may consume a long time.
  68. *
  69. * @param user The user whose library you want to fetch.
  70. * @param apiKey A Last.fm API key.
  71. * @return all artists in a user's library
  72. */
  73. public static Collection<Artist> getAllArtists(String user, String apiKey) {
  74. Collection<Artist> artists = null;
  75. int page = 1, total;
  76. do {
  77. PaginatedResult<Artist> result = getArtists(user, page, apiKey);
  78. total = result.getTotalPages();
  79. Collection<Artist> pageResults = result.getPageResults();
  80. if (artists == null) {
  81. // artists is initialized here to initialize it with the right size and avoid array copying later on
  82. artists = new ArrayList<Artist>(total * pageResults.size());
  83. }
  84. for (Artist artist : pageResults) {
  85. artists.add(artist);
  86. }
  87. page++;
  88. } while (page <= total);
  89. return artists;
  90. }
  91. /**
  92. * Retrieves a paginated list of all the albums in a user's library.
  93. *
  94. * @param user The user whose library you want to fetch.
  95. * @param apiKey A Last.fm API key.
  96. * @return a {@link PaginatedResult} of the albums
  97. */
  98. public static PaginatedResult<Album> getAlbums(String user, String apiKey) {
  99. return getAlbums(user, 1, 0, apiKey);
  100. }
  101. /**
  102. * Retrieves a paginated list of all the albums in a user's library.
  103. *
  104. * @param user The user whose library you want to fetch.
  105. * @param page The page number you wish to scan to.
  106. * @param apiKey A Last.fm API key.
  107. * @return a {@link PaginatedResult} of the albums
  108. */
  109. public static PaginatedResult<Album> getAlbums(String user, int page, String apiKey) {
  110. return getAlbums(user, page, 0, apiKey);
  111. }
  112. /**
  113. * Retrieves a paginated list of all the albums in a user's library.
  114. *
  115. * @param user The user whose library you want to fetch.
  116. * @param page The page number you wish to scan to.
  117. * @param limit Limit the amount of albumss returned (maximum/default is 50).
  118. * @param apiKey A Last.fm API key.
  119. * @return a {@link PaginatedResult} of the albums
  120. */
  121. public static PaginatedResult<Album> getAlbums(String user, int page, int limit, String apiKey) {
  122. Map<String, String> params = new HashMap<String, String>();
  123. params.put("user", user);
  124. params.put("page", String.valueOf(page));
  125. params.put("limit", String.valueOf(limit));
  126. Result result = Caller.getInstance().call("library.getAlbums", apiKey, params);
  127. if (!result.isSuccessful())
  128. return new PaginatedResult<Album>(0, 0, Collections.<Album>emptyList());
  129. DomElement element = result.getContentElement();
  130. List<Album> artists = new ArrayList<Album>();
  131. for (DomElement domElement : element.getChildren("album")) {
  132. artists.add(Album.albumFromElement(domElement));
  133. }
  134. int currentPage = Integer.valueOf(element.getAttribute("page"));
  135. int totalPages = Integer.valueOf(element.getAttribute("totalPages"));
  136. return new PaginatedResult<Album>(currentPage, totalPages, artists);
  137. }
  138. /**
  139. * Retrieves all albums in a user's library. Pay attention if you use this method as it may produce
  140. * a lot of network traffic and therefore may consume a long time.
  141. *
  142. * @param user The user whose library you want to fetch.
  143. * @param apiKey A Last.fm API key.
  144. * @return all albums in a user's library
  145. */
  146. public static Collection<Album> getAllAlbums(String user, String apiKey) {
  147. Collection<Album> albums = null;
  148. int page = 1, total;
  149. do {
  150. PaginatedResult<Album> result = getAlbums(user, page, apiKey);
  151. total = result.getTotalPages();
  152. Collection<Album> pageResults = result.getPageResults();
  153. if (albums == null) {
  154. // albums is initialized here to initialize it with the right size and avoid array copying later on
  155. albums = new ArrayList<Album>(total * pageResults.size());
  156. }
  157. for (Album album : pageResults) {
  158. albums.add(album);
  159. }
  160. page++;
  161. } while (page <= total);
  162. return albums;
  163. }
  164. /**
  165. * Retrieves a paginated list of all the tracks in a user's library.
  166. *
  167. * @param user The user whose library you want to fetch.
  168. * @param apiKey A Last.fm API key.
  169. * @return a {@link PaginatedResult} of the tracks
  170. */
  171. public static PaginatedResult<Track> getTracks(String user, String apiKey) {
  172. return getTracks(user, 1, 0, apiKey);
  173. }
  174. /**
  175. * Retrieves a paginated list of all the tracks in a user's library.
  176. *
  177. * @param user The user whose library you want to fetch.
  178. * @param page The page number you wish to scan to.
  179. * @param apiKey A Last.fm API key.
  180. * @return a {@link PaginatedResult} of the tracks
  181. */
  182. public static PaginatedResult<Track> getTracks(String user, int page, String apiKey) {
  183. return getTracks(user, page, 0, apiKey);
  184. }
  185. /**
  186. * Retrieves a paginated list of all the tracks in a user's library.
  187. *
  188. * @param user The user whose library you want to fetch.
  189. * @param page The page number you wish to scan to.
  190. * @param limit Limit the amount of albumss returned (maximum/default is 50).
  191. * @param apiKey A Last.fm API key.
  192. * @return a {@link PaginatedResult} of the tracks
  193. */
  194. public static PaginatedResult<Track> getTracks(String user, int page, int limit, String apiKey) {
  195. Map<String, String> params = new HashMap<String, String>();
  196. params.put("user", user);
  197. params.put("page", String.valueOf(page));
  198. params.put("limit", String.valueOf(limit));
  199. Result result = Caller.getInstance().call("library.getTracks", apiKey, params);
  200. if (!result.isSuccessful())
  201. return new PaginatedResult<Track>(0, 0, Collections.<Track>emptyList());
  202. DomElement element = result.getContentElement();
  203. List<Track> tracks = new ArrayList<Track>();
  204. for (DomElement domElement : element.getChildren("track")) {
  205. tracks.add(Track.trackFromElement(domElement));
  206. }
  207. int currentPage = Integer.valueOf(element.getAttribute("page"));
  208. int totalPages = Integer.valueOf(element.getAttribute("totalPages"));
  209. return new PaginatedResult<Track>(currentPage, totalPages, tracks);
  210. }
  211. /**
  212. * Retrieves all tracks in a user's library. Pay attention if you use this method as it may produce
  213. * a lot of network traffic and therefore may consume a long time.
  214. *
  215. * @param user The user whose library you want to fetch.
  216. * @param apiKey A Last.fm API key.
  217. * @return all tracks in a user's library
  218. */
  219. public static Collection<Track> getAllTracks(String user, String apiKey) {
  220. Collection<Track> tracks = null;
  221. int page = 1, total;
  222. do {
  223. PaginatedResult<Track> result = getTracks(user, page, apiKey);
  224. total = result.getTotalPages();
  225. Collection<Track> pageResults = result.getPageResults();
  226. if (tracks == null) {
  227. // tracks is initialized here to initialize it with the right size and avoid array copying later on
  228. tracks = new ArrayList<Track>(total * pageResults.size());
  229. }
  230. for (Track track : pageResults) {
  231. tracks.add(track);
  232. }
  233. page++;
  234. } while (page <= total);
  235. return tracks;
  236. }
  237. }