3 # webcollage, Copyright (c) 1999-2003 by Jamie Zawinski <jwz@jwz.org>
4 # This program decorates the screen with random images from the web.
5 # One satisfied customer described it as "a nonstop pop culture brainbath."
7 # Permission to use, copy, modify, distribute, and sell this software and its
8 # documentation for any purpose is hereby granted without fee, provided that
9 # the above copyright notice appear in all copies and that both that
10 # copyright notice and this permission notice appear in supporting
11 # documentation. No representations are made about the suitability of this
12 # software for any purpose. It is provided "as is" without express or
16 # To run this as a display mode with xscreensaver, add this to `programs':
18 # default-n: webcollage -root \n\
19 # default-n: webcollage -root -filter 'vidwhacker -stdin -stdout' \n\
22 # If you have the "driftnet" program installed, webcollage can display a
23 # collage of images sniffed off your local ethernet, instead of pulled out
24 # of search engines: in that way, your screensaver can display the images
25 # that your co-workers are downloading!
27 # Driftnet is available here: http://www.ex-parrot.com/~chris/driftnet/
30 # default-n: webcollage -root -driftnet \n\
32 # Driftnet is the Unix implementation of the MacOS "EtherPEG" program.
38 # We can't "use diagnostics" here, because that library malfunctions if
39 # you signal and catch alarms: it says "Uncaught exception from user code"
40 # and exits, even though I damned well AM catching it!
47 use Fcntl ':flock'; # import LOCK_* constants
48 use POSIX qw(strftime);
50 use bytes; # Larry can take Unicode and shove it up his ass sideways.
51 # Perl 5.8.0 causes us to start getting incomprehensible
52 # errors about UTF-8 all over the place without this.
55 my $progname = $0; $progname =~ s@.*/@@g;
56 my $version = q{ $Revision: 1.104 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
57 my $copyright = "WebCollage $version, Copyright (c) 1999-2002" .
58 " Jamie Zawinski <jwz\@jwz.org>\n" .
59 " http://www.jwz.org/xscreensaver/\n";
63 my @search_methods = ( 77, "altavista", \&pick_from_alta_vista_random_link,
64 14, "yahoorand", \&pick_from_yahoo_random_link,
65 9, "yahoonews", \&pick_from_yahoo_news_text,
67 # Alta Vista has a new "random link" URL now.
68 # They added it specifically to better support webcollage!
69 # That was super cool of them. This is how we used to do
72 # 0, "avimages", \&pick_from_alta_vista_images,
73 # 0, "avtext", \&pick_from_alta_vista_text,
75 # Google asked (nicely) for me to stop searching them.
76 # I asked them to add a "random link" url. They said
77 # "that would be easy, we'll think about it" and then
78 # never wrote back. Booo Google! Booooo!
80 # 0, "googlenums", \&pick_from_google_image_numbers,
81 # 0, "googleimgs", \&pick_from_google_images,
83 # I suspect Hotbot is actually the same search engine
86 # 0, "hotbot", \&pick_from_hotbot_text,
88 # Eh, Lycos sucks anyway.
89 # 0, "lycos", \&pick_from_lycos_text,
92 # programs we can use to write to the root window (tried in ascending order.)
94 my @root_displayers = (
95 "chbg -once -xscreensaver -max_size 100",
96 "xv -root -quit -viewonly +noresetroot -quick24 -rmode 5" .
97 " -rfg black -rbg black",
98 "xli -quiet -onroot -center -border black",
99 "xloadimage -quiet -onroot -center -border black",
101 # this lame program wasn't built with vroot.h:
102 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
106 # Some sites need cookies to work properly. These are they.
109 "www.altavista.com" => "AV_ALL=1", # request uncensored searches
110 "web.altavista.com" => "AV_ALL=1",
112 # log in as "cipherpunk"
113 "www.nytimes.com" => 'NYT-S=18cHMIlJOn2Y1bu5xvEG3Ufuk6E1oJ.' .
114 'FMxWaQV0igaB5Yi/Q/guDnLeoL.pe7i1oakSb' .
115 '/VqfdUdb2Uo27Vzt1jmPn3cpYRlTw9',
119 # If this is set, it's a helper program to use for pasting images together:
120 # this is a lot faster and more efficient than using PPM pipelines, which is
121 # what we do if this program doesn't exist. (We check for "webcollage-helper"
122 # on $PATH at startup, and set this variable appropriately.)
124 my $webcollage_helper = undef;
127 # If we have the webcollage-helper program, then it will paste the images
128 # together with transparency! 0.0 is invisible, 1.0 is totally opaque.
133 # Some sites have managed to poison the search engines. These are they.
134 # (We auto-detect sites that have poisoned the search engines via excessive
135 # keywords or dictionary words, but these are ones that slip through
138 # This can contain full host names, or 2 or 3 component domains.
141 "die.net" => 1, # 'l33t h4ck3r d00dz.
142 "genforum.genealogy.com" => 1, # Cluttering avtext with human names.
143 "rootsweb.com" => 1, # Cluttering avtext with human names.
144 "akamai.net" => 1, # Lots of sites have their images on Akamai.
145 "akamaitech.net" => 1, # But those are pretty much all banners.
146 # Since Akamai is super-expensive, let's
147 # go out on a limb and assume that all of
148 # their customers are rich-and-boring.
149 "bartleby.com" => 1, # Dictionary, cluttering avtext.
150 "encyclopedia.com" => 1, # Dictionary, cluttering avtext.
151 "onlinedictionary.datasegment.com" => 1, # Dictionary, cluttering avtext.
152 "hotlinkpics.com" => 1, # Porn site that has poisoned avimages
153 # (I don't see how they did it, though!)
154 "alwayshotels.com" => 1, # Poisoned Lycos pretty heavily.
155 "nextag.com" => 1, # Poisoned Alta Vista real good.
159 # When verbosity is turned on, we warn about sites that we seem to be hitting
160 # a lot: usually this means some new poisoner has made it into the search
161 # engines. But sometimes, the warning is just because that site has a lot
162 # of stuff on it. So these are the sites that are immune to the "frequent
163 # site" diagnostic message.
165 my %warningless_sites = (
166 "home.earthlink.net" => 1, # Lots of home pages here.
167 "www.geocities.com" => 1,
168 "www.angelfire.com" => 1,
169 "members.aol.com" => 1,
171 "yimg.com" => 1, # This is where dailynews.yahoo.com stores
172 "eimg.com" => 1, # its images, so pick_from_yahoo_news_text()
173 # hits this every time.
175 "driftnet" => 1, # builtin...
179 ##############################################################################
181 # Various global flags set by command line parameters, or computed
183 ##############################################################################
186 my $current_state = "???"; # for diagnostics
189 my $image_succeeded = -1;
190 my $suppress_audit = 0;
192 my $verbose_imgmap = 0; # print out rectangles and URLs only (stdout)
193 my $verbose_warnings = 0; # print out warnings when things go wrong
194 my $verbose_load = 0; # diagnostics about loading of URLs
195 my $verbose_filter = 0; # diagnostics about page selection/rejection
196 my $verbose_net = 0; # diagnostics about network I/O
197 my $verbose_pbm = 0; # diagnostics about PBM pipelines
198 my $verbose_http = 0; # diagnostics about all HTTP activity
199 my $verbose_exec = 0; # diagnostics about executing programs
201 my $report_performance_interval = 60 * 15; # print some stats every 15 minutes
203 my $http_proxy = undef;
204 my $http_timeout = 30;
205 my $cvt_timeout = 10;
211 my $min_gif_area = (120 * 120);
217 my @pids_to_kill = (); # forked pids we should kill when we exit, if any.
219 my $driftnet_magic = 'driftnet';
220 my $driftnet_dir = undef;
221 my $default_driftnet_cmd = "driftnet -a -m 100";
226 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
227 "arrhenius", "arteriole", "blanket", "brainchild",
228 "burdensome", "carnival", "cherub", "chord", "clever",
229 "dedicate", "dilogarithm", "dolan", "dryden",
233 ##############################################################################
237 ##############################################################################
239 # returns three values: the HTTP response line; the document headers;
240 # and the document body.
243 my ( $url, $referer, $timeout ) = @_;
245 if (!defined($timeout)) { $timeout = $http_timeout; }
246 if ($timeout > $http_timeout) { $timeout = $http_timeout; }
249 LOG (($verbose_net || $verbose_load), "timed out for $url");
253 LOG ($verbose_net, "get_document_1 $url " . ($referer ? $referer : ""));
255 if (! ($url =~ m@^http://@i)) {
256 LOG ($verbose_net, "not an HTTP URL: $url");
260 my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
261 $path = "" unless $path;
263 my ($them,$port) = split(/:/, $serverstring);
264 $port = 80 unless $port;
269 $serverstring = $http_proxy if $http_proxy;
270 ($them2,$port2) = split(/:/, $serverstring);
271 $port2 = 80 unless $port2;
274 my ($remote, $iaddr, $paddr, $proto, $line);
276 if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
278 LOG (($verbose_net || $verbose_load), "unrecognised port in $url");
281 $iaddr = inet_aton($remote);
283 LOG (($verbose_net || $verbose_load), "host not found: $remote");
286 $paddr = sockaddr_in($port2, $iaddr);
294 local $SIG{ALRM} = sub {
295 LOG (($verbose_net || $verbose_load), "timed out ($timeout) for $url");
300 $proto = getprotobyname('tcp');
301 if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
302 LOG (($verbose_net || $verbose_load), "socket: $!");
305 if (!connect(S, $paddr)) {
306 LOG (($verbose_net || $verbose_load), "connect($serverstring): $!");
310 select(S); $| = 1; select(STDOUT);
312 my $cookie = $cookies{$them};
314 my $user_agent = "$progname/$version";
316 if ($url =~ m@^http://www\.altavista\.com/@ ||
317 $url =~ m@^http://random\.yahoo\.com/@) {
318 # block this, you turkeys.
319 $user_agent = "Mozilla/4.76 [en] (X11; U; Linux 2.2.16-22 i686; Nav)";
322 my $hdrs = "GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
324 "User-Agent: $user_agent\r\n";
326 $hdrs .= "Referer: $referer\r\n";
329 my @cc = split(/\r?\n/, $cookie);
330 $hdrs .= "Cookie: " . join('; ', @cc) . "\r\n";
334 foreach (split('\r?\n', $hdrs)) {
335 LOG ($verbose_http, " ==> $_");
338 my $http = <S> || "";
340 # Kludge: the Yahoo Random Link is now returning as its first
341 # line "Status: 301" instead of "HTTP/1.0 301 Found". Fix it...
343 $http =~ s@^Status:\s+(\d+)\b@HTTP/1.0 $1@i;
347 LOG ($verbose_http, " <== $_");
353 LOG ($verbose_http, " <== $_");
355 if (m@^Set-cookie:\s*([^;\r\n]+)@i) {
356 set_cookie($them, $1)
367 " <== [ body ]: $lines lines, " . length($body) . " bytes");
372 LOG (($verbose_net || $verbose_load), "null response: $url");
376 return ( $http, $head, $body );
378 die if ($@ && $@ ne "alarm\n"); # propagate errors
393 # returns two values: the document headers; and the document body.
394 # if the given URL did a redirect, returns the redirected-to document.
397 my ( $url, $referer, $timeout ) = @_;
400 if (defined($referer) && $referer eq $driftnet_magic) {
401 return get_driftnet_file ($url);
406 my $max_loop_count = 4;
409 if (defined($timeout) && $timeout <= 0) {
410 LOG (($verbose_net || $verbose_load), "timed out for $url");
415 my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
417 if (defined ($timeout)) {
419 my $elapsed = $now - $start;
420 $timeout -= $elapsed;
424 return () unless $http; # error message already printed
426 $http =~ s/[\r\n]+$//s;
428 if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
431 my ( $location ) = m@^location:[ \t]*(.*)$@im;
433 $location =~ s/[\r\n]$//;
435 LOG ($verbose_net, "redirect from $url to $location");
440 $referer =~ m@^(http://[^/]+)@i;
442 } elsif (! ($url =~ m@^[a-z]+:@i)) {
444 s@[^/]+$@@g if m@^http://[^/]+/@i;
445 $_ .= "/" if m@^http://[^/]+$@i;
450 LOG ($verbose_net, "no Location with \"$http\"");
451 return ( $url, $body );
454 if ($loop_count++ > $max_loop_count) {
456 "too many redirects ($max_loop_count) from $orig_url");
461 } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
463 LOG (($verbose_net || $verbose_load), "failed: $1 ($url)");
465 # http errors -- return nothing.
471 LOG (($verbose_net || $verbose_load), "document contains no data: $url");
477 return ( $url, $body );
483 # If we already have a cookie defined for this site, and the site is trying
484 # to overwrite that very same cookie, let it do so. This is because nytimes
485 # expires its cookies - it lets you upgrade to a new cookie without logging
486 # in again, but you have to present the old cookie to get the new cookie.
487 # So, by doing this, the built-in cypherpunks cookie will never go "stale".
490 my ($host, $cookie) = @_;
491 my $oc = $cookies{$host};
494 my ($oc_name, $oc_value) = m@^([^= \t\r\n]+)=(.*)$@;
496 my ($nc_name, $nc_value) = m@^([^= \t\r\n]+)=(.*)$@;
498 if ($oc_name eq $nc_name &&
499 $oc_value ne $nc_value) {
500 $cookies{$host} = $cookie;
501 LOG ($verbose_net, "overwrote ${host}'s $oc_name cookie");
506 ############################################################################
508 # Extracting image URLs from HTML
510 ############################################################################
512 # given a URL and the body text at that URL, selects and returns a random
513 # image from it. returns () if no suitable images found.
515 sub pick_image_from_body {
516 my ( $url, $body ) = @_;
521 # if there's at least one slash after the host, take off the last
523 if ( m@^http://[^/]+/@io ) {
524 $base =~ s@[^/]+$@@go;
527 # if there are no slashes after the host at all, put one on the end.
528 if ( m@^http://[^/]+$@io ) {
534 # strip out newlines, compress whitespace
541 # There are certain web sites that list huge numbers of dictionary
542 # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
543 # Porn sites tend not to be reputable!)
545 # I do not want webcollage to filter on content: I want it to select
546 # randomly from the set of images on the web. All the logic here for
547 # rejecting some images is really a set of heuristics for rejecting
548 # images that are not really images: for rejecting *text* that is in
549 # GIF/JPEG form. I don't want text, I want pictures, and I want the
550 # content of the pictures to be randomly selected from among all the
553 # So, filtering out "dirty" pictures by looking for "dirty" keywords
554 # would be wrong: dirty pictures exist, like it or not, so webcollage
555 # should be able to select them.
557 # However, picking a random URL is a hard thing to do. The mechanism I'm
558 # using is to search for a selection of random words. This is not
559 # perfect, but works ok most of the time. The way it breaks down is when
560 # some URLs get precedence because their pages list *every word* as
561 # related -- those URLs come up more often than others.
563 # So, after we've retrieved a URL, if it has too many keywords, reject
564 # it. We reject it not on the basis of what those keywords are, but on
565 # the basis that by having so many, the page has gotten an unfair
566 # advantage against our randomizer.
569 foreach my $trip (@tripwire_words) {
570 $trip_count++ if m/$trip/i;
573 if ($trip_count >= $#tripwire_words - 2) {
574 LOG (($verbose_filter || $verbose_load),
575 "there is probably a dictionary in \"$url\": rejecting.");
576 $rejected_urls{$url} = -1;
586 foreach (split(/ *</)) {
589 # Likewise, reject any web pages that have a KEYWORDS meta tag
592 if (m/name ?= ?\"?keywords\"?/i &&
593 m/content ?= ?\"([^\"]+)\"/) {
596 LOG (($verbose_filter || $verbose_load),
597 "excessive keywords ($L bytes) in $url: rejecting.");
598 $rejected_urls{$url} = $L;
603 LOG ($verbose_filter, " keywords ($L bytes) in $url (ok)");
607 } elsif ( m/^(img|a) .*(src|href) ?= ?\"? ?(.*?)[ >\"]/io ) {
609 my $was_inline = (! ( "$1" eq "a" || "$1" eq "A" ));
611 my ( $width ) = m/width ?=[ \"]*(\d+)/oi;
612 my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
617 ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
619 } elsif ( ! m@^[^/:?]+:@ ) {
622 1 while (s@/[^/]+/\.\./@/@g);
626 if ( ! m@^http://@io ) {
631 if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg)$@io ) {
635 # skip really short or really narrow images
636 if ( $width && $width < $min_width) {
637 if (!$height) { $height = "?"; }
638 LOG ($verbose_filter, " skip narrow image $_ (${width}x$height)");
642 if ( $height && $height < $min_height) {
643 if (!$width) { $width = "?"; }
644 LOG ($verbose_filter, " skip short image $_ (${width}x$height)");
648 # skip images with ratios that make them look like banners.
649 if ($min_ratio && $width && $height &&
650 ($width * $min_ratio ) > $height) {
651 if (!$height) { $height = "?"; }
652 LOG ($verbose_filter, " skip bad ratio $_ (${width}x$height)");
656 # skip GIFs with a small number of pixels -- those usually suck.
657 if ($width && $height &&
659 ($width * $height) < $min_gif_area) {
660 LOG ($verbose_filter, " skip small GIF $_ (${width}x$height)");
667 if ($unique_urls{$url}) {
668 LOG ($verbose_filter, " skip duplicate image $_");
672 LOG ($verbose_filter,
674 ($width && $height ? " (${width}x${height})" : "") .
675 ($was_inline ? " (inline)" : ""));
677 $urls[++$#urls] = $url;
678 $unique_urls{$url}++;
680 # jpegs are preferable to gifs.
682 if ( ! m@[.]gif$@io ) {
683 $urls[++$#urls] = $url;
686 # pointers to images are preferable to inlined images.
687 if ( ! $was_inline ) {
688 $urls[++$#urls] = $url;
689 $urls[++$#urls] = $url;
694 my $fsp = ($body =~ m@<frameset@i);
699 @urls = depoison (@urls);
702 LOG ($verbose_load, "no images on $base" . ($fsp ? " (frameset)" : ""));
706 # pick a random element of the table
707 my $i = int(rand($#urls+1));
710 LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
717 ############################################################################
719 # Subroutines for getting pages and images out of search engines
721 ############################################################################
724 sub pick_dictionary {
725 my @dicts = ("/usr/dict/words",
726 "/usr/share/dict/words",
727 "/usr/share/lib/dict/words");
728 foreach my $f (@dicts) {
734 error ("$dicts[0] does not exist") unless defined($wordlist);
737 # returns a random word from the dictionary
741 if (open (IN, "<$wordlist")) {
742 my $size = (stat(IN))[7];
743 my $pos = rand $size;
744 if (seek (IN, $pos, 0)) {
745 $word = <IN>; # toss partial line
746 $word = <IN>; # keep next line
755 return 0 if (!$word);
757 $word =~ s/^[ \t\n\r]+//;
758 $word =~ s/[ \t\n\r]+$//;
763 $word =~ s/ally$/al/;
764 $word =~ s/izes$/ize/;
765 $word =~ tr/A-Z/a-z/;
767 if ( $word =~ s/[ \t\n\r]/\+/g ) { # convert intra-word spaces to "+".
768 $word = "\%22$word\%22"; # And put quotes (%22) around it.
776 my $sep = ($or_p ? "%20OR%20" : "%20");
777 return (random_word . $sep .
787 $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
794 $s =~ s/%([a-z0-9]{2})/chr(hex($1))/ige;
799 # Loads the given URL (a search on some search engine) and returns:
800 # - the total number of hits the search engine claimed it had;
801 # - a list of URLs from the page that the search engine returned;
802 # Note that this list contains all kinds of internal search engine
803 # junk URLs too -- caller must prune them.
805 sub pick_from_search_engine {
806 my ( $timeout, $search_url, $words ) = @_;
811 print STDERR "\n\n" if ($verbose_load);
813 LOG ($verbose_load, "words: $_");
814 LOG ($verbose_load, "URL: $search_url");
816 $last_search = $search_url; # for warnings
819 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
820 if (defined ($timeout)) {
821 $timeout -= (time - $start);
824 LOG (($verbose_net || $verbose_load),
825 "timed out (late) for $search_url");
831 return () if (! $body);
836 my $search_count = "?";
837 if ($body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
839 } elsif ($body =~ m@<NOBR>((\d{1,3})(,\d{3})*) @i) {
841 } elsif ($body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
843 } elsif ($body =~ m@found about ((\d{1,3})(,\d{3})*|\d+) results@) {
845 } elsif ($body =~ m@\b\d+ - \d+ of (\d+)\b@i) { # avimages
847 } elsif ($body =~ m@About ((\d{1,3})(,\d{3})*) images@i) { # avimages
849 } elsif ($body =~ m@We found ((\d{1,3})(,\d{3})*|\d+) results@i) { # *vista
851 } elsif ($body =~ m@ of about <B>((\d{1,3})(,\d{3})*)<@i) { # googleimages
853 } elsif ($body =~ m@<B>((\d{1,3})(,\d{3})*)</B> Web sites were found@i) {
854 $search_count = $1; # lycos
855 } elsif ($body =~ m@WEB.*?RESULTS.*?\b((\d{1,3})(,\d{3})*)\b.*?Matches@i) {
856 $search_count = $1; # hotbot
857 } elsif ($body =~ m@no photos were found containing@i) { # avimages
859 } elsif ($body =~ m@found no document matching@i) { # avtext
862 1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
864 # if ($search_count eq "?" || $search_count eq "0") {
866 # my $file = "/tmp/wc.html";
867 # open(OUT, ">$file") || error ("writing $file: $!");
870 # print STDERR blurb() . "###### wrote $file\n";
874 my $length = length($body);
883 foreach (split(/\n/)) {
885 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
888 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
889 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
891 if ( $rejected_urls{$u} ) {
892 LOG ($verbose_filter, " pre-rejecting candidate: $u");
896 LOG ($verbose_http, " HREF: $u");
898 $subpages[++$#subpages] = $u;
901 if ( $#subpages < 0 ) {
902 LOG ($verbose_filter,
903 "found nothing on $base ($length bytes, $href_count links).");
907 LOG ($verbose_filter, "" . $#subpages+1 . " links on $search_url");
909 return ($search_count, @subpages);
917 my ($h) = m@^http://([^/: \t\r\n]+)@i;
919 next unless defined($h);
921 if ($poisoners{$h}) {
922 LOG (($verbose_filter), " rejecting poisoner: $_");
925 if ($h =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
927 LOG (($verbose_filter), " rejecting poisoner: $_");
930 if ($h =~ m@([^.]+\.[^.]+)$@ &&
932 LOG (($verbose_filter), " rejecting poisoner: $_");
942 # given a list of URLs, picks one at random; loads it; and returns a
943 # random image from it.
944 # returns the url of the page loaded; the url of the image chosen;
945 # and a debugging description string.
947 sub pick_image_from_pages {
948 my ($base, $total_hit_count, $unfiltered_link_count, $timeout, @pages) = @_;
950 $total_hit_count = "?" unless defined($total_hit_count);
952 @pages = depoison (@pages);
954 "" . ($#pages+1) . " candidates of $unfiltered_link_count links" .
955 " ($total_hit_count total)");
957 return () if ($#pages < 0);
959 my $i = int(rand($#pages+1));
960 my $page = $pages[$i];
962 LOG ($verbose_load, "picked page $page");
966 my ( $base2, $body2 ) = get_document ($page, $base, $timeout);
968 if (!$base2 || !$body2) {
973 my $img = pick_image_from_body ($base2, $body2);
977 return ($base2, $img);
984 ############################################################################
986 # Pick images from random pages returned by the Yahoo Random Link
988 ############################################################################
991 my $yahoo_random_link = "http://random.yahoo.com/fast/ryl";
994 # Picks a random page; picks a random image on that page;
995 # returns two URLs: the page containing the image, and the image.
996 # Returns () if nothing found this time.
998 sub pick_from_yahoo_random_link {
999 my ( $timeout ) = @_;
1001 print STDERR "\n\n" if ($verbose_load);
1002 LOG ($verbose_load, "URL: $yahoo_random_link");
1004 $last_search = $yahoo_random_link; # for warnings
1006 $suppress_audit = 1;
1008 my ( $base, $body ) = get_document ($yahoo_random_link, undef, $timeout);
1009 if (!$base || !$body) {
1014 LOG ($verbose_load, "redirected to: $base");
1016 my $img = pick_image_from_body ($base, $body);
1020 return ($base, $img);
1027 ############################################################################
1029 # Pick images from random pages returned by the Alta Vista Random Link
1031 ############################################################################
1034 my $alta_vista_random_link = "http://www.altavista.com/image/randomlink";
1037 # Picks a random page; picks a random image on that page;
1038 # returns two URLs: the page containing the image, and the image.
1039 # Returns () if nothing found this time.
1041 sub pick_from_alta_vista_random_link {
1042 my ( $timeout ) = @_;
1044 print STDERR "\n\n" if ($verbose_load);
1045 LOG ($verbose_load, "URL: $alta_vista_random_link");
1047 $last_search = $alta_vista_random_link; # for warnings
1049 $suppress_audit = 1;
1051 my ( $base, $body ) = get_document ($alta_vista_random_link,
1053 if (!$base || !$body) {
1058 LOG ($verbose_load, "redirected to: $base");
1060 my $img = pick_image_from_body ($base, $body);
1064 return ($base, $img);
1071 ############################################################################
1073 # Pick images by feeding random words into Alta Vista Image Search
1075 ############################################################################
1078 my $alta_vista_images_url = "http://www.altavista.com/image/results" .
1079 "?ipht=1" . # photos
1080 "&igrph=1" . # graphics
1083 "&micat=1" . # no partner sites
1084 "&sc=on" . # "site collapse"
1088 sub pick_from_alta_vista_images {
1089 my ( $timeout ) = @_;
1091 my $words = random_words(0);
1092 my $page = (int(rand(9)) + 1);
1093 my $search_url = $alta_vista_images_url . $words;
1096 $search_url .= "&pgno=" . $page; # page number
1097 $search_url .= "&stq=" . (($page-1) * 12); # first hit result on page
1100 my ($search_hit_count, @subpages) =
1101 pick_from_search_engine ($timeout, $search_url, $words);
1103 my @candidates = ();
1104 foreach my $u (@subpages) {
1106 # avtext is encoding their URLs now.
1107 next unless ($u =~ m@^/r.*\&r=([^&]+).*@);
1108 $u = url_unquote($1);
1110 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1111 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
1112 next if ($u =~ m@[/.]doubleclick\.net\b@i); # you cretins
1113 next if ($u =~ m@[/.]clicktomarket\.com\b@i); # more cretins
1115 next if ($u =~ m@[/.]viewimages\.com\b@i); # stacked deck
1116 next if ($u =~ m@[/.]gettyimages\.com\b@i);
1118 LOG ($verbose_filter, " candidate: $u");
1119 push @candidates, $u;
1122 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1123 $timeout, @candidates);
1128 ############################################################################
1130 # Pick images by feeding random words into Google Image Search.
1131 # By Charles Gales <gales@us.ibm.com>
1133 ############################################################################
1136 my $google_images_url = "http://images.google.com/images" .
1137 "?site=images" . # photos
1138 "&btnG=Search" . # graphics
1139 "&safe=off" . # no screening
1144 sub pick_from_google_images {
1145 my ( $timeout ) = @_;
1147 my $words = random_word; # only one word for Google
1148 my $page = (int(rand(9)) + 1);
1149 my $num = 20; # 20 images per page
1150 my $search_url = $google_images_url . $words;
1153 $search_url .= "&start=" . $page*$num; # page number
1154 $search_url .= "&num=" . $num; #images per page
1157 my ($search_hit_count, @subpages) =
1158 pick_from_search_engine ($timeout, $search_url, $words);
1160 my @candidates = ();
1161 foreach my $u (@subpages) {
1162 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1163 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1165 if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1167 LOG ($verbose_filter, " candidate: $urlf");
1168 push @candidates, $urlf;
1172 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1173 $timeout, @candidates);
1178 ############################################################################
1180 # Pick images by feeding random *numbers* into Google Image Search.
1181 # By jwz, suggested by from Ian O'Donnell.
1183 ############################################################################
1187 sub pick_from_google_image_numbers {
1188 my ( $timeout ) = @_;
1191 my $number = int(rand($max));
1193 $number = sprintf("%04d", $number)
1196 my $words = "$number";
1197 my $page = (int(rand(40)) + 1);
1198 my $num = 20; # 20 images per page
1199 my $search_url = $google_images_url . $words;
1202 $search_url .= "&start=" . $page*$num; # page number
1203 $search_url .= "&num=" . $num; #images per page
1206 my ($search_hit_count, @subpages) =
1207 pick_from_search_engine ($timeout, $search_url, $words);
1209 my @candidates = ();
1211 foreach my $u (@subpages) {
1212 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1213 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1215 if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1217 my $img = "http://$1";
1219 LOG ($verbose_filter, " candidate: $ref");
1220 push @candidates, $img;
1221 $referers{$img} = $ref;
1225 @candidates = depoison (@candidates);
1226 return () if ($#candidates < 0);
1227 my $i = int(rand($#candidates+1));
1228 my $img = $candidates[$i];
1229 my $ref = $referers{$img};
1231 LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1232 return ($ref, $img);
1237 ############################################################################
1239 # Pick images by feeding random words into Alta Vista Text Search
1241 ############################################################################
1244 my $alta_vista_url = "http://www.altavista.com/web/results" .
1248 "&sc=on" . # "site collapse"
1253 sub pick_from_alta_vista_text {
1254 my ( $timeout ) = @_;
1256 my $words = random_words(0);
1257 my $page = (int(rand(9)) + 1);
1258 my $search_url = $alta_vista_url . $words;
1261 $search_url .= "&pgno=" . $page;
1262 $search_url .= "&stq=" . (($page-1) * 10);
1265 my ($search_hit_count, @subpages) =
1266 pick_from_search_engine ($timeout, $search_url, $words);
1268 my @candidates = ();
1269 foreach my $u (@subpages) {
1271 # Those altavista fuckers are playing really nasty redirection games
1272 # these days: the filter your clicks through their site, but use
1273 # onMouseOver to make it look like they're not! Well, it makes it
1274 # easier for us to identify search results...
1276 next unless ($u =~ m@^/r.*\&r=([^&]+).*@);
1277 $u = url_unquote($1);
1279 LOG ($verbose_filter, " candidate: $u");
1280 push @candidates, $u;
1283 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1284 $timeout, @candidates);
1289 ############################################################################
1291 # Pick images by feeding random words into Hotbot
1293 ############################################################################
1295 my $hotbot_search_url =("http://hotbot.lycos.com/default.asp" .
1297 "&descriptiontype=0" .
1304 "&cobrand=undefined" .
1307 sub pick_from_hotbot_text {
1308 my ( $timeout ) = @_;
1310 # lycos seems to always give us back dictionaries and word lists if
1311 # we search for more than one word...
1313 my $words = random_word();
1315 my $start = int(rand(8)) * 10 + 1;
1316 my $search_url = $hotbot_search_url . $words . "&first=$start&page=more";
1318 my ($search_hit_count, @subpages) =
1319 pick_from_search_engine ($timeout, $search_url, $words);
1321 my @candidates = ();
1322 foreach my $u (@subpages) {
1324 # Hotbot plays redirection games too
1325 next unless ($u =~ m@/director.asp\?.*\btarget=([^&]+)@);
1326 $u = url_decode($1);
1328 LOG ($verbose_filter, " candidate: $u");
1329 push @candidates, $u;
1332 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1333 $timeout, @candidates);
1338 ############################################################################
1340 # Pick images by feeding random words into Lycos
1342 ############################################################################
1344 my $lycos_search_url = "http://search.lycos.com/default.asp" .
1350 sub pick_from_lycos_text {
1351 my ( $timeout ) = @_;
1353 # lycos seems to always give us back dictionaries and word lists if
1354 # we search for more than one word...
1356 my $words = random_word();
1358 my $start = int(rand(8)) * 10 + 1;
1359 my $search_url = $lycos_search_url . $words . "&first=$start&page=more";
1361 my ($search_hit_count, @subpages) =
1362 pick_from_search_engine ($timeout, $search_url, $words);
1364 my @candidates = ();
1365 foreach my $u (@subpages) {
1367 # Lycos plays redirection games.
1368 next unless ($u =~ m@^http://click.lycos.com/director.asp
1373 $u = url_decode($1);
1375 LOG ($verbose_filter, " candidate: $u");
1376 push @candidates, $u;
1379 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1380 $timeout, @candidates);
1385 ############################################################################
1387 # Pick images by feeding random words into news.yahoo.com
1389 ############################################################################
1391 my $yahoo_news_url = "http://search.news.yahoo.com/search/news" .
1394 "&s=-%24s%2C-date" .
1402 sub pick_from_yahoo_news_text {
1403 my ( $timeout ) = @_;
1405 my $words = random_words(0);
1406 my $search_url = $yahoo_news_url . $words;
1408 my ($search_hit_count, @subpages) =
1409 pick_from_search_engine ($timeout, $search_url, $words);
1411 my @candidates = ();
1412 foreach my $u (@subpages) {
1413 # only accept URLs on Yahoo's news site
1414 next unless ($u =~ m@^http://dailynews\.yahoo\.com/@i ||
1415 $u =~ m@^http://story\.news\.yahoo\.com/@i);
1417 LOG ($verbose_filter, " candidate: $u");
1418 push @candidates, $u;
1421 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1422 $timeout, @candidates);
1428 ############################################################################
1430 # Pick images by waiting for driftnet to populate a temp dir with files.
1431 # Requires driftnet version 0.1.5 or later.
1432 # (Driftnet is a program by Chris Lightfoot that sniffs your local ethernet
1433 # for images being downloaded by others.)
1434 # Driftnet/webcollage integration by jwz.
1436 ############################################################################
1439 sub pick_from_driftnet {
1440 my ( $timeout ) = @_;
1442 my $id = $driftnet_magic;
1443 my $dir = $driftnet_dir;
1447 error ("\$driftnet_dir unset?") unless ($dir);
1450 error ("$dir unreadable") unless (-d "$dir/.");
1452 $timeout = $http_timeout unless ($timeout);
1455 while ($now = time, $now < $start + $timeout) {
1457 opendir (DIR, $dir) || error ("$dir: $!");
1458 while (my $file = readdir(DIR)) {
1459 next if ($file =~ m/^\./);
1460 $file = "$dir/$file";
1462 LOG ($verbose_load, "picked file $file ($id)");
1463 return ($id, $file);
1467 LOG (($verbose_net || $verbose_load), "timed out for $id");
1472 sub get_driftnet_file {
1475 error ("\$driftnet_dir unset?") unless ($driftnet_dir);
1477 my $id = $driftnet_magic;
1478 my $re = qr/$driftnet_dir/;
1479 error ("$id: $file not in $driftnet_dir?")
1480 unless ($file =~ m@^$re@o);
1483 open (IN, $file) || error ("$id: $file: $!");
1485 while (<IN>) { $body .= $_; }
1486 close IN || error ("$id: $file: $!");
1487 unlink ($file) || error ("$id: $file: rm: $!");
1488 return ($id, $body);
1492 sub spawn_driftnet {
1495 # make a directory to use.
1497 my $tmp = $ENV{TEMPDIR} || "/tmp";
1498 $driftnet_dir = sprintf ("$tmp/driftcollage-%08x", rand(0xffffffff));
1499 LOG ($verbose_exec, "mkdir $driftnet_dir");
1500 last if mkdir ($driftnet_dir, 0700);
1503 if (! ($cmd =~ m/\s/)) {
1504 # if the command didn't have any arguments in it, then it must be just
1505 # a pointer to the executable. Append the default args to it.
1506 my $dargs = $default_driftnet_cmd;
1507 $dargs =~ s/^[^\s]+//;
1511 # point the driftnet command at our newly-minted private directory.
1513 $cmd .= " -d $driftnet_dir";
1514 $cmd .= ">/dev/null" unless ($verbose_exec);
1517 if ($pid < 0) { error ("fork: $!\n"); }
1520 push @pids_to_kill, $pid;
1521 LOG ($verbose_exec, "forked for \"$cmd\"");
1524 nontrapping_system ($cmd) || error ("exec: $!");
1527 # wait a bit, then make sure the process actually started up.
1530 error ("pid $pid failed to start \"$cmd\"")
1531 unless (1 == kill (0, $pid));
1535 ############################################################################
1537 # Pick a random image in a random way
1539 ############################################################################
1542 # Picks a random image on a random page, and returns two URLs:
1543 # the page containing the image, and the image.
1544 # Returns () if nothing found this time.
1548 my ( $timeout ) = @_;
1550 $current_state = "select";
1551 $load_method = "none";
1553 my $n = int(rand(100));
1556 my @rest = @search_methods;
1559 my $pct = shift @rest;
1560 my $name = shift @rest;
1561 my $tfn = shift @rest;
1563 if ($total > $n && !defined($fn)) {
1565 $current_state = $name;
1566 $load_method = $current_state;
1570 if ($total != 100) {
1571 error ("internal error: \@search_methods totals to $total%!");
1574 record_attempt ($current_state);
1575 return $fn->($timeout);
1580 ############################################################################
1582 # Statistics and logging
1584 ############################################################################
1587 return strftime ("%H:%M:%S: ", localtime);
1591 return "$progname: " . timestr() . "$current_state: ";
1596 print STDERR blurb() . "$err\n";
1608 my $head = "$progname: DEBUG: ";
1609 foreach (split (/\n/, $lastlog)) {
1610 print STDERR "$head$_\n";
1616 my ($print, $msg) = @_;
1617 my $blurb = timestr() . "$current_state: ";
1618 $lastlog .= "$blurb$msg\n";
1619 print STDERR "$progname: $blurb$msg\n" if $print;
1624 my %stats_successes;
1627 my $last_state = undef;
1628 sub record_attempt {
1632 record_failure($last_state) unless ($image_succeeded > 0);
1634 $last_state = $name;
1637 report_performance();
1640 $image_succeeded = 0;
1641 $suppress_audit = 0;
1644 sub record_success {
1645 my ($name, $url, $base) = @_;
1646 if (defined($stats_successes{$name})) {
1647 $stats_successes{$name}++;
1649 $stats_successes{$name} = 1;
1652 stop_timer ($name, 1);
1653 my $o = $current_state;
1654 $current_state = $name;
1655 save_recent_url ($url, $base);
1656 $current_state = $o;
1657 $image_succeeded = 1;
1662 sub record_failure {
1665 return if $image_succeeded;
1667 stop_timer ($name, 0);
1668 if ($verbose_load && !$verbose_exec) {
1670 if ($suppress_audit) {
1671 print STDERR "$progname: " . timestr() . "(audit log suppressed)\n";
1675 my $o = $current_state;
1676 $current_state = "DEBUG";
1678 my $line = "#" x 78;
1679 print STDERR "\n\n\n";
1680 print STDERR ("#" x 78) . "\n";
1681 print STDERR blurb() . "failed to get an image. Full audit log:\n";
1684 print STDERR ("-" x 78) . "\n";
1685 print STDERR "\n\n";
1687 $current_state = $o;
1689 $image_succeeded = 0;
1696 my $i = $stats_successes{$name};
1697 my $j = $stats_attempts{$name};
1700 return "" . ($j ? int($i * 100 / $j) : "0") . "%";
1704 my $current_start_time = 0;
1708 $current_start_time = time;
1710 if (defined($stats_attempts{$name})) {
1711 $stats_attempts{$name}++;
1713 $stats_attempts{$name} = 1;
1715 if (!defined($stats_elapsed{$name})) {
1716 $stats_elapsed{$name} = 0;
1721 my ($name, $success) = @_;
1722 $stats_elapsed{$name} += time - $current_start_time;
1726 my $last_report_time = 0;
1727 sub report_performance {
1729 return unless $verbose_warnings;
1732 return unless ($now >= $last_report_time + $report_performance_interval);
1733 my $ot = $last_report_time;
1734 $last_report_time = $now;
1736 return if ($ot == 0);
1738 my $blurb = "$progname: " . timestr();
1741 print STDERR "${blurb}Current standings:\n";
1743 foreach my $name (sort keys (%stats_attempts)) {
1744 my $try = $stats_attempts{$name};
1745 my $suc = $stats_successes{$name} || 0;
1746 my $pct = int($suc * 100 / $try);
1747 my $secs = $stats_elapsed{$name};
1748 my $secs_link = int($secs / $try);
1749 print STDERR sprintf ("$blurb %-12s %4s (%d/%d);\t %2d secs/link\n",
1750 "$name:", "$pct%", $suc, $try, $secs_link);
1756 my $max_recent_images = 400;
1757 my $max_recent_sites = 20;
1758 my @recent_images = ();
1759 my @recent_sites = ();
1761 sub save_recent_url {
1762 my ($url, $base) = @_;
1764 return unless ($verbose_warnings);
1767 my ($site) = m@^http://([^ \t\n\r/:]+)@;
1768 return unless defined ($site);
1770 if ($base eq $driftnet_magic) {
1771 $site = $driftnet_magic;
1772 @recent_images = ();
1776 foreach (@recent_images) {
1778 print STDERR blurb() . "WARNING: recently-duplicated image: $url" .
1779 " (on $base via $last_search)\n";
1785 # suppress "duplicate site" warning via %warningless_sites.
1787 if ($warningless_sites{$site}) {
1789 } elsif ($site =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
1790 $warningless_sites{$1}) {
1792 } elsif ($site =~ m@([^.]+\.[^.]+)$@ &&
1793 $warningless_sites{$1}) {
1798 foreach (@recent_sites) {
1800 print STDERR blurb() . "WARNING: recently-duplicated site: $site" .
1801 " ($url on $base via $last_search)\n";
1807 push @recent_images, $url;
1808 push @recent_sites, $site;
1809 shift @recent_images if ($#recent_images >= $max_recent_images);
1810 shift @recent_sites if ($#recent_sites >= $max_recent_sites);
1815 ##############################################################################
1819 ##############################################################################
1826 s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
1831 # Given the raw body of a GIF document, returns the dimensions of the image.
1835 my $type = substr($body, 0, 6);
1837 return () unless ($type =~ /GIF8[7,9]a/);
1838 $s = substr ($body, 6, 10);
1839 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
1840 return (($b<<8|$a), ($d<<8|$c));
1843 # Given the raw body of a JPEG document, returns the dimensions of the image.
1848 my $L = length($body);
1850 my $c1 = substr($body, $i, 1); $i++;
1851 my $c2 = substr($body, $i, 1); $i++;
1852 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
1855 while (ord($ch) != 0xDA && $i < $L) {
1856 # Find next marker, beginning with 0xFF.
1857 while (ord($ch) != 0xFF) {
1858 return () if (length($body) <= $i);
1859 $ch = substr($body, $i, 1); $i++;
1861 # markers can be padded with any number of 0xFF.
1862 while (ord($ch) == 0xFF) {
1863 return () if (length($body) <= $i);
1864 $ch = substr($body, $i, 1); $i++;
1867 # $ch contains the value of the marker.
1868 my $marker = ord($ch);
1870 if (($marker >= 0xC0) &&
1871 ($marker <= 0xCF) &&
1872 ($marker != 0xC4) &&
1873 ($marker != 0xCC)) { # it's a SOFn marker
1875 return () if (length($body) <= $i);
1876 my $s = substr($body, $i, 4); $i += 4;
1877 my ($a,$b,$c,$d) = unpack("C"x4, $s);
1878 return (($c<<8|$d), ($a<<8|$b));
1881 # We must skip variables, since FFs in variable names aren't
1882 # valid JPEG markers.
1883 return () if (length($body) <= $i);
1884 my $s = substr($body, $i, 2); $i += 2;
1885 my ($c1, $c2) = unpack ("C"x2, $s);
1886 my $length = ($c1 << 8) | $c2;
1887 return () if ($length < 2);
1894 # Given the raw body of a GIF or JPEG document, returns the dimensions of
1899 my ($w, $h) = gif_size ($body);
1900 if ($w && $h) { return ($w, $h); }
1901 return jpeg_size ($body);
1905 # returns the full path of the named program, or undef.
1909 foreach (split (/:/, $ENV{PATH})) {
1910 if (-x "$_/$prog") {
1918 # Like rand(), but chooses numbers with a bell curve distribution.
1921 $_ = 1.0 unless defined($_);
1923 return (rand($_) + rand($_) + rand($_));
1927 sub signal_cleanup {
1929 print STDERR blurb() . (defined($sig)
1930 ? "caught signal $sig."
1937 if (@pids_to_kill) {
1938 print STDERR blurb() . "killing: " . join(' ', @pids_to_kill) . "\n";
1939 kill ('TERM', @pids_to_kill);
1945 ##############################################################################
1947 # Generating a list of urls only
1949 ##############################################################################
1951 sub url_only_output {
1953 my ($base, $img) = pick_image;
1957 print "$img $base\n";
1962 ##############################################################################
1964 # Running as an xscreensaver module
1966 ##############################################################################
1968 my $image_ppm = ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp") . "/webcollage." . $$;
1969 my $image_tmp1 = $image_ppm . "-1";
1970 my $image_tmp2 = $image_ppm . "-2";
1972 my $filter_cmd = undef;
1973 my $post_filter_cmd = undef;
1974 my $background = undef;
1976 my $img_width; # size of the image being generated.
1982 unlink $image_ppm, $image_tmp1, $image_tmp2;
1986 # Like system, but prints status about exit codes, and kills this process
1987 # with whatever signal killed the sub-process, if any.
1989 sub nontrapping_system {
1993 s/\"[^\"]+\"/\"...\"/g;
1995 LOG ($verbose_exec, "executing \"$_\"");
2000 LOG ($verbose_exec, "subproc exited normally.");
2001 } elsif (($rc & 0xff) == 0) {
2003 LOG ($verbose_exec, "subproc exited with status $rc.");
2006 LOG ($verbose_exec, "subproc dumped core.");
2009 LOG ($verbose_exec, "subproc died with signal $rc.");
2010 # die that way ourselves.
2018 # Given the URL of a GIF or JPEG image, and the body of that image, writes a
2019 # PPM to the given output file. Returns the width/height of the image if
2023 my ($url, $body, $output) = @_;
2024 my ($cmd, $cmd2, $w, $h);
2026 if ((@_ = gif_size ($body))) {
2029 } elsif ((@_ = jpeg_size ($body))) {
2033 LOG (($verbose_pbm || $verbose_load),
2034 "not a GIF or JPG" .
2035 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
2036 ? " (looks like HTML)" : "") .
2038 $suppress_audit = 1;
2042 $cmd2 = "exec $cmd"; # yes, this really is necessary. if we don't
2043 # do this, the process doesn't die properly.
2044 if (!$verbose_pbm) {
2046 # We get a "giftopnm: got a 'Application Extension' extension"
2047 # warning any time it's an animgif.
2049 # Note that "giftopnm: EOF / read error on image data" is not
2050 # always a fatal error -- sometimes the image looks fine anyway.
2052 $cmd2 .= " 2>/dev/null";
2055 # There exist corrupted GIF and JPEG files that can make giftopnm and
2056 # djpeg lose their minds and go into a loop. So this gives those programs
2057 # a small timeout -- if they don't complete in time, kill them.
2063 local $SIG{ALRM} = sub {
2065 "timed out ($cvt_timeout) for $cmd on \"$url\" in pid $pid");
2066 kill ('TERM', $pid) if ($pid);
2071 if (($pid = open(PIPE, "| $cmd2 > $output"))) {
2078 LOG ($verbose_exec, "awaiting $pid");
2080 LOG ($verbose_exec, "$pid completed");
2082 my $size = (stat($output))[7];
2083 $size = -1 unless defined($size);
2085 LOG ($verbose_pbm, "$cmd on ${w}x$h \"$url\" failed ($size bytes)");
2089 LOG ($verbose_pbm, "created ${w}x$h $output ($cmd)");
2092 print STDERR blurb() . "$cmd failed: $!\n";
2096 die if ($@ && $@ ne "alarm\n"); # propagate errors
2109 sub pick_root_displayer {
2112 foreach my $cmd (@root_displayers) {
2114 my ($name) = m/^([^ ]+)/;
2115 push @names, "\"$name\"";
2116 LOG ($verbose_exec, "looking for $name...");
2117 foreach my $dir (split (/:/, $ENV{PATH})) {
2118 LOG ($verbose_exec, " checking $dir/$name");
2119 return $cmd if (-x "$dir/$name");
2123 $names[$#names] = "or " . $names[$#names];
2124 error "none of: " . join (", ", @names) . " were found on \$PATH.";
2128 my $ppm_to_root_window_cmd = undef;
2131 sub x_or_pbm_output {
2133 # Check for our helper program, to see whether we need to use PPM pipelines.
2135 $_ = "webcollage-helper";
2136 if (defined ($webcollage_helper) || which ($_)) {
2137 $webcollage_helper = $_ unless (defined($webcollage_helper));
2138 LOG ($verbose_pbm, "found \"$webcollage_helper\"");
2139 $webcollage_helper .= " -v";
2141 LOG (($verbose_pbm || $verbose_load), "no $_ program");
2144 # make sure the various programs we execute exist, right up front.
2146 my @progs = ("ppmmake"); # always need this one
2148 if (!defined($webcollage_helper)) {
2149 # Only need these others if we don't have the helper.
2150 @progs = (@progs, "giftopnm", "djpeg", "pnmpaste", "pnmscale", "pnmcut");
2154 which ($_) || error "$_ not found on \$PATH.";
2157 # find a root-window displayer program.
2159 $ppm_to_root_window_cmd = pick_root_displayer();
2161 if (!$img_width || !$img_height) {
2163 which ($_) || error "$_ not found on \$PATH.";
2165 ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
2166 if (!defined($img_height)) {
2167 error "xdpyinfo failed.";
2171 my $bgcolor = "#000000";
2172 my $bgimage = undef;
2175 if ($background =~ m/^\#[0-9a-f]+$/i) {
2176 $bgcolor = $background;
2178 } elsif (-r $background) {
2179 $bgimage = $background;
2181 } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
2182 error "not a color or readable file: $background";
2185 # default to assuming it's a color
2186 $bgcolor = $background;
2190 # Create the sold-colored base image.
2192 $_ = "ppmmake '$bgcolor' $img_width $img_height";
2193 LOG ($verbose_pbm, "creating base image: $_");
2194 nontrapping_system "$_ > $image_ppm";
2196 # Paste the default background image in the middle of it.
2203 open(IMG, "<$bgimage") || error "couldn't open $bgimage: $!";
2205 while (<IMG>) { $body .= $_; }
2208 if ((@_ = gif_size ($body))) {
2210 $cmd = "giftopnm |";
2212 } elsif ((@_ = jpeg_size ($body))) {
2216 } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
2222 error "$bgimage is not a GIF, JPEG, or PPM.";
2225 my $x = int (($img_width - $iw) / 2);
2226 my $y = int (($img_height - $ih) / 2);
2228 "pasting $bgimage (${iw}x$ih) into base image at $x,$y");
2230 $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
2231 open (IMG, "| $cmd") || error "running $cmd: $!";
2235 LOG ($verbose_exec, "subproc exited normally.");
2236 rename ($image_tmp1, $image_ppm) ||
2237 error "renaming $image_tmp1 to $image_ppm: $!";
2243 my ($base, $img) = pick_image();
2244 my $source = $current_state;
2245 $current_state = "loadimage";
2247 my ($headers, $body) = get_document ($img, $base);
2249 paste_image ($base, $img, $body, $source);
2253 $current_state = "idle";
2254 $load_method = "none";
2256 unlink $image_tmp1, $image_tmp2;
2262 my ($base, $img, $body, $source) = @_;
2264 $current_state = "paste";
2266 $suppress_audit = 0;
2268 LOG ($verbose_pbm, "got $img (" . length($body) . ")");
2272 # If we are using the webcollage-helper, then we do not need to convert this
2273 # image to a PPM. But, if we're using a filter command, we still must, since
2274 # that's what the filters expect (webcollage-helper can read PPMs, so that's
2277 if (defined ($webcollage_helper) &&
2278 !defined ($filter_cmd)) {
2280 ($iw, $ih) = image_size ($body);
2282 LOG (($verbose_pbm || $verbose_load),
2283 "not a GIF or JPG" .
2284 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
2285 ? " (looks like HTML)" : "") .
2287 $suppress_audit = 1;
2293 open (OUT, ">$image_tmp1") || error ("writing $image_tmp1: $!");
2294 print OUT $body || error ("writing $image_tmp1: $!");
2295 close OUT || error ("writing $image_tmp1: $!");
2298 ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
2301 LOG ($verbose_pbm, "unable to make PBM from $img");
2306 record_success ($load_method, $img, $base);
2309 my $ow = $iw; # used only for error messages
2312 # don't just tack this onto the front of the pipeline -- we want it to
2313 # be able to change the size of the input image.
2316 LOG ($verbose_pbm, "running $filter_cmd");
2318 my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
2320 LOG(($verbose_pbm || $verbose_load), "failed command: \"$filter_cmd\"");
2321 LOG(($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2324 rename ($image_tmp2, $image_tmp1);
2326 # re-get the width/height in case the filter resized it.
2328 open(IMG, "<$image_tmp1") || return 0;
2331 ($iw, $ih) = m/^(\d+) (\d+)$/;
2333 return 0 unless ($iw && $ih);
2336 my $target_w = $img_width;
2337 my $target_h = $img_height;
2343 # Usually scale the image to fit on the screen -- but sometimes scale it
2344 # to fit on half or a quarter of the screen. Note that we don't merely
2345 # scale it to fit, we instead cut it in half until it fits -- that should
2346 # give a wider distribution of sizes.
2348 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2349 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2351 if ($iw > $target_w || $ih > $target_h) {
2352 while ($iw > $target_w ||
2357 if ($iw <= 10 || $ih <= 10) {
2358 LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus.");
2362 LOG ($verbose_pbm, "scaling to ${iw}x$ih");
2364 $cmd .= " | pnmscale -xsize $iw -ysize $ih";
2368 my $src = $image_tmp1;
2370 my $crop_x = 0; # the sub-rectangle of the image
2371 my $crop_y = 0; # that we will actually paste.
2375 # The chance that we will randomly crop out a section of an image starts
2376 # out fairly low, but goes up for images that are very large, or images
2377 # that have ratios that make them look like banners (we try to avoid
2378 # banner images entirely, but they slip through when the IMG tags didn't
2379 # have WIDTH and HEIGHT specified.)
2381 my $crop_chance = 0.2;
2382 if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
2383 $crop_chance += 0.2;
2385 if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
2386 $crop_chance += 0.2;
2388 if ($min_ratio && ($iw * $min_ratio) > $ih) {
2389 $crop_chance += 0.7;
2392 if ($crop_chance > 0.1) {
2393 LOG ($verbose_pbm, "crop chance: $crop_chance");
2396 if (rand() < $crop_chance) {
2401 if ($crop_w > $min_width) {
2402 # if it's a banner, select the width linearly.
2403 # otherwise, select a bell.
2404 my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
2407 $crop_w = $min_width + int ($r * ($crop_w - $min_width));
2408 $crop_x = int (rand() * ($ow - $crop_w));
2410 if ($crop_h > $min_height) {
2411 # height always selects as a bell.
2412 $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
2413 $crop_y = int (rand() * ($oh - $crop_h));
2416 if ($crop_x != 0 || $crop_y != 0 ||
2417 $crop_w != $iw || $crop_h != $ih) {
2419 "randomly cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2423 # Where the image should logically land -- this might be negative.
2425 my $x = int((rand() * ($img_width + $crop_w/2)) - $crop_w*3/4);
2426 my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
2428 # if we have chosen to paste the image outside of the rectangle of the
2429 # screen, then we need to crop it.
2433 $x + $crop_w > $img_width ||
2434 $y + $crop_h > $img_height) {
2437 "cropping for effective paste of ${crop_w}x$crop_h \@ $x,$y");
2439 if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
2440 if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
2442 if ($x + $crop_w >= $img_width) { $crop_w = $img_width - $x - 1; }
2443 if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
2446 # If any cropping needs to happen, add pnmcut.
2448 if ($crop_x != 0 || $crop_y != 0 ||
2449 $crop_w != $iw || $crop_h != $ih) {
2452 $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
2453 LOG ($verbose_pbm, "cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2456 LOG ($verbose_pbm, "pasting ${iw}x$ih \@ $x,$y in $image_ppm");
2458 $cmd .= " | pnmpaste - $x $y $image_ppm";
2460 $cmd =~ s@^ *\| *@@;
2462 if (defined ($webcollage_helper)) {
2463 $cmd = "$webcollage_helper $image_tmp1 $image_ppm " .
2464 "$scale $opacity " .
2465 "$crop_x $crop_y $x $y " .
2470 # use a PPM pipeline
2472 $_ .= " < $image_tmp1 > $image_tmp2";
2476 $_ = "($_) 2>&1 | sed s'/^/" . blurb() . "/'";
2478 $_ .= " 2> /dev/null";
2481 my $rc = nontrapping_system ($_);
2483 if (defined ($webcollage_helper) && -z $image_ppm) {
2484 LOG (1, "failed command: \"$cmd\"");
2485 print STDERR "\naudit log:\n\n\n";
2486 print STDERR ("#" x 78) . "\n";
2487 print STDERR blurb() . "$image_ppm has zero size\n";
2489 print STDERR "\n\n";
2494 LOG (($verbose_pbm || $verbose_load), "failed command: \"$cmd\"");
2495 LOG (($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2499 if (!defined ($webcollage_helper)) {
2500 rename ($image_tmp2, $image_ppm) || return;
2503 my $target = "$image_ppm";
2505 # don't just tack this onto the end of the pipeline -- we don't want it
2506 # to end up in $image_ppm, because we don't want the results to be
2509 if ($post_filter_cmd) {
2513 $target = $image_tmp1;
2514 if (!defined ($webcollage_helper)) {
2515 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2517 # Blah, my scripts need the JPEG data, but some other folks need
2518 # the PPM data -- what to do? Ignore the problem, that's what!
2519 # $cmd = "djpeg < $image_ppm | ($post_filter_cmd) > $target";
2520 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2523 $rc = nontrapping_system ($cmd);
2525 LOG ($verbose_pbm, "filter failed: \"$post_filter_cmd\"\n");
2530 if (!$no_output_p) {
2531 my $tsize = (stat($target))[7];
2533 $cmd = "$ppm_to_root_window_cmd $target";
2535 # xv seems to hate being killed. it tends to forget to clean
2536 # up after itself, and leaves windows around and colors allocated.
2537 # I had this same problem with vidwhacker, and I'm not entirely
2538 # sure what I did to fix it. But, let's try this: launch xv
2539 # in the background, so that killing this process doesn't kill it.
2540 # it will die of its own accord soon enough. So this means we
2541 # start pumping bits to the root window in parallel with starting
2542 # the next network retrieval, which is probably a better thing
2547 $rc = nontrapping_system ($cmd);
2550 LOG (($verbose_pbm || $verbose_load), "display failed: \"$cmd\"");
2555 LOG ($verbose_pbm, "$target size is $tsize");
2559 $source .= "-" . stats_of($source);
2560 print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n"
2561 if ($verbose_imgmap);
2571 $SIG{HUP} = \&signal_cleanup;
2572 $SIG{INT} = \&signal_cleanup;
2573 $SIG{QUIT} = \&signal_cleanup;
2574 $SIG{ABRT} = \&signal_cleanup;
2575 $SIG{KILL} = \&signal_cleanup;
2576 $SIG{TERM} = \&signal_cleanup;
2578 # Need this so that if giftopnm dies, we don't die.
2579 $SIG{PIPE} = 'IGNORE';
2582 END { signal_cleanup(); }
2591 my $driftnet_cmd = 0;
2593 $current_state = "init";
2594 $load_method = "none";
2598 # historical suckage: the environment variable name is lower case.
2599 $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
2601 while ($_ = $ARGV[0]) {
2603 if ($_ eq "-display" ||
2609 $ENV{DISPLAY} = shift @ARGV;
2610 } elsif ($_ eq "-root") {
2612 } elsif ($_ eq "-no-output") {
2614 } elsif ($_ eq "-urls-only") {
2617 } elsif ($_ eq "-verbose") {
2619 } elsif (m/^-v+$/) {
2620 $verbose += length($_)-1;
2621 } elsif ($_ eq "-delay") {
2622 $delay = shift @ARGV;
2623 } elsif ($_ eq "-timeout") {
2624 $http_timeout = shift @ARGV;
2625 } elsif ($_ eq "-filter") {
2626 $filter_cmd = shift @ARGV;
2627 } elsif ($_ eq "-filter2") {
2628 $post_filter_cmd = shift @ARGV;
2629 } elsif ($_ eq "-background" || $_ eq "-bg") {
2630 $background = shift @ARGV;
2631 } elsif ($_ eq "-size") {
2633 if (m@^(\d+)x(\d+)$@) {
2637 error "argument to \"-size\" must be of the form \"640x400\"";
2639 } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
2640 $http_proxy = shift @ARGV;
2641 } elsif ($_ eq "-dictionary" || $_ eq "-dict") {
2642 $dict = shift @ARGV;
2643 } elsif ($_ eq "-driftnet" || $_ eq "--driftnet") {
2644 @search_methods = ( 100, "driftnet", \&pick_from_driftnet );
2645 if (! ($ARGV[0] =~ m/^-/)) {
2646 $driftnet_cmd = shift @ARGV;
2648 $driftnet_cmd = $default_driftnet_cmd;
2650 } elsif ($_ eq "-debug" || $_ eq "--debug") {
2651 my $which = shift @ARGV;
2652 my @rest = @search_methods;
2655 my $pct = shift @rest;
2656 my $name = shift @rest;
2657 my $tfn = shift @rest;
2659 if ($name eq $which) {
2660 @search_methods = (100, $name, $tfn);
2665 error "no such search method as \"$which\"" unless ($ok);
2666 LOG (1, "DEBUG: using only \"$which\"");
2669 print STDERR "$copyright\nusage: $progname " .
2670 "[-root] [-display dpy] [-verbose] [-debug which]\n" .
2671 "\t\t [-timeout secs] [-delay secs] [-filter cmd] [-filter2 cmd]\n" .
2672 "\t\t [-no-output] [-urls-only] [-background color] [-size WxH]\n" .
2673 "\t\t [-dictionary dictionary-file] [-http-proxy host[:port]]\n" .
2674 "\t\t [-driftnet [driftnet-program-and-args]]\n" .
2680 if ($http_proxy && $http_proxy eq "") {
2681 $http_proxy = undef;
2683 if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
2684 # historical suckage: allow "http://host:port" as well as "host:port".
2688 if (!$root_p && !$no_output_p) {
2689 print STDERR $copyright;
2690 error "the -root argument is mandatory (for now.)";
2693 if (!$no_output_p && !$ENV{DISPLAY}) {
2694 error "\$DISPLAY is not set.";
2698 if ($verbose == 1) {
2699 $verbose_imgmap = 1;
2700 $verbose_warnings = 1;
2702 } elsif ($verbose == 2) {
2703 $verbose_imgmap = 1;
2704 $verbose_warnings = 1;
2707 } elsif ($verbose == 3) {
2708 $verbose_imgmap = 1;
2709 $verbose_warnings = 1;
2711 $verbose_filter = 1;
2713 } elsif ($verbose == 4) {
2714 $verbose_imgmap = 1;
2715 $verbose_warnings = 1;
2717 $verbose_filter = 1;
2720 } elsif ($verbose == 5) {
2721 $verbose_imgmap = 1;
2722 $verbose_warnings = 1;
2724 $verbose_filter = 1;
2728 } elsif ($verbose == 6) {
2729 $verbose_imgmap = 1;
2730 $verbose_warnings = 1;
2732 $verbose_filter = 1;
2737 } elsif ($verbose >= 7) {
2738 $verbose_imgmap = 1;
2739 $verbose_warnings = 1;
2741 $verbose_filter = 1;
2749 error ("$dict does not exist") unless (-f $dict);
2757 spawn_driftnet ($driftnet_cmd) if ($driftnet_cmd);