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

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 364 lines · 266 code · 48 blank · 50 comment · 26 complexity · db5b5c33629238d1dd55ff04184a8779 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.LinkedHashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import net.roarsoftware.xml.DomElement;
  10. /**
  11. * Contains user information and provides bindings to the methods in the user. namespace.
  12. *
  13. * @author Janni Kovacs
  14. */
  15. public class User extends ImageHolder {
  16. private String name;
  17. private String url;
  18. private String language;
  19. private String country;
  20. private int age;
  21. private String gender;
  22. private boolean subscriber;
  23. private int numPlaylists;
  24. private int playcount;
  25. public User(String name, String url) {
  26. this.name = name;
  27. this.url = url;
  28. }
  29. public String getName() {
  30. return name;
  31. }
  32. public String getUrl() {
  33. return url;
  34. }
  35. public int getAge() {
  36. return age;
  37. }
  38. public String getCountry() {
  39. return country;
  40. }
  41. public String getGender() {
  42. return gender;
  43. }
  44. public String getLanguage() {
  45. return language;
  46. }
  47. public int getNumPlaylists() {
  48. return numPlaylists;
  49. }
  50. public int getPlaycount() {
  51. return playcount;
  52. }
  53. public boolean isSubscriber() {
  54. return subscriber;
  55. }
  56. public String getImageURL() {
  57. return getImageURL(ImageSize.MEDIUM);
  58. }
  59. public static Collection<User> getFriends(String user, String apiKey) {
  60. return getFriends(user, false, 100, apiKey);
  61. }
  62. public static Collection<User> getFriends(String user, boolean recenttracks, int limit, String apiKey) {
  63. Result result = Caller.getInstance().call("user.getFriends", apiKey, "user", user, "recenttracks",
  64. String.valueOf(recenttracks ? 1 : 0), "limit", String.valueOf(limit));
  65. if (!result.isSuccessful())
  66. return Collections.emptyList();
  67. DomElement element = result.getContentElement();
  68. List<User> friends = new ArrayList<User>();
  69. for (DomElement domElement : element.getChildren("user")) {
  70. friends.add(userFromElement(domElement));
  71. }
  72. return friends;
  73. }
  74. public static Collection<User> getNeighbours(String user, String apiKey) {
  75. return getFriends(user, false, 100, apiKey);
  76. }
  77. public static Collection<User> getNeighbours(String user, int limit, String apiKey) {
  78. Result result = Caller.getInstance()
  79. .call("user.getNeighbours", apiKey, "user", user, "limit", String.valueOf(limit));
  80. if (!result.isSuccessful())
  81. return Collections.emptyList();
  82. DomElement element = result.getContentElement();
  83. List<User> friends = new ArrayList<User>();
  84. for (DomElement domElement : element.getChildren("user")) {
  85. friends.add(userFromElement(domElement));
  86. }
  87. return friends;
  88. }
  89. public static Collection<Track> getRecentTracks(String user, String apiKey) {
  90. return getRecentTracks(user, 10, apiKey);
  91. }
  92. public static Collection<Track> getRecentTracks(String user, int limit, String apiKey) {
  93. Result result = Caller.getInstance()
  94. .call("user.getRecentTracks", apiKey, "user", user, "limit", String.valueOf(limit));
  95. if (!result.isSuccessful())
  96. return Collections.emptyList();
  97. DomElement element = result.getContentElement();
  98. List<Track> tracks = new ArrayList<Track>();
  99. for (DomElement e : element.getChildren("track")) {
  100. tracks.add(Track.trackFromElement(e));
  101. }
  102. return tracks;
  103. }
  104. public static Collection<Album> getTopAlbums(String user, String apiKey) {
  105. return getTopAlbums(user, Period.OVERALL, apiKey);
  106. }
  107. private static Collection<Album> getTopAlbums(String user, Period period, String apiKey) {
  108. Result result = Caller.getInstance()
  109. .call("user.getTopAlbums", apiKey, "user", user, "period", period.getString());
  110. if (!result.isSuccessful())
  111. return Collections.emptyList();
  112. DomElement element = result.getContentElement();
  113. List<Album> albums = new ArrayList<Album>();
  114. for (DomElement domElement : element.getChildren("album")) {
  115. albums.add(Album.albumFromElement(domElement));
  116. }
  117. return albums;
  118. }
  119. public static Collection<Artist> getTopArtists(String user, String apiKey) {
  120. return getTopArtists(user, Period.OVERALL, apiKey);
  121. }
  122. private static Collection<Artist> getTopArtists(String user, Period period, String apiKey) {
  123. Result result = Caller.getInstance()
  124. .call("user.getTopArtists", apiKey, "user", user, "period", period.getString());
  125. if (!result.isSuccessful())
  126. return Collections.emptyList();
  127. DomElement element = result.getContentElement();
  128. List<Artist> artists = new ArrayList<Artist>();
  129. for (DomElement domElement : element.getChildren("artist")) {
  130. artists.add(Artist.artistFromElement(domElement));
  131. }
  132. return artists;
  133. }
  134. public static Collection<Track> getTopTracks(String user, String apiKey) {
  135. return getTopTracks(user, Period.OVERALL, apiKey);
  136. }
  137. private static Collection<Track> getTopTracks(String user, Period period, String apiKey) {
  138. Result result = Caller.getInstance()
  139. .call("user.getTopTracks", apiKey, "user", user, "period", period.getString());
  140. if (!result.isSuccessful())
  141. return Collections.emptyList();
  142. DomElement element = result.getContentElement();
  143. List<Track> tracks = new ArrayList<Track>();
  144. for (DomElement domElement : element.getChildren("track")) {
  145. tracks.add(Track.trackFromElement(domElement));
  146. }
  147. return tracks;
  148. }
  149. public static Collection<String> getTopTags(String user, String apiKey) {
  150. return getTopTags(user, -1, apiKey);
  151. }
  152. private static Collection<String> getTopTags(String user, int limit, String apiKey) {
  153. Map<String, String> params = new HashMap<String, String>();
  154. params.put("user", user);
  155. if (limit != -1) {
  156. params.put("limit", String.valueOf(limit));
  157. }
  158. Result result = Caller.getInstance().call("user.getTopTags", apiKey, params);
  159. if (!result.isSuccessful())
  160. return Collections.emptyList();
  161. DomElement element = result.getContentElement();
  162. List<String> tags = new ArrayList<String>();
  163. for (DomElement domElement : element.getChildren("tag")) {
  164. tags.add(domElement.getChildText("name"));
  165. }
  166. return tags;
  167. }
  168. public static Chart<Album> getWeeklyAlbumChart(String user, String apiKey) {
  169. return getWeeklyAlbumChart(user, null, null, -1, apiKey);
  170. }
  171. public static Chart<Album> getWeeklyAlbumChart(String user, int limit, String apiKey) {
  172. return getWeeklyAlbumChart(user, null, null, limit, apiKey);
  173. }
  174. public static Chart<Album> getWeeklyAlbumChart(String user, String from, String to, int limit, String apiKey) {
  175. return Chart.getChart("user.getWeeklyAlbumChart", "user", user, "album", from, to, limit, apiKey);
  176. }
  177. public static Chart<Artist> getWeeklyArtistChart(String user, String apiKey) {
  178. return getWeeklyArtistChart(user, null, null, -1, apiKey);
  179. }
  180. public static Chart<Artist> getWeeklyArtistChart(String user, int limit, String apiKey) {
  181. return getWeeklyArtistChart(user, null, null, limit, apiKey);
  182. }
  183. public static Chart<Artist> getWeeklyArtistChart(String user, String from, String to, int limit, String apiKey) {
  184. return Chart.getChart("user.getWeeklyArtistChart", "user", user, "artist", from, to, limit, apiKey);
  185. }
  186. public static Chart<Track> getWeeklyTrackChart(String user, String apiKey) {
  187. return getWeeklyTrackChart(user, null, null, -1, apiKey);
  188. }
  189. public static Chart<Track> getWeeklyTrackChart(String user, int limit, String apiKey) {
  190. return getWeeklyTrackChart(user, null, null, limit, apiKey);
  191. }
  192. public static Chart<Track> getWeeklyTrackChart(String user, String from, String to, int limit, String apiKey) {
  193. return Chart.getChart("user.getWeeklyTrackChart", "user", user, "track", from, to, limit, apiKey);
  194. }
  195. public static LinkedHashMap<String, String> getWeeklyChartList(String user, String apiKey) {
  196. return Chart.getWeeklyChartList("user", user, apiKey);
  197. }
  198. public static Collection<Chart> getWeeklyChartListAsCharts(String user, String apiKey) {
  199. return Chart.getWeeklyChartListAsCharts("user", user, apiKey);
  200. }
  201. /**
  202. * GetS a list of upcoming events that this user is attending.
  203. *
  204. * @param user The user to fetch the events for.
  205. * @param apiKey A Last.fm API key.
  206. * @return a list of upcoming events
  207. */
  208. public static Collection<Event> getEvents(String user, String apiKey) {
  209. Result result = Caller.getInstance().call("user.getEvents", apiKey, "user", user);
  210. if (!result.isSuccessful())
  211. return Collections.emptyList();
  212. DomElement element = result.getContentElement();
  213. List<Event> events = new ArrayList<Event>();
  214. for (DomElement domElement : element.getChildren("event")) {
  215. events.add(Event.eventFromElement(domElement));
  216. }
  217. return events;
  218. }
  219. /**
  220. * Get the first page of a paginated result of all events a user has attended in the past.
  221. *
  222. * @param user The username to fetch the events for.
  223. * @param apiKey A Last.fm API key.
  224. * @return a list of past {@link Event}s
  225. */
  226. public static PaginatedResult<Event> getPastEvents(String user, String apiKey) {
  227. return getPastEvents(user, 1, 0, apiKey);
  228. }
  229. /**
  230. * Gets a paginated list of all events a user has attended in the past.
  231. *
  232. * @param user The username to fetch the events for.
  233. * @param page The page number to scan to.
  234. * @param limit The number of events to return per page.
  235. * @param apiKey A Last.fm API key.
  236. * @return a list of past {@link Event}s
  237. */
  238. public static PaginatedResult<Event> getPastEvents(String user, int page, int limit, String apiKey) {
  239. Result result = Caller.getInstance().call("user.getPastEvents", apiKey, "user", user, "page",
  240. String.valueOf(page), "limit", String.valueOf(limit));
  241. if (!result.isSuccessful())
  242. return new PaginatedResult<Event>(0, 0, Collections.<Event>emptyList());
  243. DomElement element = result.getContentElement();
  244. List<Event> events = new ArrayList<Event>();
  245. for (DomElement domElement : element.getChildren("event")) {
  246. events.add(Event.eventFromElement(domElement));
  247. }
  248. int currentPage = Integer.valueOf(element.getAttribute("page"));
  249. int totalPages = Integer.valueOf(element.getAttribute("totalPages"));
  250. return new PaginatedResult<Event>(currentPage, totalPages, events);
  251. }
  252. /**
  253. * Gets a list of a user's playlists on Last.fm. Note that this method only fetches metadata regarding the user's
  254. * playlists. If you want to retrieve the list of tracks in a playlist use
  255. * {@link Playlist#fetch(String, String) Playlist.fetch()}.
  256. *
  257. * @param user The last.fm username to fetch the playlists of.
  258. * @param apiKey A Last.fm API key.
  259. * @return a list of Playlists
  260. */
  261. public static Collection<Playlist> getPlaylists(String user, String apiKey) {
  262. Result result = Caller.getInstance().call("user.getPlaylists", apiKey, "user", user);
  263. if (!result.isSuccessful())
  264. return Collections.emptyList();
  265. Collection<Playlist> playlists = new ArrayList<Playlist>();
  266. for (DomElement element : result.getContentElement().getChildren("playlist")) {
  267. playlists.add(Playlist.playlistFromElement(element));
  268. }
  269. return playlists;
  270. }
  271. /**
  272. * Gets the last 50 tracks loved by a user.
  273. *
  274. * @param user The user name to fetch the loved tracks for.
  275. * @param apiKey A Last.fm API key.
  276. * @return the loved tracks
  277. */
  278. public static Collection<Track> getLovedTracks(String user, String apiKey) {
  279. Result result = Caller.getInstance().call("user.getLovedTracks", apiKey, "user", user);
  280. if (!result.isSuccessful())
  281. return Collections.emptyList();
  282. DomElement element = result.getContentElement();
  283. Collection<Track> tracks = new ArrayList<Track>();
  284. for (DomElement domElement : element.getChildren("track")) {
  285. tracks.add(Track.trackFromElement(domElement));
  286. }
  287. return tracks;
  288. }
  289. /**
  290. * Retrieves profile information about the current authenticated user.
  291. *
  292. * @param session A Session instance
  293. * @return User info
  294. */
  295. public static User getInfo(Session session) {
  296. Result result = Caller.getInstance().call("User.getInfo", session);
  297. if (!result.isSuccessful())
  298. return null;
  299. DomElement element = result.getContentElement();
  300. return userFromElement(element);
  301. }
  302. static User userFromElement(DomElement element) {
  303. User user = new User(element.getChildText("name"), element.getChildText("url"));
  304. ImageHolder.loadImages(user, element);
  305. if (element.hasChild("lang")) { // extended user information
  306. user.language = element.getChildText("lang");
  307. user.country = element.getChildText("country");
  308. user.age = Integer.parseInt(element.getChildText("age"));
  309. user.gender = element.getChildText("gender");
  310. user.subscriber = "1".equals(element.getChildText("subscriber"));
  311. user.playcount = Integer.parseInt(element.getChildText("playcount"));
  312. user.numPlaylists = Integer.parseInt(element.getChildText("playlists"));
  313. }
  314. return user;
  315. }
  316. }