View Javadoc

1   /*
2    *  Jajuk
3    *  Copyright (C) 2007 The Jajuk Team
4    *
5    *  This program is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU General Public License
7    *  as published by the Free Software Foundation; either version 2
8    *  of the License, or any later version.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   *  $$Revision: 3228 $$
19   */
20  
21  package org.jajuk.util;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.UnsupportedEncodingException;
26  import java.net.HttpURLConnection;
27  import java.net.MalformedURLException;
28  import java.net.URL;
29  import java.net.URLEncoder;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.regex.Matcher;
33  import java.util.regex.Pattern;
34  
35  import android.util.Log;
36  
37  /***
38   * Manages network downloads
39   */
40  public class DownloadManager {
41  
42  	/***
43  	 * @param search
44  	 * @return a list of urls
45  	 * @throws IOException
46  	 */
47  	public static List<URL> getRemoteCoversList(String search)
48  			throws IOException {
49  		ArrayList<URL> alOut = new ArrayList<URL>(20); // URL list
50  		// check void searches
51  		if (search == null || search.trim().equals("")) {
52  			return alOut;
53  		}
54  		// Select cover size
55  		String sSearchUrl;
56  		try {
57  			sSearchUrl = "http://images.google.com/images?hl=en&q="
58  					+ URLEncoder.encode(search, "UTF-8")
59  					+ "&btnG=Search+Images" + "&gbv=2&imgsz=small";
60  		} catch (UnsupportedEncodingException e1) {
61  			throw new RuntimeException(e1);
62  		}
63  		String sRes = downloadHtml(new URL(sSearchUrl));
64  		if (sRes == null || sRes.length() == 0) {
65  			return alOut;
66  		}
67  		// Extract urls
68  		Pattern pattern = Pattern.compile("http://[^,<>]*(.jpg|.gif|.png)");
69  		// "http://[^,]*(.jpg|.gif|.png).*[0-9]* [xX] [0-9]*.*- [0-9]*");
70  		Matcher matcher = pattern.matcher(sRes);
71  		while (matcher.find()) {
72  			// Clean up URLS
73  			String sUrl = matcher.group().replaceAll("%2520", "%20");
74  			URL url;
75  			try {
76  				url = new URL(sUrl);
77  			} catch (MalformedURLException e) {
78  				Log.e(DownloadManager.class.getSimpleName(),
79  						"Failed to decode URL " + sUrl, e);
80  				continue;
81  			}
82  
83  			// Remove duplicates
84  			if (alOut.contains(url)) {
85  				continue;
86  			}
87  			// Ignore URLs related to Google
88  			if (url.toString().toLowerCase().matches(".*google.*")) {
89  				continue;
90  			}
91  			// Add the new url
92  			alOut.add(url);
93  		}
94  		return alOut;
95  	}
96  
97  	/***
98  	 * Returns the extension of file denoted by given URL.
99  	 * @param f the url
100 	 * @return the extension
101 	 */
102 	public static String getExtension(URL f) {
103 		String name = f.toExternalForm().toLowerCase();
104 		int i = name.lastIndexOf(".");
105 		if (i == -1)
106 			return "";
107 
108 		return name.substring(i + 1);
109 	}
110 
111 	/***
112 	 * Download the cover list
113 	 * 
114 	 * @param url
115 	 *            to download
116 	 * @param charset
117 	 *            the charset
118 	 * @return result as an array of bytes, null if a problem occured
119 	 * @throws IOException
120 	 */
121 	private static String downloadHtml(URL url, String charset)
122 			throws IOException {
123 		return readURL((HttpURLConnection) url.openConnection(), charset);
124 	}
125 
126 	private static String readURL(HttpURLConnection connection, String charset)
127 			throws IOException {
128 		connection.setConnectTimeout(60000);
129 		// Google needs this
130 		connection.addRequestProperty("Accept",
131 				"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*");
132 		connection.addRequestProperty("Accept-Language", "en-us");
133 		connection.addRequestProperty("User-Agent",
134 				"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
135 		connection.addRequestProperty("Connection", "Keep-Alive");
136 		StringBuilder builder = new StringBuilder();
137 		InputStream input = connection.getInputStream();
138 		byte[] array = new byte[1024];
139 		int read;
140 		while ((read = input.read(array)) >= 0) {
141 			builder.append(new String(array, 0, read, charset));
142 		}
143 		input.close();
144 		String out = builder.toString();
145 		return out;
146 	}
147 
148 	private static String downloadHtml(URL url) throws IOException {
149 		return downloadHtml(url, "UTF-8");
150 	}
151 
152 }