package com.rarchives.ripme.ripper.rippers;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.utils.Http;

public class ImagearnRipper extends AbstractHTMLRipper {

    public ImagearnRipper(URL url) throws IOException {
        super(url);
    }

    @Override
    public String getHost() {
        return "imagearn";
    }
    @Override
    public String getDomain() {
        return "imagearn.com";
    }

    @Override
    public String getGID(URL url) throws MalformedURLException {
        Pattern p = Pattern.compile("^.*imagearn.com/+gallery.php\\?id=([0-9]+).*$");
        Matcher m = p.matcher(url.toExternalForm());
        if (m.matches()) {
            return m.group(1);
        }
        throw new MalformedURLException(
                "Expected imagearn.com gallery formats: "
                        + "imagearn.com/gallery.php?id=####..."
                        + " Got: " + url);
    }

    public URL sanitizeURL(URL url) throws MalformedURLException {
        Pattern p = Pattern.compile("^.*imagearn.com/+image.php\\?id=[0-9]+.*$");
        Matcher m = p.matcher(url.toExternalForm());
        if (m.matches()) {
            // URL points to imagearn *image*, not gallery
            try {
                url = getGalleryFromImage(url);
            } catch (Exception e) {
                logger.error("[!] " + e.getMessage(), e);
            }
        }
        return url;
    }

    private URL getGalleryFromImage(URL url) throws IOException {
        Document doc = Http.url(url).get();
        for (Element link : doc.select("a[href~=^gallery\\.php.*$]")) {
            logger.info("LINK: " + link.toString());
            if (link.hasAttr("href")
                    && link.attr("href").contains("gallery.php")) {
                url = new URL("http://imagearn.com/" + link.attr("href"));
                logger.info("[!] Found gallery from given link: " + url);
                return url;
            }
        }
        throw new IOException("Failed to find gallery at URL " + url);
    }

    @Override
    public Document getFirstPage() throws IOException {
        return Http.url(url).get();
    }

    @Override
    public List<String> getURLsFromPage(Document doc) {
        List<String> imageURLs = new ArrayList<>();
        for (Element thumb : doc.select("div#gallery > div > a")) {
            String imageURL = thumb.attr("href");
            try {
                Document imagedoc = new Http("http://imagearn.com/" + imageURL).get();
                String image = imagedoc.select("a.thickbox").first().attr("href");
                imageURLs.add(image);
            } catch (IOException e) {
                logger.warn("Was unable to download page: " + imageURL);
            }
        }
        return imageURLs;
    }

    @Override
    public void downloadURL(URL url, int index) {
        addURLToDownload(url, getPrefix(index));
        sleep(1000);
    }
}

@@ -696,32 +696,556 @@
 rn.com%22;%0A    %7D%0A%0A
+    @Override%0A    public String getAlbumTitle(URL url) throws MalformedURLException %7B%0A        try %7B%0A            Document doc = getFirstPage();%0A            String title = doc.select(%22h3 %3E strong%22).first().text(); // profile name%0A            logger.error(title);%0A            return getHost() + %22_%22 + title;%0A        %7D catch (Exception e) %7B%0A            // Fall back to default album naming convention%0A            logger.warn(%22Failed to get album title from %22 + url, e);%0A        %7D%0A        return super.getAlbumTitle(url);%0A    %7D%0A%0A
     @Override%0A  

Change folder name in imagearn