From 6a672f05c9762b2fabbc6c0619ab12bc565f2403 Mon Sep 17 00:00:00 2001 From: cyian-1756 Date: Mon, 30 Apr 2018 17:47:19 -0400 Subject: [PATCH] Added ManganeloRipper --- .../ripme/ripper/rippers/ManganeloRipper.java | 116 ++++++++++++++++++ .../ripper/rippers/ManganeloRipperTest.java | 13 ++ 2 files changed, 129 insertions(+) create mode 100644 src/main/java/com/rarchives/ripme/ripper/rippers/ManganeloRipper.java create mode 100644 src/test/java/com/rarchives/ripme/tst/ripper/rippers/ManganeloRipperTest.java diff --git a/src/main/java/com/rarchives/ripme/ripper/rippers/ManganeloRipper.java b/src/main/java/com/rarchives/ripme/ripper/rippers/ManganeloRipper.java new file mode 100644 index 00000000..8f8f8e68 --- /dev/null +++ b/src/main/java/com/rarchives/ripme/ripper/rippers/ManganeloRipper.java @@ -0,0 +1,116 @@ +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.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; + +import com.rarchives.ripme.ripper.AbstractHTMLRipper; +import com.rarchives.ripme.utils.Http; + +import javax.print.Doc; + +public class ManganeloRipper extends AbstractHTMLRipper { + + public ManganeloRipper(URL url) throws IOException { + super(url); + } + + @Override + public String getHost() { + return "manganelo"; + } + + @Override + public String getDomain() { + return "manganelo.com"; + } + + @Override + public String getGID(URL url) throws MalformedURLException { + Pattern p = Pattern.compile("https?://manganelo.com/manga/([\\S]+)/?$"); + Matcher m = p.matcher(url.toExternalForm()); + if (m.matches()) { + return m.group(1); + } + + p = Pattern.compile("http://manganelo.com/chapter/([\\S]+)/([\\S]+)/?$"); + m = p.matcher(url.toExternalForm()); + if (m.matches()) { + return m.group(1); + } + throw new MalformedURLException("Expected manganelo URL format: " + + "/manganelo.com/manga/ID - got " + url + " instead"); + } + + @Override + public Document getFirstPage() throws IOException { + // "url" is an instance field of the superclass + return Http.url(url).get(); + } + + @Override + public Document getNextPage(Document doc) throws IOException { + Element elem = doc.select("div.btn-navigation-chap > a.back").first(); + if (elem == null) { + throw new IOException("No more pages"); + } else { + return Http.url(elem.attr("href")).get(); + } + } + + private List getURLsFromChap(String url) { + logger.debug("Getting urls from " + url); + List result = new ArrayList<>(); + try { + Document doc = Http.url(url).get(); + for (Element el : doc.select("img.img_content")) { + result.add(el.attr("src")); + } + return result; + } catch (IOException e) { + return null; + } + + } + + private List getURLsFromChap(Document doc) { + logger.debug("Getting urls from " + url); + List result = new ArrayList<>(); + for (Element el : doc.select("img.img_content")) { + result.add(el.attr("src")); + } + return result; + } + + @Override + public List getURLsFromPage(Document doc) { + List result = new ArrayList<>(); + List urlsToGrab = new ArrayList<>(); + if (url.toExternalForm().contains("/manga/")) { + for (Element el : doc.select("div.chapter-list > div.row > span > a")) { + urlsToGrab.add(el.attr("href")); + } + Collections.reverse(urlsToGrab); + + for (String url : urlsToGrab) { + result.addAll(getURLsFromChap(url)); + } + } else if (url.toExternalForm().contains("/chapter/")) { + result.addAll(getURLsFromChap(doc)); + } + return result; + } + + @Override + public void downloadURL(URL url, int index) { + addURLToDownload(url, getPrefix(index)); + } +} diff --git a/src/test/java/com/rarchives/ripme/tst/ripper/rippers/ManganeloRipperTest.java b/src/test/java/com/rarchives/ripme/tst/ripper/rippers/ManganeloRipperTest.java new file mode 100644 index 00000000..ca355a2c --- /dev/null +++ b/src/test/java/com/rarchives/ripme/tst/ripper/rippers/ManganeloRipperTest.java @@ -0,0 +1,13 @@ +package com.rarchives.ripme.tst.ripper.rippers; + +import java.io.IOException; +import java.net.URL; + +import com.rarchives.ripme.ripper.rippers.ManganeloRipper; + +public class ManganeloRipperTest extends RippersTest { + public void testRip() throws IOException { + ManganeloRipper ripper = new ManganeloRipper(new URL("http://manganelo.com/manga/black_clover")); + testRipper(ripper); + } +}