Thread: [Solved] Hitomi.la
View Single Post
  #4  
Old 22.10.2020, 18:02
damo damo is offline
Vacuum Cleaner
 
Join Date: Jan 2020
Posts: 15
Exclamation

They have slightly changed the code from the version I posted in Post https://board.jdownloader.org/showpo...8&postcount=16

IMO it still should grab the original .jpg/.png, although this could be a configurable option for the plugin, as some people might prefer the smaller webp versions. BTW they have added .avif as well as with webp there is a "hasavif: 1" entry in the <galleryid>.js file. Furthermore, I'd really appreciate a way to keep the original file names, e.g., "105_chapter10_8.jpg" instead of just "60.jpg".

Here is what has changed
Code:
 function subdomain_from_url(url, base) {
-        var retval = 'a';
+        var retval = 'b';

// ...
         var m = r.exec(url);
         if (!m) {
-                return retval;
+                return 'a';
         }
Here are the changed functions, further down is the full version.

Code:
  String subdomain_from_url(String url, String base) {
    String retval = "b";
    if (base != null) {
      retval = base;
    }

    int number_of_frontends = 3;

    Matcher m = SUBDOMAIN_FROM_URL_PATTERN.matcher(url);
    if (!m.find()) {
      return "a";
    }

    try {
      int g = Integer.parseInt(m.group(1), 16);
      if (g < 0x30) {
        number_of_frontends = 2;
      }
      if (g < 0x09) {
        g = 1;
      }
      retval = subdomain_from_galleryid(g, number_of_frontends) + retval;
    } catch (NumberFormatException ignore) {}

    return retval;
  }

  String url_from_hash(String galleryid, Map<String, String> image, String dir, String ext) {
    ext = isNotBlank(ext) ? ext : (isNotBlank(dir) ? dir :
        image.get("name").split("\\.")[1]);
    dir = isNotBlank(dir) ? dir : "images";

    return "**External links are only visible to Support Staff** + dir + '/' + full_path_from_hash(image.get("hash")) + '.' + ext;
  }
The best way to get Test Data is to open a gallery and set this breakpoint in the DevTools in common.js:65

Code:
'Assert.assertEquals("'+url_from_url(url_from_hash(galleryid, image, dir, ext), base)+'", url_from_url_from_hash("'+galleryid+'", mapOf("'+ image.hash+'", "'+ image.name+'"), '+ (dir ? '"'+dir+'"' : 'null') +', '+ (ext ? '"'+ext+'"' : 'null') +', '+ (base ? '"'+base+'"' : 'null') +'));'
This will log out assert statements that can be copy pasted into a test and then you only need this helper function to test the current code.

Code:
  Map<String, String> mapOf(String hash, String name) {
    HashMap<String, String> m = new HashMap<>();
    m.put("hash", hash);
    m.put("name", name);
    return m;
  }
Code:
    Assert.assertEquals("**External links are only visible to Support Staff**, url_from_url_from_hash("1760873", mapOf("7c3d8d36f3a73375b1194207d95127d62002f0ce94d1d9a6045c21a9bed2c78e", "105_chapter10_8.jpg"), null, null, null));
    Assert.assertEquals("**External links are only visible to Support Staff**,  url_from_url_from_hash("1760873", mapOf("7c3d8d36f3a73375b1194207d95127d62002f0ce94d1d9a6045c21a9bed2c78e", "105_chapter10_8.jpg"), "avif", null, "a"));
    Assert.assertEquals("**External links are only visible to Support Staff**,  url_from_url_from_hash("1760873", mapOf("7c3d8d36f3a73375b1194207d95127d62002f0ce94d1d9a6045c21a9bed2c78e", "105_chapter10_8.jpg"), "webp", null, "a"));
As you can see, you need to pass null for dir, ext, and base for the original image and "avif"/"webp" for dir plus "a" for base for the avif/webp versions.

This is the current working version:

Code:
  public static final Pattern SUBDOMAIN_FROM_URL_PATTERN = Pattern.compile("/[0-9a-f]/([0-9a-f]{2})/");
  public static final Pattern URL_FROM_URL_PATTERN = Pattern.compile("//..?\\.hitomi\\.la/");
  public static final Pattern FULL_PATH_FROM_HASH_PATTERN = Pattern.compile("^.*(..)(.)$");



  String subdomain_from_galleryid(int g, int number_of_frontends) {
    int o = g % number_of_frontends;

    return String.valueOf((char)(97 + o));
  }

  String subdomain_from_url(String url, String base) {
    String retval = "b";
    if (base != null) {
      retval = base;
    }

    int number_of_frontends = 3;

    Matcher m = SUBDOMAIN_FROM_URL_PATTERN.matcher(url);
    if (!m.find()) {
      return "a";
    }

    try {
      int g = Integer.parseInt(m.group(1), 16);
      if (g < 0x30) {
        number_of_frontends = 2;
      }
      if (g < 0x09) {
        g = 1;
      }
      retval = subdomain_from_galleryid(g, number_of_frontends) + retval;
    } catch (NumberFormatException ignore) {}

    return retval;
  }

  String url_from_url(String url, String base) {
    return URL_FROM_URL_PATTERN.matcher(url).replaceAll("//" + subdomain_from_url(url, base) + ".hitomi.la/");
  }


  String full_path_from_hash(String hash) {
    if (hash.length() < 3) {
      return hash;
    }
    return FULL_PATH_FROM_HASH_PATTERN.matcher(hash).replaceAll("$2/$1/" + hash);
  }

  String url_from_hash(String galleryid, Map<String, String> image, String dir, String ext) {
    ext = isNotBlank(ext) ? ext : (isNotBlank(dir) ? dir :
        image.get("name").split("\\.")[1]);
    dir = isNotBlank(dir) ? dir : "images";

    return "**External links are only visible to Support Staff** + dir + '/' + full_path_from_hash(image.get("hash")) + '.' + ext;
  }

  String url_from_url_from_hash(String galleryid, Map<String, String> image, String dir, String ext, String base) {
    return url_from_url(url_from_hash(galleryid, image, dir, ext), base);
  }

  boolean isNotBlank(String str) {
    return str != null && !str.isEmpty();
  }
Reply With Quote