2014-04-24 07:17:41 +02:00
|
|
|
package com.rarchives.ripme.ripper.rippers;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2014-06-23 04:12:29 +02:00
|
|
|
import java.util.ArrayList;
|
2014-04-25 05:49:18 +02:00
|
|
|
import java.util.HashMap;
|
2014-04-24 07:17:41 +02:00
|
|
|
import java.util.HashSet;
|
2014-06-23 04:12:29 +02:00
|
|
|
import java.util.List;
|
2014-04-25 05:49:18 +02:00
|
|
|
import java.util.Map;
|
2014-04-24 07:17:41 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2014-04-25 05:49:18 +02:00
|
|
|
import org.jsoup.Connection.Method;
|
|
|
|
import org.jsoup.Connection.Response;
|
2014-04-24 07:17:41 +02:00
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
import com.rarchives.ripme.ripper.AbstractHTMLRipper;
|
2014-04-24 07:17:41 +02:00
|
|
|
import com.rarchives.ripme.ripper.DownloadThreadPool;
|
2014-04-25 05:49:18 +02:00
|
|
|
import com.rarchives.ripme.utils.Base64;
|
2014-06-22 02:08:42 +02:00
|
|
|
import com.rarchives.ripme.utils.Http;
|
2014-04-24 07:17:41 +02:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
public class FlickrRipper extends AbstractHTMLRipper {
|
2014-04-24 07:17:41 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
private int page = 1;
|
2017-10-24 16:33:28 +02:00
|
|
|
private Set<String> attempted = new HashSet<>();
|
2014-04-24 07:17:41 +02:00
|
|
|
private Document albumDoc = null;
|
2017-10-24 16:33:28 +02:00
|
|
|
private final DownloadThreadPool flickrThreadPool;
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public DownloadThreadPool getThreadPool() {
|
|
|
|
return flickrThreadPool;
|
|
|
|
}
|
2014-04-24 07:17:41 +02:00
|
|
|
|
|
|
|
public FlickrRipper(URL url) throws IOException {
|
|
|
|
super(url);
|
|
|
|
flickrThreadPool = new DownloadThreadPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHost() {
|
2014-06-23 04:12:29 +02:00
|
|
|
return "flickr";
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String getDomain() {
|
|
|
|
return "flickr.com";
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
2014-05-14 08:16:37 +02:00
|
|
|
String sUrl = url.toExternalForm();
|
2014-05-16 07:47:17 +02:00
|
|
|
// Strip out https
|
|
|
|
sUrl = sUrl.replace("https://secure.flickr.com", "http://www.flickr.com");
|
|
|
|
// For /groups/ links, add a /pool to the end of the URL
|
2014-05-14 08:16:37 +02:00
|
|
|
if (sUrl.contains("flickr.com/groups/") && !sUrl.contains("/pool")) {
|
|
|
|
if (!sUrl.endsWith("/")) {
|
|
|
|
sUrl += "/";
|
|
|
|
}
|
|
|
|
sUrl += "pool";
|
|
|
|
}
|
|
|
|
return new URL(sUrl);
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
2017-06-19 19:32:57 +02:00
|
|
|
|
2014-04-24 07:17:41 +02:00
|
|
|
public String getAlbumTitle(URL url) throws MalformedURLException {
|
|
|
|
if (!url.toExternalForm().contains("/sets/")) {
|
|
|
|
return super.getAlbumTitle(url);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// Attempt to use album title as GID
|
2014-06-23 04:12:29 +02:00
|
|
|
Document doc = getFirstPage();
|
2014-04-24 07:17:41 +02:00
|
|
|
String user = url.toExternalForm();
|
|
|
|
user = user.substring(user.indexOf("/photos/") + "/photos/".length());
|
|
|
|
user = user.substring(0, user.indexOf("/"));
|
2014-06-23 04:12:29 +02:00
|
|
|
String title = doc.select("meta[name=description]").get(0).attr("content");
|
2014-04-24 07:17:41 +02:00
|
|
|
if (!title.equals("")) {
|
2014-06-23 04:12:29 +02:00
|
|
|
return getHost() + "_" + user + "_" + title;
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Fall back to default album naming convention
|
|
|
|
}
|
|
|
|
return super.getAlbumTitle(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getGID(URL url) throws MalformedURLException {
|
|
|
|
Pattern p; Matcher m;
|
|
|
|
|
|
|
|
// Root: https://www.flickr.com/photos/115858035@N04/
|
|
|
|
// Album: https://www.flickr.com/photos/115858035@N04/sets/72157644042355643/
|
2017-06-19 19:32:57 +02:00
|
|
|
|
2014-05-14 08:16:37 +02:00
|
|
|
final String domainRegex = "https?://[wm.]*flickr.com";
|
2014-04-24 07:17:41 +02:00
|
|
|
final String userRegex = "[a-zA-Z0-9@]+";
|
|
|
|
// Album
|
2014-05-14 08:16:37 +02:00
|
|
|
p = Pattern.compile("^" + domainRegex + "/photos/(" + userRegex + ")/sets/([0-9]+)/?.*$");
|
2014-04-24 07:17:41 +02:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1) + "_" + m.group(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// User page
|
2014-05-14 08:16:37 +02:00
|
|
|
p = Pattern.compile("^" + domainRegex + "/photos/(" + userRegex + ").*$");
|
2014-04-24 07:17:41 +02:00
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return m.group(1);
|
|
|
|
}
|
|
|
|
|
2014-05-14 08:16:37 +02:00
|
|
|
// Groups page
|
|
|
|
p = Pattern.compile("^" + domainRegex + "/groups/(" + userRegex + ").*$");
|
|
|
|
m = p.matcher(url.toExternalForm());
|
|
|
|
if (m.matches()) {
|
|
|
|
return "groups-" + m.group(1);
|
|
|
|
}
|
2014-04-24 07:17:41 +02:00
|
|
|
throw new MalformedURLException(
|
|
|
|
"Expected flickr.com URL formats: "
|
|
|
|
+ "flickr.com/photos/username or "
|
|
|
|
+ "flickr.com/photos/username/sets/albumid"
|
|
|
|
+ " Got: " + url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-23 04:12:29 +02:00
|
|
|
public Document getFirstPage() throws IOException {
|
|
|
|
if (albumDoc == null) {
|
|
|
|
albumDoc = Http.url(url).get();
|
|
|
|
}
|
|
|
|
return albumDoc;
|
|
|
|
}
|
2014-04-25 05:49:18 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public Document getNextPage(Document doc) throws IOException {
|
2015-02-10 08:29:29 +01:00
|
|
|
if (isThisATest()) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
// Find how many pages there are
|
|
|
|
int lastPage = 0;
|
|
|
|
for (Element apage : doc.select("a[data-track^=page-]")) {
|
|
|
|
String lastPageStr = apage.attr("data-track").replace("page-", "");
|
|
|
|
lastPage = Integer.parseInt(lastPageStr);
|
|
|
|
}
|
|
|
|
// If we're at the last page, stop.
|
|
|
|
if (page >= lastPage) {
|
|
|
|
throw new IOException("No more pages");
|
|
|
|
}
|
|
|
|
// Load the next page
|
|
|
|
page++;
|
|
|
|
albumDoc = null;
|
|
|
|
String nextURL = this.url.toExternalForm();
|
|
|
|
if (!nextURL.endsWith("/")) {
|
|
|
|
nextURL += "/";
|
|
|
|
}
|
|
|
|
nextURL += "page" + page + "/";
|
|
|
|
// Wait a bit
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new IOException("Interrupted while waiting to load next page " + nextURL);
|
|
|
|
}
|
|
|
|
return Http.url(nextURL).get();
|
|
|
|
}
|
2017-06-19 19:32:57 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public List<String> getURLsFromPage(Document page) {
|
2017-10-24 16:33:28 +02:00
|
|
|
List<String> imageURLs = new ArrayList<>();
|
2014-06-23 04:12:29 +02:00
|
|
|
for (Element thumb : page.select("a[data-track=photo-click]")) {
|
|
|
|
/* TODO find a way to persist the image title
|
|
|
|
String imageTitle = null;
|
|
|
|
if (thumb.hasAttr("title")) {
|
|
|
|
imageTitle = thumb.attr("title");
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
*/
|
|
|
|
String imagePage = thumb.attr("href");
|
|
|
|
if (imagePage.startsWith("/")) {
|
|
|
|
imagePage = "http://www.flickr.com" + imagePage;
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
if (imagePage.contains("/in/")) {
|
|
|
|
imagePage = imagePage.substring(0, imagePage.indexOf("/in/") + 1);
|
2014-04-25 05:49:18 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
if (!imagePage.endsWith("/")) {
|
|
|
|
imagePage += "/";
|
2014-04-25 05:49:18 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
imagePage += "sizes/o/";
|
|
|
|
|
|
|
|
// Check for duplicates
|
|
|
|
if (attempted.contains(imagePage)) {
|
|
|
|
continue;
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
attempted.add(imagePage);
|
|
|
|
imageURLs.add(imagePage);
|
2015-02-10 08:29:29 +01:00
|
|
|
if (isThisATest()) {
|
|
|
|
break;
|
|
|
|
}
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
2014-06-23 04:12:29 +02:00
|
|
|
return imageURLs;
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
2017-06-19 19:32:57 +02:00
|
|
|
|
2014-06-23 04:12:29 +02:00
|
|
|
@Override
|
|
|
|
public void downloadURL(URL url, int index) {
|
|
|
|
// Add image page to threadpool to grab the image & download it
|
|
|
|
FlickrImageThread mit = new FlickrImageThread(url, index);
|
|
|
|
flickrThreadPool.addThread(mit);
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
|
|
|
|
2014-04-25 05:49:18 +02:00
|
|
|
/**
|
|
|
|
* Login to Flickr.
|
|
|
|
* @return Cookies for logged-in session
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private Map<String,String> signinToFlickr() throws IOException {
|
|
|
|
Response resp = Jsoup.connect("http://www.flickr.com/signin/")
|
|
|
|
.userAgent(USER_AGENT)
|
|
|
|
.followRedirects(true)
|
|
|
|
.method(Method.GET)
|
|
|
|
.execute();
|
|
|
|
Document doc = resp.parse();
|
2017-10-24 16:33:28 +02:00
|
|
|
Map<String,String> postData = new HashMap<>();
|
2014-04-25 05:49:18 +02:00
|
|
|
for (Element input : doc.select("input[type=hidden]")) {
|
|
|
|
postData.put(input.attr("name"), input.attr("value"));
|
|
|
|
}
|
|
|
|
postData.put("passwd_raw", "");
|
|
|
|
postData.put(".save", "");
|
|
|
|
postData.put("login", new String(Base64.decode("bGVmYWtlZGVmYWtl")));
|
|
|
|
postData.put("passwd", new String(Base64.decode("MUZha2V5ZmFrZQ==")));
|
|
|
|
String action = doc.select("form[method=post]").get(0).attr("action");
|
|
|
|
resp = Jsoup.connect(action)
|
2014-06-20 13:09:36 +02:00
|
|
|
.cookies(resp.cookies())
|
|
|
|
.data(postData)
|
|
|
|
.method(Method.POST)
|
|
|
|
.execute();
|
2014-04-25 05:49:18 +02:00
|
|
|
return resp.cookies();
|
|
|
|
}
|
|
|
|
|
2014-04-24 07:17:41 +02:00
|
|
|
/**
|
|
|
|
* Helper class to find and download images found on "image" pages
|
|
|
|
*/
|
|
|
|
private class FlickrImageThread extends Thread {
|
|
|
|
private URL url;
|
|
|
|
private int index;
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
FlickrImageThread(URL url, int index) {
|
2014-04-24 07:17:41 +02:00
|
|
|
super();
|
|
|
|
this.url = url;
|
|
|
|
this.index = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Document doc = getLargestImagePageDocument(this.url);
|
|
|
|
Elements fullsizeImages = doc.select("div#allsizes-photo img");
|
2018-05-30 04:48:44 +02:00
|
|
|
if (fullsizeImages.isEmpty()) {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.error("Could not find flickr image at " + doc.location() + " - missing 'div#allsizes-photo img'");
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-05-26 09:31:58 +02:00
|
|
|
String prefix = "";
|
|
|
|
if (Utils.getConfigBoolean("download.save_order", true)) {
|
|
|
|
prefix = String.format("%03d_", index);
|
|
|
|
}
|
2014-05-14 08:16:37 +02:00
|
|
|
synchronized (flickrThreadPool) {
|
2014-06-23 04:12:29 +02:00
|
|
|
addURLToDownload(new URL(fullsizeImages.first().attr("src")), prefix);
|
2014-05-14 08:16:37 +02:00
|
|
|
}
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.error("[!] Exception while loading/parsing " + this.url, e);
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-19 19:32:57 +02:00
|
|
|
|
2014-04-24 07:17:41 +02:00
|
|
|
private Document getLargestImagePageDocument(URL url) throws IOException {
|
|
|
|
// Get current page
|
2014-06-22 02:08:42 +02:00
|
|
|
Document doc = Http.url(url).get();
|
2014-04-24 07:17:41 +02:00
|
|
|
// Look for larger image page
|
|
|
|
String largestImagePage = this.url.toExternalForm();
|
|
|
|
for (Element olSize : doc.select("ol.sizes-list > li > ol > li")) {
|
|
|
|
Elements ola = olSize.select("a");
|
2018-05-30 04:48:44 +02:00
|
|
|
if (ola.isEmpty()) {
|
2014-04-24 07:17:41 +02:00
|
|
|
largestImagePage = this.url.toExternalForm();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
String candImage = ola.get(0).attr("href");
|
|
|
|
if (candImage.startsWith("/")) {
|
|
|
|
candImage = "http://www.flickr.com" + candImage;
|
|
|
|
}
|
|
|
|
largestImagePage = candImage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!largestImagePage.equals(this.url.toExternalForm())) {
|
|
|
|
// Found larger image page, get it.
|
2014-06-22 02:08:42 +02:00
|
|
|
doc = Http.url(largestImagePage).get();
|
2014-04-24 07:17:41 +02:00
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|