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.103 $ }; $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> || "";
342 LOG ($verbose_http, " <== $_");
348 LOG ($verbose_http, " <== $_");
350 if (m@^Set-cookie:\s*([^;\r\n]+)@i) {
351 set_cookie($them, $1)
362 " <== [ body ]: $lines lines, " . length($body) . " bytes");
367 LOG (($verbose_net || $verbose_load), "null response: $url");
371 return ( $http, $head, $body );
373 die if ($@ && $@ ne "alarm\n"); # propagate errors
388 # returns two values: the document headers; and the document body.
389 # if the given URL did a redirect, returns the redirected-to document.
392 my ( $url, $referer, $timeout ) = @_;
395 if (defined($referer) && $referer eq $driftnet_magic) {
396 return get_driftnet_file ($url);
401 my $max_loop_count = 4;
404 if (defined($timeout) && $timeout <= 0) {
405 LOG (($verbose_net || $verbose_load), "timed out for $url");
410 my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
412 if (defined ($timeout)) {
414 my $elapsed = $now - $start;
415 $timeout -= $elapsed;
419 return () unless $http; # error message already printed
421 $http =~ s/[\r\n]+$//s;
423 if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
425 my ( $location ) = m@^location:[ \t]*(.*)$@im;
427 $location =~ s/[\r\n]$//;
429 LOG ($verbose_net, "redirect from $url to $location");
434 $referer =~ m@^(http://[^/]+)@i;
436 } elsif (! ($url =~ m@^[a-z]+:@i)) {
438 s@[^/]+$@@g if m@^http://[^/]+/@i;
439 $_ .= "/" if m@^http://[^/]+$@i;
444 LOG ($verbose_net, "no Location with \"$http\"");
445 return ( $url, $body );
448 if ($loop_count++ > $max_loop_count) {
450 "too many redirects ($max_loop_count) from $orig_url");
455 } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
457 LOG (($verbose_net || $verbose_load), "failed: $1 ($url)");
459 # http errors -- return nothing.
465 LOG (($verbose_net || $verbose_load), "document contains no data: $url");
471 return ( $url, $body );
477 # If we already have a cookie defined for this site, and the site is trying
478 # to overwrite that very same cookie, let it do so. This is because nytimes
479 # expires its cookies - it lets you upgrade to a new cookie without logging
480 # in again, but you have to present the old cookie to get the new cookie.
481 # So, by doing this, the built-in cypherpunks cookie will never go "stale".
484 my ($host, $cookie) = @_;
485 my $oc = $cookies{$host};
488 my ($oc_name, $oc_value) = m@^([^= \t\r\n]+)=(.*)$@;
490 my ($nc_name, $nc_value) = m@^([^= \t\r\n]+)=(.*)$@;
492 if ($oc_name eq $nc_name &&
493 $oc_value ne $nc_value) {
494 $cookies{$host} = $cookie;
495 LOG ($verbose_net, "overwrote ${host}'s $oc_name cookie");
500 ############################################################################
502 # Extracting image URLs from HTML
504 ############################################################################
506 # given a URL and the body text at that URL, selects and returns a random
507 # image from it. returns () if no suitable images found.
509 sub pick_image_from_body {
510 my ( $url, $body ) = @_;
515 # if there's at least one slash after the host, take off the last
517 if ( m@^http://[^/]+/@io ) {
518 $base =~ s@[^/]+$@@go;
521 # if there are no slashes after the host at all, put one on the end.
522 if ( m@^http://[^/]+$@io ) {
528 # strip out newlines, compress whitespace
535 # There are certain web sites that list huge numbers of dictionary
536 # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
537 # Porn sites tend not to be reputable!)
539 # I do not want webcollage to filter on content: I want it to select
540 # randomly from the set of images on the web. All the logic here for
541 # rejecting some images is really a set of heuristics for rejecting
542 # images that are not really images: for rejecting *text* that is in
543 # GIF/JPEG form. I don't want text, I want pictures, and I want the
544 # content of the pictures to be randomly selected from among all the
547 # So, filtering out "dirty" pictures by looking for "dirty" keywords
548 # would be wrong: dirty pictures exist, like it or not, so webcollage
549 # should be able to select them.
551 # However, picking a random URL is a hard thing to do. The mechanism I'm
552 # using is to search for a selection of random words. This is not
553 # perfect, but works ok most of the time. The way it breaks down is when
554 # some URLs get precedence because their pages list *every word* as
555 # related -- those URLs come up more often than others.
557 # So, after we've retrieved a URL, if it has too many keywords, reject
558 # it. We reject it not on the basis of what those keywords are, but on
559 # the basis that by having so many, the page has gotten an unfair
560 # advantage against our randomizer.
563 foreach my $trip (@tripwire_words) {
564 $trip_count++ if m/$trip/i;
567 if ($trip_count >= $#tripwire_words - 2) {
568 LOG (($verbose_filter || $verbose_load),
569 "there is probably a dictionary in \"$url\": rejecting.");
570 $rejected_urls{$url} = -1;
580 foreach (split(/ *</)) {
583 # Likewise, reject any web pages that have a KEYWORDS meta tag
586 if (m/name ?= ?\"?keywords\"?/i &&
587 m/content ?= ?\"([^\"]+)\"/) {
590 LOG (($verbose_filter || $verbose_load),
591 "excessive keywords ($L bytes) in $url: rejecting.");
592 $rejected_urls{$url} = $L;
597 LOG ($verbose_filter, " keywords ($L bytes) in $url (ok)");
601 } elsif ( m/^(img|a) .*(src|href) ?= ?\"? ?(.*?)[ >\"]/io ) {
603 my $was_inline = (! ( "$1" eq "a" || "$1" eq "A" ));
605 my ( $width ) = m/width ?=[ \"]*(\d+)/oi;
606 my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
611 ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
613 } elsif ( ! m@^[^/:?]+:@ ) {
616 1 while (s@/[^/]+/\.\./@/@g);
620 if ( ! m@^http://@io ) {
625 if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg)$@io ) {
629 # skip really short or really narrow images
630 if ( $width && $width < $min_width) {
631 if (!$height) { $height = "?"; }
632 LOG ($verbose_filter, " skip narrow image $_ (${width}x$height)");
636 if ( $height && $height < $min_height) {
637 if (!$width) { $width = "?"; }
638 LOG ($verbose_filter, " skip short image $_ (${width}x$height)");
642 # skip images with ratios that make them look like banners.
643 if ($min_ratio && $width && $height &&
644 ($width * $min_ratio ) > $height) {
645 if (!$height) { $height = "?"; }
646 LOG ($verbose_filter, " skip bad ratio $_ (${width}x$height)");
650 # skip GIFs with a small number of pixels -- those usually suck.
651 if ($width && $height &&
653 ($width * $height) < $min_gif_area) {
654 LOG ($verbose_filter, " skip small GIF $_ (${width}x$height)");
661 if ($unique_urls{$url}) {
662 LOG ($verbose_filter, " skip duplicate image $_");
666 LOG ($verbose_filter,
668 ($width && $height ? " (${width}x${height})" : "") .
669 ($was_inline ? " (inline)" : ""));
671 $urls[++$#urls] = $url;
672 $unique_urls{$url}++;
674 # jpegs are preferable to gifs.
676 if ( ! m@[.]gif$@io ) {
677 $urls[++$#urls] = $url;
680 # pointers to images are preferable to inlined images.
681 if ( ! $was_inline ) {
682 $urls[++$#urls] = $url;
683 $urls[++$#urls] = $url;
688 my $fsp = ($body =~ m@<frameset@i);
693 @urls = depoison (@urls);
696 LOG ($verbose_load, "no images on $base" . ($fsp ? " (frameset)" : ""));
700 # pick a random element of the table
701 my $i = int(rand($#urls+1));
704 LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
711 ############################################################################
713 # Subroutines for getting pages and images out of search engines
715 ############################################################################
718 sub pick_dictionary {
719 my @dicts = ("/usr/dict/words",
720 "/usr/share/dict/words",
721 "/usr/share/lib/dict/words");
722 foreach my $f (@dicts) {
728 error ("$dicts[0] does not exist") unless defined($wordlist);
731 # returns a random word from the dictionary
735 if (open (IN, "<$wordlist")) {
736 my $size = (stat(IN))[7];
737 my $pos = rand $size;
738 if (seek (IN, $pos, 0)) {
739 $word = <IN>; # toss partial line
740 $word = <IN>; # keep next line
749 return 0 if (!$word);
751 $word =~ s/^[ \t\n\r]+//;
752 $word =~ s/[ \t\n\r]+$//;
757 $word =~ s/ally$/al/;
758 $word =~ s/izes$/ize/;
759 $word =~ tr/A-Z/a-z/;
761 if ( $word =~ s/[ \t\n\r]/\+/g ) { # convert intra-word spaces to "+".
762 $word = "\%22$word\%22"; # And put quotes (%22) around it.
770 my $sep = ($or_p ? "%20OR%20" : "%20");
771 return (random_word . $sep .
781 $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
788 $s =~ s/%([a-z0-9]{2})/chr(hex($1))/ige;
793 # Loads the given URL (a search on some search engine) and returns:
794 # - the total number of hits the search engine claimed it had;
795 # - a list of URLs from the page that the search engine returned;
796 # Note that this list contains all kinds of internal search engine
797 # junk URLs too -- caller must prune them.
799 sub pick_from_search_engine {
800 my ( $timeout, $search_url, $words ) = @_;
805 print STDERR "\n\n" if ($verbose_load);
807 LOG ($verbose_load, "words: $_");
808 LOG ($verbose_load, "URL: $search_url");
810 $last_search = $search_url; # for warnings
813 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
814 if (defined ($timeout)) {
815 $timeout -= (time - $start);
818 LOG (($verbose_net || $verbose_load),
819 "timed out (late) for $search_url");
825 return () if (! $body);
830 my $search_count = "?";
831 if ($body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
833 } elsif ($body =~ m@<NOBR>((\d{1,3})(,\d{3})*) @i) {
835 } elsif ($body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
837 } elsif ($body =~ m@found about ((\d{1,3})(,\d{3})*|\d+) results@) {
839 } elsif ($body =~ m@\b\d+ - \d+ of (\d+)\b@i) { # avimages
841 } elsif ($body =~ m@About ((\d{1,3})(,\d{3})*) images@i) { # avimages
843 } elsif ($body =~ m@We found ((\d{1,3})(,\d{3})*|\d+) results@i) { # *vista
845 } elsif ($body =~ m@ of about <B>((\d{1,3})(,\d{3})*)<@i) { # googleimages
847 } elsif ($body =~ m@<B>((\d{1,3})(,\d{3})*)</B> Web sites were found@i) {
848 $search_count = $1; # lycos
849 } elsif ($body =~ m@WEB.*?RESULTS.*?\b((\d{1,3})(,\d{3})*)\b.*?Matches@i) {
850 $search_count = $1; # hotbot
851 } elsif ($body =~ m@no photos were found containing@i) { # avimages
853 } elsif ($body =~ m@found no document matching@i) { # avtext
856 1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
858 # if ($search_count eq "?" || $search_count eq "0") {
860 # my $file = "/tmp/wc.html";
861 # open(OUT, ">$file") || error ("writing $file: $!");
864 # print STDERR blurb() . "###### wrote $file\n";
868 my $length = length($body);
877 foreach (split(/\n/)) {
879 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
882 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
883 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
885 if ( $rejected_urls{$u} ) {
886 LOG ($verbose_filter, " pre-rejecting candidate: $u");
890 LOG ($verbose_http, " HREF: $u");
892 $subpages[++$#subpages] = $u;
895 if ( $#subpages < 0 ) {
896 LOG ($verbose_filter,
897 "found nothing on $base ($length bytes, $href_count links).");
901 LOG ($verbose_filter, "" . $#subpages+1 . " links on $search_url");
903 return ($search_count, @subpages);
911 my ($h) = m@^http://([^/: \t\r\n]+)@i;
913 next unless defined($h);
915 if ($poisoners{$h}) {
916 LOG (($verbose_filter), " rejecting poisoner: $_");
919 if ($h =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
921 LOG (($verbose_filter), " rejecting poisoner: $_");
924 if ($h =~ m@([^.]+\.[^.]+)$@ &&
926 LOG (($verbose_filter), " rejecting poisoner: $_");
936 # given a list of URLs, picks one at random; loads it; and returns a
937 # random image from it.
938 # returns the url of the page loaded; the url of the image chosen;
939 # and a debugging description string.
941 sub pick_image_from_pages {
942 my ($base, $total_hit_count, $unfiltered_link_count, $timeout, @pages) = @_;
944 $total_hit_count = "?" unless defined($total_hit_count);
946 @pages = depoison (@pages);
948 "" . ($#pages+1) . " candidates of $unfiltered_link_count links" .
949 " ($total_hit_count total)");
951 return () if ($#pages < 0);
953 my $i = int(rand($#pages+1));
954 my $page = $pages[$i];
956 LOG ($verbose_load, "picked page $page");
960 my ( $base2, $body2 ) = get_document ($page, $base, $timeout);
962 if (!$base2 || !$body2) {
967 my $img = pick_image_from_body ($base2, $body2);
971 return ($base2, $img);
978 ############################################################################
980 # Pick images from random pages returned by the Yahoo Random Link
982 ############################################################################
985 my $yahoo_random_link = "http://random.yahoo.com/bin/ryl";
988 # Picks a random page; picks a random image on that page;
989 # returns two URLs: the page containing the image, and the image.
990 # Returns () if nothing found this time.
992 sub pick_from_yahoo_random_link {
993 my ( $timeout ) = @_;
995 print STDERR "\n\n" if ($verbose_load);
996 LOG ($verbose_load, "URL: $yahoo_random_link");
998 $last_search = $yahoo_random_link; # for warnings
1000 $suppress_audit = 1;
1002 my ( $base, $body ) = get_document ($yahoo_random_link, undef, $timeout);
1003 if (!$base || !$body) {
1008 LOG ($verbose_load, "redirected to: $base");
1010 my $img = pick_image_from_body ($base, $body);
1014 return ($base, $img);
1021 ############################################################################
1023 # Pick images from random pages returned by the Alta Vista Random Link
1025 ############################################################################
1028 my $alta_vista_random_link = "http://www.altavista.com/image/randomlink";
1031 # Picks a random page; picks a random image on that page;
1032 # returns two URLs: the page containing the image, and the image.
1033 # Returns () if nothing found this time.
1035 sub pick_from_alta_vista_random_link {
1036 my ( $timeout ) = @_;
1038 print STDERR "\n\n" if ($verbose_load);
1039 LOG ($verbose_load, "URL: $alta_vista_random_link");
1041 $last_search = $alta_vista_random_link; # for warnings
1043 $suppress_audit = 1;
1045 my ( $base, $body ) = get_document ($alta_vista_random_link,
1047 if (!$base || !$body) {
1052 LOG ($verbose_load, "redirected to: $base");
1054 my $img = pick_image_from_body ($base, $body);
1058 return ($base, $img);
1065 ############################################################################
1067 # Pick images by feeding random words into Alta Vista Image Search
1069 ############################################################################
1072 my $alta_vista_images_url = "http://www.altavista.com/image/results" .
1073 "?ipht=1" . # photos
1074 "&igrph=1" . # graphics
1077 "&micat=1" . # no partner sites
1078 "&sc=on" . # "site collapse"
1082 sub pick_from_alta_vista_images {
1083 my ( $timeout ) = @_;
1085 my $words = random_words(0);
1086 my $page = (int(rand(9)) + 1);
1087 my $search_url = $alta_vista_images_url . $words;
1090 $search_url .= "&pgno=" . $page; # page number
1091 $search_url .= "&stq=" . (($page-1) * 12); # first hit result on page
1094 my ($search_hit_count, @subpages) =
1095 pick_from_search_engine ($timeout, $search_url, $words);
1097 my @candidates = ();
1098 foreach my $u (@subpages) {
1100 # avtext is encoding their URLs now.
1101 next unless ($u =~ m@^/r.*\&r=([^&]+).*@);
1102 $u = url_unquote($1);
1104 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1105 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
1106 next if ($u =~ m@[/.]doubleclick\.net\b@i); # you cretins
1107 next if ($u =~ m@[/.]clicktomarket\.com\b@i); # more cretins
1109 next if ($u =~ m@[/.]viewimages\.com\b@i); # stacked deck
1110 next if ($u =~ m@[/.]gettyimages\.com\b@i);
1112 LOG ($verbose_filter, " candidate: $u");
1113 push @candidates, $u;
1116 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1117 $timeout, @candidates);
1122 ############################################################################
1124 # Pick images by feeding random words into Google Image Search.
1125 # By Charles Gales <gales@us.ibm.com>
1127 ############################################################################
1130 my $google_images_url = "http://images.google.com/images" .
1131 "?site=images" . # photos
1132 "&btnG=Search" . # graphics
1133 "&safe=off" . # no screening
1138 sub pick_from_google_images {
1139 my ( $timeout ) = @_;
1141 my $words = random_word; # only one word for Google
1142 my $page = (int(rand(9)) + 1);
1143 my $num = 20; # 20 images per page
1144 my $search_url = $google_images_url . $words;
1147 $search_url .= "&start=" . $page*$num; # page number
1148 $search_url .= "&num=" . $num; #images per page
1151 my ($search_hit_count, @subpages) =
1152 pick_from_search_engine ($timeout, $search_url, $words);
1154 my @candidates = ();
1155 foreach my $u (@subpages) {
1156 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1157 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1159 if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1161 LOG ($verbose_filter, " candidate: $urlf");
1162 push @candidates, $urlf;
1166 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1167 $timeout, @candidates);
1172 ############################################################################
1174 # Pick images by feeding random *numbers* into Google Image Search.
1175 # By jwz, suggested by from Ian O'Donnell.
1177 ############################################################################
1181 sub pick_from_google_image_numbers {
1182 my ( $timeout ) = @_;
1185 my $number = int(rand($max));
1187 $number = sprintf("%04d", $number)
1190 my $words = "$number";
1191 my $page = (int(rand(40)) + 1);
1192 my $num = 20; # 20 images per page
1193 my $search_url = $google_images_url . $words;
1196 $search_url .= "&start=" . $page*$num; # page number
1197 $search_url .= "&num=" . $num; #images per page
1200 my ($search_hit_count, @subpages) =
1201 pick_from_search_engine ($timeout, $search_url, $words);
1203 my @candidates = ();
1205 foreach my $u (@subpages) {
1206 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1207 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1209 if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1211 my $img = "http://$1";
1213 LOG ($verbose_filter, " candidate: $ref");
1214 push @candidates, $img;
1215 $referers{$img} = $ref;
1219 @candidates = depoison (@candidates);
1220 return () if ($#candidates < 0);
1221 my $i = int(rand($#candidates+1));
1222 my $img = $candidates[$i];
1223 my $ref = $referers{$img};
1225 LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1226 return ($ref, $img);
1231 ############################################################################
1233 # Pick images by feeding random words into Alta Vista Text Search
1235 ############################################################################
1238 my $alta_vista_url = "http://www.altavista.com/web/results" .
1242 "&sc=on" . # "site collapse"
1247 sub pick_from_alta_vista_text {
1248 my ( $timeout ) = @_;
1250 my $words = random_words(0);
1251 my $page = (int(rand(9)) + 1);
1252 my $search_url = $alta_vista_url . $words;
1255 $search_url .= "&pgno=" . $page;
1256 $search_url .= "&stq=" . (($page-1) * 10);
1259 my ($search_hit_count, @subpages) =
1260 pick_from_search_engine ($timeout, $search_url, $words);
1262 my @candidates = ();
1263 foreach my $u (@subpages) {
1265 # Those altavista fuckers are playing really nasty redirection games
1266 # these days: the filter your clicks through their site, but use
1267 # onMouseOver to make it look like they're not! Well, it makes it
1268 # easier for us to identify search results...
1270 next unless ($u =~ m@^/r.*\&r=([^&]+).*@);
1271 $u = url_unquote($1);
1273 LOG ($verbose_filter, " candidate: $u");
1274 push @candidates, $u;
1277 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1278 $timeout, @candidates);
1283 ############################################################################
1285 # Pick images by feeding random words into Hotbot
1287 ############################################################################
1289 my $hotbot_search_url =("http://hotbot.lycos.com/default.asp" .
1291 "&descriptiontype=0" .
1298 "&cobrand=undefined" .
1301 sub pick_from_hotbot_text {
1302 my ( $timeout ) = @_;
1304 # lycos seems to always give us back dictionaries and word lists if
1305 # we search for more than one word...
1307 my $words = random_word();
1309 my $start = int(rand(8)) * 10 + 1;
1310 my $search_url = $hotbot_search_url . $words . "&first=$start&page=more";
1312 my ($search_hit_count, @subpages) =
1313 pick_from_search_engine ($timeout, $search_url, $words);
1315 my @candidates = ();
1316 foreach my $u (@subpages) {
1318 # Hotbot plays redirection games too
1319 next unless ($u =~ m@/director.asp\?.*\btarget=([^&]+)@);
1320 $u = url_decode($1);
1322 LOG ($verbose_filter, " candidate: $u");
1323 push @candidates, $u;
1326 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1327 $timeout, @candidates);
1332 ############################################################################
1334 # Pick images by feeding random words into Lycos
1336 ############################################################################
1338 my $lycos_search_url = "http://search.lycos.com/default.asp" .
1344 sub pick_from_lycos_text {
1345 my ( $timeout ) = @_;
1347 # lycos seems to always give us back dictionaries and word lists if
1348 # we search for more than one word...
1350 my $words = random_word();
1352 my $start = int(rand(8)) * 10 + 1;
1353 my $search_url = $lycos_search_url . $words . "&first=$start&page=more";
1355 my ($search_hit_count, @subpages) =
1356 pick_from_search_engine ($timeout, $search_url, $words);
1358 my @candidates = ();
1359 foreach my $u (@subpages) {
1361 # Lycos plays redirection games.
1362 next unless ($u =~ m@^http://click.lycos.com/director.asp
1367 $u = url_decode($1);
1369 LOG ($verbose_filter, " candidate: $u");
1370 push @candidates, $u;
1373 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1374 $timeout, @candidates);
1379 ############################################################################
1381 # Pick images by feeding random words into news.yahoo.com
1383 ############################################################################
1385 my $yahoo_news_url = "http://search.news.yahoo.com/search/news" .
1388 "&s=-%24s%2C-date" .
1396 sub pick_from_yahoo_news_text {
1397 my ( $timeout ) = @_;
1399 my $words = random_words(0);
1400 my $search_url = $yahoo_news_url . $words;
1402 my ($search_hit_count, @subpages) =
1403 pick_from_search_engine ($timeout, $search_url, $words);
1405 my @candidates = ();
1406 foreach my $u (@subpages) {
1407 # only accept URLs on Yahoo's news site
1408 next unless ($u =~ m@^http://dailynews\.yahoo\.com/@i ||
1409 $u =~ m@^http://story\.news\.yahoo\.com/@i);
1411 LOG ($verbose_filter, " candidate: $u");
1412 push @candidates, $u;
1415 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1416 $timeout, @candidates);
1422 ############################################################################
1424 # Pick images by waiting for driftnet to populate a temp dir with files.
1425 # Requires driftnet version 0.1.5 or later.
1426 # (Driftnet is a program by Chris Lightfoot that sniffs your local ethernet
1427 # for images being downloaded by others.)
1428 # Driftnet/webcollage integration by jwz.
1430 ############################################################################
1433 sub pick_from_driftnet {
1434 my ( $timeout ) = @_;
1436 my $id = $driftnet_magic;
1437 my $dir = $driftnet_dir;
1441 error ("\$driftnet_dir unset?") unless ($dir);
1444 error ("$dir unreadable") unless (-d "$dir/.");
1446 $timeout = $http_timeout unless ($timeout);
1449 while ($now = time, $now < $start + $timeout) {
1451 opendir (DIR, $dir) || error ("$dir: $!");
1452 while (my $file = readdir(DIR)) {
1453 next if ($file =~ m/^\./);
1454 $file = "$dir/$file";
1456 LOG ($verbose_load, "picked file $file ($id)");
1457 return ($id, $file);
1461 LOG (($verbose_net || $verbose_load), "timed out for $id");
1466 sub get_driftnet_file {
1469 error ("\$driftnet_dir unset?") unless ($driftnet_dir);
1471 my $id = $driftnet_magic;
1472 my $re = qr/$driftnet_dir/;
1473 error ("$id: $file not in $driftnet_dir?")
1474 unless ($file =~ m@^$re@o);
1477 open (IN, $file) || error ("$id: $file: $!");
1479 while (<IN>) { $body .= $_; }
1480 close IN || error ("$id: $file: $!");
1481 unlink ($file) || error ("$id: $file: rm: $!");
1482 return ($id, $body);
1486 sub spawn_driftnet {
1489 # make a directory to use.
1491 my $tmp = $ENV{TEMPDIR} || "/tmp";
1492 $driftnet_dir = sprintf ("$tmp/driftcollage-%08x", rand(0xffffffff));
1493 LOG ($verbose_exec, "mkdir $driftnet_dir");
1494 last if mkdir ($driftnet_dir, 0700);
1497 if (! ($cmd =~ m/\s/)) {
1498 # if the command didn't have any arguments in it, then it must be just
1499 # a pointer to the executable. Append the default args to it.
1500 my $dargs = $default_driftnet_cmd;
1501 $dargs =~ s/^[^\s]+//;
1505 # point the driftnet command at our newly-minted private directory.
1507 $cmd .= " -d $driftnet_dir";
1508 $cmd .= ">/dev/null" unless ($verbose_exec);
1511 if ($pid < 0) { error ("fork: $!\n"); }
1514 push @pids_to_kill, $pid;
1515 LOG ($verbose_exec, "forked for \"$cmd\"");
1518 nontrapping_system ($cmd) || error ("exec: $!");
1521 # wait a bit, then make sure the process actually started up.
1524 error ("pid $pid failed to start \"$cmd\"")
1525 unless (1 == kill (0, $pid));
1529 ############################################################################
1531 # Pick a random image in a random way
1533 ############################################################################
1536 # Picks a random image on a random page, and returns two URLs:
1537 # the page containing the image, and the image.
1538 # Returns () if nothing found this time.
1542 my ( $timeout ) = @_;
1544 $current_state = "select";
1545 $load_method = "none";
1547 my $n = int(rand(100));
1550 my @rest = @search_methods;
1553 my $pct = shift @rest;
1554 my $name = shift @rest;
1555 my $tfn = shift @rest;
1557 if ($total > $n && !defined($fn)) {
1559 $current_state = $name;
1560 $load_method = $current_state;
1564 if ($total != 100) {
1565 error ("internal error: \@search_methods totals to $total%!");
1568 record_attempt ($current_state);
1569 return $fn->($timeout);
1574 ############################################################################
1576 # Statistics and logging
1578 ############################################################################
1581 return strftime ("%H:%M:%S: ", localtime);
1585 return "$progname: " . timestr() . "$current_state: ";
1590 print STDERR blurb() . "$err\n";
1602 my $head = "$progname: DEBUG: ";
1603 foreach (split (/\n/, $lastlog)) {
1604 print STDERR "$head$_\n";
1610 my ($print, $msg) = @_;
1611 my $blurb = timestr() . "$current_state: ";
1612 $lastlog .= "$blurb$msg\n";
1613 print STDERR "$progname: $blurb$msg\n" if $print;
1618 my %stats_successes;
1621 my $last_state = undef;
1622 sub record_attempt {
1626 record_failure($last_state) unless ($image_succeeded > 0);
1628 $last_state = $name;
1631 report_performance();
1634 $image_succeeded = 0;
1635 $suppress_audit = 0;
1638 sub record_success {
1639 my ($name, $url, $base) = @_;
1640 if (defined($stats_successes{$name})) {
1641 $stats_successes{$name}++;
1643 $stats_successes{$name} = 1;
1646 stop_timer ($name, 1);
1647 my $o = $current_state;
1648 $current_state = $name;
1649 save_recent_url ($url, $base);
1650 $current_state = $o;
1651 $image_succeeded = 1;
1656 sub record_failure {
1659 return if $image_succeeded;
1661 stop_timer ($name, 0);
1662 if ($verbose_load && !$verbose_exec) {
1664 if ($suppress_audit) {
1665 print STDERR "$progname: " . timestr() . "(audit log suppressed)\n";
1669 my $o = $current_state;
1670 $current_state = "DEBUG";
1672 my $line = "#" x 78;
1673 print STDERR "\n\n\n";
1674 print STDERR ("#" x 78) . "\n";
1675 print STDERR blurb() . "failed to get an image. Full audit log:\n";
1678 print STDERR ("-" x 78) . "\n";
1679 print STDERR "\n\n";
1681 $current_state = $o;
1683 $image_succeeded = 0;
1690 my $i = $stats_successes{$name};
1691 my $j = $stats_attempts{$name};
1694 return "" . ($j ? int($i * 100 / $j) : "0") . "%";
1698 my $current_start_time = 0;
1702 $current_start_time = time;
1704 if (defined($stats_attempts{$name})) {
1705 $stats_attempts{$name}++;
1707 $stats_attempts{$name} = 1;
1709 if (!defined($stats_elapsed{$name})) {
1710 $stats_elapsed{$name} = 0;
1715 my ($name, $success) = @_;
1716 $stats_elapsed{$name} += time - $current_start_time;
1720 my $last_report_time = 0;
1721 sub report_performance {
1723 return unless $verbose_warnings;
1726 return unless ($now >= $last_report_time + $report_performance_interval);
1727 my $ot = $last_report_time;
1728 $last_report_time = $now;
1730 return if ($ot == 0);
1732 my $blurb = "$progname: " . timestr();
1735 print STDERR "${blurb}Current standings:\n";
1737 foreach my $name (sort keys (%stats_attempts)) {
1738 my $try = $stats_attempts{$name};
1739 my $suc = $stats_successes{$name} || 0;
1740 my $pct = int($suc * 100 / $try);
1741 my $secs = $stats_elapsed{$name};
1742 my $secs_link = int($secs / $try);
1743 print STDERR sprintf ("$blurb %-12s %4s (%d/%d);\t %2d secs/link\n",
1744 "$name:", "$pct%", $suc, $try, $secs_link);
1750 my $max_recent_images = 400;
1751 my $max_recent_sites = 20;
1752 my @recent_images = ();
1753 my @recent_sites = ();
1755 sub save_recent_url {
1756 my ($url, $base) = @_;
1758 return unless ($verbose_warnings);
1761 my ($site) = m@^http://([^ \t\n\r/:]+)@;
1762 return unless defined ($site);
1764 if ($base eq $driftnet_magic) {
1765 $site = $driftnet_magic;
1766 @recent_images = ();
1770 foreach (@recent_images) {
1772 print STDERR blurb() . "WARNING: recently-duplicated image: $url" .
1773 " (on $base via $last_search)\n";
1779 # suppress "duplicate site" warning via %warningless_sites.
1781 if ($warningless_sites{$site}) {
1783 } elsif ($site =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
1784 $warningless_sites{$1}) {
1786 } elsif ($site =~ m@([^.]+\.[^.]+)$@ &&
1787 $warningless_sites{$1}) {
1792 foreach (@recent_sites) {
1794 print STDERR blurb() . "WARNING: recently-duplicated site: $site" .
1795 " ($url on $base via $last_search)\n";
1801 push @recent_images, $url;
1802 push @recent_sites, $site;
1803 shift @recent_images if ($#recent_images >= $max_recent_images);
1804 shift @recent_sites if ($#recent_sites >= $max_recent_sites);
1809 ##############################################################################
1813 ##############################################################################
1820 s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
1825 # Given the raw body of a GIF document, returns the dimensions of the image.
1829 my $type = substr($body, 0, 6);
1831 return () unless ($type =~ /GIF8[7,9]a/);
1832 $s = substr ($body, 6, 10);
1833 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
1834 return (($b<<8|$a), ($d<<8|$c));
1837 # Given the raw body of a JPEG document, returns the dimensions of the image.
1842 my $L = length($body);
1844 my $c1 = substr($body, $i, 1); $i++;
1845 my $c2 = substr($body, $i, 1); $i++;
1846 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
1849 while (ord($ch) != 0xDA && $i < $L) {
1850 # Find next marker, beginning with 0xFF.
1851 while (ord($ch) != 0xFF) {
1852 return () if (length($body) <= $i);
1853 $ch = substr($body, $i, 1); $i++;
1855 # markers can be padded with any number of 0xFF.
1856 while (ord($ch) == 0xFF) {
1857 return () if (length($body) <= $i);
1858 $ch = substr($body, $i, 1); $i++;
1861 # $ch contains the value of the marker.
1862 my $marker = ord($ch);
1864 if (($marker >= 0xC0) &&
1865 ($marker <= 0xCF) &&
1866 ($marker != 0xC4) &&
1867 ($marker != 0xCC)) { # it's a SOFn marker
1869 return () if (length($body) <= $i);
1870 my $s = substr($body, $i, 4); $i += 4;
1871 my ($a,$b,$c,$d) = unpack("C"x4, $s);
1872 return (($c<<8|$d), ($a<<8|$b));
1875 # We must skip variables, since FFs in variable names aren't
1876 # valid JPEG markers.
1877 return () if (length($body) <= $i);
1878 my $s = substr($body, $i, 2); $i += 2;
1879 my ($c1, $c2) = unpack ("C"x2, $s);
1880 my $length = ($c1 << 8) | $c2;
1881 return () if ($length < 2);
1888 # Given the raw body of a GIF or JPEG document, returns the dimensions of
1893 my ($w, $h) = gif_size ($body);
1894 if ($w && $h) { return ($w, $h); }
1895 return jpeg_size ($body);
1899 # returns the full path of the named program, or undef.
1903 foreach (split (/:/, $ENV{PATH})) {
1904 if (-x "$_/$prog") {
1912 # Like rand(), but chooses numbers with a bell curve distribution.
1915 $_ = 1.0 unless defined($_);
1917 return (rand($_) + rand($_) + rand($_));
1921 sub signal_cleanup {
1923 print STDERR blurb() . (defined($sig)
1924 ? "caught signal $sig."
1931 if (@pids_to_kill) {
1932 print STDERR blurb() . "killing: " . join(' ', @pids_to_kill) . "\n";
1933 kill ('TERM', @pids_to_kill);
1939 ##############################################################################
1941 # Generating a list of urls only
1943 ##############################################################################
1945 sub url_only_output {
1947 my ($base, $img) = pick_image;
1951 print "$img $base\n";
1956 ##############################################################################
1958 # Running as an xscreensaver module
1960 ##############################################################################
1962 my $image_ppm = ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp") . "/webcollage." . $$;
1963 my $image_tmp1 = $image_ppm . "-1";
1964 my $image_tmp2 = $image_ppm . "-2";
1966 my $filter_cmd = undef;
1967 my $post_filter_cmd = undef;
1968 my $background = undef;
1970 my $img_width; # size of the image being generated.
1976 unlink $image_ppm, $image_tmp1, $image_tmp2;
1980 # Like system, but prints status about exit codes, and kills this process
1981 # with whatever signal killed the sub-process, if any.
1983 sub nontrapping_system {
1987 s/\"[^\"]+\"/\"...\"/g;
1989 LOG ($verbose_exec, "executing \"$_\"");
1994 LOG ($verbose_exec, "subproc exited normally.");
1995 } elsif (($rc & 0xff) == 0) {
1997 LOG ($verbose_exec, "subproc exited with status $rc.");
2000 LOG ($verbose_exec, "subproc dumped core.");
2003 LOG ($verbose_exec, "subproc died with signal $rc.");
2004 # die that way ourselves.
2012 # Given the URL of a GIF or JPEG image, and the body of that image, writes a
2013 # PPM to the given output file. Returns the width/height of the image if
2017 my ($url, $body, $output) = @_;
2018 my ($cmd, $cmd2, $w, $h);
2020 if ((@_ = gif_size ($body))) {
2023 } elsif ((@_ = jpeg_size ($body))) {
2027 LOG (($verbose_pbm || $verbose_load),
2028 "not a GIF or JPG" .
2029 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
2030 ? " (looks like HTML)" : "") .
2032 $suppress_audit = 1;
2036 $cmd2 = "exec $cmd"; # yes, this really is necessary. if we don't
2037 # do this, the process doesn't die properly.
2038 if (!$verbose_pbm) {
2040 # We get a "giftopnm: got a 'Application Extension' extension"
2041 # warning any time it's an animgif.
2043 # Note that "giftopnm: EOF / read error on image data" is not
2044 # always a fatal error -- sometimes the image looks fine anyway.
2046 $cmd2 .= " 2>/dev/null";
2049 # There exist corrupted GIF and JPEG files that can make giftopnm and
2050 # djpeg lose their minds and go into a loop. So this gives those programs
2051 # a small timeout -- if they don't complete in time, kill them.
2057 local $SIG{ALRM} = sub {
2059 "timed out ($cvt_timeout) for $cmd on \"$url\" in pid $pid");
2060 kill ('TERM', $pid) if ($pid);
2065 if (($pid = open(PIPE, "| $cmd2 > $output"))) {
2072 LOG ($verbose_exec, "awaiting $pid");
2074 LOG ($verbose_exec, "$pid completed");
2076 my $size = (stat($output))[7];
2077 $size = -1 unless defined($size);
2079 LOG ($verbose_pbm, "$cmd on ${w}x$h \"$url\" failed ($size bytes)");
2083 LOG ($verbose_pbm, "created ${w}x$h $output ($cmd)");
2086 print STDERR blurb() . "$cmd failed: $!\n";
2090 die if ($@ && $@ ne "alarm\n"); # propagate errors
2103 sub pick_root_displayer {
2106 foreach my $cmd (@root_displayers) {
2108 my ($name) = m/^([^ ]+)/;
2109 push @names, "\"$name\"";
2110 LOG ($verbose_exec, "looking for $name...");
2111 foreach my $dir (split (/:/, $ENV{PATH})) {
2112 LOG ($verbose_exec, " checking $dir/$name");
2113 return $cmd if (-x "$dir/$name");
2117 $names[$#names] = "or " . $names[$#names];
2118 error "none of: " . join (", ", @names) . " were found on \$PATH.";
2122 my $ppm_to_root_window_cmd = undef;
2125 sub x_or_pbm_output {
2127 # Check for our helper program, to see whether we need to use PPM pipelines.
2129 $_ = "webcollage-helper";
2130 if (defined ($webcollage_helper) || which ($_)) {
2131 $webcollage_helper = $_ unless (defined($webcollage_helper));
2132 LOG ($verbose_pbm, "found \"$webcollage_helper\"");
2133 $webcollage_helper .= " -v";
2135 LOG (($verbose_pbm || $verbose_load), "no $_ program");
2138 # make sure the various programs we execute exist, right up front.
2140 my @progs = ("ppmmake"); # always need this one
2142 if (!defined($webcollage_helper)) {
2143 # Only need these others if we don't have the helper.
2144 @progs = (@progs, "giftopnm", "djpeg", "pnmpaste", "pnmscale", "pnmcut");
2148 which ($_) || error "$_ not found on \$PATH.";
2151 # find a root-window displayer program.
2153 $ppm_to_root_window_cmd = pick_root_displayer();
2155 if (!$img_width || !$img_height) {
2157 which ($_) || error "$_ not found on \$PATH.";
2159 ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
2160 if (!defined($img_height)) {
2161 error "xdpyinfo failed.";
2165 my $bgcolor = "#000000";
2166 my $bgimage = undef;
2169 if ($background =~ m/^\#[0-9a-f]+$/i) {
2170 $bgcolor = $background;
2172 } elsif (-r $background) {
2173 $bgimage = $background;
2175 } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
2176 error "not a color or readable file: $background";
2179 # default to assuming it's a color
2180 $bgcolor = $background;
2184 # Create the sold-colored base image.
2186 $_ = "ppmmake '$bgcolor' $img_width $img_height";
2187 LOG ($verbose_pbm, "creating base image: $_");
2188 nontrapping_system "$_ > $image_ppm";
2190 # Paste the default background image in the middle of it.
2197 open(IMG, "<$bgimage") || error "couldn't open $bgimage: $!";
2199 while (<IMG>) { $body .= $_; }
2202 if ((@_ = gif_size ($body))) {
2204 $cmd = "giftopnm |";
2206 } elsif ((@_ = jpeg_size ($body))) {
2210 } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
2216 error "$bgimage is not a GIF, JPEG, or PPM.";
2219 my $x = int (($img_width - $iw) / 2);
2220 my $y = int (($img_height - $ih) / 2);
2222 "pasting $bgimage (${iw}x$ih) into base image at $x,$y");
2224 $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
2225 open (IMG, "| $cmd") || error "running $cmd: $!";
2229 LOG ($verbose_exec, "subproc exited normally.");
2230 rename ($image_tmp1, $image_ppm) ||
2231 error "renaming $image_tmp1 to $image_ppm: $!";
2237 my ($base, $img) = pick_image();
2238 my $source = $current_state;
2239 $current_state = "loadimage";
2241 my ($headers, $body) = get_document ($img, $base);
2243 paste_image ($base, $img, $body, $source);
2247 $current_state = "idle";
2248 $load_method = "none";
2250 unlink $image_tmp1, $image_tmp2;
2256 my ($base, $img, $body, $source) = @_;
2258 $current_state = "paste";
2260 $suppress_audit = 0;
2262 LOG ($verbose_pbm, "got $img (" . length($body) . ")");
2266 # If we are using the webcollage-helper, then we do not need to convert this
2267 # image to a PPM. But, if we're using a filter command, we still must, since
2268 # that's what the filters expect (webcollage-helper can read PPMs, so that's
2271 if (defined ($webcollage_helper) &&
2272 !defined ($filter_cmd)) {
2274 ($iw, $ih) = image_size ($body);
2276 LOG (($verbose_pbm || $verbose_load),
2277 "not a GIF or JPG" .
2278 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
2279 ? " (looks like HTML)" : "") .
2281 $suppress_audit = 1;
2287 open (OUT, ">$image_tmp1") || error ("writing $image_tmp1: $!");
2288 print OUT $body || error ("writing $image_tmp1: $!");
2289 close OUT || error ("writing $image_tmp1: $!");
2292 ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
2295 LOG ($verbose_pbm, "unable to make PBM from $img");
2300 record_success ($load_method, $img, $base);
2303 my $ow = $iw; # used only for error messages
2306 # don't just tack this onto the front of the pipeline -- we want it to
2307 # be able to change the size of the input image.
2310 LOG ($verbose_pbm, "running $filter_cmd");
2312 my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
2314 LOG(($verbose_pbm || $verbose_load), "failed command: \"$filter_cmd\"");
2315 LOG(($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2318 rename ($image_tmp2, $image_tmp1);
2320 # re-get the width/height in case the filter resized it.
2322 open(IMG, "<$image_tmp1") || return 0;
2325 ($iw, $ih) = m/^(\d+) (\d+)$/;
2327 return 0 unless ($iw && $ih);
2330 my $target_w = $img_width;
2331 my $target_h = $img_height;
2337 # Usually scale the image to fit on the screen -- but sometimes scale it
2338 # to fit on half or a quarter of the screen. Note that we don't merely
2339 # scale it to fit, we instead cut it in half until it fits -- that should
2340 # give a wider distribution of sizes.
2342 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2343 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2345 if ($iw > $target_w || $ih > $target_h) {
2346 while ($iw > $target_w ||
2351 if ($iw <= 10 || $ih <= 10) {
2352 LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus.");
2356 LOG ($verbose_pbm, "scaling to ${iw}x$ih");
2358 $cmd .= " | pnmscale -xsize $iw -ysize $ih";
2362 my $src = $image_tmp1;
2364 my $crop_x = 0; # the sub-rectangle of the image
2365 my $crop_y = 0; # that we will actually paste.
2369 # The chance that we will randomly crop out a section of an image starts
2370 # out fairly low, but goes up for images that are very large, or images
2371 # that have ratios that make them look like banners (we try to avoid
2372 # banner images entirely, but they slip through when the IMG tags didn't
2373 # have WIDTH and HEIGHT specified.)
2375 my $crop_chance = 0.2;
2376 if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
2377 $crop_chance += 0.2;
2379 if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
2380 $crop_chance += 0.2;
2382 if ($min_ratio && ($iw * $min_ratio) > $ih) {
2383 $crop_chance += 0.7;
2386 if ($crop_chance > 0.1) {
2387 LOG ($verbose_pbm, "crop chance: $crop_chance");
2390 if (rand() < $crop_chance) {
2395 if ($crop_w > $min_width) {
2396 # if it's a banner, select the width linearly.
2397 # otherwise, select a bell.
2398 my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
2401 $crop_w = $min_width + int ($r * ($crop_w - $min_width));
2402 $crop_x = int (rand() * ($ow - $crop_w));
2404 if ($crop_h > $min_height) {
2405 # height always selects as a bell.
2406 $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
2407 $crop_y = int (rand() * ($oh - $crop_h));
2410 if ($crop_x != 0 || $crop_y != 0 ||
2411 $crop_w != $iw || $crop_h != $ih) {
2413 "randomly cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2417 # Where the image should logically land -- this might be negative.
2419 my $x = int((rand() * ($img_width + $crop_w/2)) - $crop_w*3/4);
2420 my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
2422 # if we have chosen to paste the image outside of the rectangle of the
2423 # screen, then we need to crop it.
2427 $x + $crop_w > $img_width ||
2428 $y + $crop_h > $img_height) {
2431 "cropping for effective paste of ${crop_w}x$crop_h \@ $x,$y");
2433 if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
2434 if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
2436 if ($x + $crop_w >= $img_width) { $crop_w = $img_width - $x - 1; }
2437 if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
2440 # If any cropping needs to happen, add pnmcut.
2442 if ($crop_x != 0 || $crop_y != 0 ||
2443 $crop_w != $iw || $crop_h != $ih) {
2446 $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
2447 LOG ($verbose_pbm, "cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2450 LOG ($verbose_pbm, "pasting ${iw}x$ih \@ $x,$y in $image_ppm");
2452 $cmd .= " | pnmpaste - $x $y $image_ppm";
2454 $cmd =~ s@^ *\| *@@;
2456 if (defined ($webcollage_helper)) {
2457 $cmd = "$webcollage_helper $image_tmp1 $image_ppm " .
2458 "$scale $opacity " .
2459 "$crop_x $crop_y $x $y " .
2464 # use a PPM pipeline
2466 $_ .= " < $image_tmp1 > $image_tmp2";
2470 $_ = "($_) 2>&1 | sed s'/^/" . blurb() . "/'";
2472 $_ .= " 2> /dev/null";
2475 my $rc = nontrapping_system ($_);
2477 if (defined ($webcollage_helper) && -z $image_ppm) {
2478 LOG (1, "failed command: \"$cmd\"");
2479 print STDERR "\naudit log:\n\n\n";
2480 print STDERR ("#" x 78) . "\n";
2481 print STDERR blurb() . "$image_ppm has zero size\n";
2483 print STDERR "\n\n";
2488 LOG (($verbose_pbm || $verbose_load), "failed command: \"$cmd\"");
2489 LOG (($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2493 if (!defined ($webcollage_helper)) {
2494 rename ($image_tmp2, $image_ppm) || return;
2497 my $target = "$image_ppm";
2499 # don't just tack this onto the end of the pipeline -- we don't want it
2500 # to end up in $image_ppm, because we don't want the results to be
2503 if ($post_filter_cmd) {
2507 $target = $image_tmp1;
2508 if (!defined ($webcollage_helper)) {
2509 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2511 # Blah, my scripts need the JPEG data, but some other folks need
2512 # the PPM data -- what to do? Ignore the problem, that's what!
2513 # $cmd = "djpeg < $image_ppm | ($post_filter_cmd) > $target";
2514 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2517 $rc = nontrapping_system ($cmd);
2519 LOG ($verbose_pbm, "filter failed: \"$post_filter_cmd\"\n");
2524 if (!$no_output_p) {
2525 my $tsize = (stat($target))[7];
2527 $cmd = "$ppm_to_root_window_cmd $target";
2529 # xv seems to hate being killed. it tends to forget to clean
2530 # up after itself, and leaves windows around and colors allocated.
2531 # I had this same problem with vidwhacker, and I'm not entirely
2532 # sure what I did to fix it. But, let's try this: launch xv
2533 # in the background, so that killing this process doesn't kill it.
2534 # it will die of its own accord soon enough. So this means we
2535 # start pumping bits to the root window in parallel with starting
2536 # the next network retrieval, which is probably a better thing
2541 $rc = nontrapping_system ($cmd);
2544 LOG (($verbose_pbm || $verbose_load), "display failed: \"$cmd\"");
2549 LOG ($verbose_pbm, "$target size is $tsize");
2553 $source .= "-" . stats_of($source);
2554 print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n"
2555 if ($verbose_imgmap);
2565 $SIG{HUP} = \&signal_cleanup;
2566 $SIG{INT} = \&signal_cleanup;
2567 $SIG{QUIT} = \&signal_cleanup;
2568 $SIG{ABRT} = \&signal_cleanup;
2569 $SIG{KILL} = \&signal_cleanup;
2570 $SIG{TERM} = \&signal_cleanup;
2572 # Need this so that if giftopnm dies, we don't die.
2573 $SIG{PIPE} = 'IGNORE';
2576 END { signal_cleanup(); }
2585 my $driftnet_cmd = 0;
2587 $current_state = "init";
2588 $load_method = "none";
2592 # historical suckage: the environment variable name is lower case.
2593 $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
2595 while ($_ = $ARGV[0]) {
2597 if ($_ eq "-display" ||
2603 $ENV{DISPLAY} = shift @ARGV;
2604 } elsif ($_ eq "-root") {
2606 } elsif ($_ eq "-no-output") {
2608 } elsif ($_ eq "-urls-only") {
2611 } elsif ($_ eq "-verbose") {
2613 } elsif (m/^-v+$/) {
2614 $verbose += length($_)-1;
2615 } elsif ($_ eq "-delay") {
2616 $delay = shift @ARGV;
2617 } elsif ($_ eq "-timeout") {
2618 $http_timeout = shift @ARGV;
2619 } elsif ($_ eq "-filter") {
2620 $filter_cmd = shift @ARGV;
2621 } elsif ($_ eq "-filter2") {
2622 $post_filter_cmd = shift @ARGV;
2623 } elsif ($_ eq "-background" || $_ eq "-bg") {
2624 $background = shift @ARGV;
2625 } elsif ($_ eq "-size") {
2627 if (m@^(\d+)x(\d+)$@) {
2631 error "argument to \"-size\" must be of the form \"640x400\"";
2633 } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
2634 $http_proxy = shift @ARGV;
2635 } elsif ($_ eq "-dictionary" || $_ eq "-dict") {
2636 $dict = shift @ARGV;
2637 } elsif ($_ eq "-driftnet" || $_ eq "--driftnet") {
2638 @search_methods = ( 100, "driftnet", \&pick_from_driftnet );
2639 if (! ($ARGV[0] =~ m/^-/)) {
2640 $driftnet_cmd = shift @ARGV;
2642 $driftnet_cmd = $default_driftnet_cmd;
2644 } elsif ($_ eq "-debug" || $_ eq "--debug") {
2645 my $which = shift @ARGV;
2646 my @rest = @search_methods;
2649 my $pct = shift @rest;
2650 my $name = shift @rest;
2651 my $tfn = shift @rest;
2653 if ($name eq $which) {
2654 @search_methods = (100, $name, $tfn);
2659 error "no such search method as \"$which\"" unless ($ok);
2660 LOG (1, "DEBUG: using only \"$which\"");
2663 print STDERR "$copyright\nusage: $progname " .
2664 "[-root] [-display dpy] [-verbose] [-debug which]\n" .
2665 "\t\t [-timeout secs] [-delay secs] [-filter cmd] [-filter2 cmd]\n" .
2666 "\t\t [-no-output] [-urls-only] [-background color] [-size WxH]\n" .
2667 "\t\t [-dictionary dictionary-file] [-http-proxy host[:port]]\n" .
2668 "\t\t [-driftnet [driftnet-program-and-args]]\n" .
2674 if ($http_proxy && $http_proxy eq "") {
2675 $http_proxy = undef;
2677 if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
2678 # historical suckage: allow "http://host:port" as well as "host:port".
2682 if (!$root_p && !$no_output_p) {
2683 print STDERR $copyright;
2684 error "the -root argument is mandatory (for now.)";
2687 if (!$no_output_p && !$ENV{DISPLAY}) {
2688 error "\$DISPLAY is not set.";
2692 if ($verbose == 1) {
2693 $verbose_imgmap = 1;
2694 $verbose_warnings = 1;
2696 } elsif ($verbose == 2) {
2697 $verbose_imgmap = 1;
2698 $verbose_warnings = 1;
2701 } elsif ($verbose == 3) {
2702 $verbose_imgmap = 1;
2703 $verbose_warnings = 1;
2705 $verbose_filter = 1;
2707 } elsif ($verbose == 4) {
2708 $verbose_imgmap = 1;
2709 $verbose_warnings = 1;
2711 $verbose_filter = 1;
2714 } elsif ($verbose == 5) {
2715 $verbose_imgmap = 1;
2716 $verbose_warnings = 1;
2718 $verbose_filter = 1;
2722 } elsif ($verbose == 6) {
2723 $verbose_imgmap = 1;
2724 $verbose_warnings = 1;
2726 $verbose_filter = 1;
2731 } elsif ($verbose >= 7) {
2732 $verbose_imgmap = 1;
2733 $verbose_warnings = 1;
2735 $verbose_filter = 1;
2743 error ("$dict does not exist") unless (-f $dict);
2751 spawn_driftnet ($driftnet_cmd) if ($driftnet_cmd);