package com.rarchives.ripme.ripper.rippers.video;

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

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

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

public class XhamsterRipper extends VideoRipper {

    private static final String HOST = "xhamster";

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

    @Override
    public String getHost() {
        return HOST;
    }

    @Override
    public boolean canRip(URL url) {
        Pattern p = Pattern.compile("^https?://[wm.]*xhamster\\.com/movies/[0-9]+.*$");
        Matcher m = p.matcher(url.toExternalForm());
        return m.matches();
    }
    
    @Override
    public URL sanitizeURL(URL url) throws MalformedURLException {
        return url;
    }

    @Override
    public String getGID(URL url) throws MalformedURLException {
        Pattern p = Pattern.compile("^https?://[wm.]*xhamster\\.com/movies/([0-9]+).*$");
        Matcher m = p.matcher(url.toExternalForm());
        if (m.matches()) {
            return m.group(1);
        }

        throw new MalformedURLException(
                "Expected xhamster format:"
                        + "xhamster.com/movies/####"
                        + " Got: " + url);
    }

    @Override
    public void rip() throws IOException {
        logger.info("Retrieving " + this.url);
        Document doc = Http.url(url).get();
        Elements videos = doc.select("video");
        if (videos.size() == 0) {
            throw new IOException("Could not find Embed code at " + url);
        }
        String vidUrl = videos.first().attr("file");
        addURLToDownload(new URL(vidUrl), HOST + "_" + getGID(this.url));
        waitForThreads();
    }
}
@@ -700,37 +700,33 @@
 pile(%22%5Ehttps?://
-%5Bwm.%5D
+.
 *xhamster%5C%5C.com/
@@ -1075,13 +1075,9 @@
 ?://
-%5Bwm.%5D
+.
 *xha
@@ -1606,21 +1606,26 @@
 select(%22
-video
+a.mp4Thumb
 %22);%0A    
@@ -1773,26 +1773,18 @@
 eos.
-first().
 attr(%22
-file
+href
 %22);%0A

Fix xhamster. Closes #276