|
| 1 | +package com.rarchives.ripme.ripper.rippers; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.regex.Matcher; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +import javax.swing.JOptionPane; |
| 15 | +import javax.swing.JPasswordField; |
| 16 | + |
| 17 | +import org.jsoup.Connection.Method; |
| 18 | +import org.jsoup.Connection.Response; |
| 19 | +import org.jsoup.nodes.Document; |
| 20 | +import org.jsoup.nodes.Element; |
| 21 | +import org.jsoup.select.Elements; |
| 22 | + |
| 23 | +import com.rarchives.ripme.ripper.AbstractHTMLRipper; |
| 24 | +import com.rarchives.ripme.ripper.DownloadThreadPool; |
| 25 | +import com.rarchives.ripme.utils.Http; |
| 26 | + |
| 27 | +public class FuraffinityRipper extends AbstractHTMLRipper { |
| 28 | + |
| 29 | + static Map<String, String> cookies=null; |
| 30 | + static final String urlBase = "http://www.furaffinity.net"; |
| 31 | + |
| 32 | + // Thread pool for finding direct image links from "image" pages (html) |
| 33 | + private DownloadThreadPool furaffinityThreadPool = new DownloadThreadPool( |
| 34 | + "furaffinity"); |
| 35 | + |
| 36 | + @Override |
| 37 | + public DownloadThreadPool getThreadPool() { |
| 38 | + return furaffinityThreadPool; |
| 39 | + } |
| 40 | + |
| 41 | + public FuraffinityRipper(URL url) throws IOException { |
| 42 | + super(url); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getDomain() { |
| 47 | + return "furaffinity.net"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getHost() { |
| 52 | + return "furaffinity"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Document getFirstPage() throws IOException { |
| 57 | + if (cookies == null || cookies.size() == 0) { |
| 58 | + JPasswordField passwordField=new JPasswordField(); |
| 59 | + String user = JOptionPane.showInputDialog("Username"); |
| 60 | + JOptionPane.showMessageDialog(null,passwordField,"Password",JOptionPane.QUESTION_MESSAGE|JOptionPane.OK_OPTION); |
| 61 | + String pass = Arrays.toString(passwordField.getPassword()); |
| 62 | + |
| 63 | + Response loginPage=Http.url(urlBase+"/login/") |
| 64 | + .referrer(urlBase) |
| 65 | + .response(); |
| 66 | + cookies=loginPage.cookies(); |
| 67 | + System.out.println("Cookies: "+cookies); |
| 68 | + |
| 69 | + Map<String,String> formData=new HashMap<String,String>(); |
| 70 | + formData.put("action", "login"); |
| 71 | + formData.put("retard_protection", "1"); |
| 72 | + formData.put("name", user); |
| 73 | + formData.put("pass", pass); |
| 74 | + formData.put("login", "Login to FurAffinity"); |
| 75 | + |
| 76 | + Response doLogin=Http.url(urlBase+"/login/?ref=http://www.furaffinity.net/") |
| 77 | + .referrer(urlBase+"/login/") |
| 78 | + .cookies(cookies) |
| 79 | + .data(formData) |
| 80 | + .method(Method.POST) |
| 81 | + .response(); |
| 82 | + cookies.putAll(doLogin.cookies()); |
| 83 | + System.out.println("Cookies: "+cookies); |
| 84 | + } |
| 85 | + |
| 86 | + return Http.url(url).cookies(cookies).get(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Document getNextPage(Document doc) throws IOException { |
| 91 | + // Find next page |
| 92 | + Elements nextPageUrl = doc.select("td[align=right] form"); |
| 93 | + String nextUrl = urlBase+nextPageUrl.first().attr("action"); |
| 94 | + if (nextPageUrl.size() == 0) { |
| 95 | + throw new IOException("No more pages"); |
| 96 | + } |
| 97 | + sleep(500); |
| 98 | + Document nextPage = Http.url(nextUrl).cookies(cookies).get(); |
| 99 | + |
| 100 | + Elements hrefs = nextPage.select("div#no-images"); |
| 101 | + if (hrefs.size() != 0) { |
| 102 | + throw new IOException("No more pages"); |
| 103 | + } |
| 104 | + return nextPage; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public List<String> getURLsFromPage(Document page) { |
| 109 | + List<String> urls = new ArrayList<String>(); |
| 110 | + Elements urlElements = page.select("b[id^=sid_]"); |
| 111 | + for (Element e : urlElements) { |
| 112 | + urls.add(urlBase + e.select("a").first().attr("href")); |
| 113 | + } |
| 114 | + return urls; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void downloadURL(URL url, int index) { |
| 119 | + furaffinityThreadPool.addThread(new FuraffinityDocumentThread(url, |
| 120 | + index)); |
| 121 | + sleep(250); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String getGID(URL url) throws MalformedURLException { |
| 126 | + Pattern p = Pattern |
| 127 | + .compile("^https?://www\\.furaffinity\\.net/gallery/([-_.0-9a-zA-Z]+).*$"); |
| 128 | + Matcher m = p.matcher(url.toExternalForm()); |
| 129 | + if (m.matches()) { |
| 130 | + return m.group(1); |
| 131 | + } |
| 132 | + throw new MalformedURLException("Expected furaffinity.net URL format: " |
| 133 | + + "www.furaffinity.net/gallery/username - got " + url |
| 134 | + + " instead"); |
| 135 | + } |
| 136 | + |
| 137 | + private class FuraffinityDocumentThread extends Thread { |
| 138 | + private URL url; |
| 139 | + private int index; |
| 140 | + |
| 141 | + public FuraffinityDocumentThread(URL url, int index) { |
| 142 | + super(); |
| 143 | + this.url = url; |
| 144 | + this.index = index; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void run() { |
| 149 | + try { |
| 150 | + Document doc = Http.url(url).cookies(cookies).get(); |
| 151 | + // Find image |
| 152 | + Elements donwloadLink = doc.select("div.alt1 b a[href^=//d.facdn.net/]"); |
| 153 | + if (donwloadLink.size() == 0) { |
| 154 | + logger.warn("Could not download " + this.url); |
| 155 | + return; |
| 156 | + } |
| 157 | + String link = "http:" + donwloadLink.first().attr("href"); |
| 158 | + logger.info("Found URL " + link); |
| 159 | + addURLToDownload(new URL(link),"","",url.toExternalForm(),cookies); |
| 160 | + } catch (IOException e) { |
| 161 | + logger.error("[!] Exception while loading/parsing " + this.url, |
| 162 | + e); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments