diff --git a/vripper-server/src/main/java/tn/mnlr/vripper/host/PixRouteHost.java b/vripper-server/src/main/java/tn/mnlr/vripper/host/PixRouteHost.java new file mode 100644 index 0000000..b8658ab --- /dev/null +++ b/vripper-server/src/main/java/tn/mnlr/vripper/host/PixRouteHost.java @@ -0,0 +1,58 @@ +package tn.mnlr.vripper.host; + +import org.apache.http.client.protocol.HttpClientContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.q.ImageFileData; + +@Service +public class PixRouteHost extends Host { + + private static final Logger logger = LoggerFactory.getLogger(PixRouteHost.class); + + private static final String host = "pixroute.com"; + private static final String IMG_XPATH = "//img[@id='imgpreview']"; + + @Override + public String getHost() { + return host; + } + + @Override + public String getLookup() { + return host; + } + + @Override + protected void setNameAndUrl(final String _url, final ImageFileData imageFileData, final HttpClientContext context) throws HostException { + + String url = _url.replace("http://", "https://"); + Document doc = getResponse(url, context).getDocument(); + + Node imgNode; + try { + logger.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("Failed to locate image"); + } + + try { + logger.debug(String.format("Resolving name and image url for %s", url)); + + imageFileData.setImageUrl(imgNode.getAttributes().getNamedItem("src").getTextContent().trim()); + imageFileData.setImageName(imgNode.getAttributes().getNamedItem("alt").getTextContent().trim()); + } catch (Exception e) { + throw new HostException("Unexpected error occurred", e); + } + } +}