[Polycasso-commit] SF.net SVN: polycasso:[52] trunk/polycasso/src/com/mebigfatguy/polycasso/ Random
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-11-25 05:56:23
|
Revision: 52
http://polycasso.svn.sourceforge.net/polycasso/?rev=52&view=rev
Author: dbrosius
Date: 2009-11-25 05:56:12 +0000 (Wed, 25 Nov 2009)
Log Message:
-----------
attempt to look for an image 5 times, in case of transient failures
Modified Paths:
--------------
trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java
Modified: trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java
===================================================================
--- trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java 2009-11-25 05:42:04 UTC (rev 51)
+++ trunk/polycasso/src/com/mebigfatguy/polycasso/RandomImageFinder.java 2009-11-25 05:56:12 UTC (rev 52)
@@ -47,6 +47,7 @@
private static final Pattern IMAGE_HTML_PATTERN = Pattern.compile("<div\\s+class=\"imageResult\"><a\\s+href=\"([^\"]*)\"");
private static final Pattern IMAGE_PATTERN = Pattern.compile("image_url=([^&]*)&");
private static final int NAMELEN = 3;
+ private static final int ATTEMPTS = 5;
/**
* private to avoid construction of this static access only class
@@ -63,14 +64,33 @@
*/
public static Image findImage() throws IOException {
- char[] ranName = new char[NAMELEN];
- Random ran = new Random(System.currentTimeMillis());
- for (int i = 0; i < NAMELEN; i++) {
- ranName[i] = (char)('A' + ran.nextInt(26));
+ for (int i = 0; i < ATTEMPTS; i++) {
+ try {
+ char[] ranName = new char[NAMELEN];
+ Random ran = new Random(System.currentTimeMillis());
+ for (int c = 0; c < NAMELEN; c++) {
+ ranName[c] = (char)('A' + ran.nextInt(26));
+ }
+
+ return findImageAt(MessageFormat.format(URL, new String(ranName)));
+ } catch (IOException ioe) {
+ }
}
- String html = new String(getUrlData(MessageFormat.format(URL, new String(ranName))), "UTF-8");
+ throw new IOException("Failed to find an inspiration to draw");
+ }
+
+ /**
+ * retrieve an image thru lycos image search
+ *
+ * @param url the url to look for the image
+ * @return the image
+ * @throws IOException if the site that is hosting the image is down, or non responsive
+ */
+ private static final Image findImageAt(String url) throws IOException {
+ String html = new String(getUrlData(url), "UTF-8");
+
List<String> images = new ArrayList<String>();
Matcher m = IMAGE_HTML_PATTERN.matcher(html);
@@ -82,9 +102,9 @@
m = IMAGE_PATTERN.matcher(html);
if (m.find()) {
- String url = URLDecoder.decode(m.group(1), "UTF-8");
+ String imageUrl = URLDecoder.decode(m.group(1), "UTF-8");
- return new ImageIcon(getUrlData(url)).getImage();
+ return new ImageIcon(getUrlData(imageUrl)).getImage();
}
throw new IOException("Failed to find image");
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|