Some pages in amazon provide images which user can zoom on, like this page.
The question is "Where is the original hi-resolution image?".
You can easily find the explanation page for how to construct url for original hi-res image. (See this stack overflow page.)
In this post I will show you the code snippet for getting original hires imgae from ASIN code. Note!: This method might be unavailable if amazon change the implementation of zoom and hires image url construnction.
The question is "Where is the original hi-resolution image?".
You can easily find the explanation page for how to construct url for original hi-res image. (See this stack overflow page.)
In this post I will show you the code snippet for getting original hires imgae from ASIN code. Note!: This method might be unavailable if amazon change the implementation of zoom and hires image url construnction.
Code
Actually code is nothing special. Just find " DynAPI.addZoomViewer( ".// pattern for extracting image code used for retrieving original image. private static Pattern AMAZON_ZOOM_IMAGE_PATTERN = Pattern.compile("DynAPI\\.addZoomViewer\\(\".+/(.+?)\",\\s*(.+?),\\s*(.+?),\\s*(.+?),\\s*(.+?),\\s*(.+?),.*\\)"); public static String findOriginalZoomImageUrl(String asin, String country) { String url = getAmazonZoomImageWindowUrl(asin, country); try{ // tricky part: you should set an User-Agent manually. // If you use default Java User-Agent, you cannot get html which contains the image code. String content = getStringContentsFromURL(url, "utf-8"); Matcher matcher = AMAZON_ZOOM_IMAGE_PATTERN.matcher(content.trim()); if(matcher.find()) { String base = matcher.group(1); String v = matcher.group(6); return "http://z2-ec2.images-amazon.com/images/P/"+base+"._SX_SCRMZZZZZZZ_V"+v+"_.jpg"; } } catch(Exception e){} return null; } private static String getAmazonZoomImageWindowUrl(String asin, String country) { if("it".equalsIgnoreCase(country)) { return "http://www.amazon.it/gp/product/images/"+asin+"/"; } else if("jp".equalsIgnoreCase(country)) { return "http://www.amazon.jp/gp/product/images/"+asin+"/"; } return "http://www.amazon.com/gp/product/images/"+asin+"/"; } public static String getStringContentsFromURL(String u, String charset) throws URISyntaxException, IOException { URL url = new URL(u); HttpURLConnection connection = null; try{ connection = (HttpURLConnection)url.openConnection(); // !!IMPORTANT!! // please investigate the User-Agent yourself ;) // I impersonated a browser and it worked fine. connection.setRequestProperty("User-Agent","Please investigate yourself..."); connection.setRequestProperty("Accept","Please investigate yourself..."); connection.setRequestProperty("Accept-Language","Please investigate yourself..."); return toString(connection.getInputStream(), charset); } finally{ if(connection != null){ connection.disconnect(); } } } // trivial IO utility methods public static String toString(InputStream is, String charsetName) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(is, baos, _1K_BYTES); return baos.toString(charsetName); } public final static void copy(InputStream is, OutputStream os, int bufsize) throws IOException { copy(is, os, new byte[bufsize]); } public final static void copy(InputStream is, OutputStream os, byte[] buffer)throws IOException { try{ for (int bytes = 0 ;(bytes = is.read(buffer)) != -1; ) { os.write(buffer, 0, bytes); } os.flush(); }finally{ closeQuietly(is); } }
コメント