static Image imageFromElement(DomElement e) { Image i = new Image(); i.title = e.getChildText("title"); i.url = e.getChildText("url"); i.format = e.getChildText("format"); try { i.dateAdded = DATE_ADDED_FORMAT.parse(e.getChildText("dateadded")); } catch (ParseException e1) { e1.printStackTrace(); } DomElement owner = e.getChild("owner"); if (owner != null) i.owner = owner.getChildText("name"); DomElement votes = e.getChild("votes"); if (votes != null) { i.thumbsUp = Integer.parseInt(votes.getChildText("thumbsup")); i.thumbsDown = Integer.parseInt(votes.getChildText("thumbsdown")); } DomElement sizes = e.getChild("sizes"); for (DomElement image : sizes.getChildren("size")) { // code copied from ImageHolder.loadImages String attribute = image.getAttribute("name"); ImageSize size; if (attribute == null) size = ImageSize.MEDIUM; // workaround for image responses without size attr. else size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH)); i.imageUrls.put(size, image.getText()); } return i; }
/** * Get the tags applied by an individual user to an album on Last.fm. * * @param artist The artist name in question * @param album The album name in question * @param session A Session instance * @return a list of tags */ public static Collection<String> getTags(String artist, String album, Session session) { Result result = Caller.getInstance().call("album.getTags", session, "artist", artist, "album", album); if (!result.isSuccessful()) return Collections.emptyList(); DomElement element = result.getContentElement(); Collection<String> tags = new ArrayList<String>(); for (DomElement domElement : element.getChildren("tag")) { tags.add(domElement.getChildText("name")); } return tags; }
public static PaginatedResult<User> getMembers(String group, int page, String apiKey) { Result result = Caller.getInstance() .call("group.getMembers", apiKey, "group", group, "page", String.valueOf(page)); if (!result.isSuccessful()) return new PaginatedResult<User>(0, 0, Collections.<User>emptyList()); DomElement root = result.getContentElement(); Collection<DomElement> children = root.getChildren("user"); List<User> users = new ArrayList<User>(children.size()); for (DomElement child : children) { users.add(User.userFromElement(child)); } page = Integer.parseInt(root.getAttribute("page")); int total = Integer.parseInt(root.getAttribute("totalPages")); return new PaginatedResult<User>(page, total, users); }