1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-10 16:04:19 +02:00

Added method to determine best quality image for download [#1324]

This commit is contained in:
Jesse Bate
2019-06-04 22:10:58 +10:00
parent b58b0181c9
commit 788e0b14e8

View File

@@ -220,18 +220,9 @@ public class FlickrRipper extends AbstractHTMLRipper {
for (int i = 0; i < pictures.length(); i++) {
LOGGER.info(i);
JSONObject data = (JSONObject) pictures.get(i);
// TODO this is a total hack, we should loop over all image sizes and pick the biggest one and not
// just assume
List<String> imageSizes = Arrays.asList("k", "h", "l", "n", "c", "z", "t");
for ( String imageSize : imageSizes) {
try {
addURLToDownload(new URL(data.getString("url_" + imageSize)));
LOGGER.info("Adding picture " + data.getString("url_" + imageSize));
break;
} catch (org.json.JSONException ignore) {
// TODO warn the user when we hit a Malformed url
} catch (MalformedURLException e) {}
}
try {
addURLToDownload(getLargestImageURL(data.getString("id"), getAPIKey(doc)));
} catch (IOException ignore) { }
}
if (x >= totalPages) {
// The rips done
@@ -250,4 +241,22 @@ public class FlickrRipper extends AbstractHTMLRipper {
public void downloadURL(URL url, int index) {
addURLToDownload(url, getPrefix(index));
}
private URL getLargestImageURL(String imageID, String apiKey) throws IOException {
URL imageAPIURL = new URL("https://www.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=" + apiKey + "&photo_id=" + imageID + "&format=json&nojsoncallback=1");
TreeMap<Integer, String> imageURLMap = new TreeMap<>();
try {
JSONArray imageSizes = new JSONObject(Http.url(imageAPIURL).ignoreContentType().get().text()).getJSONObject("sizes").getJSONArray("size");
for (int i = 0; i < imageSizes.length(); i++) {
JSONObject imageInfo = imageSizes.getJSONObject(i);
imageURLMap.put(imageInfo.getInt("width") * imageInfo.getInt("height"), imageInfo.getString("source"));
}
} catch (org.json.JSONException ignore) {
} catch (MalformedURLException e) {}
return new URL(imageURLMap.lastEntry().getValue());
}
}