package me.devsaki.hentoid.parsers.images;

import androidx.annotation.NonNull;

import org.jsoup.nodes.Document;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import me.devsaki.hentoid.database.domains.Content;
import me.devsaki.hentoid.util.JsonHelper;

import static me.devsaki.hentoid.util.HttpHelper.getOnlineDocument;

/**
 * Created by robb_w on 01/31/2018.
 * Handles parsing of content from pururin.io
 */
public class PururinParser extends BaseParser {

    private static final String IMAGE_PATH = "//cdn.pururin.io/assets/images/data/";

    public static class PururinInfo {
        String image_extension;
        String id;
    }

    @Override
    protected List<String> parseImages(@NonNull Content content) throws Exception {
        List<String> result = new ArrayList<>();
        String url = content.getReaderUrl();
        String protocol = url.substring(0, 5);
        if ("https".equals(protocol)) protocol = "https:";

        // The whole algorithm is in app.js
        // 1- Get image extension from gallery data (JSON on HTML body)
        // 2- Generate image URL from  imagePath constant, gallery ID, page number and extension

        // 1- Get image extension from gallery data (JSON on HTML body)
        Document doc = getOnlineDocument(url);
        if (doc != null) {
            String json = doc.select("gallery-read").attr(":gallery");
            PururinInfo info = JsonHelper.jsonToObject(json, PururinInfo.class);

            // 2- Get imagePath from app.js => it is constant anyway, and app.js is 3 MB long => put it there as a const
            for (int i = 0; i < content.getQtyPages(); i++) {
                result.add(protocol + IMAGE_PATH + info.id + File.separator + (i + 1) + "." + info.image_extension);
            }
        }

        return result;
    }
}

@@ -37,16 +37,43 @@
 mages;%0A%0A
+import android.util.Pair;%0A%0A
 import a
@@ -100,16 +100,16 @@
 onNull;%0A
-
 %0Aimport 
@@ -156,16 +156,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -234,16 +234,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -310,16 +310,97 @@
 ontent;%0A
+import me.devsaki.hentoid.enums.Site;%0Aimport me.devsaki.hentoid.util.HttpHelper;%0A
 import m
@@ -433,16 +433,42 @@
 nHelper;
+%0Aimport timber.log.Timber;
 %0A%0Aimport
@@ -994,24 +994,755 @@
 ayList%3C%3E();%0A
+%0A        String downloadParamsStr = content.getDownloadParams();%0A        if (null == downloadParamsStr %7C%7C downloadParamsStr.isEmpty()) %7B%0A            Timber.e(%22Download parameters not set%22);%0A            return result;%0A        %7D%0A%0A        Map%3CString, String%3E downloadParams;%0A        try %7B%0A            downloadParams = JsonHelper.jsonToObject(downloadParamsStr, JsonHelper.MAP_STRINGS);%0A        %7D catch (IOException e) %7B%0A            Timber.e(e);%0A            return result;%0A        %7D%0A%0A        List%3CPair%3CString, String%3E%3E headers = new ArrayList%3C%3E();%0A        String cookieStr = downloadParams.get(HttpHelper.HEADER_COOKIE_KEY);%0A        if (null != cookieStr)%0A            headers.add(new Pair%3C%3E(HttpHelper.HEADER_COOKIE_KEY, cookieStr));%0A%0A
         Stri
@@ -2159,32 +2159,32 @@
 N on HTML body)%0A
-
         Document
@@ -2211,16 +2211,61 @@
 ment(url
+, headers, Site.PURURIN.canKnowHentoidAgent()
 );%0A     

Fix Pururin downloader