Skip to content

Commit

Permalink
Merge pull request #1331 from jbatesy/flickr-fix
Browse files Browse the repository at this point in the history
Added method to determine best quality image for flickr ripper [#1324]
  • Loading branch information
cyian-1756 authored Jun 22, 2019
2 parents bcaaa4f + 81293b5 commit 55c1d54
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/main/java/com/rarchives/ripme/ripper/rippers/FlickrRipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ public Document getFirstPage() throws IOException {
@Override
public List<String> getURLsFromPage(Document doc) {
List<String> imageURLs = new ArrayList<>();

String apiKey = getAPIKey(doc);
int x = 1;
while (true) {
JSONObject jsonData = getJSON(String.valueOf(x), getAPIKey(doc));
JSONObject jsonData = getJSON(String.valueOf(x), apiKey);
if (jsonData.has("stat") && jsonData.getString("stat").equals("fail")) {
break;
} else {
Expand All @@ -220,18 +220,12 @@ public List<String> getURLsFromPage(Document doc) {
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"), apiKey));
} catch (MalformedURLException e) {
LOGGER.error("Flickr MalformedURLException: " + e.getMessage());
}

}
if (x >= totalPages) {
// The rips done
Expand All @@ -250,4 +244,26 @@ public List<String> getURLsFromPage(Document doc) {
public void downloadURL(URL url, int index) {
addURLToDownload(url, getPrefix(index));
}

private URL getLargestImageURL(String imageID, String apiKey) throws MalformedURLException {
TreeMap<Integer, String> imageURLMap = new TreeMap<>();

try {
URL imageAPIURL = new URL("https://www.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=" + apiKey + "&photo_id=" + imageID + "&format=json&nojsoncallback=1");
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 e) {
LOGGER.error("Error in parsing of Flickr API: " + e.getMessage());
} catch (MalformedURLException e) {
LOGGER.error("Malformed URL returned by API");
} catch (IOException e) {
LOGGER.error("IOException while looking at image sizes: " + e.getMessage());
}

return new URL(imageURLMap.lastEntry().getValue());
}
}

0 comments on commit 55c1d54

Please sign in to comment.