From a58171477900c933b710443dbbaa74ab72902e8e Mon Sep 17 00:00:00 2001 From: soloturn Date: Mon, 3 Jan 2022 16:08:12 +0100 Subject: [PATCH] reformat mangadex --- .../ripme/ripper/rippers/MangadexRipper.java | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/MangadexRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/MangadexRipper.java index cfe2e53f..d9a80080 100644 --- a/src/main/java/com/rarchives/ripme/ripper/rippers/MangadexRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/MangadexRipper.java @@ -1,39 +1,40 @@ package com.rarchives.ripme.ripper.rippers; import com.rarchives.ripme.ripper.AbstractJSONRipper; -import com.rarchives.ripme.ui.History; import com.rarchives.ripme.ui.RipStatusMessage; import com.rarchives.ripme.utils.Http; -import com.rarchives.ripme.utils.Utils; import org.json.JSONArray; import org.json.JSONObject; -import org.jsoup.Connection; -import org.jsoup.nodes.Document; -import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MangadexRipper extends AbstractJSONRipper { - private String chapterApiEndPoint = "https://mangadex.org/api/chapter/"; - private String mangaApiEndPoint = "https://mangadex.org/api/manga/"; + private final String chapterApiEndPoint = "https://mangadex.org/api/chapter/"; + private final String mangaApiEndPoint = "https://mangadex.org/api/manga/"; private boolean isSingleChapter; - private String getImageUrl(String chapterHash, String imageName, String server) { - return server + chapterHash + "/" + imageName; - } public MangadexRipper(URL url) throws IOException { super(url); } + private String getImageUrl(String chapterHash, String imageName, String server) { + return server + chapterHash + "/" + imageName; + } + @Override public String getHost() { return "mangadex"; } + @Override public String getDomain() { return "mangadex.org"; @@ -49,14 +50,12 @@ public class MangadexRipper extends AbstractJSONRipper { String capID = getChapterID(url.toExternalForm()); String mangaID = getMangaID(url.toExternalForm()); if (capID != null) { - isSingleChapter=true; + isSingleChapter = true; return capID; + } else if (mangaID != null) { + isSingleChapter = false; + return mangaID; } - else - if(mangaID!=null){ - isSingleChapter=false; - return mangaID; - } throw new MalformedURLException("Unable to get chapter ID from" + url); } @@ -68,10 +67,11 @@ public class MangadexRipper extends AbstractJSONRipper { } return null; } - private String getMangaID(String url){ + + private String getMangaID(String url) { Pattern p = Pattern.compile("https://mangadex.org/title/([\\d]+)/(.+)"); Matcher m = p.matcher(url); - if(m.matches()){ + if (m.matches()) { return m.group(1); } return null; @@ -83,16 +83,15 @@ public class MangadexRipper extends AbstractJSONRipper { // Get the chapter ID String chapterID = getChapterID(url.toExternalForm()); String mangaID = getMangaID(url.toExternalForm()); - if(mangaID!=null){ - return Http.url(new URL(mangaApiEndPoint+mangaID)).getJSON(); - } - else + if (mangaID != null) { + return Http.url(new URL(mangaApiEndPoint + mangaID)).getJSON(); + } else return Http.url(new URL(chapterApiEndPoint + chapterID)).getJSON(); } @Override protected List getURLsFromJSON(JSONObject json) { - if(isSingleChapter){ + if (isSingleChapter) { List assetURLs = new ArrayList<>(); JSONArray currentObject; String chapterHash; @@ -110,12 +109,12 @@ public class MangadexRipper extends AbstractJSONRipper { JSONObject chaptersJSON = (JSONObject) json.get("chapter"); JSONObject temp; Iterator keys = chaptersJSON.keys(); - HashMap chapterIDs = new HashMap<>(); + HashMap chapterIDs = new HashMap<>(); while (keys.hasNext()) { - String keyValue = (String) keys.next(); - temp=(JSONObject)chaptersJSON.get(keyValue); - if(temp.getString("lang_name").equals("English")) { - chapterIDs.put(temp.getDouble("chapter"),keyValue); + String keyValue = keys.next(); + temp = (JSONObject) chaptersJSON.get(keyValue); + if (temp.getString("lang_name").equals("English")) { + chapterIDs.put(temp.getDouble("chapter"), keyValue); } } @@ -125,17 +124,17 @@ public class MangadexRipper extends AbstractJSONRipper { String chapterHash; // Server is the cdn hosting the images. String server; - JSONObject chapterJSON=null; - TreeMap treeMap = new TreeMap<>(chapterIDs); + JSONObject chapterJSON = null; + TreeMap treeMap = new TreeMap<>(chapterIDs); Iterator it = treeMap.keySet().iterator(); - while(it.hasNext()) { - double key =(double) it.next(); + while (it.hasNext()) { + double key = (double) it.next(); try { chapterJSON = Http.url(new URL(chapterApiEndPoint + treeMap.get(key))).getJSON(); } catch (IOException e) { e.printStackTrace(); } - sendUpdate(RipStatusMessage.STATUS.LOADING_RESOURCE,"chapter "+key); + sendUpdate(RipStatusMessage.STATUS.LOADING_RESOURCE, "chapter " + key); chapterHash = chapterJSON.getString("hash"); server = chapterJSON.getString("server"); for (int i = 0; i < chapterJSON.getJSONArray("page_array").length(); i++) {