Do you use Flickr? 1TB is also free, so you want to upload all the images and videos here! There is also an API, so it can be used nicely from the program ... When I tried and searched for it, there are quite a few ways to get public images, but surprisingly there aren't many ways to get private images ... So, I will post it instead of a memorandum (* ・ ω ・) ノ
It won't start without this (> _ <) Please obtain from the following. https://www.flickr.com/services/apps/create/apply/
If you want to get a private image, you need an OAuth Token and an OAuth Token Secret. It will be necessary for uploading, so it will not hurt to get it (・ ∀ ・) Please refer to the following procedure to obtain it. Getting a Request Token
with this
Are ready! Now it's time to get the code running.
This time I will use the library. In case of gradle, add the following.
build.gradle
.....
dependencies {
compile('com.flickr4java:flickr4java:2.17')
}
.....
By generating an Auth class as shown below and setting it in RequestContext, you can access private files as well. Please read the $ {Api Key} part as appropriate for yourself.
Main.java
import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.REST;
import com.flickr4java.flickr.RequestContext;
import com.flickr4java.flickr.auth.Auth;
public class Main {
public static void main(String... args) throws FlickrException {
final Flickr flickr = new Flickr(
"${Api Key}",
"${Shared Secret}",
new REST()
);
Auth auth = new Auth();
auth.setToken("${OAuth Token}");
auth.setTokenSecret("${OAuth Token Secret}");
RequestContext requestContext = RequestContext.getRequestContext();
requestContext.setAuth(auth);
PhotosetsInterface photosetsInterface = flickr.getPhotosetsInterface();
}
}
You can get the public URL by formatting it referring to the following. https://www.flickr.com/services/api/misc.urls.html
By doing this, you can get all the small size (320px) images of API users regardless of whether they are public or private.
Main.java
import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.REST;
import com.flickr4java.flickr.RequestContext;
import com.flickr4java.flickr.auth.Auth;
import com.flickr4java.flickr.photosets.Photoset;
import com.flickr4java.flickr.photosets.Photosets;
import com.flickr4java.flickr.photosets.PhotosetsInterface;
public class Main {
private static final String IMAGE_URL = "https://farm%s.staticflickr.com/%s/%s_%s_%s.jpg ";
public static void main(String... args) throws FlickrException {
final Flickr flickr = new Flickr(
"${Api Key}",
"${Shared Secret}",
new REST()
);
Auth auth = new Auth();
auth.setToken("${OAuth Token}");
auth.setTokenSecret("${OAuth Token Secret}");
RequestContext requestContext = RequestContext.getRequestContext();
requestContext.setAuth(auth);
PhotosetsInterface photosetsInterface = flickr.getPhotosetsInterface();
Photosets photosets = photosetsInterface.getList(null);
photosets.getPhotosets().stream().map(Photoset::getId).flatMap(id -> {
try {
return photosetsInterface.getPhotos(id, 0, 0).stream();
} catch (FlickrException e) {
throw new RuntimeException();
}
}).forEach(photo -> System.out.println(
String.format(IMAGE_URL, photo.getFarm(), photo.getServer(), photo.getId(), photo.getSecret(), "n")
)
);
}
}
I was able to get the public URL of a private file with a relatively simple code! If it is a personal application, I think you can use this kind of processing ♪
Recommended Posts