diff options
Diffstat (limited to 'difference.cpp')
-rw-r--r-- | difference.cpp | 127 |
1 files changed, 53 insertions, 74 deletions
diff --git a/difference.cpp b/difference.cpp index 6444efd..3d3bb18 100644 --- a/difference.cpp +++ b/difference.cpp | |||
@@ -36,6 +36,53 @@ std::string capitalize(std::string input) | |||
36 | return result; | 36 | return result; |
37 | } | 37 | } |
38 | 38 | ||
39 | bool downloadImage(std::string url, curl::curl_header headers, Magick::Blob& img, Magick::Image& pic) | ||
40 | { | ||
41 | // willyfogg.com is a thumbnail generator known to return 200 even if the target image no longer exists | ||
42 | if (url.find("willyfogg.com/thumb.php") != std::string::npos) | ||
43 | { | ||
44 | return false; | ||
45 | } | ||
46 | |||
47 | std::ostringstream imgbuf; | ||
48 | curl::curl_ios<std::ostringstream> imgios(imgbuf); | ||
49 | curl::curl_easy imghandle(imgios); | ||
50 | |||
51 | imghandle.add<CURLOPT_HTTPHEADER>(headers.get()); | ||
52 | imghandle.add<CURLOPT_URL>(url.c_str()); | ||
53 | imghandle.add<CURLOPT_CONNECTTIMEOUT>(30); | ||
54 | |||
55 | try { | ||
56 | imghandle.perform(); | ||
57 | } catch (curl::curl_easy_exception error) { | ||
58 | error.print_traceback(); | ||
59 | |||
60 | return false; | ||
61 | } | ||
62 | |||
63 | if (imghandle.get_info<CURLINFO_RESPONSE_CODE>().get() != 200) | ||
64 | { | ||
65 | return false; | ||
66 | } | ||
67 | |||
68 | if (std::string(imghandle.get_info<CURLINFO_CONTENT_TYPE>().get()).substr(0, 6) != "image/") | ||
69 | { | ||
70 | return false; | ||
71 | } | ||
72 | |||
73 | std::string imgstr = imgbuf.str(); | ||
74 | img = Magick::Blob(imgstr.c_str(), imgstr.length()); | ||
75 | pic.read(img); | ||
76 | if (pic.rows() == 0) | ||
77 | { | ||
78 | return false; | ||
79 | } | ||
80 | |||
81 | std::cout << url << std::endl; | ||
82 | |||
83 | return true; | ||
84 | } | ||
85 | |||
39 | int main(int argc, char** argv) | 86 | int main(int argc, char** argv) |
40 | { | 87 | { |
41 | srand(time(NULL)); | 88 | srand(time(NULL)); |
@@ -154,45 +201,11 @@ int main(int argc, char** argv) | |||
154 | int curind = 0; | 201 | int curind = 0; |
155 | for (; curind < lstvec.size(); curind++) | 202 | for (; curind < lstvec.size(); curind++) |
156 | { | 203 | { |
157 | std::ostringstream img1buf; | 204 | if (downloadImage(lstvec[curind], headers, img1, pic1)) |
158 | curl::curl_ios<std::ostringstream> img1ios(img1buf); | ||
159 | curl::curl_easy img1handle(img1ios); | ||
160 | std::string img1url = lstvec[curind]; | ||
161 | |||
162 | img1handle.add<CURLOPT_HTTPHEADER>(headers.get()); | ||
163 | img1handle.add<CURLOPT_URL>(img1url.c_str()); | ||
164 | img1handle.add<CURLOPT_CONNECTTIMEOUT>(30); | ||
165 | |||
166 | try { | ||
167 | img1handle.perform(); | ||
168 | } catch (curl::curl_easy_exception error) { | ||
169 | error.print_traceback(); | ||
170 | |||
171 | continue; | ||
172 | } | ||
173 | |||
174 | if (img1handle.get_info<CURLINFO_RESPONSE_CODE>().get() != 200) | ||
175 | { | 205 | { |
176 | continue; | 206 | success = true; |
207 | break; | ||
177 | } | 208 | } |
178 | |||
179 | if (std::string(img1handle.get_info<CURLINFO_CONTENT_TYPE>().get()).substr(0, 6) != "image/") | ||
180 | { | ||
181 | continue; | ||
182 | } | ||
183 | |||
184 | std::string img1str = img1buf.str(); | ||
185 | img1 = Magick::Blob(img1str.c_str(), img1str.length()); | ||
186 | pic1.read(img1); | ||
187 | if (pic1.rows() == 0) | ||
188 | { | ||
189 | continue; | ||
190 | } | ||
191 | |||
192 | std::cout << img1url << std::endl; | ||
193 | success = true; | ||
194 | |||
195 | break; | ||
196 | } | 209 | } |
197 | 210 | ||
198 | if (!success) | 211 | if (!success) |
@@ -205,45 +218,11 @@ int main(int argc, char** argv) | |||
205 | Magick::Image pic2; | 218 | Magick::Image pic2; |
206 | for (curind++; curind < lstvec.size(); curind++) | 219 | for (curind++; curind < lstvec.size(); curind++) |
207 | { | 220 | { |
208 | std::ostringstream img2buf; | 221 | if (downloadImage(lstvec[curind], headers, img2, pic2)) |
209 | curl::curl_ios<std::ostringstream> img2ios(img2buf); | ||
210 | curl::curl_easy img2handle(img2ios); | ||
211 | std::string img2url = lstvec[curind]; | ||
212 | |||
213 | img2handle.add<CURLOPT_HTTPHEADER>(headers.get()); | ||
214 | img2handle.add<CURLOPT_URL>(img2url.c_str()); | ||
215 | img2handle.add<CURLOPT_CONNECTTIMEOUT>(30); | ||
216 | |||
217 | try { | ||
218 | img2handle.perform(); | ||
219 | } catch (curl::curl_easy_exception error) { | ||
220 | error.print_traceback(); | ||
221 | |||
222 | continue; | ||
223 | } | ||
224 | |||
225 | if (img2handle.get_info<CURLINFO_RESPONSE_CODE>().get() != 200) | ||
226 | { | ||
227 | continue; | ||
228 | } | ||
229 | |||
230 | if (std::string(img2handle.get_info<CURLINFO_CONTENT_TYPE>().get()).substr(0, 6) != "image/") | ||
231 | { | ||
232 | continue; | ||
233 | } | ||
234 | |||
235 | std::string img2str = img2buf.str(); | ||
236 | img2 = Magick::Blob(img2str.c_str(), img2str.length()); | ||
237 | pic2.read(img2); | ||
238 | if (pic2.rows() == 0) | ||
239 | { | 222 | { |
240 | continue; | 223 | success = true; |
224 | break; | ||
241 | } | 225 | } |
242 | |||
243 | std::cout << img2url << std::endl; | ||
244 | success = true; | ||
245 | |||
246 | break; | ||
247 | } | 226 | } |
248 | 227 | ||
249 | if (!success) | 228 | if (!success) |