@Test public final void imgDownloadTest() throws IOException { final LinkInformation link = new LinkInformation(); link.setUrl("http: final String html = "<html><body>" + "<img src=\"http: context.checking(new Expectations() { { oneOf(urlUtils).getImgHeight("http: will(returnValue(BasicLinkImageParser.MIN_IMG_SIZE + 1)); oneOf(urlUtils).getImgWidth("http: will(returnValue(BasicLinkImageParser.MIN_IMG_SIZE + 1)); } }); sut.parseInformation(html, link, TEST_ACCOUNT); Assert.assertTrue(link.getImageUrls().contains("http: context.assertIsSatisfied(); }
public void parseInformation(final String htmlString, final LinkInformation link, final String inAccountId) { Set<String> imageUrls = new HashSet<String>(); Matcher isImagePattern = Pattern.compile(".*\\.(jpg|png|gif|jpeg)", Pattern.CASE_INSENSITIVE).matcher( link.getUrl()); if (isImagePattern.find()) { imageUrls.add(link.getUrl()); link.setImageUrls(imageUrls); return; } Pattern imagePattern = Pattern.compile("<img[^>]*>", Pattern.CASE_INSENSITIVE); Matcher imgMatcher = imagePattern.matcher(htmlString); long start = System.currentTimeMillis(); int largestImgScore = 0; while (imgMatcher.find()) { String img = imgMatcher.group(); Matcher widthHeightMatch = Pattern.compile("(width|height)\\s?=\\s?[\"\']?([0-5]?[0-9])[\"\'\\s]").matcher( img); if (!widthHeightMatch.find()) { Pattern imgSrcPattern = Pattern.compile("src\\s?=\\s?[\"\']?([^\"\'\\s]*)", Pattern.CASE_INSENSITIVE); Matcher srcMatcher = imgSrcPattern.matcher(img); if (srcMatcher.find()) { String imgUrl = srcMatcher.group(1); try { String linkUrl = link.getUrl(); if (imgUrl.startsWith("/")) { log.trace("Image URL started with '/'"); imgUrl = urlUtilities.getProtocol(linkUrl) + ": } else if (!(imgUrl.startsWith("http: { log.trace("No protocol found"); log.trace("Link URL: " + linkUrl); if (linkUrl.indexOf("/", "https: { imgUrl = linkUrl.substring(0, linkUrl.lastIndexOf("/") + 1) + imgUrl; } else { imgUrl = linkUrl + "/" + imgUrl; } log.trace("New Image Url: " + imgUrl); } int height = urlUtilities.getImgHeight(imgUrl, inAccountId); int width = urlUtilities.getImgWidth(imgUrl, inAccountId); if (height > MIN_IMG_SIZE && width > MIN_IMG_SIZE) { if ((height * width) > largestImgScore) { largestImgScore = height * width; link.setLargestImageUrl(imgUrl); } imageUrls.add(imgUrl); if (imageUrls.size() == maxResults) { break; } } } catch (MalformedURLException e) { log.error("Error parsing URL: " + e); } catch (IOException e) { log.error("Error parsing URL: " + e); } } } if (System.currentTimeMillis() - start > timeOut) { break; } } link.setImageUrls(imageUrls); }
BasicLinkImageParser implements HtmlLinkInformationParserStrategy { public void parseInformation(final String htmlString, final LinkInformation link, final String inAccountId) { Set<String> imageUrls = new HashSet<String>(); Matcher isImagePattern = Pattern.compile(".*\\.(jpg|png|gif|jpeg)", Pattern.CASE_INSENSITIVE).matcher( link.getUrl()); if (isImagePattern.find()) { imageUrls.add(link.getUrl()); link.setImageUrls(imageUrls); return; } Pattern imagePattern = Pattern.compile("<img[^>]*>", Pattern.CASE_INSENSITIVE); Matcher imgMatcher = imagePattern.matcher(htmlString); long start = System.currentTimeMillis(); int largestImgScore = 0; while (imgMatcher.find()) { String img = imgMatcher.group(); Matcher widthHeightMatch = Pattern.compile("(width|height)\\s?=\\s?[\"\']?([0-5]?[0-9])[\"\'\\s]").matcher( img); if (!widthHeightMatch.find()) { Pattern imgSrcPattern = Pattern.compile("src\\s?=\\s?[\"\']?([^\"\'\\s]*)", Pattern.CASE_INSENSITIVE); Matcher srcMatcher = imgSrcPattern.matcher(img); if (srcMatcher.find()) { String imgUrl = srcMatcher.group(1); try { String linkUrl = link.getUrl(); if (imgUrl.startsWith("/")) { log.trace("Image URL started with '/'"); imgUrl = urlUtilities.getProtocol(linkUrl) + ": } else if (!(imgUrl.startsWith("http: { log.trace("No protocol found"); log.trace("Link URL: " + linkUrl); if (linkUrl.indexOf("/", "https: { imgUrl = linkUrl.substring(0, linkUrl.lastIndexOf("/") + 1) + imgUrl; } else { imgUrl = linkUrl + "/" + imgUrl; } log.trace("New Image Url: " + imgUrl); } int height = urlUtilities.getImgHeight(imgUrl, inAccountId); int width = urlUtilities.getImgWidth(imgUrl, inAccountId); if (height > MIN_IMG_SIZE && width > MIN_IMG_SIZE) { if ((height * width) > largestImgScore) { largestImgScore = height * width; link.setLargestImageUrl(imgUrl); } imageUrls.add(imgUrl); if (imageUrls.size() == maxResults) { break; } } } catch (MalformedURLException e) { log.error("Error parsing URL: " + e); } catch (IOException e) { log.error("Error parsing URL: " + e); } } } if (System.currentTimeMillis() - start > timeOut) { break; } } link.setImageUrls(imageUrls); } }
BasicLinkImageParser implements HtmlLinkInformationParserStrategy { public void parseInformation(final String htmlString, final LinkInformation link, final String inAccountId) { Set<String> imageUrls = new HashSet<String>(); Matcher isImagePattern = Pattern.compile(".*\\.(jpg|png|gif|jpeg)", Pattern.CASE_INSENSITIVE).matcher( link.getUrl()); if (isImagePattern.find()) { imageUrls.add(link.getUrl()); link.setImageUrls(imageUrls); return; } Pattern imagePattern = Pattern.compile("<img[^>]*>", Pattern.CASE_INSENSITIVE); Matcher imgMatcher = imagePattern.matcher(htmlString); long start = System.currentTimeMillis(); int largestImgScore = 0; while (imgMatcher.find()) { String img = imgMatcher.group(); Matcher widthHeightMatch = Pattern.compile("(width|height)\\s?=\\s?[\"\']?([0-5]?[0-9])[\"\'\\s]").matcher( img); if (!widthHeightMatch.find()) { Pattern imgSrcPattern = Pattern.compile("src\\s?=\\s?[\"\']?([^\"\'\\s]*)", Pattern.CASE_INSENSITIVE); Matcher srcMatcher = imgSrcPattern.matcher(img); if (srcMatcher.find()) { String imgUrl = srcMatcher.group(1); try { String linkUrl = link.getUrl(); if (imgUrl.startsWith("/")) { log.trace("Image URL started with '/'"); imgUrl = urlUtilities.getProtocol(linkUrl) + ": } else if (!(imgUrl.startsWith("http: { log.trace("No protocol found"); log.trace("Link URL: " + linkUrl); if (linkUrl.indexOf("/", "https: { imgUrl = linkUrl.substring(0, linkUrl.lastIndexOf("/") + 1) + imgUrl; } else { imgUrl = linkUrl + "/" + imgUrl; } log.trace("New Image Url: " + imgUrl); } int height = urlUtilities.getImgHeight(imgUrl, inAccountId); int width = urlUtilities.getImgWidth(imgUrl, inAccountId); if (height > MIN_IMG_SIZE && width > MIN_IMG_SIZE) { if ((height * width) > largestImgScore) { largestImgScore = height * width; link.setLargestImageUrl(imgUrl); } imageUrls.add(imgUrl); if (imageUrls.size() == maxResults) { break; } } } catch (MalformedURLException e) { log.error("Error parsing URL: " + e); } catch (IOException e) { log.error("Error parsing URL: " + e); } } } if (System.currentTimeMillis() - start > timeOut) { break; } } link.setImageUrls(imageUrls); } BasicLinkImageParser(final ConnectionFacade inUrlUtilities, final int inMaxResults, final long inTimeOut); }
BasicLinkImageParser implements HtmlLinkInformationParserStrategy { public void parseInformation(final String htmlString, final LinkInformation link, final String inAccountId) { Set<String> imageUrls = new HashSet<String>(); Matcher isImagePattern = Pattern.compile(".*\\.(jpg|png|gif|jpeg)", Pattern.CASE_INSENSITIVE).matcher( link.getUrl()); if (isImagePattern.find()) { imageUrls.add(link.getUrl()); link.setImageUrls(imageUrls); return; } Pattern imagePattern = Pattern.compile("<img[^>]*>", Pattern.CASE_INSENSITIVE); Matcher imgMatcher = imagePattern.matcher(htmlString); long start = System.currentTimeMillis(); int largestImgScore = 0; while (imgMatcher.find()) { String img = imgMatcher.group(); Matcher widthHeightMatch = Pattern.compile("(width|height)\\s?=\\s?[\"\']?([0-5]?[0-9])[\"\'\\s]").matcher( img); if (!widthHeightMatch.find()) { Pattern imgSrcPattern = Pattern.compile("src\\s?=\\s?[\"\']?([^\"\'\\s]*)", Pattern.CASE_INSENSITIVE); Matcher srcMatcher = imgSrcPattern.matcher(img); if (srcMatcher.find()) { String imgUrl = srcMatcher.group(1); try { String linkUrl = link.getUrl(); if (imgUrl.startsWith("/")) { log.trace("Image URL started with '/'"); imgUrl = urlUtilities.getProtocol(linkUrl) + ": } else if (!(imgUrl.startsWith("http: { log.trace("No protocol found"); log.trace("Link URL: " + linkUrl); if (linkUrl.indexOf("/", "https: { imgUrl = linkUrl.substring(0, linkUrl.lastIndexOf("/") + 1) + imgUrl; } else { imgUrl = linkUrl + "/" + imgUrl; } log.trace("New Image Url: " + imgUrl); } int height = urlUtilities.getImgHeight(imgUrl, inAccountId); int width = urlUtilities.getImgWidth(imgUrl, inAccountId); if (height > MIN_IMG_SIZE && width > MIN_IMG_SIZE) { if ((height * width) > largestImgScore) { largestImgScore = height * width; link.setLargestImageUrl(imgUrl); } imageUrls.add(imgUrl); if (imageUrls.size() == maxResults) { break; } } } catch (MalformedURLException e) { log.error("Error parsing URL: " + e); } catch (IOException e) { log.error("Error parsing URL: " + e); } } } if (System.currentTimeMillis() - start > timeOut) { break; } } link.setImageUrls(imageUrls); } BasicLinkImageParser(final ConnectionFacade inUrlUtilities, final int inMaxResults, final long inTimeOut); void parseInformation(final String htmlString, final LinkInformation link, final String inAccountId); }
BasicLinkImageParser implements HtmlLinkInformationParserStrategy { public void parseInformation(final String htmlString, final LinkInformation link, final String inAccountId) { Set<String> imageUrls = new HashSet<String>(); Matcher isImagePattern = Pattern.compile(".*\\.(jpg|png|gif|jpeg)", Pattern.CASE_INSENSITIVE).matcher( link.getUrl()); if (isImagePattern.find()) { imageUrls.add(link.getUrl()); link.setImageUrls(imageUrls); return; } Pattern imagePattern = Pattern.compile("<img[^>]*>", Pattern.CASE_INSENSITIVE); Matcher imgMatcher = imagePattern.matcher(htmlString); long start = System.currentTimeMillis(); int largestImgScore = 0; while (imgMatcher.find()) { String img = imgMatcher.group(); Matcher widthHeightMatch = Pattern.compile("(width|height)\\s?=\\s?[\"\']?([0-5]?[0-9])[\"\'\\s]").matcher( img); if (!widthHeightMatch.find()) { Pattern imgSrcPattern = Pattern.compile("src\\s?=\\s?[\"\']?([^\"\'\\s]*)", Pattern.CASE_INSENSITIVE); Matcher srcMatcher = imgSrcPattern.matcher(img); if (srcMatcher.find()) { String imgUrl = srcMatcher.group(1); try { String linkUrl = link.getUrl(); if (imgUrl.startsWith("/")) { log.trace("Image URL started with '/'"); imgUrl = urlUtilities.getProtocol(linkUrl) + ": } else if (!(imgUrl.startsWith("http: { log.trace("No protocol found"); log.trace("Link URL: " + linkUrl); if (linkUrl.indexOf("/", "https: { imgUrl = linkUrl.substring(0, linkUrl.lastIndexOf("/") + 1) + imgUrl; } else { imgUrl = linkUrl + "/" + imgUrl; } log.trace("New Image Url: " + imgUrl); } int height = urlUtilities.getImgHeight(imgUrl, inAccountId); int width = urlUtilities.getImgWidth(imgUrl, inAccountId); if (height > MIN_IMG_SIZE && width > MIN_IMG_SIZE) { if ((height * width) > largestImgScore) { largestImgScore = height * width; link.setLargestImageUrl(imgUrl); } imageUrls.add(imgUrl); if (imageUrls.size() == maxResults) { break; } } } catch (MalformedURLException e) { log.error("Error parsing URL: " + e); } catch (IOException e) { log.error("Error parsing URL: " + e); } } } if (System.currentTimeMillis() - start > timeOut) { break; } } link.setImageUrls(imageUrls); } BasicLinkImageParser(final ConnectionFacade inUrlUtilities, final int inMaxResults, final long inTimeOut); void parseInformation(final String htmlString, final LinkInformation link, final String inAccountId); static final int MIN_IMG_SIZE; }