import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.people.User;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.photos.PhotosInterface;
import com.aetrion.flickr.photos.SearchParameters;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Iterator;
/**
* @author Jesse Wilson
*/
public class Kwikr {
private static final String API_KEY = ""; // YOUR API KEY HERE
private static final int PHOTOS_PER_SEARCH = 3;
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.out.println("Usage: Kwikr ");
return;
}
Flickr flickr = new Flickr(API_KEY);
// get the user Id from the username
String username = args[0];
User user = flickr.getPeopleInterface().findByUsername(username);
String userId = user.getId();
System.out.println("User " + username + " has id \"" + userId + "\"");
File userDirectory = new File(username);
userDirectory.mkdirs();
// prepare our photo search
SearchParameters usernameSearch = new SearchParameters();
usernameSearch.setUserId(userId);
PhotosInterface photosInterface = flickr.getPhotosInterface();
// continousouly search and write out the results
int page = 1;
int totalWritten = 0;
while(true) {
// do a query
PhotoList result = photosInterface.search(usernameSearch, PHOTOS_PER_SEARCH, page);
// for all photos in the query, write out those photos
for(Iterator i = result.iterator(); i.hasNext(); ) {
Photo photo = (Photo)i.next();
String filename = "flickr_" + photo.getId() + ".png";
System.out.print(totalWritten + " \"" + filename + "\" downloading...");
BufferedImage original = photo.getOriginalImage();
// Save as PNG
System.out.print("done. Saving...");
File file = new File(userDirectory, filename);
ImageIO.write(original, "png", file);
System.out.print("done.\n");
totalWritten++;
}
// loop if we retrieved a full set of photos
if(result.size() == PHOTOS_PER_SEARCH) {
page++;
} else {
break;
}
}
}
}