1 #!/usr/local/bin/perl5 -w
3 # webcollage, Copyright (c) 1999, 2000 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
15 # To run this as a display mode with xscreensaver, add this to `programs':
17 # default-n: webcollage -root \n\
18 # default-n: webcollage -root -filter 'vidwhacker -stdin -stdout' \n\
27 use Fcntl ':flock'; # import LOCK_* constants
28 use POSIX qw(strftime);
31 my $version = q{ $Revision: 1.61 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
32 my $copyright = "WebCollage $version, Copyright (c) 1999" .
33 " Jamie Zawinski <jwz\@jwz.org>\n" .
34 " http://www.jwz.org/xscreensaver/\n";
37 my $progname = $argv0; $progname =~ s@.*/@@g;
39 my $random_redirector = "http://random.yahoo.com/bin/ryl";
40 my $image_randomizer_1 = "http://www.altavista.com/query" .
49 my $image_randomizer_2 = "http://www.hotbot.com/?clickSrc=search" .
50 "&submit=SEARCH&SM=SC&LG=any" .
51 "&AM0=MC&AT0=words&AW0=" .
52 "&AM1=MN&AT1=words&AW1=" .
53 "&savenummod=2&date=within" .
54 "&DV=0&DR=newer&DM=1&DD=1&DY=99&FVI=1&FS=&RD=RG" .
55 "&RG=all&Domain=&PS=A&PD=&STEM=1&DC=50&DE=0&_v=2" .
56 "&OPs=MDRTP&NUMMOD=2" .
58 my $image_randomizer_3 = "http://www.altavista.com/cgi-bin/query?pg=q" .
59 "&text=yes&kl=XX&stype=stext&q=";
60 my $image_randomizer_4 = "http://search.news.yahoo.com/search/news_photos?" .
61 "&z=&n=100&o=o&2=&3=&p=";
63 # I guess Photopoint got wise to me, because now they are doing error
64 # checking on the user ("u=") and album ("a=") parameters. Oh well.
66 #my $photo_randomizer = "http://albums.photopoint.com/j/View?u=1&a=1&p=";
67 #my $photo_randomizer_lo = 10000001;
68 #my $photo_randomizer_hi = 12400000;
70 my $image_ppm = ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp") . "/webcollage." . $$;
71 my $image_tmp1 = $image_ppm . "-1";
72 my $image_tmp2 = $image_ppm . "-2";
74 my $img_width; # size of the image being generated.
77 my $http_proxy = undef;
78 my $http_timeout = 30;
81 # programs we can use to write to the root window (tried in ascending order.)
82 my $ppm_to_root_window_cmd_1 = "xloadimage -onroot -quiet %%PPM%%";
83 my $ppm_to_root_window_cmd_2 = "xli -quiet -onroot -center" .
84 " -border black %%PPM%%";
85 my $ppm_to_root_window_cmd_3 = "xv -root -rmode 5 -viewonly" .
86 " +noresetroot %%PPM%% -quit";
88 my $ppm_to_root_window_cmd = undef; # initialized by x_output()
90 my $filter_cmd = undef;
91 my $post_filter_cmd = undef;
92 my $background = undef;
97 my $wordlist = "/usr/dict/words";
100 $wordlist = "/usr/share/dict/words"; # BSD
103 $wordlist = "/usr/share/lib/dict/words"; # Irix
105 die "$wordlist doesn't exist!\n" unless (-r $wordlist);
115 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
116 "arrhenius", "arteriole", "blanket", "brainchild",
117 "burdensome", "carnival", "cherub", "chord", "clever",
118 "dedicate", "dilogarithm", "dolan", "dryden",
123 return "$progname: " . strftime ("%H:%M:%S: ", localtime);
127 ##############################################################################
131 ##############################################################################
133 # returns three values: the HTTP response line; the document headers;
134 # and the document body.
137 my ( $url, $referer, $timeout ) = @_;
139 if (!defined($timeout)) { $timeout = $http_timeout; }
140 if ($timeout <= 0) { return (); }
141 if ($timeout > $http_timeout) { $timeout = $http_timeout; }
143 if ( $verbose > 3 ) {
144 print STDERR blurb() . "get_document_1 $url " .
145 ($referer ? $referer : "") . "\n";
148 my($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
149 if (! ($url_proto && $url_proto =~ m/^http:$/i)) {
150 if ($verbose) { print STDERR blurb() . "not an HTTP URL: $url\n"; }
154 $path = "" unless $path;
156 my($them,$port) = split(/:/, $serverstring);
157 $port = 80 unless $port;
162 $serverstring = $http_proxy if $http_proxy;
163 ($them2,$port2) = split(/:/, $serverstring);
164 $port2 = 80 unless $port2;
167 my ($remote, $iaddr, $paddr, $proto, $line);
169 if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
170 return unless $port2;
171 $iaddr = inet_aton($remote) || return;
172 $paddr = sockaddr_in($port2, $iaddr);
180 local $SIG{ALRM} = sub {
182 print STDERR blurb() . "timed out ($timeout) for $url\n";
188 $proto = getprotobyname('tcp');
189 if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
190 print STDERR blurb() . "socket: $!\n" if ($verbose);
193 if (!connect(S, $paddr)) {
194 print STDERR blurb() . "connect($serverstring): $!\n"
199 select(S); $| = 1; select(STDOUT);
202 if ($remote =~ m/\baltavista\.com$/i) {
203 # kludge to tell the various altavista sites to be uncensored.
204 $cookie = "AV_ALL=1";
207 print S ("GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
209 "User-Agent: $progname/$version\r\n" .
210 ($referer ? "Referer: $referer\r\n" : "") .
211 ($cookie ? "Cookie: $cookie\r\n" : "") .
225 if ( $verbose > 3 ) {
226 print STDERR blurb() . " ==> $http\n";
229 return ( $http, $head, $body );
231 die if ($@ && $@ ne "alarm\n"); # propagate errors
245 # returns two values: the document headers; and the document body.
246 # if the given URL did a redirect, returns the redirected-to document.
249 my ( $url, $referer, $timeout ) = @_;
254 my $max_loop_count = 4;
257 if (defined($timeout) && $timeout <= 0) { return (); }
259 my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
261 if (defined ($timeout)) {
263 my $elapsed = $now - $start;
264 $timeout -= $elapsed;
268 return () if ( ! $body );
270 if ( $http =~ m@HTTP/[0-9.]+ 30[23]@ ) {
272 my ( $location ) = m@^location:[ \t]*(.*)$@im;
274 $location =~ s/[\r\n]$//;
276 if ( $verbose > 3 ) {
277 print STDERR blurb() . "redirect from " .
278 "$url to $location\n";
284 $referer =~ m@^(http://[^/]+)@i;
286 } elsif (! ($url =~ m@^[a-z]+:@i)) {
288 s@[^/]+$@@g if m@^http://[^/]+/@i;
289 $_ .= "/" if m@^http://[^/]+$@i;
294 return ( $url, $body );
297 if ($loop_count++ > $max_loop_count) {
298 if ( $verbose > 1 ) {
299 print STDERR blurb() . "too many redirects " .
300 "($max_loop_count) from $orig_url\n";
306 } elsif ( $http =~ m@HTTP/[0-9.]+ [4-9][0-9][0-9]@ ) {
307 # http errors -- return nothing.
313 return ( $url, $body );
320 # given a URL and the body text at that URL, selects and returns a random
321 # image from it. returns () if no suitable images found.
323 sub pick_image_from_body {
324 my ( $url, $body ) = @_;
329 # if there's at least one slash after the host, take off the last
331 if ( m@^http://[^/]+/@io ) {
332 $base =~ s@[^/]+$@@go;
335 # if there are no slashes after the host at all, put one on the end.
336 if ( m@^http://[^/]+$@io ) {
340 if ( $verbose > 3 ) {
341 print STDERR blurb() . "base is $base\n";
347 # strip out newlines, compress whitespace
354 # There are certain web sites that list huge numbers of dictionary
355 # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
356 # Porn sites tend not to be reputable!)
358 # I do not want webcollage to filter on content: I want it to select
359 # randomly from the set of images on the web. All the logic here for
360 # rejecting some images is really a set of heuristics for rejecting
361 # images that are not really images: for rejecting *text* that is in
362 # GIF/JPEG form. I don't want text, I want pictures, and I want the
363 # content of the pictures to be randomly selected from among all the
366 # So, filtering out "dirty" pictures by looking for "dirty" keywords
367 # would be wrong: dirty pictures exist, like it or not, so webcollage
368 # should be able to select them.
370 # However, picking a random URL is a hard thing to do. The mechanism I'm
371 # using is to search for a selection of random words. This is not
372 # perfect, but works ok most of the time. The way it breaks down is when
373 # some URLs get precedence because their pages list *every word* as
374 # related -- those URLs come up more often than others.
376 # So, after we've retrieved a URL, if it has too many keywords, reject
377 # it. We reject it not on the basis of what those keywords are, but on
378 # the basis that by having so many, the page has gotten an unfair
379 # advantage against our randomizer.
382 foreach my $trip (@tripwire_words) {
383 $trip_count++ if m/$trip/i;
385 if ($trip_count >= $#tripwire_words - 2) {
387 print STDERR blurb() . "there is probably a dictionary in" .
388 " \"$url\": rejecting.\n";
390 $rejected_urls{$url} = -1;
400 foreach (split(/ *</)) {
403 # Likewise, reject any web pages that have a KEYWORDS meta tag
406 if (m/name ?= ?\"?keywords\"?/i &&
407 m/content ?= ?\"([^\"]+)\"/) {
411 print STDERR blurb() . "keywords of" .
412 " length $L in $url: rejecting.\n";
414 $rejected_urls{$url} = $L;
418 } elsif ( $verbose > 2 ) {
419 print STDERR blurb() . "keywords of length $L" .
424 } elsif ( m/^(img|a) .*(src|href) ?= ?\"? ?(.*?)[ >\"]/io ) {
426 my $was_inline = ( "$1" eq "a" || "$1" eq "A" );
428 my ( $width ) = m/width ?=[ \"]*(\d+)/oi;
429 my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
434 ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
436 } elsif ( ! m@^[^/:?]+:@ ) {
439 while (s@/\.\./@/@g) {
444 if ( ! m@^http://@io ) {
449 if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg)$@io ) {
453 # skip really short or really narrow images
454 if ( $width && $width < $min_width) {
455 if ( $verbose > 2 ) {
456 if (!$height) { $height = "?"; }
457 print STDERR blurb() . "skip narrow image " .
458 "$_ (${width}x$height)\n";
463 if ( $height && $height < $min_height) {
464 if ( $verbose > 2 ) {
465 if (!$width) { $width = "?"; }
466 print STDERR blurb() . "skip short image " .
467 "$_ (${width}x$height)\n";
472 # skip images with ratios that make them look like banners.
473 if ( $min_ratio && $width && $height &&
474 ($width * $min_ratio ) > $height ) {
475 if ( $verbose > 2 ) {
476 if (!$height) { $height = "?"; }
477 print STDERR blurb() . "skip bad ratio " .
478 "$_ (${width}x$height)\n";
485 if ( $unique_urls{$url} ) {
486 if ( $verbose > 2 ) {
487 print STDERR blurb() . "skip duplicate image $_\n";
492 if ( $verbose > 2 ) {
493 print STDERR blurb() . "got $url" .
494 ($width && $height ? " (${width}x${height})" : "") .
495 ($was_inline ? " (inline)" : "") . "\n";
498 $urls[++$#urls] = $url;
499 $unique_urls{$url}++;
501 # jpegs are preferable to gifs.
503 if ( ! m@[.]gif$@io ) {
504 $urls[++$#urls] = $url;
507 # pointers to images are preferable to inlined images.
508 if ( ! $was_inline ) {
509 $urls[++$#urls] = $url;
510 $urls[++$#urls] = $url;
519 if ( $verbose > 2 ) {
520 print STDERR blurb() . "no images on $base\n";
525 return () if ( $#urls < 1 );
527 # pick a random element of the table
528 my $i = ((rand() * 99999) % $#urls);
531 if ( $verbose > 2 ) {
532 print STDERR blurb() . "picked $url\n";
539 # Using the URL-randomizer, picks a random image on a random page, and
540 # returns two URLs: the page containing the image, and the image.
541 # Returns () if nothing found this time.
543 sub pick_from_url_randomizer {
544 my ( $timeout ) = @_;
546 if ( $verbose > 3 ) {
547 print STDERR "\n\n$progname: picking from $random_redirector...\n\n";
550 my ( $base, $body ) = get_document ($random_redirector, undef, $timeout);
552 if (!$base || !$body) {
556 my $img = pick_image_from_body ($base, $body);
560 return ($base, $img, "yahoo");
570 if (open (IN, "<$wordlist")) {
571 my $size = (stat(IN))[7];
572 my $pos = rand $size;
573 if (seek (IN, $pos, 0)) {
574 $word = <IN>; # toss partial line
575 $word = <IN>; # keep next line
584 return 0 if (!$word);
586 $word =~ s/^[ \t\n\r]+//;
587 $word =~ s/[ \t\n\r]+$//;
592 $word =~ s/ally$/al/;
593 $word =~ s/izes$/ize/;
594 $word =~ tr/A-Z/a-z/;
596 if ( $word =~ s/[ \t\n\r]/\+/g ) { # convert intra-word spaces to "+".
597 $word = "\%22$word\%22"; # And put quotes (%22) around it.
605 # Using the image-randomizer, picks a random image on a random page, and
606 # returns two URLs: the page containing the image, and the image.
607 # Returns () if nothing found this time.
609 sub pick_from_image_randomizer {
610 my ( $timeout, $which ) = @_;
612 my $words = random_word;
613 $words .= "%20" . random_word;
614 $words .= "%20" . random_word;
615 $words .= "%20" . random_word;
616 $words .= "%20" . random_word;
618 my $search_url = ($which == 0 ? $image_randomizer_1 :
619 $which == 1 ? $image_randomizer_2 :
620 $which == 2 ? $image_randomizer_3 :
621 $image_randomizer_4) .
624 # Pick a random search-result page instead of always taking the first.
625 # This assumes there are at least 10 pages...
627 $search_url .= "&pgno=" . (int(rand(9)) + 1);
628 } elsif ($which == 2) {
629 $search_url .= "&stq=" . (10 * (int(rand(9)) + 1));
632 if ( $verbose > 3 ) {
633 $_ = $words; s/%20/ /g; print STDERR blurb() . "search words: $_\n";
636 if ( $verbose > 3 ) {
637 print STDERR "\n\n$progname: picking from $search_url\n";
641 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
642 if (defined ($timeout)) {
643 $timeout -= (time - $start);
650 return () if (! $body);
656 my $search_count = "?";
658 $body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
660 } elsif ($which == 1 && $body =~ m@<NOBR>((\d{1,3})(,\d{3})*) @i) {
662 } elsif ($which == 2 && $body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
665 1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
667 my $length = length($body);
672 # s/Result [Pp]ages:.*$//s; # trim off page footer
673 # s/^.*?IMAGE RESULTS//s; # trim off page header
675 s/Have you tried these resources.*//s; # let's try it again
680 foreach (split(/\n/)) {
682 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
684 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
685 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
688 # Kludge to decode HotBot pages
689 next unless ($u =~ m@/director\.asp\?target=(http%3A[^&>]+)@);
693 next unless ($u =~ m@^http://@i); # skip non-http and relative urls.
695 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
696 next if ($u =~ m@[/.]altavista\.[a-z]{2}\b@i); # altavista.fr, etc
697 next if ($u =~ m@[/.]av\.com\b@i);
698 next if ($u =~ m@[/.]virage\.com\b@i);
699 next if ($u =~ m@[/.]photoloft\.com\b@i);
700 next if ($u =~ m@[/.]shopping\.com\b@i);
701 next if ($u =~ m@[/.]thetrip\.com\b@i);
702 next if ($u =~ m@[/.]cmgi\.com\b@i);
703 next if ($u =~ m@[/.]intelihealth\.com\b@i);
704 next if ($u =~ m@[/.]wildweb\.com\b@i);
705 next if ($u =~ m@[/.]digital\.com\b@i);
706 next if ($u =~ m@[/.]doubleclick\.net\b@i);
707 next if ($u =~ m@[/.]freeim\.org\b@i);
708 next if ($u =~ m@[/.]clicktomarket\.com\b@i); # you cretins
709 next if ($u =~ m@[/.]teragram\.com\b@i);
711 # must lose this one for altavista, even though it loses images of
712 # every single customer of akamai. Oh well, those people have lots
713 # of money, and so their images are probably boring anyway.
714 next if ($u =~ m@[/.]akamai\.net@i);
716 if ($which == 0 && $u =~ m@[/.]corbis\.com@) {
718 if ( $verbose > 3 ) {
719 print STDERR blurb() . "skipping corbis URL: $u\n";
723 } elsif ($which == 3 &&
724 ($u =~ m@^http://[^/]+$@ || # no slashes
725 $u =~ m@/$@ || # ends in /
726 ! ($u =~ m@dailynews\.yahoo\.com@))) { # not dailynews
728 if ( $verbose > 3 ) {
729 print STDERR blurb() . "skipping non-AP URL: $u\n";
733 } elsif ( $rejected_urls{$u} ) {
734 if ( $verbose > 3 ) {
735 my $L = $rejected_urls{$u};
736 print STDERR blurb() . "pre-rejecting sub-page: $u\n";
740 } elsif ( $verbose > 3 ) {
741 print STDERR blurb() . "sub-page: $u\n";
744 $subpages[++$#subpages] = $u;
747 if ( $#subpages < 0 ) {
748 if (!$skipped && $verbose > 1) {
749 print STDERR blurb() . "found nothing on $base " .
750 "($length bytes, $href_count links).\n";
757 # pick a random element of the table
758 my $i = ((rand() * 99999) % ($#subpages + 1));
759 my $subpage = $subpages[$i];
761 if ( $verbose > 3 ) {
762 print STDERR blurb() . "picked page $subpage\n";
769 my ( $base2, $body2 ) = get_document ($subpage, $base, $timeout);
771 if (!$base2 || !$body2) {
776 my $img = pick_image_from_body ($base2, $body2);
780 return ($base2, $img,
781 ($which == 0 ? "imagevista" :
782 $which == 1 ? "hotbot" :
783 $which == 2 ? "altavista" :
792 # Using the photo site, generate a random URL that will hopefully point
793 # to an image. Returns two URLs, both of which are the URL of the image.
794 # Returns () if nothing found this time.
796 #sub pick_from_photo_randomizer {
797 # my ( $timeout ) = @_;
798 # my $n = ($photo_randomizer_lo +
799 # int(rand() * ($photo_randomizer_hi - $photo_randomizer_lo)));
800 # my $url = $photo_randomizer . $n;
801 # return ( $url, $url, "photopoint" );
805 # Picks a random image on a random page, and returns two URLs:
806 # the page containing the image, and the image.
807 # Returns () if nothing found this time.
808 # Uses the url-randomizer 1 time in 5, else the image randomizer.
822 my ( $timeout ) = @_;
823 my $r = int(rand(100));
825 my ($base, $img, $source, $total, $count);
828 ($base, $img, $source) = pick_from_url_randomizer ($timeout);
830 $count = ++$count_0 if $img;
833 ($base, $img, $source) = pick_from_image_randomizer ($timeout, 0);
835 $count = ++$count_1 if $img;
838 ($base, $img, $source) = pick_from_image_randomizer ($timeout, 3);
840 $count = ++$count_4 if $img;
842 # } elsif ($r < 70) {
843 # ($base, $img, $source) = pick_from_photo_randomizer ($timeout);
844 # $total = ++$total_4;
845 # $count = ++$count_4 if $img;
847 # } elsif ($r < 80) {
848 # # HotBot sucks: 98% of the time, it says "no pages match your
849 # # search", and then if I load the URL again by hand, it works.
850 # # I don't understand what's going wrong here, but we're not getting
851 # # any good data back from them, so forget it for now.
853 # ($base, $img, $source) = pick_from_image_randomizer ($timeout, 1);
854 # $total = ++$total_2;
855 # $count = ++$count_2 if $img;
858 ($base, $img, $source) = pick_from_image_randomizer ($timeout, 2);
860 $count = ++$count_3 if $img;
863 if ($source && $total > 0) {
864 $source .= " " . int(($count / $total) * 100) . "%";
866 return ($base, $img, $source);
875 s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
880 # Given the raw body of a GIF document, returns the dimensions of the image.
884 my $type = substr($body, 0, 6);
886 return () unless ($type =~ /GIF8[7,9]a/);
887 $s = substr ($body, 6, 10);
888 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
889 return (($b<<8|$a), ($d<<8|$c));
892 # Given the raw body of a JPEG document, returns the dimensions of the image.
897 my $L = length($body);
899 my $c1 = substr($body, $i, 1); $i++;
900 my $c2 = substr($body, $i, 1); $i++;
901 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
904 while (ord($ch) != 0xDA && $i < $L) {
905 # Find next marker, beginning with 0xFF.
906 while (ord($ch) != 0xFF) {
907 $ch = substr($body, $i, 1); $i++;
909 # markers can be padded with any number of 0xFF.
910 while (ord($ch) == 0xFF) {
911 $ch = substr($body, $i, 1); $i++;
914 # $ch contains the value of the marker.
915 my $marker = ord($ch);
917 if (($marker >= 0xC0) &&
920 ($marker != 0xCC)) { # it's a SOFn marker
922 my $s = substr($body, $i, 4); $i += 4;
923 my ($a,$b,$c,$d) = unpack("C"x4, $s);
924 return (($c<<8|$d), ($a<<8|$b));
927 # We must skip variables, since FFs in variable names aren't
928 # valid JPEG markers.
929 my $s = substr($body, $i, 2); $i += 2;
930 my ($c1, $c2) = unpack ("C"x2, $s);
931 my $length = ($c1 << 8) | $c2;
932 return () if ($length < 2);
939 # Given the raw body of a GIF or JPEG document, returns the dimensions of
944 my ($w, $h) = gif_size ($body);
945 if ($w && $h) { return ($w, $h); }
946 return jpeg_size ($body);
950 # returns the full path of the named program, or undef.
954 foreach (split (/:/, $ENV{PATH})) {
963 # Like rand(), but chooses numbers with a bell curve distribution.
966 $_ = 1.0 unless defined($_);
968 return (rand($_) + rand($_) + rand($_));
972 ##############################################################################
974 # Generating a list of urls only
976 ##############################################################################
978 sub url_only_output {
980 my ($base, $img) = pick_image;
984 print "$img $base\n";
989 ##############################################################################
991 # Running as an xscreensaver module
993 ##############################################################################
997 if ($verbose > 0) { print STDERR blurb() . "caught signal $sig.\n"; }
998 unlink $image_ppm, $image_tmp1, $image_tmp2;
1003 # Like system, but prints status about exit codes, and kills this process
1004 # with whatever signal killed the sub-process, if any.
1006 sub nontrapping_system {
1011 s/\"[^\"]+\"/\"...\"/g;
1012 print STDERR blurb() . "executing \"$_\"\n";
1019 print STDERR blurb() . "subproc exited normally.\n";
1021 } elsif (($rc & 0xff) == 0) {
1024 print blurb() . "subproc exited with status $rc.\n";
1029 print blurb() . "subproc dumped core.\n";
1034 print blurb() . "subproc died with signal $rc.\n";
1036 # die that way ourselves.
1044 # Given the URL of a GIF or JPEG image, and the body of that image, writes a
1045 # PPM to the given output file. Returns the width/height of the image if
1049 my ($url, $body, $output) = @_;
1050 my ($cmd, $cmd2, $w, $h);
1052 if ((@_ = gif_size ($body))) {
1055 } elsif ((@_ = jpeg_size ($body))) {
1062 $cmd2 = "exec $cmd"; # yes, this really is necessary. if we don't
1063 # do this, the process doesn't die properly.
1064 if ($verbose <= 1) {
1066 # We get a "giftopnm: got a 'Application Extension' extension"
1067 # warning any time it's an animgif.
1069 # Note that "giftopnm: EOF / read error on image data" is not
1070 # always a fatal error -- sometimes the image looks fine anyway.
1072 $cmd2 .= " 2>/dev/null";
1075 # There exist corrupted GIF and JPEG files that can make giftopnm and
1076 # djpeg lose their minds and go into a loop. So this gives those programs
1077 # a small timeout -- if they don't complete in time, kill them.
1083 local $SIG{ALRM} = sub {
1085 print STDERR blurb() . "timed out ($cvt_timeout) for " .
1086 "$cmd on \"$url\" in pid $pid\n";
1088 kill ('TERM', $pid) if ($pid);
1093 if (($pid = open(PIPE, "| $cmd2 > $output"))) {
1100 if ($verbose > 3) { print STDERR blurb() . "awaiting $pid\n"; }
1102 if ($verbose > 3) { print STDERR blurb() . "$pid completed\n"; }
1105 my $size = (stat($output))[7];
1108 print STDERR blurb() . "$cmd on ${w}x$h \"$url\" failed" .
1115 print STDERR blurb() . "created ${w}x$h $output ($cmd)\n";
1119 print STDERR blurb() . "$cmd failed: $!\n";
1123 die if ($@ && $@ ne "alarm\n"); # propagate errors
1138 my $win_cmd_1 = $ppm_to_root_window_cmd_1;
1139 my $win_cmd_2 = $ppm_to_root_window_cmd_2;
1140 my $win_cmd_3 = $ppm_to_root_window_cmd_3;
1141 $win_cmd_1 =~ s/^([^ \t\r\n]+).*$/$1/;
1142 $win_cmd_2 =~ s/^([^ \t\r\n]+).*$/$1/;
1143 $win_cmd_3 =~ s/^([^ \t\r\n]+).*$/$1/;
1145 # make sure the various programs we execute exist, right up front.
1146 foreach ("ppmmake", "giftopnm", "djpeg", "pnmpaste", "pnmscale",
1148 which ($_) || die blurb() . "$_ not found on \$PATH.\n";
1151 if (which($win_cmd_1)) {
1152 $ppm_to_root_window_cmd = $ppm_to_root_window_cmd_1;
1153 } elsif (which($win_cmd_2)) {
1154 $ppm_to_root_window_cmd = $ppm_to_root_window_cmd_2;
1155 } elsif (which($win_cmd_3)) {
1156 $ppm_to_root_window_cmd = $ppm_to_root_window_cmd_3;
1158 die blurb() . "didn't find $win_cmd_1, $win_cmd_2, or $win_cmd_3 on \$PATH.\n";
1161 $SIG{HUP} = \&x_cleanup;
1162 $SIG{INT} = \&x_cleanup;
1163 $SIG{QUIT} = \&x_cleanup;
1164 $SIG{ABRT} = \&x_cleanup;
1165 $SIG{KILL} = \&x_cleanup;
1166 $SIG{TERM} = \&x_cleanup;
1168 # Need this so that if giftopnm dies, we don't die.
1169 $SIG{PIPE} = 'IGNORE';
1171 if (!$img_width || !$img_height) {
1173 which ($_) || die blurb() . "$_ not found on \$PATH.\n";
1175 ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
1176 if (!defined($img_height)) {
1177 die blurb() . "xdpyinfo failed.\n";
1181 my $bgcolor = "#000000";
1182 my $bgimage = undef;
1185 if ($background =~ m/^\#[0-9a-f]+$/i) {
1186 $bgcolor = $background;
1187 } elsif (-r $background) {
1188 $bgimage = $background;
1190 } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
1191 print STDERR blurb() . "not a color or readable file: " .
1195 # default to assuming it's a color
1196 $bgcolor = $background;
1200 # Create the sold-colored base image.
1202 $_ = "ppmmake '$bgcolor' $img_width $img_height";
1204 print STDERR blurb() . "creating base image: $_\n";
1206 nontrapping_system "$_ > $image_ppm";
1208 # Paste the default background image in the middle of it.
1215 open(IMG, "<$bgimage") || die ("couldn't open $bgimage: $!\n");
1217 while (<IMG>) { $body .= $_; }
1219 if ((@_ = gif_size ($body))) {
1221 $cmd = "giftopnm |";
1222 } elsif ((@_ = jpeg_size ($body))) {
1225 } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
1230 die blurb() . "$bgimage is not a GIF, JPEG, or PPM.\n";
1233 my $x = int (($img_width - $iw) / 2);
1234 my $y = int (($img_height - $ih) / 2);
1236 print STDERR blurb() . "pasting $bgimage (${iw}x$ih) into base ".
1240 $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
1241 open (IMG, "| $cmd") || die ("running $cmd: $!\n");
1246 print STDERR blurb() . "subproc exited normally.\n";
1248 rename ($image_tmp1, $image_ppm) ||
1249 die ("renaming $image_tmp1 to $image_ppm: $!\n");
1253 my ($base, $img, $source) = pick_image();
1255 my ($headers, $body) = get_document ($img, $base);
1257 handle_image ($base, $img, $body, $source);
1261 unlink $image_tmp1, $image_tmp2;
1267 my ($base, $img, $body, $source) = @_;
1270 print STDERR blurb() . "got $img (" . length($body) . ")\n";
1273 my ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
1275 return 0 unless ($iw && $ih);
1277 my $ow = $iw; # used only for error messages
1280 # don't just tack this onto the front of the pipeline -- we want it to
1281 # be able to change the size of the input image.
1285 print STDERR blurb() . "running $filter_cmd\n";
1288 my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
1291 print STDERR blurb() . "failed command: \"$filter_cmd\"\n";
1292 print STDERR blurb() . "failed url: \"$img\" (${ow}x$oh)\n";
1296 rename ($image_tmp2, $image_tmp1);
1298 # re-get the width/height in case the filter resized it.
1300 open(IMG, "<$image_tmp1") || return 0;
1303 ($iw, $ih) = m/^(\d+) (\d+)$/;
1305 return 0 unless ($iw && $ih);
1308 my $target_w = $img_width;
1309 my $target_h = $img_height;
1314 # Usually scale the image to fit on the screen -- but sometimes scale it
1315 # to fit on half or a quarter of the screen. Note that we don't merely
1316 # scale it to fit, we instead cut it in half until it fits -- that should
1317 # give a wider distribution of sizes.
1319 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; }
1320 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; }
1322 if ($iw > $target_w || $ih > $target_h) {
1323 while ($iw > $target_w ||
1328 if ($iw <= 10 || $ih <= 10) {
1330 print STDERR blurb() . "scaling to ${iw}x$ih would " .
1331 "have been bogus.\n";
1337 print STDERR blurb() . "scaling to ${iw}x$ih\n";
1340 $cmd .= " | pnmscale -xsize $iw -ysize $ih";
1344 my $src = $image_tmp1;
1346 my $crop_x = 0; # the sub-rectangle of the image
1347 my $crop_y = 0; # that we will actually paste.
1351 # The chance that we will randomly crop out a section of an image starts
1352 # out fairly low, but goes up for images that are very large, or images
1353 # that have ratios that make them look like banners (we try to avoid
1354 # banner images entirely, but they slip through when the IMG tags didn't
1355 # have WIDTH and HEIGHT specified.)
1357 my $crop_chance = 0.2;
1358 if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
1359 $crop_chance += 0.2;
1361 if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
1362 $crop_chance += 0.2;
1364 if ($min_ratio && ($iw * $min_ratio) > $ih) {
1365 $crop_chance += 0.7;
1368 if ($verbose > 2 && $crop_chance > 0.1) {
1369 print STDERR blurb() . "crop chance: $crop_chance\n";
1372 if (rand() < $crop_chance) {
1377 if ($crop_w > $min_width) {
1378 # if it's a banner, select the width linearly.
1379 # otherwise, select a bell.
1380 my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
1383 $crop_w = $min_width + int ($r * ($crop_w - $min_width));
1384 $crop_x = int (rand() * ($ow - $crop_w));
1386 if ($crop_h > $min_height) {
1387 # height always selects as a bell.
1388 $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
1389 $crop_y = int (rand() * ($oh - $crop_h));
1393 ($crop_x != 0 || $crop_y != 0 ||
1394 $crop_w != $iw || $crop_h != $ih)) {
1395 print STDERR blurb() . "randomly cropping to " .
1396 "${crop_w}x$crop_h \@ $crop_x,$crop_y\n";
1400 # Where the image should logically land -- this might be negative.
1402 my $x = int((rand() * ($img_width + $crop_w/2)) - $crop_w*3/4);
1403 my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
1405 # if we have chosen to paste the image outside of the rectangle of the
1406 # screen, then we need to crop it.
1410 $x + $crop_w > $img_width ||
1411 $y + $crop_h > $img_height) {
1414 print STDERR blurb() . "cropping for effective paste of " .
1415 "${crop_w}x$crop_h \@ $x,$y\n";
1418 if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
1419 if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
1421 if ($x + $crop_w >= $img_width) { $crop_w = $img_width - $x - 1; }
1422 if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
1425 # If any cropping needs to happen, add pnmcut.
1427 if ($crop_x != 0 || $crop_y != 0 ||
1428 $crop_w != $iw || $crop_h != $ih) {
1431 $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
1433 print STDERR blurb() . "cropping to ${crop_w}x$crop_h \@ " .
1434 "$crop_x,$crop_y\n";
1439 print STDERR blurb() . "pasting ${iw}x$ih \@ $x,$y in $image_ppm\n";
1442 $cmd .= " | pnmpaste - $x $y $image_ppm";
1444 $cmd =~ s@^ *\| *@@;
1445 my $rc = nontrapping_system "($cmd) < $image_tmp1 > $image_tmp2";
1449 print STDERR blurb() . "failed command: \"$cmd\"\n";
1450 print STDERR blurb() . "failed url: \"$img\" (${ow}x$oh)\n";
1455 rename ($image_tmp2, $image_ppm) || return;
1457 my $target = "$image_ppm";
1459 # don't just tack this onto the end of the pipeline -- we don't want it
1460 # to end up in $image_ppm, because we don't want the results to be
1463 if ($post_filter_cmd) {
1464 $target = $image_tmp1;
1465 $rc = nontrapping_system "($post_filter_cmd) < $image_ppm > $target";
1468 print STDERR blurb() . "filter failed: " .
1469 "\"$post_filter_cmd\"\n";
1475 if (!$no_output_p) {
1476 my $tsize = (stat($target))[7];
1478 $cmd = $ppm_to_root_window_cmd;
1479 $cmd =~ s/%%PPM%%/$target/;
1481 # xv seems to hate being killed. it tends to forget to clean
1482 # up after itself, and leaves windows around and colors allocated.
1483 # I had this same problem with vidwhacker, and I'm not entirely
1484 # sure what I did to fix it. But, let's try this: launch xv
1485 # in the background, so that killing this process doesn't kill it.
1486 # it will die of its own accord soon enough. So this means we
1487 # start pumping bits to the root window in parallel with starting
1488 # the next network retrieval, which is probably a better thing
1493 $rc = nontrapping_system ($cmd);
1497 print STDERR blurb() . "display failed: \"$cmd\"\n";
1502 } elsif ($verbose > 1) {
1503 print STDERR blurb() . "$target size is $tsize\n";
1508 print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n";
1521 # historical suckage: the environment variable name is lower case.
1522 $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
1524 while ($_ = $ARGV[0]) {
1526 if ($_ eq "-display" ||
1532 $ENV{DISPLAY} = shift @ARGV;
1533 } elsif ($_ eq "-root") {
1535 } elsif ($_ eq "-no-output") {
1537 } elsif ($_ eq "-urls-only") {
1540 } elsif ($_ eq "-verbose") {
1542 } elsif (m/^-v+$/) {
1543 $verbose += length($_)-1;
1544 } elsif ($_ eq "-delay") {
1545 $delay = shift @ARGV;
1546 } elsif ($_ eq "-timeout") {
1547 $http_timeout = shift @ARGV;
1548 } elsif ($_ eq "-filter") {
1549 $filter_cmd = shift @ARGV;
1550 } elsif ($_ eq "-filter2") {
1551 $post_filter_cmd = shift @ARGV;
1552 } elsif ($_ eq "-background" || $_ eq "-bg") {
1553 $background = shift @ARGV;
1554 } elsif ($_ eq "-size") {
1556 if (m@^(\d+)x(\d+)$@) {
1560 die blurb() . "argument to \"-size\" must be" .
1561 " of the form \"640x400\"\n";
1563 } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
1564 $http_proxy = shift @ARGV;
1566 die "$copyright\nusage: $progname [-root]" .
1567 " [-display dpy] [-root] [-verbose] [-timeout secs]\n" .
1568 "\t\t [-delay secs] [-filter cmd] [-filter2 cmd]\n" .
1569 "\t\t [-http-proxy host[:port]]\n";
1573 if ($http_proxy && $http_proxy eq "") {
1574 $http_proxy = undef;
1576 if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
1577 # historical suckage: allow "http://host:port" as well as "host:port".
1581 if (!$root_p && !$no_output_p) {
1583 blurb() . "the -root argument is mandatory (for now.)\n";
1586 if (!$no_output_p && !$ENV{DISPLAY}) {
1587 die blurb() . "\$DISPLAY is not set.\n";