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

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 162 lines · 88 code · 11 blank · 63 comment · 16 complexity · 7b8905842a896aec33116e6cfcf50ede 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. * Provides nothing more than a namespace for the API methods starting with geo.
  11. *
  12. * @author Janni Kovacs
  13. */
  14. public class Geo {
  15. private Geo() {
  16. }
  17. /**
  18. * Get all events in a specific location by country or city name.<br/>
  19. * This method returns <em>all</em> events by subsequently calling {@link #getEvents(String, String, int, String)}
  20. * and concatenating the single results into one list.<br/>
  21. * Pay attention if you use this method as it may produce a lot of network traffic and therefore
  22. * may consume a long time.
  23. *
  24. * @param location Specifies a location to retrieve events for
  25. * @param distance Find events within a specified distance
  26. * @param apiKey A Last.fm API key.
  27. * @return a list containing all events
  28. */
  29. public static Collection<Event> getAllEvents(String location, String distance, String apiKey) {
  30. Collection<Event> events = null;
  31. int page = 1, total;
  32. do {
  33. PaginatedResult<Event> result = getEvents(location, distance, page, apiKey);
  34. total = result.getTotalPages();
  35. Collection<Event> pageResults = result.getPageResults();
  36. if (events == null) {
  37. // events is initialized here to initialize it with the right size and avoid array copying later on
  38. events = new ArrayList<Event>(total * pageResults.size());
  39. }
  40. for (Event artist : pageResults) {
  41. events.add(artist);
  42. }
  43. page++;
  44. } while (page <= total);
  45. return events;
  46. }
  47. /**
  48. * Get all events in a specific location by country or city name.<br/>
  49. * This method only returns the first page of a possibly paginated result. To retrieve all pages
  50. * get the total number of pages via {@link net.roarsoftware.lastfm.PaginatedResult#getTotalPages()} and
  51. * subsequently call {@link #getEvents(String, String, int, String)} with the successive page numbers.
  52. *
  53. * @param location Specifies a location to retrieve events for
  54. * @param distance Find events within a specified distance
  55. * @param apiKey A Last.fm API key.
  56. * @return a {@link PaginatedResult} containing a list of events
  57. */
  58. public static PaginatedResult<Event> getEvents(String location, String distance, String apiKey) {
  59. return getEvents(location, distance, 1, apiKey);
  60. }
  61. /**
  62. * Get all events in a specific location by country or city name.<br/>
  63. * This method only returns the specified page of a paginated result.
  64. *
  65. * @param location Specifies a location to retrieve events for
  66. * @param distance Find events within a specified distance
  67. * @param page A page number for pagination
  68. * @param apiKey A Last.fm API key.
  69. * @return a {@link PaginatedResult} containing a list of events
  70. */
  71. public static PaginatedResult<Event> getEvents(String location, String distance, int page, String apiKey) {
  72. Map<String, String> params = new HashMap<String, String>();
  73. params.put("page", String.valueOf(page));
  74. if (location != null)
  75. params.put("location", location);
  76. if (distance != null)
  77. params.put("distance", distance);
  78. Result result = Caller.getInstance().call("geo.getEvents", apiKey, params);
  79. if (!result.isSuccessful())
  80. return new PaginatedResult<Event>(0, 0, Collections.<Event>emptyList());
  81. DomElement element = result.getContentElement();
  82. List<Event> events = new ArrayList<Event>();
  83. for (DomElement domElement : element.getChildren("event")) {
  84. events.add(Event.eventFromElement(domElement));
  85. }
  86. int currentPage = Integer.valueOf(element.getAttribute("page"));
  87. int totalPages = Integer.valueOf(element.getAttribute("totalpages"));
  88. return new PaginatedResult<Event>(page, totalPages, events);
  89. }
  90. /**
  91. * Get all events in a specific location by country or city name.<br/>
  92. * This method only returns the specified page of a paginated result.
  93. *
  94. * @param latitude Latitude
  95. * @param longitude Longitude
  96. * @param page A page number for pagination
  97. * @param apiKey A Last.fm API key.
  98. * @return a {@link PaginatedResult} containing a list of events
  99. */
  100. public static PaginatedResult<Event> getEvents(double latitude, double longitude, int page, String apiKey) {
  101. Map<String, String> params = new HashMap<String, String>();
  102. params.put("page", String.valueOf(page));
  103. params.put("lat", String.valueOf(latitude));
  104. params.put("long", String.valueOf(longitude));
  105. Result result = Caller.getInstance().call("geo.getEvents", apiKey, params);
  106. if (!result.isSuccessful())
  107. return new PaginatedResult<Event>(0, 0, Collections.<Event>emptyList());
  108. DomElement element = result.getContentElement();
  109. List<Event> events = new ArrayList<Event>();
  110. for (DomElement domElement : element.getChildren("event")) {
  111. events.add(Event.eventFromElement(domElement));
  112. }
  113. int currentPage = Integer.valueOf(element.getAttribute("page"));
  114. int totalPages = Integer.valueOf(element.getAttribute("totalpages"));
  115. return new PaginatedResult<Event>(page, totalPages, events);
  116. }
  117. /**
  118. * Get the most popular artists on Last.fm by country
  119. *
  120. * @param country A country name, as defined by the ISO 3166-1 country names standard
  121. * @param apiKey A Last.fm API key.
  122. * @return list of Artists
  123. */
  124. public static Collection<Artist> getTopArtists(String country, String apiKey) {
  125. Result result = Caller.getInstance().call("geo.getTopArtists", apiKey, "country", country);
  126. if (!result.isSuccessful())
  127. return Collections.emptyList();
  128. List<Artist> artists = new ArrayList<Artist>();
  129. for (DomElement domElement : result.getContentElement().getChildren("artist")) {
  130. artists.add(Artist.artistFromElement(domElement));
  131. }
  132. return artists;
  133. }
  134. /**
  135. * Get the most popular tracks on Last.fm by country
  136. *
  137. * @param country A country name, as defined by the ISO 3166-1 country names standard
  138. * @param apiKey A Last.fm API key.
  139. * @return a list of Tracks
  140. */
  141. public static Collection<Track> getTopTracks(String country, String apiKey) {
  142. Result result = Caller.getInstance().call("geo.getTopTracks", apiKey, "country", country);
  143. if (!result.isSuccessful())
  144. return Collections.emptyList();
  145. List<Track> tracks = new ArrayList<Track>();
  146. for (DomElement domElement : result.getContentElement().getChildren("track")) {
  147. tracks.add(Track.trackFromElement(domElement));
  148. }
  149. return tracks;
  150. }
  151. }