OSDN Git Service

Apollo: update Last.fm api
authorKyrylo Mikos <kiril.mik.os@gmail.com>
Sun, 10 Nov 2013 17:37:41 +0000 (19:37 +0200)
committerKyrylo Mikos <kiril.mik.os@gmail.com>
Wed, 13 Nov 2013 13:13:49 +0000 (15:13 +0200)
Update ImageSize class.

Artist.getImages is deprecated now. Remove it
and use Artist.getInfo to load images for Artist.

Use default album size to EXTRALARGE.
Add a function to check album size and fallback to smaller
one if does not exists,

Change-Id: I40e3d5c60397a620057619dca1a7ea3e5839c7de

src/com/andrew/apollo/cache/ImageFetcher.java
src/com/andrew/apollo/lastfm/Artist.java
src/com/andrew/apollo/lastfm/Image.java
src/com/andrew/apollo/lastfm/ImageHolder.java
src/com/andrew/apollo/lastfm/ImageSize.java

index e86234d..0c41482 100644 (file)
@@ -21,10 +21,8 @@ import com.andrew.apollo.Config;
 import com.andrew.apollo.MusicPlaybackService;
 import com.andrew.apollo.lastfm.Album;
 import com.andrew.apollo.lastfm.Artist;
-import com.andrew.apollo.lastfm.Image;
+import com.andrew.apollo.lastfm.MusicEntry;
 import com.andrew.apollo.lastfm.ImageSize;
-import com.andrew.apollo.lastfm.PaginatedResult;
-import com.andrew.apollo.utils.ApolloUtils;
 import com.andrew.apollo.utils.MusicUtils;
 import com.andrew.apollo.utils.PreferenceUtils;
 
@@ -36,7 +34,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
-import java.util.Iterator;
 
 /**
  * A subclass of {@link ImageWorker} that fetches images from a URL.
@@ -95,6 +92,18 @@ public class ImageFetcher extends ImageWorker {
         return null;
     }
 
+    private static String getBestImage(MusicEntry e) {
+        final ImageSize[] QUALITY = {ImageSize.EXTRALARGE, ImageSize.LARGE, ImageSize.MEDIUM,
+                ImageSize.SMALL, ImageSize.UNKNOWN};
+        for(ImageSize q : QUALITY) {
+            String url = e.getImageURL(q);
+            if (url != null) {
+                return url;
+            }
+        }
+        return null;
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -105,17 +114,9 @@ public class ImageFetcher extends ImageWorker {
             case ARTIST:
                 if (!TextUtils.isEmpty(artistName)) {
                     if (PreferenceUtils.getInstance(mContext).downloadMissingArtistImages()) {
-                        final PaginatedResult<Image> paginatedResult = Artist.getImages(mContext,
-                                artistName);
-                        if (paginatedResult != null) {
-                            final Iterator<Image> iterator = paginatedResult.pageResults.iterator();
-                            while (iterator.hasNext()) {
-                                final Image temp = iterator.next();
-                                final String url = temp.getImageURL(ImageSize.EXTRALARGE);
-                                if (url != null) {
-                                    return url;
-                                }
-                            }
+                        final Artist artist = Artist.getInfo(mContext, artistName);
+                        if (artist != null) {
+                            return getBestImage(artist);
                         }
                     }
                 }
@@ -128,10 +129,7 @@ public class ImageFetcher extends ImageWorker {
                             final Album album = Album.getInfo(mContext, correction.getName(),
                                     albumName);
                             if (album != null) {
-                                final String url = album.getImageURL(ImageSize.LARGE);
-                                if (url != null) {
-                                    return url;
-                                }
+                                return getBestImage(album);
                             }
                         }
                     }
index 6644db1..9f06705 100644 (file)
@@ -49,12 +49,10 @@ public class Artist extends MusicEntry {
      * Retrieves detailed artist info for the given artist or mbid entry.
      * 
      * @param artistOrMbid Name of the artist or an mbid
-     * @param apiKey The API key
      * @return detailed artist info
      */
-    public final static Artist getInfo(final Context context, final String artistOrMbid,
-            final String apiKey) {
-        return getInfo(context, artistOrMbid, Locale.getDefault(), apiKey);
+    public final static Artist getInfo(final Context context, final String artistOrMbid) {
+        return getInfo(context, artistOrMbid, Locale.getDefault(), Config.LASTFM_API_KEY);
     }
 
     /**
@@ -62,9 +60,6 @@ public class Artist extends MusicEntry {
      * 
      * @param artistOrMbid Name of the artist or an mbid
      * @param locale The language to fetch info in, or <code>null</code>
-     * @param username The username for the context of the request, or
-     *            <code>null</code>. If supplied, the user's playcount for this
-     *            artist is included in the response
      * @param apiKey The API key
      * @return detailed artist info
      */
@@ -107,41 +102,6 @@ public class Artist extends MusicEntry {
         }
     }
 
-    /**
-     * Get {@link Image}s for this artist in a variety of sizes.
-     * 
-     * @param artistOrMbid The artist name in question
-     * @return a list of {@link Image}s
-     */
-    public final static PaginatedResult<Image> getImages(final Context context,
-            final String artistOrMbid) {
-        return getImages(context, artistOrMbid, -1, -1, Config.LASTFM_API_KEY);
-    }
-
-    /**
-     * Get {@link Image}s for this artist in a variety of sizes.
-     * 
-     * @param artistOrMbid The artist name in question
-     * @param page Which page of limit amount to display
-     * @param limit How many to return. Defaults and maxes out at 50
-     * @param apiKey A Last.fm API key
-     * @return a list of {@link Image}s
-     */
-    public final static PaginatedResult<Image> getImages(final Context context,
-            final String artistOrMbid, final int page, final int limit, final String apiKey) {
-        final Map<String, String> params = new WeakHashMap<String, String>();
-        params.put("artist", artistOrMbid);
-        MapUtilities.nullSafePut(params, "page", page);
-        MapUtilities.nullSafePut(params, "limit", limit);
-        Result result = null;
-        try {
-            result = Caller.getInstance(context).call("artist.getImages", apiKey, params);
-        } catch (final Exception ignored) {
-            return null;
-        }
-        return ResponseBuilder.buildPaginatedResult(result, Image.class);
-    }
-
     private final static class ArtistFactory implements ItemFactory<Artist> {
 
         /**
index b54aa7b..ec09701 100644 (file)
@@ -50,24 +50,7 @@ public class Image extends ImageHolder {
         public Image createItemFromElement(final DomElement element) {
             final Image i = new Image();
             i.url = element.getChildText("url");
-            final DomElement sizes = element.getChild("sizes");
-            for (final DomElement image : sizes.getChildren("size")) {
-                // code copied from ImageHolder.loadImages
-                final String attribute = image.getAttribute("name");
-                ImageSize size = null;
-                if (attribute == null) {
-                    size = ImageSize.LARGESQUARE;
-                } else {
-                    try {
-                        size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH));
-                    } catch (final IllegalArgumentException e) {
-                        // if they suddenly again introduce a new image size
-                    }
-                }
-                if (size != null) {
-                    i.imageUrls.put(size, image.getText());
-                }
-            }
+            loadImages(i, element);
             return i;
         }
     }
index 5cb24b1..69c7c4d 100644 (file)
@@ -67,7 +67,7 @@ public abstract class ImageHolder {
             final String attribute = image.getAttribute("size");
             ImageSize size = null;
             if (attribute == null) {
-                size = ImageSize.LARGESQUARE;
+                size = ImageSize.UNKNOWN;
             } else {
                 try {
                     size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH));
index 927d75d..f568feb 100644 (file)
@@ -26,6 +26,6 @@ package com.andrew.apollo.lastfm;
  */
 public enum ImageSize {
 
-    LARGE, LARGESQUARE, ORIGINAL, EXTRALARGE
+    SMALL, MEDIUM, LARGE, EXTRALARGE, MEGA, UNKNOWN
 
 }