+\f
+############################################################################
+#
+# Pick images from Imgur.
+#
+############################################################################
+
+my $imgur_base = 'http://imgur.com/search?qs=thumb&q_any=';
+
+sub pick_from_imgur($) {
+ my $timeout = shift;
+
+ my $words = random_words('%20');
+ $last_search = $imgur_base . $words;
+
+ $_ = $words;
+ s/%20/ /g;
+
+ print STDERR "\n\n" if ($verbose_load);
+ LOG ($verbose_load, "words: $_");
+ LOG ($verbose_load, "URL: $last_search");
+
+ $suppress_audit = 1;
+
+ my ( $base, $body ) = get_document ($last_search, undef, $timeout);
+ if (!$base || !$body) {
+ $body = undef;
+ return;
+ }
+
+ my @imgs = ($body =~ m@\bHREF=[\"\']([^\'\"<>]*/gallery/[^\'\"<>]+)@gsi);
+ return () unless @imgs;
+
+ my $n = @imgs;
+ my $i = int(rand($n));
+ my $page = $imgs[$i];
+ $page =~ s/[?&].*$//s;
+ $page = "http://imgur.com$page" if ($page =~ m@^/@s);
+
+ my ($id) = ($page =~ m@([^/?&]+)$@s);
+ my $img = "http://i.imgur.com/$id.jpg";
+
+ LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
+
+ return ($page, $img);
+}
+
+\f
+############################################################################
+#
+# Pick images from Tumblr.
+#
+############################################################################
+
+my $tumblr_base = 'https://www.tumblr.com/search/';
+
+sub pick_from_tumblr($) {
+ my $timeout = shift;
+
+ # Tumblr doesn't have an "or" search, which means our vocabulary is
+ # a bit too extensive to work well...
+
+ my $words = random_word();
+ $last_search = $tumblr_base . $words;
+
+ print STDERR "\n\n" if ($verbose_load);
+ LOG ($verbose_load, "words: $words");
+ LOG ($verbose_load, "URL: $last_search");
+
+ $suppress_audit = 1;
+
+ my ( $base, $body ) = get_document ($last_search, undef, $timeout);
+ if (!$base || !$body) {
+ $body = undef;
+ return;
+ }
+
+ my @imgs0 = ($body =~ m@<IMG\b([^<>]*)>@gsi);
+ return () unless @imgs0;
+ my @imgs;
+ foreach my $img (@imgs0) {
+ my ($src) = ($img =~ m@\bsrc=[\"\'](.*?)[\"\']@si);
+ my ($href) = ($img =~ m@\bdata-pin-url=[\"\'](.*?)[\"\']@si);
+ next unless ($src && $href);
+ next if ($src =~ m/^data:/s);
+ foreach ($src, $href) { $_ = "http://www.tumblr.com$_" if (m@^/@s); }
+ push @imgs, [$href, $src];
+ }
+ return () unless @imgs;
+
+ my $n = @imgs;
+ my $i = int(rand($n));
+ my $page = $imgs[$i]->[0];
+ my $img = $imgs[$i]->[1];
+
+ LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
+
+ return ($page, $img);
+}
+