3 # webcollage, Copyright (c) 1999-2002 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.96 $ }; $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 = ( 40, "imagevista", \&pick_from_alta_vista_images,
64 30, "altavista", \&pick_from_alta_vista_text,
65 19, "yahoorand", \&pick_from_yahoo_random_link,
66 9, "lycos", \&pick_from_lycos_text,
67 2, "yahoonews", \&pick_from_yahoo_news_text,
69 # Hotbot gives me "no matches" just about every time.
70 # Then I try the same URL again, and it works. I guess
71 # it caches searches, and webcollage always busts its
72 # cache and time out? Or it just sucks.
73 # 0, "hotbot", \&pick_from_hotbot_text,
75 # Google asked (nicely) for me to stop searching them.
76 # 0, "googlenums", \&pick_from_google_image_numbers,
77 # 0, "googleimgs", \&pick_from_google_images,
81 # programs we can use to write to the root window (tried in ascending order.)
83 my @root_displayers = (
84 "chbg -once -xscreensaver -max_size 100",
85 "xv -root -quit -viewonly +noresetroot -quick24 -rmode 5" .
86 " -rfg black -rbg black",
87 "xli -quiet -onroot -center -border black",
88 "xloadimage -quiet -onroot -center -border black",
90 # this lame program wasn't built with vroot.h:
91 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
95 # Some sites need cookies to work properly. These are they.
98 "www.altavista.com" => "AV_ALL=1", # request uncensored searches
99 "web.altavista.com" => "AV_ALL=1",
101 # log in as "cipherpunk"
102 "www.nytimes.com" => 'NYT-S=18cHMIlJOn2Y1bu5xvEG3Ufuk6E1oJ.' .
103 'FMxWaQV0igaB5Yi/Q/guDnLeoL.pe7i1oakSb' .
104 '/VqfdUdb2Uo27Vzt1jmPn3cpYRlTw9',
108 # If this is set, it's a helper program to use for pasting images together:
109 # this is a lot faster and more efficient than using PPM pipelines, which is
110 # what we do if this program doesn't exist. (We check for "webcollage-helper"
111 # on $PATH at startup, and set this variable appropriately.)
113 my $webcollage_helper = undef;
116 # If we have the webcollage-helper program, then it will paste the images
117 # together with transparency! 0.0 is invisible, 1.0 is totally opaque.
122 # Some sites have managed to poison the search engines. These are they.
123 # (We auto-detect sites that have poisoned the search engines via excessive
124 # keywords or dictionary words, but these are ones that slip through
127 # This can contain full host names, or 2 or 3 component domains.
130 "die.net" => 1, # 'l33t h4ck3r d00dz.
131 "genforum.genealogy.com" => 1, # Cluttering altavista with human names.
132 "rootsweb.com" => 1, # Cluttering altavista with human names.
133 "akamai.net" => 1, # Lots of sites have their images on Akamai.
134 # But those are pretty much all banners.
135 # Since Akamai is super-expensive, let's
136 # go out on a limb and assume that all of
137 # their customers are rich-and-boring.
138 "bartleby.com" => 1, # Dictionary, cluttering altavista.
139 "encyclopedia.com" => 1, # Dictionary, cluttering altavista.
140 "onlinedictionary.datasegment.com" => 1, # Dictionary, cluttering altavista.
141 "hotlinkpics.com" => 1, # Porn site that has poisoned imagevista
142 # (I don't see how they did it, though!)
143 "alwayshotels.com" => 1, # Poisoned Lycos pretty heavily.
147 # When verbosity is turned on, we warn about sites that we seem to be hitting
148 # a lot: usually this means some new poisoner has made it into the search
149 # engines. But sometimes, the warning is just because that site has a lot
150 # of stuff on it. So these are the sites that are immune to the "frequent
151 # site" diagnostic message.
153 my %warningless_sites = (
154 "home.earthlink.net" => 1, # Lots of home pages here.
155 "www.geocities.com" => 1,
156 "www.angelfire.com" => 1,
157 "members.aol.com" => 1,
159 "yimg.com" => 1, # This is where dailynews.yahoo.com stores
160 "eimg.com" => 1, # its images, so pick_from_yahoo_news_text()
161 # hits this every time.
163 "driftnet" => 1, # builtin...
167 ##############################################################################
169 # Various global flags set by command line parameters, or computed
171 ##############################################################################
174 my $current_state = "???"; # for diagnostics
177 my $image_succeeded = -1;
178 my $suppress_audit = 0;
180 my $verbose_imgmap = 0; # print out rectangles and URLs only (stdout)
181 my $verbose_warnings = 0; # print out warnings when things go wrong
182 my $verbose_load = 0; # diagnostics about loading of URLs
183 my $verbose_filter = 0; # diagnostics about page selection/rejection
184 my $verbose_net = 0; # diagnostics about network I/O
185 my $verbose_pbm = 0; # diagnostics about PBM pipelines
186 my $verbose_http = 0; # diagnostics about all HTTP activity
187 my $verbose_exec = 0; # diagnostics about executing programs
189 my $report_performance_interval = 60 * 15; # print some stats every 15 minutes
191 my $http_proxy = undef;
192 my $http_timeout = 30;
193 my $cvt_timeout = 10;
199 my $min_gif_area = (120 * 120);
205 my @pids_to_kill = (); # forked pids we should kill when we exit, if any.
207 my $driftnet_magic = 'driftnet';
208 my $driftnet_dir = undef;
209 my $default_driftnet_cmd = "driftnet -a -m 100";
214 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
215 "arrhenius", "arteriole", "blanket", "brainchild",
216 "burdensome", "carnival", "cherub", "chord", "clever",
217 "dedicate", "dilogarithm", "dolan", "dryden",
221 ##############################################################################
225 ##############################################################################
227 # returns three values: the HTTP response line; the document headers;
228 # and the document body.
231 my ( $url, $referer, $timeout ) = @_;
233 if (!defined($timeout)) { $timeout = $http_timeout; }
234 if ($timeout > $http_timeout) { $timeout = $http_timeout; }
237 LOG (($verbose_net || $verbose_load), "timed out for $url");
241 LOG ($verbose_net, "get_document_1 $url " . ($referer ? $referer : ""));
243 if (! ($url =~ m@^http://@i)) {
244 LOG ($verbose_net, "not an HTTP URL: $url");
248 my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
249 $path = "" unless $path;
251 my ($them,$port) = split(/:/, $serverstring);
252 $port = 80 unless $port;
257 $serverstring = $http_proxy if $http_proxy;
258 ($them2,$port2) = split(/:/, $serverstring);
259 $port2 = 80 unless $port2;
262 my ($remote, $iaddr, $paddr, $proto, $line);
264 if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
266 LOG (($verbose_net || $verbose_load), "unrecognised port in $url");
269 $iaddr = inet_aton($remote);
271 LOG (($verbose_net || $verbose_load), "host not found: $remote");
274 $paddr = sockaddr_in($port2, $iaddr);
282 local $SIG{ALRM} = sub {
283 LOG (($verbose_net || $verbose_load), "timed out ($timeout) for $url");
288 $proto = getprotobyname('tcp');
289 if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
290 LOG (($verbose_net || $verbose_load), "socket: $!");
293 if (!connect(S, $paddr)) {
294 LOG (($verbose_net || $verbose_load), "connect($serverstring): $!");
298 select(S); $| = 1; select(STDOUT);
300 my $cookie = $cookies{$them};
302 my $user_agent = "$progname/$version";
303 if ($url =~ m@^http://www\.altavista\.com/@) {
304 # block this, you turkeys.
305 $user_agent = "Mozilla/4.76 [en] (X11; U; Linux 2.2.16-22 i686; Nav)";
308 my $hdrs = "GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
310 "User-Agent: $user_agent\r\n";
312 $hdrs .= "Referer: $referer\r\n";
315 my @cc = split(/\r?\n/, $cookie);
316 $hdrs .= "Cookie: " . join('; ', @cc) . "\r\n";
320 foreach (split('\r?\n', $hdrs)) {
321 LOG ($verbose_http, " ==> $_");
324 my $http = <S> || "";
328 LOG ($verbose_http, " <== $_");
334 LOG ($verbose_http, " <== $_");
336 if (m@^Set-cookie:\s*([^;\r\n]+)@i) {
337 set_cookie($them, $1)
348 " <== [ body ]: $lines lines, " . length($body) . " bytes");
353 LOG (($verbose_net || $verbose_load), "null response: $url");
357 return ( $http, $head, $body );
359 die if ($@ && $@ ne "alarm\n"); # propagate errors
374 # returns two values: the document headers; and the document body.
375 # if the given URL did a redirect, returns the redirected-to document.
378 my ( $url, $referer, $timeout ) = @_;
381 if (defined($referer) && $referer eq $driftnet_magic) {
382 return get_driftnet_file ($url);
387 my $max_loop_count = 4;
390 if (defined($timeout) && $timeout <= 0) {
391 LOG (($verbose_net || $verbose_load), "timed out for $url");
396 my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
398 if (defined ($timeout)) {
400 my $elapsed = $now - $start;
401 $timeout -= $elapsed;
405 return () unless $http; # error message already printed
407 $http =~ s/[\r\n]+$//s;
409 if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
411 my ( $location ) = m@^location:[ \t]*(.*)$@im;
413 $location =~ s/[\r\n]$//;
415 LOG ($verbose_net, "redirect from $url to $location");
420 $referer =~ m@^(http://[^/]+)@i;
422 } elsif (! ($url =~ m@^[a-z]+:@i)) {
424 s@[^/]+$@@g if m@^http://[^/]+/@i;
425 $_ .= "/" if m@^http://[^/]+$@i;
430 LOG ($verbose_net, "no Location with \"$http\"");
431 return ( $url, $body );
434 if ($loop_count++ > $max_loop_count) {
436 "too many redirects ($max_loop_count) from $orig_url");
441 } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
443 LOG (($verbose_net || $verbose_load), "failed: $1 ($url)");
445 # http errors -- return nothing.
451 LOG (($verbose_net || $verbose_load), "document contains no data: $url");
457 return ( $url, $body );
463 # If we already have a cookie defined for this site, and the site is trying
464 # to overwrite that very same cookie, let it do so. This is because nytimes
465 # expires its cookies - it lets you upgrade to a new cookie without logging
466 # in again, but you have to present the old cookie to get the new cookie.
467 # So, by doing this, the built-in cypherpunks cookie will never go "stale".
470 my ($host, $cookie) = @_;
471 my $oc = $cookies{$host};
474 my ($oc_name, $oc_value) = m@^([^= \t\r\n]+)=(.*)$@;
476 my ($nc_name, $nc_value) = m@^([^= \t\r\n]+)=(.*)$@;
478 if ($oc_name eq $nc_name &&
479 $oc_value ne $nc_value) {
480 $cookies{$host} = $cookie;
481 LOG ($verbose_net, "overwrote ${host}'s $oc_name cookie");
486 ############################################################################
488 # Extracting image URLs from HTML
490 ############################################################################
492 # given a URL and the body text at that URL, selects and returns a random
493 # image from it. returns () if no suitable images found.
495 sub pick_image_from_body {
496 my ( $url, $body ) = @_;
501 # if there's at least one slash after the host, take off the last
503 if ( m@^http://[^/]+/@io ) {
504 $base =~ s@[^/]+$@@go;
507 # if there are no slashes after the host at all, put one on the end.
508 if ( m@^http://[^/]+$@io ) {
514 # strip out newlines, compress whitespace
521 # There are certain web sites that list huge numbers of dictionary
522 # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
523 # Porn sites tend not to be reputable!)
525 # I do not want webcollage to filter on content: I want it to select
526 # randomly from the set of images on the web. All the logic here for
527 # rejecting some images is really a set of heuristics for rejecting
528 # images that are not really images: for rejecting *text* that is in
529 # GIF/JPEG form. I don't want text, I want pictures, and I want the
530 # content of the pictures to be randomly selected from among all the
533 # So, filtering out "dirty" pictures by looking for "dirty" keywords
534 # would be wrong: dirty pictures exist, like it or not, so webcollage
535 # should be able to select them.
537 # However, picking a random URL is a hard thing to do. The mechanism I'm
538 # using is to search for a selection of random words. This is not
539 # perfect, but works ok most of the time. The way it breaks down is when
540 # some URLs get precedence because their pages list *every word* as
541 # related -- those URLs come up more often than others.
543 # So, after we've retrieved a URL, if it has too many keywords, reject
544 # it. We reject it not on the basis of what those keywords are, but on
545 # the basis that by having so many, the page has gotten an unfair
546 # advantage against our randomizer.
549 foreach my $trip (@tripwire_words) {
550 $trip_count++ if m/$trip/i;
553 if ($trip_count >= $#tripwire_words - 2) {
554 LOG (($verbose_filter || $verbose_load),
555 "there is probably a dictionary in \"$url\": rejecting.");
556 $rejected_urls{$url} = -1;
566 foreach (split(/ *</)) {
569 # Likewise, reject any web pages that have a KEYWORDS meta tag
572 if (m/name ?= ?\"?keywords\"?/i &&
573 m/content ?= ?\"([^\"]+)\"/) {
576 LOG (($verbose_filter || $verbose_load),
577 "excessive keywords ($L bytes) in $url: rejecting.");
578 $rejected_urls{$url} = $L;
583 LOG ($verbose_filter, " keywords ($L bytes) in $url (ok)");
587 } elsif ( m/^(img|a) .*(src|href) ?= ?\"? ?(.*?)[ >\"]/io ) {
589 my $was_inline = ( "$1" eq "a" || "$1" eq "A" );
591 my ( $width ) = m/width ?=[ \"]*(\d+)/oi;
592 my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
597 ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
599 } elsif ( ! m@^[^/:?]+:@ ) {
602 1 while (s@/[^/]+/\.\./@/@g);
606 if ( ! m@^http://@io ) {
611 if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg)$@io ) {
615 # skip really short or really narrow images
616 if ( $width && $width < $min_width) {
617 if (!$height) { $height = "?"; }
618 LOG ($verbose_filter, " skip narrow image $_ (${width}x$height)");
622 if ( $height && $height < $min_height) {
623 if (!$width) { $width = "?"; }
624 LOG ($verbose_filter, " skip short image $_ (${width}x$height)");
628 # skip images with ratios that make them look like banners.
629 if ($min_ratio && $width && $height &&
630 ($width * $min_ratio ) > $height) {
631 if (!$height) { $height = "?"; }
632 LOG ($verbose_filter, " skip bad ratio $_ (${width}x$height)");
636 # skip GIFs with a small number of pixels -- those usually suck.
637 if ($width && $height &&
639 ($width * $height) < $min_gif_area) {
640 LOG ($verbose_filter, " skip small GIF $_ (${width}x$height)");
647 if ($unique_urls{$url}) {
648 LOG ($verbose_filter, " skip duplicate image $_");
652 LOG ($verbose_filter,
654 ($width && $height ? " (${width}x${height})" : "") .
655 ($was_inline ? " (inline)" : ""));
657 $urls[++$#urls] = $url;
658 $unique_urls{$url}++;
660 # jpegs are preferable to gifs.
662 if ( ! m@[.]gif$@io ) {
663 $urls[++$#urls] = $url;
666 # pointers to images are preferable to inlined images.
667 if ( ! $was_inline ) {
668 $urls[++$#urls] = $url;
669 $urls[++$#urls] = $url;
674 my $fsp = ($body =~ m@<frameset@i);
679 @urls = depoison (@urls);
682 LOG ($verbose_load, "no images on $base" . ($fsp ? " (frameset)" : ""));
686 # pick a random element of the table
687 my $i = int(rand($#urls+1));
690 LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
697 ############################################################################
699 # Subroutines for getting pages and images out of search engines
701 ############################################################################
704 sub pick_dictionary {
705 my @dicts = ("/usr/dict/words",
706 "/usr/share/dict/words",
707 "/usr/share/lib/dict/words");
708 foreach my $f (@dicts) {
714 error ("$dicts[0] does not exist") unless defined($wordlist);
717 # returns a random word from the dictionary
721 if (open (IN, "<$wordlist")) {
722 my $size = (stat(IN))[7];
723 my $pos = rand $size;
724 if (seek (IN, $pos, 0)) {
725 $word = <IN>; # toss partial line
726 $word = <IN>; # keep next line
735 return 0 if (!$word);
737 $word =~ s/^[ \t\n\r]+//;
738 $word =~ s/[ \t\n\r]+$//;
743 $word =~ s/ally$/al/;
744 $word =~ s/izes$/ize/;
745 $word =~ tr/A-Z/a-z/;
747 if ( $word =~ s/[ \t\n\r]/\+/g ) { # convert intra-word spaces to "+".
748 $word = "\%22$word\%22"; # And put quotes (%22) around it.
756 my $sep = ($or_p ? "%20OR%20" : "%20");
757 return (random_word . $sep .
767 $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
774 $s =~ s/%([a-z0-9]{2})/chr(hex($1))/ige;
779 # Loads the given URL (a search on some search engine) and returns:
780 # - the total number of hits the search engine claimed it had;
781 # - a list of URLs from the page that the search engine returned;
782 # Note that this list contains all kinds of internal search engine
783 # junk URLs too -- caller must prune them.
785 sub pick_from_search_engine {
786 my ( $timeout, $search_url, $words ) = @_;
791 print STDERR "\n\n" if ($verbose_load);
793 LOG ($verbose_load, "words: $_");
794 LOG ($verbose_load, "URL: $search_url");
796 $last_search = $search_url; # for warnings
799 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
800 if (defined ($timeout)) {
801 $timeout -= (time - $start);
804 LOG (($verbose_net || $verbose_load),
805 "timed out (late) for $search_url");
811 return () if (! $body);
816 my $search_count = "?";
817 if ($body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
819 } elsif ($body =~ m@<NOBR>((\d{1,3})(,\d{3})*) @i) {
821 } elsif ($body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
823 } elsif ($body =~ m@found about ((\d{1,3})(,\d{3})*|\d+) results@) {
825 } elsif ($body =~ m@\b\d+ - \d+ of (\d+)\b@i) { # imagevista
827 } elsif ($body =~ m@About ((\d{1,3})(,\d{3})*) images@i) { # imagevista
829 } elsif ($body =~ m@We found ((\d{1,3})(,\d{3})*|\d+) results@i) { # *vista
831 } elsif ($body =~ m@ of about <B>((\d{1,3})(,\d{3})*)<@i) { # googleimages
833 } elsif ($body =~ m@<B>((\d{1,3})(,\d{3})*)</B> Web sites were found@i) {
834 $search_count = $1; # lycos
835 } elsif ($body =~ m@WEB.*?RESULTS.*?\b((\d{1,3})(,\d{3})*)\b.*?Matches@i) {
836 $search_count = $1; # hotbot
837 } elsif ($body =~ m@no photos were found containing@i) { # imagevista
839 } elsif ($body =~ m@found no document matching@i) { # altavista
842 1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
844 # if ($search_count eq "?" || $search_count eq "0") {
846 # my $file = "/tmp/wc.html";
847 # open(OUT, ">$file") || error ("writing $file: $!");
850 # print STDERR blurb() . "###### wrote $file\n";
854 my $length = length($body);
863 foreach (split(/\n/)) {
865 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
868 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
869 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
871 if ( $rejected_urls{$u} ) {
872 LOG ($verbose_filter, " pre-rejecting candidate: $u");
876 LOG ($verbose_http, " HREF: $u");
878 $subpages[++$#subpages] = $u;
881 if ( $#subpages < 0 ) {
882 LOG ($verbose_filter,
883 "found nothing on $base ($length bytes, $href_count links).");
887 LOG ($verbose_filter, "" . $#subpages+1 . " links on $search_url");
889 return ($search_count, @subpages);
897 my ($h) = m@^http://([^/: \t\r\n]+)@i;
899 next unless defined($h);
901 if ($poisoners{$h}) {
902 LOG (($verbose_filter), " rejecting poisoner: $_");
905 if ($h =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
907 LOG (($verbose_filter), " rejecting poisoner: $_");
910 if ($h =~ m@([^.]+\.[^.]+)$@ &&
912 LOG (($verbose_filter), " rejecting poisoner: $_");
922 # given a list of URLs, picks one at random; loads it; and returns a
923 # random image from it.
924 # returns the url of the page loaded; the url of the image chosen;
925 # and a debugging description string.
927 sub pick_image_from_pages {
928 my ($base, $total_hit_count, $unfiltered_link_count, $timeout, @pages) = @_;
930 $total_hit_count = "?" unless defined($total_hit_count);
932 @pages = depoison (@pages);
934 "" . ($#pages+1) . " candidates of $unfiltered_link_count links" .
935 " ($total_hit_count total)");
937 return () if ($#pages < 0);
939 my $i = int(rand($#pages+1));
940 my $page = $pages[$i];
942 LOG ($verbose_load, "picked page $page");
946 my ( $base2, $body2 ) = get_document ($page, $base, $timeout);
948 if (!$base2 || !$body2) {
953 my $img = pick_image_from_body ($base2, $body2);
957 return ($base2, $img);
964 ############################################################################
966 # Pick images from random pages returned by the Yahoo Random Link
968 ############################################################################
971 my $yahoo_random_link = "http://random.yahoo.com/bin/ryl";
974 # Picks a random page; picks a random image on that page;
975 # returns two URLs: the page containing the image, and the image.
976 # Returns () if nothing found this time.
978 sub pick_from_yahoo_random_link {
979 my ( $timeout ) = @_;
981 print STDERR "\n\n" if ($verbose_load);
982 LOG ($verbose_load, "URL: $yahoo_random_link");
984 $last_search = $yahoo_random_link; # for warnings
988 my ( $base, $body ) = get_document ($yahoo_random_link, undef, $timeout);
989 if (!$base || !$body) {
994 LOG ($verbose_load, "redirected to: $base");
996 my $img = pick_image_from_body ($base, $body);
1000 return ($base, $img);
1007 ############################################################################
1009 # Pick images by feeding random words into Alta Vista Image Search
1011 ############################################################################
1014 my $alta_vista_images_url = "http://www.altavista.com/cgi-bin/query" .
1015 "?ipht=1" . # photos
1016 "&igrph=1" . # graphics
1019 "&micat=1" . # no partner sites
1020 "&imgset=1" . # no partner sites
1021 "&stype=simage" . # do image search
1022 "&mmW=1" . # unknown, but required
1026 sub pick_from_alta_vista_images {
1027 my ( $timeout ) = @_;
1029 my $words = random_words(1);
1030 my $page = (int(rand(9)) + 1);
1031 my $search_url = $alta_vista_images_url . $words;
1034 $search_url .= "&pgno=" . $page; # page number
1035 $search_url .= "&stq=" . (($page-1) * 12); # first hit result on page
1038 my ($search_hit_count, @subpages) =
1039 pick_from_search_engine ($timeout, $search_url, $words);
1041 my @candidates = ();
1042 foreach my $u (@subpages) {
1044 # altavista is encoding their URLs now.
1045 next unless ($u =~ m@^/r.*\&r=([^&]+).*@);
1046 $u = url_unquote($1);
1048 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1049 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
1050 next if ($u =~ m@[/.]doubleclick\.net\b@i); # you cretins
1051 next if ($u =~ m@[/.]clicktomarket\.com\b@i); # more cretins
1053 next if ($u =~ m@[/.]viewimages\.com\b@i); # stacked deck
1054 next if ($u =~ m@[/.]gettyimages\.com\b@i);
1056 LOG ($verbose_filter, " candidate: $u");
1057 push @candidates, $u;
1060 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1061 $timeout, @candidates);
1066 ############################################################################
1068 # Pick images by feeding random words into Google Image Search.
1069 # By Charles Gales <gales@us.ibm.com>
1071 ############################################################################
1074 my $google_images_url = "http://images.google.com/images" .
1075 "?site=images" . # photos
1076 "&btnG=Search" . # graphics
1077 "&safe=off" . # no screening
1082 sub pick_from_google_images {
1083 my ( $timeout ) = @_;
1085 my $words = random_word; # only one word for Google
1086 my $page = (int(rand(9)) + 1);
1087 my $num = 20; # 20 images per page
1088 my $search_url = $google_images_url . $words;
1091 $search_url .= "&start=" . $page*$num; # page number
1092 $search_url .= "&num=" . $num; #images per page
1095 my ($search_hit_count, @subpages) =
1096 pick_from_search_engine ($timeout, $search_url, $words);
1098 my @candidates = ();
1099 foreach my $u (@subpages) {
1100 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1101 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1103 if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1105 LOG ($verbose_filter, " candidate: $urlf");
1106 push @candidates, $urlf;
1110 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1111 $timeout, @candidates);
1116 ############################################################################
1118 # Pick images by feeding random *numbers* into Google Image Search.
1119 # By jwz, suggested by from Ian O'Donnell.
1121 ############################################################################
1125 sub pick_from_google_image_numbers {
1126 my ( $timeout ) = @_;
1129 my $number = int(rand($max));
1131 $number = sprintf("%04d", $number)
1134 my $words = "$number";
1135 my $page = (int(rand(40)) + 1);
1136 my $num = 20; # 20 images per page
1137 my $search_url = $google_images_url . $words;
1140 $search_url .= "&start=" . $page*$num; # page number
1141 $search_url .= "&num=" . $num; #images per page
1144 my ($search_hit_count, @subpages) =
1145 pick_from_search_engine ($timeout, $search_url, $words);
1147 my @candidates = ();
1149 foreach my $u (@subpages) {
1150 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1151 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1153 if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1155 my $img = "http://$1";
1157 LOG ($verbose_filter, " candidate: $ref");
1158 push @candidates, $img;
1159 $referers{$img} = $ref;
1163 @candidates = depoison (@candidates);
1164 return () if ($#candidates < 0);
1165 my $i = int(rand($#candidates+1));
1166 my $img = $candidates[$i];
1167 my $ref = $referers{$img};
1169 LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1170 return ($ref, $img);
1175 ############################################################################
1177 # Pick images by feeding random words into Alta Vista Text Search
1179 ############################################################################
1182 my $alta_vista_url_1 = "http://www.altavista.com/cgi-bin/query?pg=q" .
1183 "&text=yes&kl=XX&stype=stext&q=";
1184 my $alta_vista_url_2 = "http://www.altavista.com/sites/search/web?pg=q" .
1185 "&kl=XX&search=Search&q=";
1187 my $alta_vista_url = $alta_vista_url_2;
1190 sub pick_from_alta_vista_text {
1191 my ( $timeout ) = @_;
1193 my $words = random_words(1);
1194 my $page = (int(rand(9)) + 1);
1195 my $search_url = $alta_vista_url . $words;
1198 $search_url .= "&pgno=" . $page;
1199 $search_url .= "&stq=" . (($page-1) * 10);
1202 my ($search_hit_count, @subpages) =
1203 pick_from_search_engine ($timeout, $search_url, $words);
1205 my @candidates = ();
1206 foreach my $u (@subpages) {
1208 # Those altavista fuckers are playing really nasty redirection games
1209 # these days: the filter your clicks through their site, but use
1210 # onMouseOver to make it look like they're not! Well, it makes it
1211 # easier for us to identify search results...
1213 next unless ($u =~ m@^/r.*\&r=([^&]+).*@);
1214 $u = url_unquote($1);
1216 LOG ($verbose_filter, " candidate: $u");
1217 push @candidates, $u;
1220 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1221 $timeout, @candidates);
1226 ############################################################################
1228 # Pick images by feeding random words into Hotbot
1230 ############################################################################
1232 my $hotbot_search_url = "http://hotbot.lycos.com/" .
1244 sub pick_from_hotbot_text {
1245 my ( $timeout ) = @_;
1247 my $words = random_words(0);
1248 my $search_url = $hotbot_search_url . $words;
1250 my ($search_hit_count, @subpages) =
1251 pick_from_search_engine ($timeout, $search_url, $words);
1253 my @candidates = ();
1254 foreach my $u (@subpages) {
1256 # Hotbot plays redirection games too
1257 next unless ($u =~ m@^/director.asp\?target=([^&]+)@);
1258 $u = url_decode($1);
1260 LOG ($verbose_filter, " candidate: $u");
1261 push @candidates, $u;
1264 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1265 $timeout, @candidates);
1270 ############################################################################
1272 # Pick images by feeding random words into Lycos
1274 ############################################################################
1276 my $lycos_search_url = "http://lycospro.lycos.com/srchpro/" .
1281 sub pick_from_lycos_text {
1282 my ( $timeout ) = @_;
1284 my $words = random_words(0);
1285 my $start = int(rand(8)) * 10 + 1;
1286 my $search_url = $lycos_search_url . $words . "&start=$start";
1288 my ($search_hit_count, @subpages) =
1289 pick_from_search_engine ($timeout, $search_url, $words);
1291 my @candidates = ();
1292 foreach my $u (@subpages) {
1294 # Lycos plays exact the same redirection game as hotbot.
1295 # Note that "id=0" is used for internal advertising links,
1296 # and 1+ are used for search results.
1297 next unless ($u =~ m@^http://click.hotbot.com/director.asp
1304 $u = url_decode($1);
1306 LOG ($verbose_filter, " candidate: $u");
1307 push @candidates, $u;
1310 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1311 $timeout, @candidates);
1316 ############################################################################
1318 # Pick images by feeding random words into news.yahoo.com
1320 ############################################################################
1322 my $yahoo_news_url = "http://search.news.yahoo.com/search/news_photos?" .
1323 "&z=&n=100&o=o&2=&3=&p=";
1326 sub pick_from_yahoo_news_text {
1327 my ( $timeout ) = @_;
1329 my $words = random_words(1);
1330 my $search_url = $yahoo_news_url . $words;
1332 my ($search_hit_count, @subpages) =
1333 pick_from_search_engine ($timeout, $search_url, $words);
1335 my @candidates = ();
1336 foreach my $u (@subpages) {
1337 # only accept URLs on Yahoo's news site
1338 next unless ($u =~ m@^http://dailynews\.yahoo\.com/@i ||
1339 $u =~ m@^http://story\.news\.yahoo\.com/@i);
1341 LOG ($verbose_filter, " candidate: $u");
1342 push @candidates, $u;
1345 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1346 $timeout, @candidates);
1352 ############################################################################
1354 # Pick images by waiting for driftnet to populate a temp dir with files.
1355 # Requires driftnet version 0.1.5 or later.
1356 # (Driftnet is a program by Chris Lightfoot that sniffs your local ethernet
1357 # for images being downloaded by others.)
1358 # Driftnet/webcollage integration by jwz.
1360 ############################################################################
1363 sub pick_from_driftnet {
1364 my ( $timeout ) = @_;
1366 my $id = $driftnet_magic;
1367 my $dir = $driftnet_dir;
1371 error ("\$driftnet_dir unset?") unless ($dir);
1374 error ("$dir unreadable") unless (-d "$dir/.");
1376 $timeout = $http_timeout unless ($timeout);
1379 while ($now = time, $now < $start + $timeout) {
1381 opendir (DIR, $dir) || error ("$dir: $!");
1382 while (my $file = readdir(DIR)) {
1383 next if ($file =~ m/^\./);
1384 $file = "$dir/$file";
1386 LOG ($verbose_load, "picked file $file ($id)");
1387 return ($id, $file);
1391 LOG (($verbose_net || $verbose_load), "timed out for $id");
1396 sub get_driftnet_file {
1399 error ("\$driftnet_dir unset?") unless ($driftnet_dir);
1401 my $id = $driftnet_magic;
1402 my $re = qr/$driftnet_dir/;
1403 error ("$id: $file not in $driftnet_dir?")
1404 unless ($file =~ m@^$re@o);
1407 open (IN, $file) || error ("$id: $file: $!");
1409 while (<IN>) { $body .= $_; }
1412 return ($id, $body);
1416 sub spawn_driftnet {
1419 # make a directory to use.
1421 my $tmp = $ENV{TEMPDIR} || "/tmp";
1422 $driftnet_dir = sprintf ("$tmp/driftcollage-%08x", rand(0xffffffff));
1423 LOG ($verbose_exec, "mkdir $driftnet_dir");
1424 last if mkdir ($driftnet_dir, 0700);
1427 if (! ($cmd =~ m/\s/)) {
1428 # if the command didn't have any arguments in it, then it must be just
1429 # a pointer to the executable. Append the default args to it.
1430 my $dargs = $default_driftnet_cmd;
1431 $dargs =~ s/^[^\s]+//;
1435 # point the driftnet command at our newly-minted private directory.
1437 $cmd .= " -d $driftnet_dir";
1438 $cmd .= ">/dev/null" unless ($verbose_exec);
1441 if ($pid < 0) { error ("fork: $!\n"); }
1444 push @pids_to_kill, $pid;
1445 LOG ($verbose_exec, "forked for \"$cmd\"");
1448 nontrapping_system ($cmd) || error ("exec: $!");
1451 # wait a bit, then make sure the process actually started up.
1454 error ("pid $pid failed to start \"$cmd\"")
1455 unless (1 == kill (0, $pid));
1459 ############################################################################
1461 # Pick a random image in a random way
1463 ############################################################################
1466 # Picks a random image on a random page, and returns two URLs:
1467 # the page containing the image, and the image.
1468 # Returns () if nothing found this time.
1472 my ( $timeout ) = @_;
1474 $current_state = "select";
1475 $load_method = "none";
1477 my $n = int(rand(100));
1480 my @rest = @search_methods;
1483 my $pct = shift @rest;
1484 my $name = shift @rest;
1485 my $tfn = shift @rest;
1487 if ($total > $n && !defined($fn)) {
1489 $current_state = $name;
1490 $load_method = $current_state;
1494 if ($total != 100) {
1495 error ("internal error: \@search_methods totals to $total%!");
1498 record_attempt ($current_state);
1499 return $fn->($timeout);
1504 ############################################################################
1506 # Statistics and logging
1508 ############################################################################
1511 return strftime ("%H:%M:%S: ", localtime);
1515 return "$progname: " . timestr() . "$current_state: ";
1520 print STDERR blurb() . "$err\n";
1532 my $head = "$progname: DEBUG: ";
1533 foreach (split (/\n/, $lastlog)) {
1534 print STDERR "$head$_\n";
1540 my ($print, $msg) = @_;
1541 my $blurb = timestr() . "$current_state: ";
1542 $lastlog .= "$blurb$msg\n";
1543 print STDERR "$progname: $blurb$msg\n" if $print;
1548 my %stats_successes;
1551 my $last_state = undef;
1552 sub record_attempt {
1556 record_failure($last_state) unless ($image_succeeded > 0);
1558 $last_state = $name;
1561 report_performance();
1564 $image_succeeded = 0;
1565 $suppress_audit = 0;
1568 sub record_success {
1569 my ($name, $url, $base) = @_;
1570 if (defined($stats_successes{$name})) {
1571 $stats_successes{$name}++;
1573 $stats_successes{$name} = 1;
1576 stop_timer ($name, 1);
1577 my $o = $current_state;
1578 $current_state = $name;
1579 save_recent_url ($url, $base);
1580 $current_state = $o;
1581 $image_succeeded = 1;
1586 sub record_failure {
1589 return if $image_succeeded;
1591 stop_timer ($name, 0);
1592 if ($verbose_load && !$verbose_exec) {
1594 if ($suppress_audit) {
1595 print STDERR "$progname: " . timestr() . "(audit log suppressed)\n";
1599 my $o = $current_state;
1600 $current_state = "DEBUG";
1602 my $line = "#" x 78;
1603 print STDERR "\n\n\n";
1604 print STDERR ("#" x 78) . "\n";
1605 print STDERR blurb() . "failed to get an image. Full audit log:\n";
1608 print STDERR ("-" x 78) . "\n";
1609 print STDERR "\n\n";
1611 $current_state = $o;
1613 $image_succeeded = 0;
1620 my $i = $stats_successes{$name};
1621 my $j = $stats_attempts{$name};
1624 return "" . ($j ? int($i * 100 / $j) : "0") . "%";
1628 my $current_start_time = 0;
1632 $current_start_time = time;
1634 if (defined($stats_attempts{$name})) {
1635 $stats_attempts{$name}++;
1637 $stats_attempts{$name} = 1;
1639 if (!defined($stats_elapsed{$name})) {
1640 $stats_elapsed{$name} = 0;
1645 my ($name, $success) = @_;
1646 $stats_elapsed{$name} += time - $current_start_time;
1650 my $last_report_time = 0;
1651 sub report_performance {
1653 return unless $verbose_warnings;
1656 return unless ($now >= $last_report_time + $report_performance_interval);
1657 my $ot = $last_report_time;
1658 $last_report_time = $now;
1660 return if ($ot == 0);
1662 my $blurb = "$progname: " . timestr();
1665 print STDERR "${blurb}Current standings:\n";
1667 foreach my $name (sort keys (%stats_attempts)) {
1668 my $try = $stats_attempts{$name};
1669 my $suc = $stats_successes{$name} || 0;
1670 my $pct = int($suc * 100 / $try);
1671 my $secs = $stats_elapsed{$name};
1672 my $secs_link = int($secs / $try);
1673 print STDERR sprintf ("$blurb %-12s %4s (%d/%d);\t %2d secs/link\n",
1674 "$name:", "$pct%", $suc, $try, $secs_link);
1680 my $max_recent_images = 400;
1681 my $max_recent_sites = 20;
1682 my @recent_images = ();
1683 my @recent_sites = ();
1685 sub save_recent_url {
1686 my ($url, $base) = @_;
1688 return unless ($verbose_warnings);
1691 my ($site) = m@^http://([^ \t\n\r/:]+)@;
1693 if ($base eq $driftnet_magic) {
1694 $site = $driftnet_magic;
1695 @recent_images = ();
1699 foreach (@recent_images) {
1701 print STDERR blurb() . "WARNING: recently-duplicated image: $url" .
1702 " (on $base via $last_search)\n";
1708 # suppress "duplicate site" warning via %warningless_sites.
1710 if ($warningless_sites{$site}) {
1712 } elsif ($site =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
1713 $warningless_sites{$1}) {
1715 } elsif ($site =~ m@([^.]+\.[^.]+)$@ &&
1716 $warningless_sites{$1}) {
1721 foreach (@recent_sites) {
1723 print STDERR blurb() . "WARNING: recently-duplicated site: $site" .
1724 " ($url on $base via $last_search)\n";
1730 push @recent_images, $url;
1731 push @recent_sites, $site;
1732 shift @recent_images if ($#recent_images >= $max_recent_images);
1733 shift @recent_sites if ($#recent_sites >= $max_recent_sites);
1738 ##############################################################################
1742 ##############################################################################
1749 s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
1754 # Given the raw body of a GIF document, returns the dimensions of the image.
1758 my $type = substr($body, 0, 6);
1760 return () unless ($type =~ /GIF8[7,9]a/);
1761 $s = substr ($body, 6, 10);
1762 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
1763 return (($b<<8|$a), ($d<<8|$c));
1766 # Given the raw body of a JPEG document, returns the dimensions of the image.
1771 my $L = length($body);
1773 my $c1 = substr($body, $i, 1); $i++;
1774 my $c2 = substr($body, $i, 1); $i++;
1775 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
1778 while (ord($ch) != 0xDA && $i < $L) {
1779 # Find next marker, beginning with 0xFF.
1780 while (ord($ch) != 0xFF) {
1781 return () if (length($body) <= $i);
1782 $ch = substr($body, $i, 1); $i++;
1784 # markers can be padded with any number of 0xFF.
1785 while (ord($ch) == 0xFF) {
1786 return () if (length($body) <= $i);
1787 $ch = substr($body, $i, 1); $i++;
1790 # $ch contains the value of the marker.
1791 my $marker = ord($ch);
1793 if (($marker >= 0xC0) &&
1794 ($marker <= 0xCF) &&
1795 ($marker != 0xC4) &&
1796 ($marker != 0xCC)) { # it's a SOFn marker
1798 return () if (length($body) <= $i);
1799 my $s = substr($body, $i, 4); $i += 4;
1800 my ($a,$b,$c,$d) = unpack("C"x4, $s);
1801 return (($c<<8|$d), ($a<<8|$b));
1804 # We must skip variables, since FFs in variable names aren't
1805 # valid JPEG markers.
1806 return () if (length($body) <= $i);
1807 my $s = substr($body, $i, 2); $i += 2;
1808 my ($c1, $c2) = unpack ("C"x2, $s);
1809 my $length = ($c1 << 8) | $c2;
1810 return () if ($length < 2);
1817 # Given the raw body of a GIF or JPEG document, returns the dimensions of
1822 my ($w, $h) = gif_size ($body);
1823 if ($w && $h) { return ($w, $h); }
1824 return jpeg_size ($body);
1828 # returns the full path of the named program, or undef.
1832 foreach (split (/:/, $ENV{PATH})) {
1833 if (-x "$_/$prog") {
1841 # Like rand(), but chooses numbers with a bell curve distribution.
1844 $_ = 1.0 unless defined($_);
1846 return (rand($_) + rand($_) + rand($_));
1850 sub signal_cleanup {
1852 print STDERR blurb() . (defined($sig)
1853 ? "caught signal $sig."
1860 if (@pids_to_kill) {
1861 print STDERR blurb() . "killing: " . join(' ', @pids_to_kill) . "\n";
1862 kill ('TERM', @pids_to_kill);
1868 ##############################################################################
1870 # Generating a list of urls only
1872 ##############################################################################
1874 sub url_only_output {
1876 my ($base, $img) = pick_image;
1880 print "$img $base\n";
1885 ##############################################################################
1887 # Running as an xscreensaver module
1889 ##############################################################################
1891 my $image_ppm = ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp") . "/webcollage." . $$;
1892 my $image_tmp1 = $image_ppm . "-1";
1893 my $image_tmp2 = $image_ppm . "-2";
1895 my $filter_cmd = undef;
1896 my $post_filter_cmd = undef;
1897 my $background = undef;
1899 my $img_width; # size of the image being generated.
1905 unlink $image_ppm, $image_tmp1, $image_tmp2;
1909 # Like system, but prints status about exit codes, and kills this process
1910 # with whatever signal killed the sub-process, if any.
1912 sub nontrapping_system {
1916 s/\"[^\"]+\"/\"...\"/g;
1918 LOG ($verbose_exec, "executing \"$_\"");
1923 LOG ($verbose_exec, "subproc exited normally.");
1924 } elsif (($rc & 0xff) == 0) {
1926 LOG ($verbose_exec, "subproc exited with status $rc.");
1929 LOG ($verbose_exec, "subproc dumped core.");
1932 LOG ($verbose_exec, "subproc died with signal $rc.");
1933 # die that way ourselves.
1941 # Given the URL of a GIF or JPEG image, and the body of that image, writes a
1942 # PPM to the given output file. Returns the width/height of the image if
1946 my ($url, $body, $output) = @_;
1947 my ($cmd, $cmd2, $w, $h);
1949 if ((@_ = gif_size ($body))) {
1952 } elsif ((@_ = jpeg_size ($body))) {
1956 LOG (($verbose_pbm || $verbose_load),
1957 "not a GIF or JPG" .
1958 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
1959 ? " (looks like HTML)" : "") .
1961 $suppress_audit = 1;
1965 $cmd2 = "exec $cmd"; # yes, this really is necessary. if we don't
1966 # do this, the process doesn't die properly.
1967 if (!$verbose_pbm) {
1969 # We get a "giftopnm: got a 'Application Extension' extension"
1970 # warning any time it's an animgif.
1972 # Note that "giftopnm: EOF / read error on image data" is not
1973 # always a fatal error -- sometimes the image looks fine anyway.
1975 $cmd2 .= " 2>/dev/null";
1978 # There exist corrupted GIF and JPEG files that can make giftopnm and
1979 # djpeg lose their minds and go into a loop. So this gives those programs
1980 # a small timeout -- if they don't complete in time, kill them.
1986 local $SIG{ALRM} = sub {
1988 "timed out ($cvt_timeout) for $cmd on \"$url\" in pid $pid");
1989 kill ('TERM', $pid) if ($pid);
1994 if (($pid = open(PIPE, "| $cmd2 > $output"))) {
2001 LOG ($verbose_exec, "awaiting $pid");
2003 LOG ($verbose_exec, "$pid completed");
2005 my $size = (stat($output))[7];
2006 $size = -1 unless defined($size);
2008 LOG ($verbose_pbm, "$cmd on ${w}x$h \"$url\" failed ($size bytes)");
2012 LOG ($verbose_pbm, "created ${w}x$h $output ($cmd)");
2015 print STDERR blurb() . "$cmd failed: $!\n";
2019 die if ($@ && $@ ne "alarm\n"); # propagate errors
2032 sub pick_root_displayer {
2035 foreach my $cmd (@root_displayers) {
2037 my ($name) = m/^([^ ]+)/;
2038 push @names, "\"$name\"";
2039 LOG ($verbose_exec, "looking for $name...");
2040 foreach my $dir (split (/:/, $ENV{PATH})) {
2041 LOG ($verbose_exec, " checking $dir/$name");
2042 return $cmd if (-x "$dir/$name");
2046 $names[$#names] = "or " . $names[$#names];
2047 error "none of: " . join (", ", @names) . " were found on \$PATH.";
2051 my $ppm_to_root_window_cmd = undef;
2054 sub x_or_pbm_output {
2056 # Check for our helper program, to see whether we need to use PPM pipelines.
2058 $_ = "webcollage-helper";
2059 if (defined ($webcollage_helper) || which ($_)) {
2060 $webcollage_helper = $_ unless (defined($webcollage_helper));
2061 LOG ($verbose_pbm, "found \"$webcollage_helper\"");
2062 $webcollage_helper .= " -v";
2064 LOG (($verbose_pbm || $verbose_load), "no $_ program");
2067 # make sure the various programs we execute exist, right up front.
2069 my @progs = ("ppmmake"); # always need this one
2071 if (!defined($webcollage_helper)) {
2072 # Only need these others if we don't have the helper.
2073 @progs = (@progs, "giftopnm", "djpeg", "pnmpaste", "pnmscale", "pnmcut");
2077 which ($_) || error "$_ not found on \$PATH.";
2080 # find a root-window displayer program.
2082 $ppm_to_root_window_cmd = pick_root_displayer();
2084 if (!$img_width || !$img_height) {
2086 which ($_) || error "$_ not found on \$PATH.";
2088 ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
2089 if (!defined($img_height)) {
2090 error "xdpyinfo failed.";
2094 my $bgcolor = "#000000";
2095 my $bgimage = undef;
2098 if ($background =~ m/^\#[0-9a-f]+$/i) {
2099 $bgcolor = $background;
2101 } elsif (-r $background) {
2102 $bgimage = $background;
2104 } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
2105 error "not a color or readable file: $background";
2108 # default to assuming it's a color
2109 $bgcolor = $background;
2113 # Create the sold-colored base image.
2115 $_ = "ppmmake '$bgcolor' $img_width $img_height";
2116 LOG ($verbose_pbm, "creating base image: $_");
2117 nontrapping_system "$_ > $image_ppm";
2119 # Paste the default background image in the middle of it.
2126 open(IMG, "<$bgimage") || error "couldn't open $bgimage: $!";
2128 while (<IMG>) { $body .= $_; }
2131 if ((@_ = gif_size ($body))) {
2133 $cmd = "giftopnm |";
2135 } elsif ((@_ = jpeg_size ($body))) {
2139 } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
2145 error "$bgimage is not a GIF, JPEG, or PPM.";
2148 my $x = int (($img_width - $iw) / 2);
2149 my $y = int (($img_height - $ih) / 2);
2151 "pasting $bgimage (${iw}x$ih) into base image at $x,$y");
2153 $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
2154 open (IMG, "| $cmd") || error "running $cmd: $!";
2158 LOG ($verbose_exec, "subproc exited normally.");
2159 rename ($image_tmp1, $image_ppm) ||
2160 error "renaming $image_tmp1 to $image_ppm: $!";
2166 my ($base, $img) = pick_image();
2167 my $source = $current_state;
2168 $current_state = "loadimage";
2170 my ($headers, $body) = get_document ($img, $base);
2172 paste_image ($base, $img, $body, $source);
2176 $current_state = "idle";
2177 $load_method = "none";
2179 unlink $image_tmp1, $image_tmp2;
2185 my ($base, $img, $body, $source) = @_;
2187 $current_state = "paste";
2189 $suppress_audit = 0;
2191 LOG ($verbose_pbm, "got $img (" . length($body) . ")");
2195 # If we are using the webcollage-helper, then we do not need to convert this
2196 # image to a PPM. But, if we're using a filter command, we still must, since
2197 # that's what the filters expect (webcollage-helper can read PPMs, so that's
2200 if (defined ($webcollage_helper) &&
2201 !defined ($filter_cmd)) {
2203 ($iw, $ih) = image_size ($body);
2205 LOG (($verbose_pbm || $verbose_load),
2206 "not a GIF or JPG" .
2207 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
2208 ? " (looks like HTML)" : "") .
2210 $suppress_audit = 1;
2216 open (OUT, ">$image_tmp1") || error ("writing $image_tmp1: $!");
2217 print OUT $body || error ("writing $image_tmp1: $!");
2218 close OUT || error ("writing $image_tmp1: $!");
2221 ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
2224 LOG ($verbose_pbm, "unable to make PBM from $img");
2229 record_success ($load_method, $img, $base);
2232 my $ow = $iw; # used only for error messages
2235 # don't just tack this onto the front of the pipeline -- we want it to
2236 # be able to change the size of the input image.
2239 LOG ($verbose_pbm, "running $filter_cmd");
2241 my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
2243 LOG(($verbose_pbm || $verbose_load), "failed command: \"$filter_cmd\"");
2244 LOG(($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2247 rename ($image_tmp2, $image_tmp1);
2249 # re-get the width/height in case the filter resized it.
2251 open(IMG, "<$image_tmp1") || return 0;
2254 ($iw, $ih) = m/^(\d+) (\d+)$/;
2256 return 0 unless ($iw && $ih);
2259 my $target_w = $img_width;
2260 my $target_h = $img_height;
2266 # Usually scale the image to fit on the screen -- but sometimes scale it
2267 # to fit on half or a quarter of the screen. Note that we don't merely
2268 # scale it to fit, we instead cut it in half until it fits -- that should
2269 # give a wider distribution of sizes.
2271 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2272 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2274 if ($iw > $target_w || $ih > $target_h) {
2275 while ($iw > $target_w ||
2280 if ($iw <= 10 || $ih <= 10) {
2281 LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus.");
2285 LOG ($verbose_pbm, "scaling to ${iw}x$ih");
2287 $cmd .= " | pnmscale -xsize $iw -ysize $ih";
2291 my $src = $image_tmp1;
2293 my $crop_x = 0; # the sub-rectangle of the image
2294 my $crop_y = 0; # that we will actually paste.
2298 # The chance that we will randomly crop out a section of an image starts
2299 # out fairly low, but goes up for images that are very large, or images
2300 # that have ratios that make them look like banners (we try to avoid
2301 # banner images entirely, but they slip through when the IMG tags didn't
2302 # have WIDTH and HEIGHT specified.)
2304 my $crop_chance = 0.2;
2305 if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
2306 $crop_chance += 0.2;
2308 if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
2309 $crop_chance += 0.2;
2311 if ($min_ratio && ($iw * $min_ratio) > $ih) {
2312 $crop_chance += 0.7;
2315 if ($crop_chance > 0.1) {
2316 LOG ($verbose_pbm, "crop chance: $crop_chance");
2319 if (rand() < $crop_chance) {
2324 if ($crop_w > $min_width) {
2325 # if it's a banner, select the width linearly.
2326 # otherwise, select a bell.
2327 my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
2330 $crop_w = $min_width + int ($r * ($crop_w - $min_width));
2331 $crop_x = int (rand() * ($ow - $crop_w));
2333 if ($crop_h > $min_height) {
2334 # height always selects as a bell.
2335 $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
2336 $crop_y = int (rand() * ($oh - $crop_h));
2339 if ($crop_x != 0 || $crop_y != 0 ||
2340 $crop_w != $iw || $crop_h != $ih) {
2342 "randomly cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2346 # Where the image should logically land -- this might be negative.
2348 my $x = int((rand() * ($img_width + $crop_w/2)) - $crop_w*3/4);
2349 my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
2351 # if we have chosen to paste the image outside of the rectangle of the
2352 # screen, then we need to crop it.
2356 $x + $crop_w > $img_width ||
2357 $y + $crop_h > $img_height) {
2360 "cropping for effective paste of ${crop_w}x$crop_h \@ $x,$y");
2362 if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
2363 if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
2365 if ($x + $crop_w >= $img_width) { $crop_w = $img_width - $x - 1; }
2366 if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
2369 # If any cropping needs to happen, add pnmcut.
2371 if ($crop_x != 0 || $crop_y != 0 ||
2372 $crop_w != $iw || $crop_h != $ih) {
2375 $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
2376 LOG ($verbose_pbm, "cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2379 LOG ($verbose_pbm, "pasting ${iw}x$ih \@ $x,$y in $image_ppm");
2381 $cmd .= " | pnmpaste - $x $y $image_ppm";
2383 $cmd =~ s@^ *\| *@@;
2385 if (defined ($webcollage_helper)) {
2386 $cmd = "$webcollage_helper $image_tmp1 $image_ppm " .
2387 "$scale $opacity " .
2388 "$crop_x $crop_y $x $y " .
2393 # use a PPM pipeline
2395 $_ .= " < $image_tmp1 > $image_tmp2";
2399 $_ = "($_) 2>&1 | sed s'/^/" . blurb() . "/'";
2401 $_ .= " 2> /dev/null";
2404 my $rc = nontrapping_system ($_);
2406 if (defined ($webcollage_helper) && -z $image_ppm) {
2407 LOG (1, "failed command: \"$cmd\"");
2408 print STDERR "\naudit log:\n\n\n";
2409 print STDERR ("#" x 78) . "\n";
2410 print STDERR blurb() . "$image_ppm has zero size\n";
2412 print STDERR "\n\n";
2417 LOG (($verbose_pbm || $verbose_load), "failed command: \"$cmd\"");
2418 LOG (($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2422 if (!defined ($webcollage_helper)) {
2423 rename ($image_tmp2, $image_ppm) || return;
2426 my $target = "$image_ppm";
2428 # don't just tack this onto the end of the pipeline -- we don't want it
2429 # to end up in $image_ppm, because we don't want the results to be
2432 if ($post_filter_cmd) {
2436 $target = $image_tmp1;
2437 if (!defined ($webcollage_helper)) {
2438 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2440 # Blah, my scripts need the JPEG data, but some other folks need
2441 # the PPM data -- what to do? Ignore the problem, that's what!
2442 # $cmd = "djpeg < $image_ppm | ($post_filter_cmd) > $target";
2443 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2446 $rc = nontrapping_system ($cmd);
2448 LOG ($verbose_pbm, "filter failed: \"$post_filter_cmd\"\n");
2453 if (!$no_output_p) {
2454 my $tsize = (stat($target))[7];
2456 $cmd = "$ppm_to_root_window_cmd $target";
2458 # xv seems to hate being killed. it tends to forget to clean
2459 # up after itself, and leaves windows around and colors allocated.
2460 # I had this same problem with vidwhacker, and I'm not entirely
2461 # sure what I did to fix it. But, let's try this: launch xv
2462 # in the background, so that killing this process doesn't kill it.
2463 # it will die of its own accord soon enough. So this means we
2464 # start pumping bits to the root window in parallel with starting
2465 # the next network retrieval, which is probably a better thing
2470 $rc = nontrapping_system ($cmd);
2473 LOG (($verbose_pbm || $verbose_load), "display failed: \"$cmd\"");
2478 LOG ($verbose_pbm, "$target size is $tsize");
2482 $source .= "-" . stats_of($source);
2483 print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n"
2484 if ($verbose_imgmap);
2494 $SIG{HUP} = \&signal_cleanup;
2495 $SIG{INT} = \&signal_cleanup;
2496 $SIG{QUIT} = \&signal_cleanup;
2497 $SIG{ABRT} = \&signal_cleanup;
2498 $SIG{KILL} = \&signal_cleanup;
2499 $SIG{TERM} = \&signal_cleanup;
2501 # Need this so that if giftopnm dies, we don't die.
2502 $SIG{PIPE} = 'IGNORE';
2505 END { signal_cleanup(); }
2514 my $driftnet_cmd = 0;
2516 $current_state = "init";
2517 $load_method = "none";
2521 # historical suckage: the environment variable name is lower case.
2522 $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
2524 while ($_ = $ARGV[0]) {
2526 if ($_ eq "-display" ||
2532 $ENV{DISPLAY} = shift @ARGV;
2533 } elsif ($_ eq "-root") {
2535 } elsif ($_ eq "-no-output") {
2537 } elsif ($_ eq "-urls-only") {
2540 } elsif ($_ eq "-verbose") {
2542 } elsif (m/^-v+$/) {
2543 $verbose += length($_)-1;
2544 } elsif ($_ eq "-delay") {
2545 $delay = shift @ARGV;
2546 } elsif ($_ eq "-timeout") {
2547 $http_timeout = shift @ARGV;
2548 } elsif ($_ eq "-filter") {
2549 $filter_cmd = shift @ARGV;
2550 } elsif ($_ eq "-filter2") {
2551 $post_filter_cmd = shift @ARGV;
2552 } elsif ($_ eq "-background" || $_ eq "-bg") {
2553 $background = shift @ARGV;
2554 } elsif ($_ eq "-size") {
2556 if (m@^(\d+)x(\d+)$@) {
2560 error "argument to \"-size\" must be of the form \"640x400\"";
2562 } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
2563 $http_proxy = shift @ARGV;
2564 } elsif ($_ eq "-dictionary" || $_ eq "-dict") {
2565 $dict = shift @ARGV;
2566 } elsif ($_ eq "-driftnet" || $_ eq "--driftnet") {
2567 @search_methods = ( 100, "driftnet", \&pick_from_driftnet );
2568 if (! ($ARGV[0] =~ m/^-/)) {
2569 $driftnet_cmd = shift @ARGV;
2571 $driftnet_cmd = $default_driftnet_cmd;
2573 } elsif ($_ eq "-debug" || $_ eq "--debug") {
2574 my $which = shift @ARGV;
2575 my @rest = @search_methods;
2578 my $pct = shift @rest;
2579 my $name = shift @rest;
2580 my $tfn = shift @rest;
2582 if ($name eq $which) {
2583 @search_methods = (100, $name, $tfn);
2588 error "no such search method as \"$which\"" unless ($ok);
2589 LOG (1, "DEBUG: using only \"$which\"");
2592 print STDERR "$copyright\nusage: $progname " .
2593 "[-root] [-display dpy] [-verbose] [-debug which]\n" .
2594 "\t\t [-timeout secs] [-delay secs] [-filter cmd] [-filter2 cmd]\n" .
2595 "\t\t [-no-output] [-urls-only] [-background color] [-size WxH]\n" .
2596 "\t\t [-dictionary dictionary-file] [-http-proxy host[:port]]\n" .
2597 "\t\t [-driftnet [driftnet-program-and-args]]\n" .
2603 if ($http_proxy && $http_proxy eq "") {
2604 $http_proxy = undef;
2606 if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
2607 # historical suckage: allow "http://host:port" as well as "host:port".
2611 if (!$root_p && !$no_output_p) {
2612 print STDERR $copyright;
2613 error "the -root argument is mandatory (for now.)";
2616 if (!$no_output_p && !$ENV{DISPLAY}) {
2617 error "\$DISPLAY is not set.";
2621 if ($verbose == 1) {
2622 $verbose_imgmap = 1;
2623 $verbose_warnings = 1;
2625 } elsif ($verbose == 2) {
2626 $verbose_imgmap = 1;
2627 $verbose_warnings = 1;
2630 } elsif ($verbose == 3) {
2631 $verbose_imgmap = 1;
2632 $verbose_warnings = 1;
2634 $verbose_filter = 1;
2636 } elsif ($verbose == 4) {
2637 $verbose_imgmap = 1;
2638 $verbose_warnings = 1;
2640 $verbose_filter = 1;
2643 } elsif ($verbose == 5) {
2644 $verbose_imgmap = 1;
2645 $verbose_warnings = 1;
2647 $verbose_filter = 1;
2651 } elsif ($verbose == 6) {
2652 $verbose_imgmap = 1;
2653 $verbose_warnings = 1;
2655 $verbose_filter = 1;
2660 } elsif ($verbose >= 7) {
2661 $verbose_imgmap = 1;
2662 $verbose_warnings = 1;
2664 $verbose_filter = 1;
2672 error ("$dict does not exist") unless (-f $dict);
2680 spawn_driftnet ($driftnet_cmd) if ($driftnet_cmd);