1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
50
51 if (search == null || search.trim().equals("")) {
52 return alOut;
53 }
54
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
68 Pattern pattern = Pattern.compile("http://[^,<>]*(.jpg|.gif|.png)");
69
70 Matcher matcher = pattern.matcher(sRes);
71 while (matcher.find()) {
72
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
84 if (alOut.contains(url)) {
85 continue;
86 }
87
88 if (url.toString().toLowerCase().matches(".*google.*")) {
89 continue;
90 }
91
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
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 }