Last.fm java API - how to get users from Paginated

2019-07-29 05:48发布

问题:

I am a but puzzled about the lastfm API, more exactly about the PaginatedResult

I read all users from a group in a PaginatedResult:

PaginatedResult<de.umass.lastfm.User> users = Group.getMembers("Classic Rock", key);

Then I tried two methods to display all the users. There should be about 25000, but I only get about 25. Only first page? How could I get all the results?

//        for (int i = 0; i < users.getTotalPages();i++){
            for (User thisuser: users.getPageResults()){
                //for each user
                System.out.print(thisuser.getName() + " - age: " + thisuser.getAge() + " - country: " + thisuser.getCountry() + " - "+ thisuser.getGender() + " - is: " + thisuser.getId() + " - playcount: " + thisuser.getPlaycount() + " - num playlists: " + thisuser.getNumPlaylists() + "\n");
            }
//        }

        for (User thisuser: users){
            //for each user
            System.out.print(thisuser.getName() + " - age: " + thisuser.getAge() + " - country: " + thisuser.getCountry() + " - "+ thisuser.getGender() + " - is: " + thisuser.getId() + " - playcount: " + thisuser.getPlaycount() + " - num playlists: " + thisuser.getNumPlaylists() + "\n");
        }

回答1:

The getMembers(String group, String apiKey) method you're using only gets the first page in the paginated result. You need to use the getMembers(String group, int page, String apiKey) overload and call it for each page you want to retrieve beyond the first page. Each result contains information about how many pages are available.

And don't forget the Last.FM API usage rules about how often you can make API calls - the 1000 calls needed to get the 25000 Classic Rockers would take 1½ hours to retrieve (5 seconds between requests).

From the documentation;

A PaginatedResult is returned by methods which result set might be so large that it needs to be paginated. Each PaginatedResult contains the total number of result pages, the current page and a Collection of entries for the current page.



标签: java last.fm