Fixed furaffinity ripper. #84

Logs in correctly via https.
Spaces instead of tabs.
No more warnings.
This commit is contained in:
4pr0n 2014-06-27 23:20:52 -07:00
parent 9e40e55ee9
commit b0e5fa8cb0

View File

@ -4,16 +4,12 @@ import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import org.jsoup.Connection.Method; import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response; import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
@ -22,146 +18,143 @@ import org.jsoup.select.Elements;
import com.rarchives.ripme.ripper.AbstractHTMLRipper; import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.ripper.DownloadThreadPool; import com.rarchives.ripme.ripper.DownloadThreadPool;
import com.rarchives.ripme.utils.Base64;
import com.rarchives.ripme.utils.Http; import com.rarchives.ripme.utils.Http;
public class FuraffinityRipper extends AbstractHTMLRipper { public class FuraffinityRipper extends AbstractHTMLRipper {
static Map<String, String> cookies=null; static Map<String, String> cookies=null;
static final String urlBase = "http://www.furaffinity.net"; static final String urlBase = "https://www.furaffinity.net";
// Thread pool for finding direct image links from "image" pages (html) // Thread pool for finding direct image links from "image" pages (html)
private DownloadThreadPool furaffinityThreadPool = new DownloadThreadPool( private DownloadThreadPool furaffinityThreadPool
"furaffinity"); = new DownloadThreadPool( "furaffinity");
@Override @Override
public DownloadThreadPool getThreadPool() { public DownloadThreadPool getThreadPool() {
return furaffinityThreadPool; return furaffinityThreadPool;
} }
public FuraffinityRipper(URL url) throws IOException { public FuraffinityRipper(URL url) throws IOException {
super(url); super(url);
} }
@Override @Override
public String getDomain() { public String getDomain() {
return "furaffinity.net"; return "furaffinity.net";
} }
@Override @Override
public String getHost() { public String getHost() {
return "furaffinity"; return "furaffinity";
} }
@Override @Override
public Document getFirstPage() throws IOException { public Document getFirstPage() throws IOException {
if (cookies == null || cookies.size() == 0) { if (cookies == null || cookies.size() == 0) {
JPasswordField passwordField=new JPasswordField(); login();
String user = JOptionPane.showInputDialog("Username"); }
JOptionPane.showMessageDialog(null,passwordField,"Password",JOptionPane.QUESTION_MESSAGE|JOptionPane.OK_OPTION);
String pass = Arrays.toString(passwordField.getPassword());
Response loginPage=Http.url(urlBase+"/login/") return Http.url(url).cookies(cookies).get();
.referrer(urlBase) }
.response();
cookies=loginPage.cookies(); private void login() throws IOException {
System.out.println("Cookies: "+cookies); String user = new String(Base64.decode("cmlwbWU="));
String pass = new String(Base64.decode("cmlwbWVwYXNzd29yZA=="));
Map<String,String> formData=new HashMap<String,String>();
formData.put("action", "login");
formData.put("retard_protection", "1");
formData.put("name", user);
formData.put("pass", pass);
formData.put("login", "Login to FurAffinity");
Response doLogin=Http.url(urlBase+"/login/?ref=http://www.furaffinity.net/")
.referrer(urlBase+"/login/")
.cookies(cookies)
.data(formData)
.method(Method.POST)
.response();
cookies.putAll(doLogin.cookies());
System.out.println("Cookies: "+cookies);
}
return Http.url(url).cookies(cookies).get(); Response loginPage = Http.url(urlBase + "/login/")
} .referrer(urlBase)
.response();
cookies = loginPage.cookies();
@Override Map<String,String> formData = new HashMap<String,String>();
public Document getNextPage(Document doc) throws IOException { formData.put("action", "login");
// Find next page formData.put("retard_protection", "1");
Elements nextPageUrl = doc.select("td[align=right] form"); formData.put("name", user);
String nextUrl = urlBase+nextPageUrl.first().attr("action"); formData.put("pass", pass);
if (nextPageUrl.size() == 0) { formData.put("login", "Login to FurAffinity");
throw new IOException("No more pages");
}
sleep(500);
Document nextPage = Http.url(nextUrl).cookies(cookies).get();
Elements hrefs = nextPage.select("div#no-images"); Response doLogin = Http.url(urlBase + "/login/?ref=" + url)
if (hrefs.size() != 0) { .referrer(urlBase + "/login/")
throw new IOException("No more pages"); .cookies(cookies)
} .data(formData)
return nextPage; .method(Method.POST)
} .response();
cookies.putAll(doLogin.cookies());
}
@Override @Override
public List<String> getURLsFromPage(Document page) { public Document getNextPage(Document doc) throws IOException {
List<String> urls = new ArrayList<String>(); // Find next page
Elements urlElements = page.select("b[id^=sid_]"); Elements nextPageUrl = doc.select("td[align=right] form");
for (Element e : urlElements) { String nextUrl = urlBase+nextPageUrl.first().attr("action");
urls.add(urlBase + e.select("a").first().attr("href")); if (nextPageUrl.size() == 0) {
} throw new IOException("No more pages");
return urls; }
} sleep(500);
Document nextPage = Http.url(nextUrl).cookies(cookies).get();
@Override Elements hrefs = nextPage.select("div#no-images");
public void downloadURL(URL url, int index) { if (hrefs.size() != 0) {
furaffinityThreadPool.addThread(new FuraffinityDocumentThread(url, throw new IOException("No more pages");
index)); }
sleep(250); return nextPage;
} }
@Override @Override
public String getGID(URL url) throws MalformedURLException { public List<String> getURLsFromPage(Document page) {
Pattern p = Pattern List<String> urls = new ArrayList<String>();
.compile("^https?://www\\.furaffinity\\.net/gallery/([-_.0-9a-zA-Z]+).*$"); Elements urlElements = page.select("b[id^=sid_]");
Matcher m = p.matcher(url.toExternalForm()); for (Element e : urlElements) {
if (m.matches()) { urls.add(urlBase + e.select("a").first().attr("href"));
return m.group(1); }
} return urls;
throw new MalformedURLException("Expected furaffinity.net URL format: " }
+ "www.furaffinity.net/gallery/username - got " + url
+ " instead");
}
private class FuraffinityDocumentThread extends Thread { @Override
private URL url; public void downloadURL(URL url, int index) {
private int index; furaffinityThreadPool.addThread(new FuraffinityDocumentThread(url));
sleep(250);
}
public FuraffinityDocumentThread(URL url, int index) { @Override
super(); public String getGID(URL url) throws MalformedURLException {
this.url = url; Pattern p = Pattern
this.index = index; .compile("^https?://www\\.furaffinity\\.net/gallery/([-_.0-9a-zA-Z]+).*$");
} Matcher m = p.matcher(url.toExternalForm());
if (m.matches()) {
return m.group(1);
}
throw new MalformedURLException("Expected furaffinity.net URL format: "
+ "www.furaffinity.net/gallery/username - got " + url
+ " instead");
}
@Override private class FuraffinityDocumentThread extends Thread {
public void run() { private URL url;
try {
Document doc = Http.url(url).cookies(cookies).get(); public FuraffinityDocumentThread(URL url) {
// Find image super();
Elements donwloadLink = doc.select("div.alt1 b a[href^=//d.facdn.net/]"); this.url = url;
if (donwloadLink.size() == 0) { }
logger.warn("Could not download " + this.url);
return; @Override
} public void run() {
String link = "http:" + donwloadLink.first().attr("href"); try {
logger.info("Found URL " + link); Document doc = Http.url(url).cookies(cookies).get();
addURLToDownload(new URL(link),"","",url.toExternalForm(),cookies); // Find image
} catch (IOException e) { Elements donwloadLink = doc.select("div.alt1 b a[href^=//d.facdn.net/]");
logger.error("[!] Exception while loading/parsing " + this.url, if (donwloadLink.size() == 0) {
e); logger.warn("Could not download " + this.url);
} return;
} }
} String link = "http:" + donwloadLink.first().attr("href");
logger.info("Found URL " + link);
addURLToDownload(new URL(link),"","",url.toExternalForm(),cookies);
} catch (IOException e) {
logger.error("[!] Exception while loading/parsing " + this.url, e);
}
}
}
} }