From b0f5a15943924a887a8b2c06edace4b81c09b894 Mon Sep 17 00:00:00 2001 From: death-claw <53543762+death-claw@users.noreply.github.com> Date: Mon, 31 May 2021 19:06:41 +0100 Subject: [PATCH] Add support for dpic.me --- .../java/tn/mnlr/vripper/host/DPicMeHost.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 vripper-server/src/main/java/tn/mnlr/vripper/host/DPicMeHost.java diff --git a/vripper-server/src/main/java/tn/mnlr/vripper/host/DPicMeHost.java b/vripper-server/src/main/java/tn/mnlr/vripper/host/DPicMeHost.java new file mode 100644 index 00000000..40e03b28 --- /dev/null +++ b/vripper-server/src/main/java/tn/mnlr/vripper/host/DPicMeHost.java @@ -0,0 +1,85 @@ +package tn.mnlr.vripper.host; + +import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.protocol.HttpClientContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import tn.mnlr.vripper.exception.HostException; +import tn.mnlr.vripper.exception.XpathException; +import tn.mnlr.vripper.services.HostService; +import tn.mnlr.vripper.services.XpathService; + +import java.util.Optional; +import java.util.UUID; + +@Service +@Slf4j +public class DPicMeHost extends Host { + + private static final String IMG_XPATH = "//img[@id='pic']"; + private static final String host = "dpic.me"; + + private final HostService hostService; + private final XpathService xpathService; + + @Autowired + public DPicMeHost(HostService hostService, XpathService xpathService) { + this.hostService = hostService; + this.xpathService = xpathService; + } + + @Override + public String getHost() { + return host; + } + + @Override + public String getLookup() { + return host; + } + + @Override + public HostService.NameUrl getNameAndUrl(final String url, final HttpClientContext context) + throws HostException { + + HostService.Response response = + hostService.getResponse(url.replace("http://", "https://"), context); + Document doc = response.getDocument(); + + Node imgNode; + try { + log.debug(String.format("Looking for xpath expression %s in %s", IMG_XPATH, url)); + imgNode = xpathService.getAsNode(doc, IMG_XPATH); + } catch (XpathException e) { + throw new HostException(e); + } + + if (imgNode == null) { + throw new HostException(String.format("Xpath '%s' cannot be found in '%s'", IMG_XPATH, url)); + } + + try { + log.debug(String.format("Resolving name and image url for %s", url)); + String imgTitle = + Optional.ofNullable(imgNode.getAttributes().getNamedItem("alt")) + .map(e -> e.getTextContent().trim()) + .orElse(""); + String imgUrl = + Optional.ofNullable(imgNode.getAttributes().getNamedItem("src")) + .map(e -> e.getTextContent().trim()) + .orElse(""); + String defaultName = UUID.randomUUID().toString(); + + int index = imgUrl.lastIndexOf('/'); + if (index != -1 && index < imgUrl.length()) { + defaultName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1); + } + + return new HostService.NameUrl(imgTitle.isEmpty() ? defaultName : imgTitle, imgUrl); + } catch (Exception e) { + throw new HostException("Unexpected error occurred", e); + } + } +}