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 com.rarchives.ripme.ripper.AbstractSingleFileRipper;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.rarchives.ripme.utils.Http;
import com.rarchives.ripme.utils.Utils;

public class GfycatRipper extends AbstractSingleFileRipper {

    private static final String HOST = "gfycat.com";

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

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

    @Override
    public String getHost() {
        return "gfycat";
    }

    @Override
    public boolean canRip(URL url) {
        return url.getHost().endsWith(HOST);
    }

    @Override
    public URL sanitizeURL(URL url) throws MalformedURLException {
        url = new URL(url.toExternalForm().replace("/gifs/detail", ""));

        return url;
    }

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

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

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

        throw new MalformedURLException("Expected gfycat.com format:" + "gfycat.com/id" + " Got: " + url);
    }

    @Override
    public List<String> getURLsFromPage(Document doc) {
        List<String> result = new ArrayList<>();
        Elements videos = doc.select("video source");
        String vidUrl = videos.first().attr("src");
        // Check preference for mp4 over webm/gif.
        if (Utils.getConfigBoolean("prefer.mp4", false)) {
            for (Element e : videos) {
                if (e.hasAttr("src") && e.attr("src").endsWith(".mp4")) {
                    vidUrl = e.attr("src");
                    break;
                }
            }
        }
        if (vidUrl.startsWith("//")) {
            vidUrl = "http:" + vidUrl;
        }
        result.add(vidUrl);
        return result;
    }

    /**
     * Helper method for retrieving video URLs.
     * 
     * @param url
     *            URL to gfycat page
     * @return URL to video
     * @throws IOException
     */
    public static String getVideoURL(URL url) throws IOException {
        LOGGER.info("Retrieving " + url.toExternalForm());

        // Sanitize the URL first
        url = new URL(url.toExternalForm().replace("/gifs/detail", ""));

        Document doc = Http.url(url).get();
        Elements videos = doc.select("video source");
        if (videos.isEmpty()) {
            throw new IOException("Could not find source at " + url);
        }
        String vidUrl = videos.first().attr("src");
        // Check preference for mp4 over webm/gif.
        if (Utils.getConfigBoolean("prefer.mp4", false)) {
            for (Element e : videos) {
                if (e.hasAttr("src") && e.attr("src").endsWith(".mp4")) {
                    vidUrl = e.attr("src");
                    break;
                }
            }
        }
        if (vidUrl.startsWith("//")) {
            vidUrl = "http:" + vidUrl;
        }
        return vidUrl;
    }
}
@@ -299,24 +299,110 @@
 FileRipper;%0A
+import org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A
 import org.j
@@ -532,47 +532,8 @@
 tp;%0A
-import com.rarchives.ripme.utils.Utils;
 %0A%0Apu
@@ -1134,32 +1134,40 @@
 /detail%22, %22%22));%0A
+        
 %0A        return 
@@ -1739,16 +1739,33 @@
 ception(
+%0A                
 %22Expecte
@@ -1789,26 +1789,74 @@
 at:%22
- + %22gfycat.com/id%22
+%0A                        + %22gfycat.com/id%22%0A                       
  + %22
@@ -2039,293 +2039,125 @@
 ct(%22
-video source%22);%0A        String vidUrl = videos.first().attr(%22src%22);%0A        // Check preference for mp4 over webm/gif.%0A        if (Utils.getConfigBoolean(%22prefer.mp4%22, false)) %7B%0A            for (Element e : videos) %7B%0A                if (e.hasAttr(%22src%22) && e.attr(%22src%22).end
+script%22);%0A        for (Element el : videos) %7B%0A            String json = el.html();%0A            if (json.start
 sWith(%22
-.mp4
+%7B
 %22)) 
@@ -2178,220 +2178,152 @@
     
-    vidUrl = e.attr(%22src%22);%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        if (vidUrl.startsWith(%22//%22)) %7B%0A            vidUrl = %22http:%22 + vidUrl;%0A        %7D%0A        result.add(vidUrl);
+JSONObject page = new JSONObject(json);%0A                result.add(page.getJSONObject(%22video%22).getString(%22contentUrl%22));%0A            %7D%0A        %7D
 %0A   
@@ -2409,16 +2409,8 @@
 Ls.%0A
-     * %0A
     
@@ -2426,26 +2426,8 @@
  url
-%0A     *           
  URL
@@ -2642,17 +2642,16 @@
       //
- 
 Sanitize
@@ -2825,136 +2825,58 @@
 ct(%22
-video source%22);%0A        if (videos.isEmpty()) %7B%0A            throw new IOException(%22Could not find source at %22 + url);
+script%22);%0A        for (Element el : videos) %7B
 %0A    
-    %7D%0A
     
@@ -2890,150 +2890,61 @@
 ing 
-vidUrl = videos.first().attr(%22src%22);%0A        // Check preference for mp4 over webm/gif.%0A        if (Utils.getConfigBoolean(%22prefer.mp4%22, false
+json = el.html();%0A            if (json.startsWith(%22%7B%22
 )) %7B
@@ -2960,331 +2960,183 @@
     
+   
-for (Element e : videos) %7B%0A                if (e.hasAttr(%22src%22) && e.attr(%22src%22).endsWith(%22.mp4%22)) %7B%0A                    vidUrl = e.attr(%22src%22);%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        if (vidUrl.startsWith(%22//%22)) %7B%0A            vidUrl = %22http:%22 + vidUrl;%0A        %7D%0A        return vidUrl
+ JSONObject page = new JSONObject(json);%0A                return page.getJSONObject(%22video%22).getString(%22contentUrl%22);%0A            %7D%0A        %7D%0A        throw new IOException()
 ;%0A  

Undo gfycat changes, again.