PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/foobnix-parent-utils/foobnix-lastfm/src/main/java/de/umass/lastfm/Geo.java

https://github.com/foobnix/foobnix-android
Java | 285 lines | 140 code | 36 blank | 109 comment | 6 complexity | 730a424910c6b4a0419f300cd3fdd8ce MD5 | raw file
  1. /*
  2. * Copyright (c) 2011, the Last.fm Java Project and Committers
  3. * All rights reserved.
  4. *
  5. * Redistribution and use of this software in source and binary forms, with or without modification, are
  6. * permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above
  9. * copyright notice, this list of conditions and the
  10. * following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the
  14. * following disclaimer in the documentation and/or other
  15. * materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  23. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package de.umass.lastfm;
  27. import java.util.*;
  28. import de.umass.util.MapUtilities;
  29. import de.umass.util.StringUtilities;
  30. import de.umass.xml.DomElement;
  31. /**
  32. * Provides nothing more than a namespace for the API methods starting with geo.
  33. *
  34. * @author Janni Kovacs
  35. */
  36. public class Geo {
  37. /**
  38. * This inner class represents a Metro, which is composed of its name and the name of its country.
  39. *
  40. * @see Geo#getMetros(String, String)
  41. */
  42. public static class Metro {
  43. private String name;
  44. private String country;
  45. public Metro(String name, String country) {
  46. this.name = name;
  47. this.country = country;
  48. }
  49. public String getName() {
  50. return name;
  51. }
  52. public String getCountry() {
  53. return country;
  54. }
  55. }
  56. private Geo() {
  57. }
  58. /**
  59. * Get all events in a specific location by country or city name.<br/> This method returns <em>all</em> events by subsequently calling
  60. * {@link #getEvents(String, String, int, String)} and concatenating the single results into one list.<br/> Pay attention if you use this
  61. * method as it may produce a lot of network traffic and therefore may consume a long time.
  62. *
  63. * @param location Specifies a location to retrieve events for
  64. * @param distance Find events within a specified distance
  65. * @param apiKey A Last.fm API key.
  66. * @return a list containing all events
  67. */
  68. public static Collection<Event> getAllEvents(String location, String distance, String apiKey) {
  69. Collection<Event> events = null;
  70. int page = 1, total;
  71. do {
  72. PaginatedResult<Event> result = getEvents(location, distance, page, apiKey);
  73. total = result.getTotalPages();
  74. Collection<Event> pageResults = result.getPageResults();
  75. if (events == null) {
  76. // events is initialized here to initialize it with the right size and avoid array copying later on
  77. events = new ArrayList<Event>(total * pageResults.size());
  78. }
  79. for (Event artist : pageResults) {
  80. events.add(artist);
  81. }
  82. page++;
  83. } while (page <= total);
  84. return events;
  85. }
  86. /**
  87. * Get all events in a specific location by country or city name.<br/> This method only returns the first page of a possibly paginated
  88. * result. To retrieve all pages get the total number of pages via {@link de.umass.lastfm.PaginatedResult#getTotalPages()} and subsequently
  89. * call {@link #getEvents(String, String, int, String)} with the successive page numbers.
  90. *
  91. * @param location Specifies a location to retrieve events for
  92. * @param distance Find events within a specified distance
  93. * @param apiKey A Last.fm API key.
  94. * @return a {@link PaginatedResult} containing a list of events
  95. */
  96. public static PaginatedResult<Event> getEvents(String location, String distance, String apiKey) {
  97. return getEvents(location, distance, 1, apiKey);
  98. }
  99. /**
  100. * Get all events in a specific location by country or city name.<br/> This method only returns the specified page of a paginated result.
  101. *
  102. * @param location Specifies a location to retrieve events for
  103. * @param distance Find events within a specified distance
  104. * @param page A page number for pagination
  105. * @param apiKey A Last.fm API key.
  106. * @return a {@link PaginatedResult} containing a list of events
  107. */
  108. public static PaginatedResult<Event> getEvents(String location, String distance, int page, String apiKey) {
  109. Map<String, String> params = new HashMap<String, String>();
  110. params.put("page", String.valueOf(page));
  111. MapUtilities.nullSafePut(params, "location", location);
  112. MapUtilities.nullSafePut(params, "distance", distance);
  113. Result result = Caller.getInstance().call("geo.getEvents", apiKey, params);
  114. return ResponseBuilder.buildPaginatedResult(result, Event.class);
  115. }
  116. /**
  117. * Get all events in a specific location by country or city name.<br/> This method only returns the specified page of a paginated result.
  118. *
  119. * @param latitude Latitude
  120. * @param longitude Longitude
  121. * @param page A page number for pagination
  122. * @param apiKey A Last.fm API key.
  123. * @return a {@link PaginatedResult} containing a list of events
  124. */
  125. public static PaginatedResult<Event> getEvents(double latitude, double longitude, int page, String apiKey) {
  126. Map<String, String> params = new HashMap<String, String>();
  127. params.put("page", String.valueOf(page));
  128. params.put("lat", String.valueOf(latitude));
  129. params.put("long", String.valueOf(longitude));
  130. Result result = Caller.getInstance().call("geo.getEvents", apiKey, params);
  131. return ResponseBuilder.buildPaginatedResult(result, Event.class);
  132. }
  133. /**
  134. * Get the most popular artists on Last.fm by country
  135. *
  136. * @param country A country name, as defined by the ISO 3166-1 country names standard
  137. * @param apiKey A Last.fm API key.
  138. * @return list of Artists
  139. */
  140. public static Collection<Artist> getTopArtists(String country, String apiKey) {
  141. Result result = Caller.getInstance().call("geo.getTopArtists", apiKey, "country", country);
  142. return ResponseBuilder.buildCollection(result, Artist.class);
  143. }
  144. /**
  145. * Get the most popular tracks on Last.fm by country
  146. *
  147. * @param country A country name, as defined by the ISO 3166-1 country names standard
  148. * @param apiKey A Last.fm API key.
  149. * @return a list of Tracks
  150. */
  151. public static Collection<Track> getTopTracks(String country, String apiKey) {
  152. Result result = Caller.getInstance().call("geo.getTopTracks", apiKey, "country", country);
  153. return ResponseBuilder.buildCollection(result, Track.class);
  154. }
  155. /**
  156. * Get a list of valid countries and {@link Metro}s for use in the other webservices.
  157. *
  158. * @param apiKey A Last.fm API key
  159. * @return a List of {@link Metro}s
  160. */
  161. public static Collection<Metro> getMetros(String apiKey) {
  162. return getMetros(null, apiKey);
  163. }
  164. /**
  165. * Get a list of valid countries and {@link Metro}s for use in the other webservices.
  166. *
  167. * @param country Optionally restrict the results to those Metros from a particular country, as defined by the ISO 3166-1 country names
  168. * standard
  169. * @param apiKey A Last.fm API key
  170. * @return a List of {@link Metro}s
  171. */
  172. public static Collection<Metro> getMetros(String country, String apiKey) {
  173. Map<String, String> params = new HashMap<String, String>();
  174. MapUtilities.nullSafePut(params, "country", country);
  175. Result result = Caller.getInstance().call("geo.getMetros", apiKey, params);
  176. if (!result.isSuccessful())
  177. return Collections.emptyList();
  178. Collection<DomElement> children = result.getContentElement().getChildren("metro");
  179. Collection<Metro> metros = new ArrayList<Metro>(children.size());
  180. for (DomElement child : children) {
  181. metros.add(new Metro(child.getChildText("name"), child.getChildText("country")));
  182. }
  183. return metros;
  184. }
  185. /**
  186. * Get a list of available chart periods for this metro, expressed as date ranges which can be sent to the chart services.
  187. *
  188. * @param metro The name of the metro, or <code>null</code>
  189. * @param apiKey A Last.fm API key
  190. * @return a list of available charts as a Map
  191. */
  192. public static LinkedHashMap<String, String> getMetroWeeklyChartList(String metro, String apiKey) {
  193. return Chart.getWeeklyChartList("geo.getMetroWeeklyChartList", "metro", metro, apiKey);
  194. }
  195. public static Chart<Artist> getMetroArtistChart(String country, String metro, String apiKey) {
  196. return getMetroArtistChart(country, metro, null, null, apiKey);
  197. }
  198. public static Chart<Artist> getMetroArtistChart(Metro metro, String start, String end, String apiKey) {
  199. return getMetroArtistChart(metro.getCountry(), metro.getName(), start, end, apiKey);
  200. }
  201. public static Chart<Artist> getMetroArtistChart(String country, String metro, String start, String end, String apiKey) {
  202. return Chart.getChart("geo.getMetroArtistChart", "artist", StringUtilities.map("country", country, "metro", metro), start, end, -1, apiKey);
  203. }
  204. public static Chart<Track> getMetroTrackChart(String country, String metro, String apiKey) {
  205. return getMetroTrackChart(country, metro, null, null, apiKey);
  206. }
  207. public static Chart<Track> getMetroTrackChart(Metro metro, String start, String end, String apiKey) {
  208. return getMetroTrackChart(metro.getCountry(), metro.getName(), start, end, apiKey);
  209. }
  210. public static Chart<Track> getMetroTrackChart(String country, String metro, String start, String end, String apiKey) {
  211. return Chart.getChart("geo.getMetroTrackChart", "track", StringUtilities.map("country", country, "metro", metro), start, end, -1, apiKey);
  212. }
  213. public static Chart<Artist> getMetroHypeArtistChart(String country, String metro, String apiKey) {
  214. return getMetroHypeArtistChart(country, metro, null, null, apiKey);
  215. }
  216. public static Chart<Artist> getMetroHypeArtistChart(Metro metro, String start, String end, String apiKey) {
  217. return getMetroHypeArtistChart(metro.getCountry(), metro.getName(), start, end, apiKey);
  218. }
  219. public static Chart<Artist> getMetroHypeArtistChart(String country, String metro, String start, String end, String apiKey) {
  220. return Chart.getChart("geo.getMetroHypeArtistChart", "artist", StringUtilities.map("country", country, "metro", metro), start, end, -1, apiKey);
  221. }
  222. public static Chart<Track> getMetroHypeTrackChart(String country, String metro, String apiKey) {
  223. return getMetroHypeTrackChart(country, metro, null, null, apiKey);
  224. }
  225. public static Chart<Track> getMetroHypeTrackChart(Metro metro, String start, String end, String apiKey) {
  226. return getMetroHypeTrackChart(metro.getCountry(), metro.getName(), start, end, apiKey);
  227. }
  228. public static Chart<Track> getMetroHypeTrackChart(String country, String metro, String start, String end, String apiKey) {
  229. return Chart.getChart("geo.getMetroHypeTrackChart", "track", StringUtilities.map("country", country, "metro", metro), start, end, -1, apiKey);
  230. }
  231. public static Chart<Artist> getMetroUniqueArtistChart(String country, String metro, String apiKey) {
  232. return getMetroUniqueArtistChart(country, metro, null, null, apiKey);
  233. }
  234. public static Chart<Artist> getMetroUniqueArtistChart(Metro metro, String start, String end, String apiKey) {
  235. return getMetroUniqueArtistChart(metro.getCountry(), metro.getName(), start, end, apiKey);
  236. }
  237. public static Chart<Artist> getMetroUniqueArtistChart(String country, String metro, String start, String end, String apiKey) {
  238. return Chart.getChart("geo.getMetroUniqueArtistChart", "artist", StringUtilities.map("country", country, "metro", metro), start, end, -1, apiKey);
  239. }
  240. public static Chart<Track> getMetroUniqueTrackChart(String country, String metro, String apiKey) {
  241. return getMetroUniqueTrackChart(country, metro, null, null, apiKey);
  242. }
  243. public static Chart<Track> getMetroUniqueTrackChart(Metro metro, String start, String end, String apiKey) {
  244. return getMetroUniqueTrackChart(metro.getCountry(), metro.getName(), start, end, apiKey);
  245. }
  246. public static Chart<Track> getMetroUniqueTrackChart(String country, String metro, String start, String end, String apiKey) {
  247. return Chart.getChart("geo.getMetroUniqueTrackChart", "track", StringUtilities.map("country", country, "metro", metro), start, end, -1, apiKey);
  248. }
  249. }