3 # webcollage, Copyright (c) 1999-2005 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':
19 # webcollage -root -filter 'vidwhacker -stdin -stdout'
22 # You can see this in action at http://www.jwz.org/webcollage/ --
23 # it auto-reloads about once a minute. To make a page similar to
24 # that on your own system, do this:
26 # webcollage -size '800x600' -imagemap $HOME/www/webcollage/index
29 # If you have the "driftnet" program installed, webcollage can display a
30 # collage of images sniffed off your local ethernet, instead of pulled out
31 # of search engines: in that way, your screensaver can display the images
32 # that your co-workers are downloading!
34 # Driftnet is available here: http://www.ex-parrot.com/~chris/driftnet/
37 # webcollage -root -driftnet
39 # Driftnet is the Unix implementation of the MacOS "EtherPEG" program.
45 # We can't "use diagnostics" here, because that library malfunctions if
46 # you signal and catch alarms: it says "Uncaught exception from user code"
47 # and exits, even though I damned well AM catching it!
54 use Fcntl ':flock'; # import LOCK_* constants
55 use POSIX qw(strftime);
57 use bytes; # Larry can take Unicode and shove it up his ass sideways.
58 # Perl 5.8.0 causes us to start getting incomprehensible
59 # errors about UTF-8 all over the place without this.
62 my $progname = $0; $progname =~ s@.*/@@g;
63 my $version = q{ $Revision: 1.125 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
64 my $copyright = "WebCollage $version, Copyright (c) 1999-2005" .
65 " Jamie Zawinski <jwz\@jwz.org>\n" .
66 " http://www.jwz.org/webcollage/\n";
70 my @search_methods = ( 58, "altavista", \&pick_from_alta_vista_random_link,
71 11, "livejournal", \&pick_from_livejournal_images,
72 7, "yahoorand", \&pick_from_yahoo_random_link,
73 10, "googlephotos", \&pick_from_google_image_photos,
74 6, "googleimgs", \&pick_from_google_images,
75 3, "googlenums", \&pick_from_google_image_numbers,
76 5, "flickr", \&pick_from_flickr,
78 # In Apr 2002, Google asked me to stop searching them.
79 # I asked them to add a "random link" url. They said
80 # "that would be easy, we'll think about it" and then
81 # never wrote back. Booo Google! Booooo! So, screw
82 # those turkeys, I've turned Google searching back on.
83 # I'm sure they can take it. (Jan 2005.)
85 # Jan 2005: Yahoo fucked up their search form so that
86 # it's no longer possible to do "or" searches on news
87 # images, so we rarely get any hits there any more.
89 # 0, "yahoonews", \&pick_from_yahoo_news_text,
91 # Dec 2004: the ircimages guy's server can't take the
92 # heat, so he started banning the webcollage user agent.
93 # I tried to convince him to add a lighter-weight page to
94 # support webcollage better, but he doesn't care.
96 # 0, "ircimages", \&pick_from_ircimages,
98 # Dec 2002: Alta Vista has a new "random link" URL now.
99 # They added it specifically to better support webcollage!
100 # That was super cool of them. This is how we used to do
103 # 0, "avimages", \&pick_from_alta_vista_images,
104 # 0, "avtext", \&pick_from_alta_vista_text,
106 # This broke in 2004. Eh, Lycos sucks anyway.
108 # 0, "lycos", \&pick_from_lycos_text,
110 # This broke in 2003, I think. I suspect Hotbot is
111 # actually the same search engine data as Lycos.
113 # 0, "hotbot", \&pick_from_hotbot_text,
116 # programs we can use to write to the root window (tried in ascending order.)
118 my @root_displayers = (
119 "xscreensaver-getimage -root -file",
120 "chbg -once -xscreensaver -max_size 100",
121 "xv -root -quit -viewonly +noresetroot -quick24 -rmode 5" .
122 " -rfg black -rbg black",
123 "xli -quiet -onroot -center -border black",
124 "xloadimage -quiet -onroot -center -border black",
126 # this lame program wasn't built with vroot.h:
127 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
131 # Some sites need cookies to work properly. These are they.
134 "www.altavista.com" => "AV_ALL=1", # request uncensored searches
135 "web.altavista.com" => "AV_ALL=1",
137 # log in as "cipherpunk"
138 "www.nytimes.com" => 'NYT-S=18cHMIlJOn2Y1bu5xvEG3Ufuk6E1oJ.' .
139 'FMxWaQV0igaB5Yi/Q/guDnLeoL.pe7i1oakSb' .
140 '/VqfdUdb2Uo27Vzt1jmPn3cpYRlTw9',
142 "ircimages.com" => 'disclaimer=1',
146 # If this is set, it's a helper program to use for pasting images together:
147 # this is a lot faster and more efficient than using PPM pipelines, which is
148 # what we do if this program doesn't exist. (We check for "webcollage-helper"
149 # on $PATH at startup, and set this variable appropriately.)
151 my $webcollage_helper = undef;
154 # If we have the webcollage-helper program, then it will paste the images
155 # together with transparency! 0.0 is invisible, 1.0 is totally opaque.
160 # Some sites have managed to poison the search engines. These are they.
161 # (We auto-detect sites that have poisoned the search engines via excessive
162 # keywords or dictionary words, but these are ones that slip through
165 # This can contain full host names, or 2 or 3 component domains.
168 "die.net" => 1, # 'l33t h4ck3r d00dz.
169 "genforum.genealogy.com" => 1, # Cluttering avtext with human names.
170 "rootsweb.com" => 1, # Cluttering avtext with human names.
171 "akamai.net" => 1, # Lots of sites have their images on Akamai.
172 "akamaitech.net" => 1, # But those are pretty much all banners.
173 # Since Akamai is super-expensive, let's
174 # go out on a limb and assume that all of
175 # their customers are rich-and-boring.
176 "bartleby.com" => 1, # Dictionary, cluttering avtext.
177 "encyclopedia.com" => 1, # Dictionary, cluttering avtext.
178 "onlinedictionary.datasegment.com" => 1, # Dictionary, cluttering avtext.
179 "hotlinkpics.com" => 1, # Porn site that has poisoned avimages
180 # (I don't see how they did it, though!)
181 "alwayshotels.com" => 1, # Poisoned Lycos pretty heavily.
182 "nextag.com" => 1, # Poisoned Alta Vista real good.
186 # When verbosity is turned on, we warn about sites that we seem to be hitting
187 # a lot: usually this means some new poisoner has made it into the search
188 # engines. But sometimes, the warning is just because that site has a lot
189 # of stuff on it. So these are the sites that are immune to the "frequent
190 # site" diagnostic message.
192 my %warningless_sites = (
193 "home.earthlink.net" => 1, # Lots of home pages here.
194 "www.geocities.com" => 1,
195 "www.angelfire.com" => 1,
196 "members.aol.com" => 1,
197 "img.photobucket.com" => 1,
198 "pics.livejournal.com" => 1,
202 "yimg.com" => 1, # This is where dailynews.yahoo.com stores
203 "eimg.com" => 1, # its images, so pick_from_yahoo_news_text()
204 # hits this every time.
206 "images.quizfarm.com" => 1, # damn those LJ quizzes...
207 "images.quizilla.com" => 1,
208 "images.quizdiva.net" => 1,
210 "driftnet" => 1, # builtin...
214 ##############################################################################
216 # Various global flags set by command line parameters, or computed
218 ##############################################################################
221 my $current_state = "???"; # for diagnostics
224 my $image_succeeded = -1;
225 my $suppress_audit = 0;
227 my $verbose_imgmap = 0; # print out rectangles and URLs only (stdout)
228 my $verbose_warnings = 0; # print out warnings when things go wrong
229 my $verbose_load = 0; # diagnostics about loading of URLs
230 my $verbose_filter = 0; # diagnostics about page selection/rejection
231 my $verbose_net = 0; # diagnostics about network I/O
232 my $verbose_pbm = 0; # diagnostics about PBM pipelines
233 my $verbose_http = 0; # diagnostics about all HTTP activity
234 my $verbose_exec = 0; # diagnostics about executing programs
236 my $report_performance_interval = 60 * 15; # print some stats every 15 minutes
238 my $http_proxy = undef;
239 my $http_timeout = 20;
240 my $cvt_timeout = 10;
246 my $min_gif_area = (120 * 120);
251 my $imagemap_base = undef;
253 my @pids_to_kill = (); # forked pids we should kill when we exit, if any.
255 my $driftnet_magic = 'driftnet';
256 my $driftnet_dir = undef;
257 my $default_driftnet_cmd = "driftnet -a -m 100";
262 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
263 "arrhenius", "arteriole", "blanket", "brainchild",
264 "burdensome", "carnival", "cherub", "chord", "clever",
265 "dedicate", "dilogarithm", "dolan", "dryden",
269 ##############################################################################
273 ##############################################################################
275 # returns three values: the HTTP response line; the document headers;
276 # and the document body.
279 my ( $url, $referer, $timeout ) = @_;
281 if (!defined($timeout)) { $timeout = $http_timeout; }
282 if ($timeout > $http_timeout) { $timeout = $http_timeout; }
285 LOG (($verbose_net || $verbose_load), "timed out for $url");
289 LOG ($verbose_net, "get_document_1 $url " . ($referer ? $referer : ""));
291 if (! ($url =~ m@^http://@i)) {
292 LOG ($verbose_net, "not an HTTP URL: $url");
296 my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
297 $path = "" unless $path;
299 if (!$url_proto || !$serverstring) {
300 LOG (($verbose_net || $verbose_load), "unparsable URL: $url");
304 my ($them,$port) = split(/:/, $serverstring);
305 $port = 80 unless $port;
310 $serverstring = $http_proxy if $http_proxy;
311 $serverstring =~ s@^[a-z]+://@@;
312 ($them2,$port2) = split(/:/, $serverstring);
313 $port2 = 80 unless $port2;
316 my ($remote, $iaddr, $paddr, $proto, $line);
318 if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
320 LOG (($verbose_net || $verbose_load), "unrecognised port in $url");
323 $iaddr = inet_aton($remote);
325 LOG (($verbose_net || $verbose_load), "host not found: $remote");
328 $paddr = sockaddr_in($port2, $iaddr);
336 local $SIG{ALRM} = sub {
337 LOG (($verbose_net || $verbose_load), "timed out ($timeout) for $url");
342 $proto = getprotobyname('tcp');
343 if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
344 LOG (($verbose_net || $verbose_load), "socket: $!");
347 if (!connect(S, $paddr)) {
348 LOG (($verbose_net || $verbose_load), "connect($serverstring): $!");
352 select(S); $| = 1; select(STDOUT);
354 my $cookie = $cookies{$them};
356 my $user_agent = "$progname/$version";
358 if ($url =~ m@^http://www\.altavista\.com/@ ||
359 $url =~ m@^http://random\.yahoo\.com/@ ||
360 $url =~ m@^http://images\.google\.com/@) {
361 # block this, you turkeys.
362 $user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5)" .
363 " Gecko/20041111 Firefox/1.0";
366 my $hdrs = "GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
368 "User-Agent: $user_agent\r\n";
370 $hdrs .= "Referer: $referer\r\n";
373 my @cc = split(/\r?\n/, $cookie);
374 $hdrs .= "Cookie: " . join('; ', @cc) . "\r\n";
378 foreach (split('\r?\n', $hdrs)) {
379 LOG ($verbose_http, " ==> $_");
382 my $http = <S> || "";
384 # Kludge: the Yahoo Random Link is now returning as its first
385 # line "Status: 301" instead of "HTTP/1.0 301 Found". Fix it...
387 $http =~ s@^Status:\s+(\d+)\b@HTTP/1.0 $1@i;
391 LOG ($verbose_http, " <== $_");
397 LOG ($verbose_http, " <== $_");
399 if (m@^Set-cookie:\s*([^;\r\n]+)@i) {
400 set_cookie($them, $1)
411 " <== [ body ]: $lines lines, " . length($body) . " bytes");
416 LOG (($verbose_net || $verbose_load), "null response: $url");
420 $SIG{ALRM} = 'DEFAULT'; # seem to be suffering a race?
421 return ( $http, $head, $body );
423 die if ($@ && $@ ne "alarm\n"); # propagate errors
425 if ($@ && $@ ne "alarm\n") {
426 print STDERR blurb() . "DIE " . join(" ", $@) . "\n";
444 # returns two values: the document headers; and the document body.
445 # if the given URL did a redirect, returns the redirected-to document.
448 my ( $url, $referer, $timeout ) = @_;
451 if (defined($referer) && $referer eq $driftnet_magic) {
452 return get_driftnet_file ($url);
457 my $max_loop_count = 4;
460 if (defined($timeout) && $timeout <= 0) {
461 LOG (($verbose_net || $verbose_load), "timed out for $url");
466 my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
468 if (defined ($timeout)) {
470 my $elapsed = $now - $start;
471 $timeout -= $elapsed;
475 return () unless $http; # error message already printed
477 $http =~ s/[\r\n]+$//s;
479 if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
482 my ( $location ) = m@^location:[ \t]*(.*)$@im;
484 $location =~ s/[\r\n]$//;
486 LOG ($verbose_net, "redirect from $url to $location");
491 $referer =~ m@^(http://[^/]+)@i;
493 } elsif (! ($url =~ m@^[a-z]+:@i)) {
495 s@[^/]+$@@g if m@^http://[^/]+/@i;
496 $_ .= "/" if m@^http://[^/]+$@i;
501 LOG ($verbose_net, "no Location with \"$http\"");
502 return ( $url, $body );
505 if ($loop_count++ > $max_loop_count) {
507 "too many redirects ($max_loop_count) from $orig_url");
512 } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
514 LOG (($verbose_net || $verbose_load), "failed: $1 ($url)");
516 # http errors -- return nothing.
522 LOG (($verbose_net || $verbose_load), "document contains no data: $url");
528 return ( $url, $body );
534 # If we already have a cookie defined for this site, and the site is trying
535 # to overwrite that very same cookie, let it do so. This is because nytimes
536 # expires its cookies - it lets you upgrade to a new cookie without logging
537 # in again, but you have to present the old cookie to get the new cookie.
538 # So, by doing this, the built-in cypherpunks cookie will never go "stale".
541 my ($host, $cookie) = @_;
542 my $oc = $cookies{$host};
545 my ($oc_name, $oc_value) = m@^([^= \t\r\n]+)=(.*)$@;
547 my ($nc_name, $nc_value) = m@^([^= \t\r\n]+)=(.*)$@;
549 if ($oc_name eq $nc_name &&
550 $oc_value ne $nc_value) {
551 $cookies{$host} = $cookie;
552 LOG ($verbose_net, "overwrote ${host}'s $oc_name cookie");
557 ############################################################################
559 # Extracting image URLs from HTML
561 ############################################################################
563 # given a URL and the body text at that URL, selects and returns a random
564 # image from it. returns () if no suitable images found.
566 sub pick_image_from_body {
567 my ( $url, $body ) = @_;
572 # if there's at least one slash after the host, take off the last
574 if ( m@^http://[^/]+/@io ) {
575 $base =~ s@[^/]+$@@go;
578 # if there are no slashes after the host at all, put one on the end.
579 if ( m@^http://[^/]+$@io ) {
585 # strip out newlines, compress whitespace
592 # There are certain web sites that list huge numbers of dictionary
593 # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
594 # Porn sites tend not to be reputable!)
596 # I do not want webcollage to filter on content: I want it to select
597 # randomly from the set of images on the web. All the logic here for
598 # rejecting some images is really a set of heuristics for rejecting
599 # images that are not really images: for rejecting *text* that is in
600 # GIF/JPEG/PNG form. I don't want text, I want pictures, and I want
601 # the content of the pictures to be randomly selected from among all
602 # the available content.
604 # So, filtering out "dirty" pictures by looking for "dirty" keywords
605 # would be wrong: dirty pictures exist, like it or not, so webcollage
606 # should be able to select them.
608 # However, picking a random URL is a hard thing to do. The mechanism I'm
609 # using is to search for a selection of random words. This is not
610 # perfect, but works ok most of the time. The way it breaks down is when
611 # some URLs get precedence because their pages list *every word* as
612 # related -- those URLs come up more often than others.
614 # So, after we've retrieved a URL, if it has too many keywords, reject
615 # it. We reject it not on the basis of what those keywords are, but on
616 # the basis that by having so many, the page has gotten an unfair
617 # advantage against our randomizer.
620 foreach my $trip (@tripwire_words) {
621 $trip_count++ if m/$trip/i;
624 if ($trip_count >= $#tripwire_words - 2) {
625 LOG (($verbose_filter || $verbose_load),
626 "there is probably a dictionary in \"$url\": rejecting.");
627 $rejected_urls{$url} = -1;
637 foreach (split(/ *</)) {
640 # Likewise, reject any web pages that have a KEYWORDS meta tag
643 if (m/name ?= ?\"?keywords\"?/i &&
644 m/content ?= ?\"([^\"]+)\"/) {
647 LOG (($verbose_filter || $verbose_load),
648 "excessive keywords ($L bytes) in $url: rejecting.");
649 $rejected_urls{$url} = $L;
654 LOG ($verbose_filter, " keywords ($L bytes) in $url (ok)");
658 } elsif ( m/^(img|a) .*(src|href) ?= ?\"? ?(.*?)[ >\"]/io ) {
660 my $was_inline = (! ( "$1" eq "a" || "$1" eq "A" ));
662 my ( $width ) = m/width ?=[ \"]*(\d+)/oi;
663 my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
668 ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
670 } elsif ( ! m@^[^/:?]+:@ ) {
673 1 while (s@/[^/]+/\.\./@/@g);
677 if ( ! m@^http://@io ) {
682 if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg|png)$@io ) {
686 # skip really short or really narrow images
687 if ( $width && $width < $min_width) {
688 if (!$height) { $height = "?"; }
689 LOG ($verbose_filter, " skip narrow image $_ (${width}x$height)");
693 if ( $height && $height < $min_height) {
694 if (!$width) { $width = "?"; }
695 LOG ($verbose_filter, " skip short image $_ (${width}x$height)");
699 # skip images with ratios that make them look like banners.
700 if ($min_ratio && $width && $height &&
701 ($width * $min_ratio ) > $height) {
702 if (!$height) { $height = "?"; }
703 LOG ($verbose_filter, " skip bad ratio $_ (${width}x$height)");
707 # skip GIFs with a small number of pixels -- those usually suck.
708 if ($width && $height &&
710 ($width * $height) < $min_gif_area) {
711 LOG ($verbose_filter, " skip small GIF $_ (${width}x$height)");
715 # skip images with a URL that indicates a Yahoo thumbnail.
716 if (m@\.yimg\.com/.*/t/@) {
717 if (!$width) { $width = "?"; }
718 if (!$height) { $height = "?"; }
719 LOG ($verbose_filter, " skip yahoo thumb $_ (${width}x$height)");
725 if ($unique_urls{$url}) {
726 LOG ($verbose_filter, " skip duplicate image $_");
730 LOG ($verbose_filter,
732 ($width && $height ? " (${width}x${height})" : "") .
733 ($was_inline ? " (inline)" : ""));
735 $urls[++$#urls] = $url;
736 $unique_urls{$url}++;
738 # JPEGs are preferable to GIFs and PNGs.
740 if ( ! m@[.](gif|png)$@io ) {
741 $urls[++$#urls] = $url;
744 # pointers to images are preferable to inlined images.
745 if ( ! $was_inline ) {
746 $urls[++$#urls] = $url;
747 $urls[++$#urls] = $url;
752 my $fsp = ($body =~ m@<frameset@i);
757 @urls = depoison (@urls);
760 LOG ($verbose_load, "no images on $base" . ($fsp ? " (frameset)" : ""));
764 # pick a random element of the table
765 my $i = int(rand($#urls+1));
768 LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
775 ############################################################################
777 # Subroutines for getting pages and images out of search engines
779 ############################################################################
782 sub pick_dictionary {
783 my @dicts = ("/usr/dict/words",
784 "/usr/share/dict/words",
785 "/usr/share/lib/dict/words");
786 foreach my $f (@dicts) {
792 error ("$dicts[0] does not exist") unless defined($wordlist);
795 # returns a random word from the dictionary
800 if (! open (IN, "<$wordlist")) {
804 my $size = (stat(IN))[7];
809 error ("looping ($count) while reading $wordlist")
812 my $pos = int (rand ($size));
813 if (seek (IN, $pos, 0)) {
814 $word = <IN>; # toss partial line
815 $word = <IN>; # keep next line
819 next if ($word =~ m/^[-\']/);
823 $word =~ s/^[^a-z]+//s;
824 $word =~ s/[^a-z]+$//s;
830 $word =~ s/ally$/al/s;
831 $word =~ s/izes$/ize/s;
832 $word =~ s/esses$/ess/s;
833 $word =~ s/(.{5})ing$/$1/s;
835 next if (length ($word) > 14);
841 if ( $word =~ s/\s/\+/gs ) { # convert intra-word spaces to "+".
842 $word = "\%22$word\%22"; # And put quotes (%22) around it.
851 my $sep = ($or_p ? "%20OR%20" : "%20");
852 return (random_word . $sep .
862 $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
869 $s =~ s/%([a-z0-9]{2})/chr(hex($1))/ige;
878 $s =~ s/\"/"/gi;
884 $s =~ s/</</gi; # far from exhaustive...
886 $s =~ s/"/\"/gi;
892 # Loads the given URL (a search on some search engine) and returns:
893 # - the total number of hits the search engine claimed it had;
894 # - a list of URLs from the page that the search engine returned;
895 # Note that this list contains all kinds of internal search engine
896 # junk URLs too -- caller must prune them.
898 sub pick_from_search_engine {
899 my ( $timeout, $search_url, $words ) = @_;
904 print STDERR "\n\n" if ($verbose_load);
906 LOG ($verbose_load, "words: $_");
907 LOG ($verbose_load, "URL: $search_url");
909 $last_search = $search_url; # for warnings
912 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
913 if (defined ($timeout)) {
914 $timeout -= (time - $start);
917 LOG (($verbose_net || $verbose_load),
918 "timed out (late) for $search_url");
924 return () if (! $body);
929 my $search_count = "?";
930 if ($body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
932 } elsif ($body =~ m@<NOBR>((\d{1,3})(,\d{3})*) @i) {
934 } elsif ($body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
936 } elsif ($body =~ m@found about ((\d{1,3})(,\d{3})*|\d+) results@) {
938 } elsif ($body =~ m@\b\d+ - \d+ of (\d+)\b@i) { # avimages
940 } elsif ($body =~ m@About ((\d{1,3})(,\d{3})*) images@i) { # avimages
942 } elsif ($body =~ m@We found ((\d{1,3})(,\d{3})*|\d+) results@i) { # *vista
944 } elsif ($body =~ m@ of about <B>((\d{1,3})(,\d{3})*)<@i) { # googleimages
946 } elsif ($body =~ m@<B>((\d{1,3})(,\d{3})*)</B> Web sites were found@i) {
947 $search_count = $1; # lycos
948 } elsif ($body =~ m@WEB.*?RESULTS.*?\b((\d{1,3})(,\d{3})*)\b.*?Matches@i) {
949 $search_count = $1; # hotbot
950 } elsif ($body =~ m@no photos were found containing@i) { # avimages
952 } elsif ($body =~ m@found no document matching@i) { # avtext
955 1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
957 # if ($search_count eq "?" || $search_count eq "0") {
959 # my $file = "/tmp/wc.html";
960 # open(OUT, ">$file") || error ("writing $file: $!");
963 # print STDERR blurb() . "###### wrote $file\n";
967 my $length = length($body);
976 foreach (split(/\n/)) {
978 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
981 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
982 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
984 if ( $rejected_urls{$u} ) {
985 LOG ($verbose_filter, " pre-rejecting candidate: $u");
989 LOG ($verbose_http, " HREF: $u");
991 $subpages[++$#subpages] = $u;
994 if ( $#subpages < 0 ) {
995 LOG ($verbose_filter,
996 "found nothing on $base ($length bytes, $href_count links).");
1000 LOG ($verbose_filter, "" . $#subpages+1 . " links on $search_url");
1002 return ($search_count, @subpages);
1010 my ($h) = m@^http://([^/: \t\r\n]+)@i;
1012 next unless defined($h);
1014 if ($poisoners{$h}) {
1015 LOG (($verbose_filter), " rejecting poisoner: $_");
1018 if ($h =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
1020 LOG (($verbose_filter), " rejecting poisoner: $_");
1023 if ($h =~ m@([^.]+\.[^.]+)$@ &&
1025 LOG (($verbose_filter), " rejecting poisoner: $_");
1035 # given a list of URLs, picks one at random; loads it; and returns a
1036 # random image from it.
1037 # returns the url of the page loaded; the url of the image chosen.
1039 sub pick_image_from_pages {
1040 my ($base, $total_hit_count, $unfiltered_link_count, $timeout, @pages) = @_;
1042 $total_hit_count = "?" unless defined($total_hit_count);
1044 @pages = depoison (@pages);
1046 "" . ($#pages+1) . " candidates of $unfiltered_link_count links" .
1047 " ($total_hit_count total)");
1049 return () if ($#pages < 0);
1051 my $i = int(rand($#pages+1));
1052 my $page = $pages[$i];
1054 LOG ($verbose_load, "picked page $page");
1056 $suppress_audit = 1;
1058 my ( $base2, $body2 ) = get_document ($page, $base, $timeout);
1060 if (!$base2 || !$body2) {
1065 my $img = pick_image_from_body ($base2, $body2);
1069 return ($base2, $img);
1076 ############################################################################
1078 # Pick images from random pages returned by the Yahoo Random Link
1080 ############################################################################
1083 my $yahoo_random_link = "http://random.yahoo.com/fast/ryl";
1086 # Picks a random page; picks a random image on that page;
1087 # returns two URLs: the page containing the image, and the image.
1088 # Returns () if nothing found this time.
1090 sub pick_from_yahoo_random_link {
1091 my ( $timeout ) = @_;
1093 print STDERR "\n\n" if ($verbose_load);
1094 LOG ($verbose_load, "URL: $yahoo_random_link");
1096 $last_search = $yahoo_random_link; # for warnings
1098 $suppress_audit = 1;
1100 my ( $base, $body ) = get_document ($yahoo_random_link, undef, $timeout);
1101 if (!$base || !$body) {
1106 LOG ($verbose_load, "redirected to: $base");
1108 my $img = pick_image_from_body ($base, $body);
1112 return ($base, $img);
1119 ############################################################################
1121 # Pick images from random pages returned by the Alta Vista Random Link
1123 ############################################################################
1126 my $alta_vista_random_link = "http://www.altavista.com/image/randomlink";
1129 # Picks a random page; picks a random image on that page;
1130 # returns two URLs: the page containing the image, and the image.
1131 # Returns () if nothing found this time.
1133 sub pick_from_alta_vista_random_link {
1134 my ( $timeout ) = @_;
1136 print STDERR "\n\n" if ($verbose_load);
1137 LOG ($verbose_load, "URL: $alta_vista_random_link");
1139 $last_search = $alta_vista_random_link; # for warnings
1141 $suppress_audit = 1;
1143 my ( $base, $body ) = get_document ($alta_vista_random_link,
1145 if (!$base || !$body) {
1150 LOG ($verbose_load, "redirected to: $base");
1152 my $img = pick_image_from_body ($base, $body);
1156 return ($base, $img);
1163 ############################################################################
1165 # Pick images by feeding random words into Alta Vista Image Search
1167 ############################################################################
1170 my $alta_vista_images_url = "http://www.altavista.com/image/results" .
1171 "?ipht=1" . # photos
1172 "&igrph=1" . # graphics
1175 "&micat=1" . # no partner sites
1176 "&sc=on" . # "site collapse"
1180 sub pick_from_alta_vista_images {
1181 my ( $timeout ) = @_;
1183 my $words = random_word();
1184 my $page = (int(rand(9)) + 1);
1185 my $search_url = $alta_vista_images_url . $words;
1188 $search_url .= "&pgno=" . $page; # page number
1189 $search_url .= "&stq=" . (($page-1) * 12); # first hit result on page
1192 my ($search_hit_count, @subpages) =
1193 pick_from_search_engine ($timeout, $search_url, $words);
1195 my @candidates = ();
1196 foreach my $u (@subpages) {
1198 # avimages is encoding their URLs now.
1199 next unless ($u =~ s/^.*\*\*(http%3a.*$)/$1/gsi);
1200 $u = url_unquote($u);
1202 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1203 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
1204 next if ($u =~ m@[/.]yahoo\.com\b@i); # yahoo and av in cahoots?
1205 next if ($u =~ m@[/.]doubleclick\.net\b@i); # you cretins
1206 next if ($u =~ m@[/.]clicktomarket\.com\b@i); # more cretins
1208 next if ($u =~ m@[/.]viewimages\.com\b@i); # stacked deck
1209 next if ($u =~ m@[/.]gettyimages\.com\b@i);
1211 LOG ($verbose_filter, " candidate: $u");
1212 push @candidates, $u;
1215 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1216 $timeout, @candidates);
1221 ############################################################################
1223 # Pick images by feeding random words into Google Image Search.
1224 # By Charles Gales <gales@us.ibm.com>
1226 ############################################################################
1229 my $google_images_url = "http://images.google.com/images" .
1230 "?site=images" . # photos
1231 "&btnG=Search" . # graphics
1232 "&safe=off" . # no screening
1237 sub pick_from_google_images {
1238 my ( $timeout, $words, $max_page ) = @_;
1240 if (!defined($words)) {
1241 $words = random_word; # only one word for Google
1244 my $page = (int(rand(9)) + 1);
1245 my $num = 20; # 20 images per page
1246 my $search_url = $google_images_url . $words;
1249 $search_url .= "&start=" . $page*$num; # page number
1250 $search_url .= "&num=" . $num; #images per page
1253 my ($search_hit_count, @subpages) =
1254 pick_from_search_engine ($timeout, $search_url, $words);
1256 my @candidates = ();
1258 foreach my $u (@subpages) {
1259 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1260 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1262 if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1265 $img = "http://$img" unless ($img =~ m/^http:/i);
1267 LOG ($verbose_filter, " candidate: $ref");
1268 push @candidates, $img;
1269 $referers{$img} = $ref;
1273 @candidates = depoison (@candidates);
1274 return () if ($#candidates < 0);
1275 my $i = int(rand($#candidates+1));
1276 my $img = $candidates[$i];
1277 my $ref = $referers{$img};
1279 LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1280 return ($ref, $img);
1285 ############################################################################
1287 # Pick images by feeding random numbers into Google Image Search.
1288 # By jwz, suggested by Ian O'Donnell.
1290 ############################################################################
1294 sub pick_from_google_image_numbers {
1295 my ( $timeout ) = @_;
1298 my $number = int(rand($max));
1300 $number = sprintf("%04d", $number)
1303 pick_from_google_images ($timeout, "$number");
1308 ############################################################################
1310 # Pick images by feeding random digital camera file names into
1311 # Google Image Search.
1312 # By jwz, inspired by the excellent Random Personal Picture Finder
1313 # at http://www.diddly.com/random/
1315 ############################################################################
1319 # Common digital camera file name formats, as described at
1320 # http://www.diddly.com/random/about.html
1322 sub { sprintf ("dcp%05d.jpg", int(rand(4000))); }, # Kodak
1323 sub { sprintf ("dsc%05d.jpg", int(rand(4000))); }, # Nikon
1324 sub { sprintf ("dscn%04d.jpg", int(rand(4000))); }, # Nikon
1325 sub { sprintf ("mvc-%03d.jpg", int(rand(999))); }, # Sony Mavica
1326 sub { sprintf ("mvc%05d.jpg", int(rand(9999))); }, # Sony Mavica
1327 sub { sprintf ("P101%04d.jpg", int(rand(9999))); }, # Olympus w/ date=101
1328 sub { sprintf ("P%x%02d%04d.jpg", # Olympus
1329 int(rand(0xC)), int(rand(30))+1,
1331 sub { sprintf ("IMG_%03d.jpg", int(rand(999))); }, # ?
1332 sub { sprintf ("IMAG%04d.jpg", int(rand(9999))); }, # RCA and Samsung
1333 sub { my $n = int(rand(9999)); # Canon
1334 sprintf ("1%02d-%04d.jpg", int($n/100), $n); },
1335 sub { my $n = int(rand(9999)); # Canon
1336 sprintf ("1%02d-%04d_IMG.jpg",
1337 int($n/100), $n); },
1338 sub { sprintf ("IMG_%04d.jpg", int(rand(9999))); }, # Canon
1339 sub { sprintf ("dscf%04d.jpg", int(rand(9999))); }, # Fuji Finepix
1340 sub { sprintf ("pdrm%04d.jpg", int(rand(9999))); }, # Toshiba PDR
1341 sub { sprintf ("IM%06d.jpg", int(rand(9999))); }, # HP Photosmart
1342 sub { sprintf ("EX%06d.jpg", int(rand(9999))); }, # HP Photosmart
1343 # sub { my $n = int(rand(3)); # Kodak DC-40,50,120
1344 # sprintf ("DC%04d%s.jpg", int(rand(9999)),
1345 # $n == 0 ? 'S' : $n == 1 ? 'M' : 'L'); },
1346 sub { sprintf ("pict%04d.jpg", int(rand(9999))); }, # Minolta Dimage
1347 sub { sprintf ("P%07d.jpg", int(rand(9999))); }, # Kodak DC290
1348 # sub { sprintf ("%02d%02d%04d.jpg", # Casio QV3000, QV4000
1349 # int(rand(12))+1, int(rand(31))+1,
1350 # int(rand(999))); },
1351 # sub { sprintf ("%02d%x%02d%04d.jpg", # Casio QV7000
1352 # int(rand(6)), # year
1353 # int(rand(12))+1, int(rand(31))+1,
1354 # int(rand(999))); },
1355 sub { sprintf ("IMGP%04d.jpg", int(rand(9999))); }, # Pentax Optio S
1356 sub { sprintf ("PANA%04d.jpg", int(rand(9999))); }, # Panasonic vid still
1357 sub { sprintf ("HPIM%04d.jpg", int(rand(9999))); }, # HP Photosmart
1358 sub { sprintf ("PCDV%04d.jpg", int(rand(9999))); }, # ?
1363 sub pick_from_google_image_photos {
1364 my ( $timeout ) = @_;
1366 my $i = int(rand($#photomakers + 1));
1367 my $fn = $photomakers[$i];
1369 my $words .= $file . "%20filetype:jpg";
1371 pick_from_google_images ($timeout, $words);
1376 ############################################################################
1378 # Pick images by feeding random words into Alta Vista Text Search
1380 ############################################################################
1383 my $alta_vista_url = "http://www.altavista.com/web/results" .
1387 "&sc=on" . # "site collapse"
1392 sub pick_from_alta_vista_text {
1393 my ( $timeout ) = @_;
1395 my $words = random_words(0);
1396 my $page = (int(rand(9)) + 1);
1397 my $search_url = $alta_vista_url . $words;
1400 $search_url .= "&pgno=" . $page;
1401 $search_url .= "&stq=" . (($page-1) * 10);
1404 my ($search_hit_count, @subpages) =
1405 pick_from_search_engine ($timeout, $search_url, $words);
1407 my @candidates = ();
1408 foreach my $u (@subpages) {
1410 # Those altavista fuckers are playing really nasty redirection games
1411 # these days: the filter your clicks through their site, but use
1412 # onMouseOver to make it look like they're not! Well, it makes it
1413 # easier for us to identify search results...
1415 next unless ($u =~ s/^.*\*\*(http%3a.*$)/$1/gsi);
1416 $u = url_unquote($u);
1418 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1419 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
1420 next if ($u =~ m@[/.]yahoo\.com\b@i); # yahoo and av in cahoots?
1422 LOG ($verbose_filter, " candidate: $u");
1423 push @candidates, $u;
1426 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1427 $timeout, @candidates);
1432 ############################################################################
1434 # Pick images by feeding random words into Hotbot
1436 ############################################################################
1438 my $hotbot_search_url =("http://hotbot.lycos.com/default.asp" .
1440 "&descriptiontype=0" .
1447 "&cobrand=undefined" .
1450 sub pick_from_hotbot_text {
1451 my ( $timeout ) = @_;
1453 $last_search = $hotbot_search_url; # for warnings
1455 # lycos seems to always give us back dictionaries and word lists if
1456 # we search for more than one word...
1458 my $words = random_word();
1460 my $start = int(rand(8)) * 10 + 1;
1461 my $search_url = $hotbot_search_url . $words . "&first=$start&page=more";
1463 my ($search_hit_count, @subpages) =
1464 pick_from_search_engine ($timeout, $search_url, $words);
1466 my @candidates = ();
1467 foreach my $u (@subpages) {
1469 # Hotbot plays redirection games too
1471 # next unless ($u =~ m@/director.asp\?.*\btarget=([^&]+)@);
1472 # $u = url_decode($1);
1474 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1475 next if ($u =~ m@[/.]hotbot\.com\b@i); # skip hotbot builtins
1476 next if ($u =~ m@[/.]lycos\.com\b@i); # skip hotbot builtins
1477 next if ($u =~ m@[/.]inktomi\.com\b@i); # skip hotbot builtins
1479 LOG ($verbose_filter, " candidate: $u");
1480 push @candidates, $u;
1483 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1484 $timeout, @candidates);
1489 ############################################################################
1491 # Pick images by feeding random words into Lycos
1493 ############################################################################
1495 my $lycos_search_url = "http://search.lycos.com/default.asp" .
1501 sub pick_from_lycos_text {
1502 my ( $timeout ) = @_;
1504 $last_search = $lycos_search_url; # for warnings
1506 # lycos seems to always give us back dictionaries and word lists if
1507 # we search for more than one word...
1509 my $words = random_word();
1511 my $start = int(rand(8)) * 10 + 1;
1512 my $search_url = $lycos_search_url . $words . "&first=$start&page=more";
1514 my ($search_hit_count, @subpages) =
1515 pick_from_search_engine ($timeout, $search_url, $words);
1517 my @candidates = ();
1518 foreach my $u (@subpages) {
1520 # Lycos plays redirection games.
1522 # next unless ($u =~ m@^http://click.lycos.com/director.asp
1527 # $u = url_decode($1);
1529 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1530 next if ($u =~ m@[/.]hotbot\.com\b@i); # skip lycos builtins
1531 next if ($u =~ m@[/.]lycos\.com\b@i); # skip lycos builtins
1532 next if ($u =~ m@[/.]terralycos\.com\b@i); # skip lycos builtins
1533 next if ($u =~ m@[/.]inktomi\.com\b@i); # skip lycos builtins
1536 LOG ($verbose_filter, " candidate: $u");
1537 push @candidates, $u;
1540 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1541 $timeout, @candidates);
1546 ############################################################################
1548 # Pick images by feeding random words into news.yahoo.com
1550 ############################################################################
1552 my $yahoo_news_url = "http://news.search.yahoo.com/search/news" .
1557 sub pick_from_yahoo_news_text {
1558 my ( $timeout ) = @_;
1560 $last_search = $yahoo_news_url; # for warnings
1562 my $words = random_word();
1563 my $search_url = $yahoo_news_url . $words;
1565 my ($search_hit_count, @subpages) =
1566 pick_from_search_engine ($timeout, $search_url, $words);
1568 my @candidates = ();
1569 foreach my $u (@subpages) {
1571 # de-redirectize the URLs
1572 $u =~ s@^http://rds\.yahoo\.com/.*-http%3A@http:@s;
1574 # only accept URLs on Yahoo's news site
1575 next unless ($u =~ m@^http://dailynews\.yahoo\.com/@i ||
1576 $u =~ m@^http://story\.news\.yahoo\.com/@i);
1577 next unless ($u =~ m@&u=/@);
1579 LOG ($verbose_filter, " candidate: $u");
1580 push @candidates, $u;
1583 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1584 $timeout, @candidates);
1589 ############################################################################
1591 # Pick images from LiveJournal's list of recently-posted images.
1593 ############################################################################
1595 my $livejournal_img_url = "http://www.livejournal.com/stats/latest-img.bml";
1597 # With most of our image sources, we get a random page and then select
1598 # from the images on it. However, in the case of LiveJournal, the page
1599 # of images tends to update slowly; so we'll remember the last N entries
1600 # on it and randomly select from those, to get a wider variety each time.
1602 my $lj_cache_size = 1000;
1603 my @lj_cache = (); # fifo, for ordering by age
1604 my %lj_cache = (); # hash, for detecting dups
1607 sub pick_from_livejournal_images {
1608 my ( $timeout ) = @_;
1610 $last_search = $livejournal_img_url; # for warnings
1612 my ( $base, $body ) = get_document ($livejournal_img_url, undef, $timeout);
1613 return () unless $body;
1616 $body =~ s/(<recent-image)\b/\n$1/gsi;
1618 foreach (split (/\n/, $body)) {
1619 next unless (m/^<recent-image\b/);
1620 next unless (m/\bIMG=[\'\"]([^\'\"]+)[\'\"]/si);
1621 my $img = html_unquote ($1);
1623 next if ($lj_cache{$img}); # already have it
1625 next unless (m/\bURL=[\'\"]([^\'\"]+)[\'\"]/si);
1626 my $page = html_unquote ($1);
1627 my @pair = ($img, $page);
1628 LOG ($verbose_filter, " candidate: $img");
1629 push @lj_cache, \@pair;
1630 $lj_cache{$img} = \@pair;
1633 return () if ($#lj_cache == -1);
1635 my $n = $#lj_cache+1;
1636 my $i = int(rand($n));
1637 my ($img, $page) = @{$lj_cache[$i]};
1639 # delete this one from @lj_cache and from %lj_cache.
1641 @lj_cache = ( @lj_cache[0 .. $i-1],
1642 @lj_cache[$i+1 .. $#lj_cache] );
1643 delete $lj_cache{$img};
1645 # Keep the size of the cache under the limit by nuking older entries
1647 while ($#lj_cache >= $lj_cache_size) {
1648 my $pairP = shift @lj_cache;
1649 my $img = $pairP->[0];
1650 delete $lj_cache{$img};
1653 LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
1655 return ($page, $img);
1659 ############################################################################
1661 # Pick images from ircimages.com (images that have been in the /topic of
1662 # various IRC channels.)
1664 ############################################################################
1666 my $ircimages_url = "http://ircimages.com/";
1669 sub pick_from_ircimages {
1670 my ( $timeout ) = @_;
1672 $last_search = $ircimages_url; # for warnings
1674 my $n = int(rand(2900));
1675 my $search_url = $ircimages_url . "page-$n";
1677 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
1678 return () unless $body;
1680 my @candidates = ();
1683 $body =~ s/(<A)\b/\n$1/gsi;
1685 foreach (split (/\n/, $body)) {
1687 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
1690 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
1691 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
1693 next unless ($u =~ m/^http:/i);
1694 next if ($u =~ m@^http://(searchirc\.com\|ircimages\.com)@i);
1695 next unless ($u =~ m@[.](gif|jpg|jpeg|pjpg|pjpeg|png)$@i);
1697 LOG ($verbose_http, " HREF: $u");
1698 push @candidates, $u;
1701 LOG ($verbose_filter, "" . $#candidates+1 . " links on $search_url");
1703 return () if ($#candidates == -1);
1705 my $i = int(rand($#candidates+1));
1706 my $img = $candidates[$i];
1708 LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#candidates+1) .
1711 $search_url = $img; # hmm...
1712 return ($search_url, $img);
1716 ############################################################################
1718 # Pick images from Flickr's page of recently-posted photos.
1720 ############################################################################
1722 my $flickr_img_url = "http://www.flickr.com/photos/";
1724 # Like LiveJournal, the Flickr page of images tends to update slowly,
1725 # so remember the last N entries on it and randomly select from those.
1727 # I know that Flickr has an API (http://www.flickr.com/services/api/)
1728 # but it was easy enough to scrape the HTML, so I didn't bother exploring.
1730 my $flickr_cache_size = 1000;
1731 my @flickr_cache = (); # fifo, for ordering by age
1732 my %flickr_cache = (); # hash, for detecting dups
1736 sub pick_from_flickr {
1737 my ( $timeout ) = @_;
1739 my $start = 16 * int(rand(100));
1741 $last_search = $flickr_img_url; # for warnings
1742 $last_search .= "?start=$start" if ($start > 0);
1744 my ( $base, $body ) = get_document ($last_search, undef, $timeout);
1745 return () unless $body;
1747 $body =~ s/[\r\n]/ /gs;
1748 $body =~ s/(<a)\b/\n$1/gsi;
1752 foreach (split (/\n/, $body)) {
1753 my ($page, $thumb) = m@<A \s [^<>]* \b HREF=\"([^<>\"]+)\" [^<>]* > \s*
1754 <IMG \s [^<>]* \b SRC=\"([^<>\"]+)\" @xsi;
1755 next unless defined ($thumb);
1756 $page = html_unquote ($page);
1757 $thumb = html_unquote ($thumb);
1759 next unless ($thumb =~ m@^http://photos\d*\.flickr\.com/@);
1761 my $base = "http://www.flickr.com/";
1762 $page =~ s@^/@$base@;
1763 $thumb =~ s@^/@$base@;
1766 $img =~ s/_[a-z](\.[a-z\d]+)$/$1/si; # take off "thumb" suffix
1769 next if ($flickr_cache{$img}); # already have it
1771 my @pair = ($img, $page, $start);
1772 LOG ($verbose_filter, " candidate: $img");
1773 push @flickr_cache, \@pair;
1774 $flickr_cache{$img} = \@pair;
1778 return () if ($#flickr_cache == -1);
1780 my $n = $#flickr_cache+1;
1781 my $i = int(rand($n));
1782 my ($img, $page) = @{$flickr_cache[$i]};
1784 # delete this one from @flickr_cache and from %flickr_cache.
1786 @flickr_cache = ( @flickr_cache[0 .. $i-1],
1787 @flickr_cache[$i+1 .. $#flickr_cache] );
1788 delete $flickr_cache{$img};
1790 # Keep the size of the cache under the limit by nuking older entries
1792 while ($#flickr_cache >= $flickr_cache_size) {
1793 my $pairP = shift @flickr_cache;
1794 my $img = $pairP->[0];
1795 delete $flickr_cache{$img};
1798 LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
1800 return ($page, $img);
1804 ############################################################################
1806 # Pick images by waiting for driftnet to populate a temp dir with files.
1807 # Requires driftnet version 0.1.5 or later.
1808 # (Driftnet is a program by Chris Lightfoot that sniffs your local ethernet
1809 # for images being downloaded by others.)
1810 # Driftnet/webcollage integration by jwz.
1812 ############################################################################
1815 sub pick_from_driftnet {
1816 my ( $timeout ) = @_;
1818 my $id = $driftnet_magic;
1819 my $dir = $driftnet_dir;
1823 error ("\$driftnet_dir unset?") unless ($dir);
1826 error ("$dir unreadable") unless (-d "$dir/.");
1828 $timeout = $http_timeout unless ($timeout);
1831 while ($now = time, $now < $start + $timeout) {
1833 opendir (DIR, $dir) || error ("$dir: $!");
1834 while (my $file = readdir(DIR)) {
1835 next if ($file =~ m/^\./);
1836 $file = "$dir/$file";
1838 LOG ($verbose_load, "picked file $file ($id)");
1839 return ($id, $file);
1843 LOG (($verbose_net || $verbose_load), "timed out for $id");
1848 sub get_driftnet_file {
1851 error ("\$driftnet_dir unset?") unless ($driftnet_dir);
1853 my $id = $driftnet_magic;
1854 my $re = qr/$driftnet_dir/;
1855 error ("$id: $file not in $driftnet_dir?")
1856 unless ($file =~ m@^$re@o);
1859 open (IN, $file) || error ("$id: $file: $!");
1861 while (<IN>) { $body .= $_; }
1862 close IN || error ("$id: $file: $!");
1863 unlink ($file) || error ("$id: $file: rm: $!");
1864 return ($id, $body);
1868 sub spawn_driftnet {
1871 # make a directory to use.
1873 my $tmp = $ENV{TEMPDIR} || "/tmp";
1874 $driftnet_dir = sprintf ("$tmp/driftcollage-%08x", rand(0xffffffff));
1875 LOG ($verbose_exec, "mkdir $driftnet_dir");
1876 last if mkdir ($driftnet_dir, 0700);
1879 if (! ($cmd =~ m/\s/)) {
1880 # if the command didn't have any arguments in it, then it must be just
1881 # a pointer to the executable. Append the default args to it.
1882 my $dargs = $default_driftnet_cmd;
1883 $dargs =~ s/^[^\s]+//;
1887 # point the driftnet command at our newly-minted private directory.
1889 $cmd .= " -d $driftnet_dir";
1890 $cmd .= ">/dev/null" unless ($verbose_exec);
1893 if ($pid < 0) { error ("fork: $!\n"); }
1896 push @pids_to_kill, $pid;
1897 LOG ($verbose_exec, "forked for \"$cmd\"");
1900 nontrapping_system ($cmd) || error ("exec: $!");
1903 # wait a bit, then make sure the process actually started up.
1906 error ("pid $pid failed to start \"$cmd\"")
1907 unless (1 == kill (0, $pid));
1911 ############################################################################
1913 # Pick a random image in a random way
1915 ############################################################################
1918 # Picks a random image on a random page, and returns two URLs:
1919 # the page containing the image, and the image.
1920 # Returns () if nothing found this time.
1924 my ( $timeout ) = @_;
1926 $current_state = "select";
1927 $load_method = "none";
1929 my $n = int(rand(100));
1932 my @rest = @search_methods;
1935 my $pct = shift @rest;
1936 my $name = shift @rest;
1937 my $tfn = shift @rest;
1939 if ($total > $n && !defined($fn)) {
1941 $current_state = $name;
1942 $load_method = $current_state;
1946 if ($total != 100) {
1947 error ("internal error: \@search_methods totals to $total%!");
1950 record_attempt ($current_state);
1951 return $fn->($timeout);
1956 ############################################################################
1958 # Statistics and logging
1960 ############################################################################
1963 return strftime ("%H:%M:%S: ", localtime);
1967 return "$progname: " . timestr() . "$current_state: ";
1972 print STDERR blurb() . "$err\n";
1978 print STDERR "$progname: stack trace:\n";
1980 my ($package, $filename, $line, $subroutine) = caller($i++);
1981 last unless defined($package);
1982 $filename =~ s@^.*/@@;
1983 print STDERR " $filename#$line, $subroutine\n";
1995 my $head = "$progname: DEBUG: ";
1996 foreach (split (/\n/, $lastlog)) {
1997 print STDERR "$head$_\n";
2003 my ($print, $msg) = @_;
2004 my $blurb = timestr() . "$current_state: ";
2005 $lastlog .= "$blurb$msg\n";
2006 print STDERR "$progname: $blurb$msg\n" if $print;
2011 my %stats_successes;
2014 my $last_state = undef;
2015 sub record_attempt {
2019 record_failure($last_state) unless ($image_succeeded > 0);
2021 $last_state = $name;
2024 report_performance();
2027 $image_succeeded = 0;
2028 $suppress_audit = 0;
2031 sub record_success {
2032 my ($name, $url, $base) = @_;
2033 if (defined($stats_successes{$name})) {
2034 $stats_successes{$name}++;
2036 $stats_successes{$name} = 1;
2039 stop_timer ($name, 1);
2040 my $o = $current_state;
2041 $current_state = $name;
2042 save_recent_url ($url, $base);
2043 $current_state = $o;
2044 $image_succeeded = 1;
2049 sub record_failure {
2052 return if $image_succeeded;
2054 stop_timer ($name, 0);
2055 if ($verbose_load && !$verbose_exec) {
2057 if ($suppress_audit) {
2058 print STDERR "$progname: " . timestr() . "(audit log suppressed)\n";
2062 my $o = $current_state;
2063 $current_state = "DEBUG";
2065 my $line = "#" x 78;
2066 print STDERR "\n\n\n";
2067 print STDERR ("#" x 78) . "\n";
2068 print STDERR blurb() . "failed to get an image. Full audit log:\n";
2071 print STDERR ("-" x 78) . "\n";
2072 print STDERR "\n\n";
2074 $current_state = $o;
2076 $image_succeeded = 0;
2083 my $i = $stats_successes{$name};
2084 my $j = $stats_attempts{$name};
2087 return "" . ($j ? int($i * 100 / $j) : "0") . "%";
2091 my $current_start_time = 0;
2095 $current_start_time = time;
2097 if (defined($stats_attempts{$name})) {
2098 $stats_attempts{$name}++;
2100 $stats_attempts{$name} = 1;
2102 if (!defined($stats_elapsed{$name})) {
2103 $stats_elapsed{$name} = 0;
2108 my ($name, $success) = @_;
2109 $stats_elapsed{$name} += time - $current_start_time;
2113 my $last_report_time = 0;
2114 sub report_performance {
2116 return unless $verbose_warnings;
2119 return unless ($now >= $last_report_time + $report_performance_interval);
2120 my $ot = $last_report_time;
2121 $last_report_time = $now;
2123 return if ($ot == 0);
2125 my $blurb = "$progname: " . timestr();
2128 print STDERR "${blurb}Current standings:\n";
2130 foreach my $name (sort keys (%stats_attempts)) {
2131 my $try = $stats_attempts{$name};
2132 my $suc = $stats_successes{$name} || 0;
2133 my $pct = int($suc * 100 / $try);
2134 my $secs = $stats_elapsed{$name};
2135 my $secs_link = int($secs / $try);
2136 print STDERR sprintf ("$blurb %-12s %4s (%d/%d);\t %2d secs/link\n",
2137 "$name:", "$pct%", $suc, $try, $secs_link);
2143 my $max_recent_images = 400;
2144 my $max_recent_sites = 20;
2145 my @recent_images = ();
2146 my @recent_sites = ();
2148 sub save_recent_url {
2149 my ($url, $base) = @_;
2151 return unless ($verbose_warnings);
2154 my ($site) = m@^http://([^ \t\n\r/:]+)@;
2155 return unless defined ($site);
2157 if ($base eq $driftnet_magic) {
2158 $site = $driftnet_magic;
2159 @recent_images = ();
2163 foreach (@recent_images) {
2165 print STDERR blurb() . "WARNING: recently-duplicated image: $url" .
2166 " (on $base via $last_search)\n";
2172 # suppress "duplicate site" warning via %warningless_sites.
2174 if ($warningless_sites{$site}) {
2176 } elsif ($site =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
2177 $warningless_sites{$1}) {
2179 } elsif ($site =~ m@([^.]+\.[^.]+)$@ &&
2180 $warningless_sites{$1}) {
2185 foreach (@recent_sites) {
2187 print STDERR blurb() . "WARNING: recently-duplicated site: $site" .
2188 " ($url on $base via $last_search)\n";
2194 push @recent_images, $url;
2195 push @recent_sites, $site;
2196 shift @recent_images if ($#recent_images >= $max_recent_images);
2197 shift @recent_sites if ($#recent_sites >= $max_recent_sites);
2202 ##############################################################################
2206 ##############################################################################
2213 s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
2218 # Given the raw body of a GIF document, returns the dimensions of the image.
2222 my $type = substr($body, 0, 6);
2224 return () unless ($type =~ /GIF8[7,9]a/);
2225 $s = substr ($body, 6, 10);
2226 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
2227 return (($b<<8|$a), ($d<<8|$c));
2230 # Given the raw body of a JPEG document, returns the dimensions of the image.
2235 my $L = length($body);
2237 my $c1 = substr($body, $i, 1); $i++;
2238 my $c2 = substr($body, $i, 1); $i++;
2239 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
2242 while (ord($ch) != 0xDA && $i < $L) {
2243 # Find next marker, beginning with 0xFF.
2244 while (ord($ch) != 0xFF) {
2245 return () if (length($body) <= $i);
2246 $ch = substr($body, $i, 1); $i++;
2248 # markers can be padded with any number of 0xFF.
2249 while (ord($ch) == 0xFF) {
2250 return () if (length($body) <= $i);
2251 $ch = substr($body, $i, 1); $i++;
2254 # $ch contains the value of the marker.
2255 my $marker = ord($ch);
2257 if (($marker >= 0xC0) &&
2258 ($marker <= 0xCF) &&
2259 ($marker != 0xC4) &&
2260 ($marker != 0xCC)) { # it's a SOFn marker
2262 return () if (length($body) <= $i);
2263 my $s = substr($body, $i, 4); $i += 4;
2264 my ($a,$b,$c,$d) = unpack("C"x4, $s);
2265 return (($c<<8|$d), ($a<<8|$b));
2268 # We must skip variables, since FFs in variable names aren't
2269 # valid JPEG markers.
2270 return () if (length($body) <= $i);
2271 my $s = substr($body, $i, 2); $i += 2;
2272 my ($c1, $c2) = unpack ("C"x2, $s);
2273 my $length = ($c1 << 8) | $c2;
2274 return () if ($length < 2);
2281 # Given the raw body of a PNG document, returns the dimensions of the image.
2285 return () unless ($body =~ m/^\211PNG\r/);
2286 my ($bits) = ($body =~ m/^.{12}(.{12})/s);
2287 return () unless defined ($bits);
2288 return () unless ($bits =~ /^IHDR/);
2289 my ($ign, $w, $h) = unpack("a4N2", $bits);
2294 # Given the raw body of a GIF, JPEG, or PNG document, returns the dimensions
2299 my ($w, $h) = gif_size ($body);
2300 if ($w && $h) { return ($w, $h); }
2301 ($w, $h) = jpeg_size ($body);
2302 if ($w && $h) { return ($w, $h); }
2303 return png_size ($body);
2307 # returns the full path of the named program, or undef.
2311 foreach (split (/:/, $ENV{PATH})) {
2312 if (-x "$_/$prog") {
2320 # Like rand(), but chooses numbers with a bell curve distribution.
2323 $_ = 1.0 unless defined($_);
2325 return (rand($_) + rand($_) + rand($_));
2331 print STDERR "$progname: exiting\n" if ($verbose_warnings);
2332 if (@pids_to_kill) {
2333 print STDERR blurb() . "killing: " . join(' ', @pids_to_kill) . "\n";
2334 kill ('TERM', @pids_to_kill);
2338 sub signal_cleanup {
2340 print STDERR blurb() . (defined($sig)
2341 ? "caught signal $sig."
2344 if ($verbose_exec || $verbose_warnings);
2350 ##############################################################################
2352 # Generating a list of urls only
2354 ##############################################################################
2356 sub url_only_output {
2358 my ($base, $img) = pick_image;
2362 print "$img $base\n";
2367 ##############################################################################
2369 # Running as an xscreensaver module, or as a web page imagemap
2371 ##############################################################################
2373 my $image_ppm = sprintf ("%s/webcollage-%08x",
2374 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
2376 my $image_tmp1 = sprintf ("%s/webcollage-1-%08x",
2377 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
2379 my $image_tmp2 = sprintf ("%s/webcollage-2-%08x",
2380 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
2383 my $filter_cmd = undef;
2384 my $post_filter_cmd = undef;
2385 my $background = undef;
2387 my @imagemap_areas = ();
2388 my $imagemap_html_tmp = undef;
2389 my $imagemap_jpg_tmp = undef;
2392 my $img_width; # size of the image being generated.
2398 unlink $image_ppm, $image_tmp1, $image_tmp2;
2399 unlink $imagemap_html_tmp, $imagemap_jpg_tmp
2400 if (defined ($imagemap_html_tmp));
2404 # Like system, but prints status about exit codes, and kills this process
2405 # with whatever signal killed the sub-process, if any.
2407 sub nontrapping_system {
2411 s/\"[^\"]+\"/\"...\"/g;
2413 LOG ($verbose_exec, "executing \"$_\"");
2418 LOG ($verbose_exec, "subproc exited normally.");
2419 } elsif (($rc & 0xff) == 0) {
2421 LOG ($verbose_exec, "subproc exited with status $rc.");
2424 LOG ($verbose_exec, "subproc dumped core.");
2427 LOG ($verbose_exec, "subproc died with signal $rc.");
2428 # die that way ourselves.
2436 # Given the URL of a GIF, JPEG, or PNG image, and the body of that image,
2437 # writes a PPM to the given output file. Returns the width/height of the
2438 # image if successful.
2441 my ($url, $body, $output) = @_;
2442 my ($cmd, $cmd2, $w, $h);
2444 if ((@_ = gif_size ($body))) {
2447 } elsif ((@_ = jpeg_size ($body))) {
2450 } elsif ((@_ = png_size ($body))) {
2454 LOG (($verbose_pbm || $verbose_load),
2455 "not a GIF, JPG, or PNG" .
2456 (($body =~ m@<(base|html|head|body|script|table|a href)\b@i)
2457 ? " (looks like HTML)" : "") .
2459 $suppress_audit = 1;
2463 $cmd2 = "exec $cmd"; # yes, this really is necessary. if we don't
2464 # do this, the process doesn't die properly.
2465 if (!$verbose_pbm) {
2467 # We get a "giftopnm: got a 'Application Extension' extension"
2468 # warning any time it's an animgif.
2470 # Note that "giftopnm: EOF / read error on image data" is not
2471 # always a fatal error -- sometimes the image looks fine anyway.
2473 $cmd2 .= " 2>/dev/null";
2476 # There exist corrupted GIF and JPEG files that can make giftopnm and
2477 # djpeg lose their minds and go into a loop. So this gives those programs
2478 # a small timeout -- if they don't complete in time, kill them.
2484 local $SIG{ALRM} = sub {
2486 "timed out ($cvt_timeout) for $cmd on \"$url\" in pid $pid");
2487 kill ('TERM', $pid) if ($pid);
2492 if (($pid = open(PIPE, "| $cmd2 > $output"))) {
2499 LOG ($verbose_exec, "awaiting $pid");
2501 LOG ($verbose_exec, "$pid completed");
2503 my $size = (stat($output))[7];
2504 $size = -1 unless defined($size);
2506 LOG ($verbose_pbm, "$cmd on ${w}x$h \"$url\" failed ($size bytes)");
2510 LOG ($verbose_pbm, "created ${w}x$h $output ($cmd)");
2513 print STDERR blurb() . "$cmd failed: $!\n";
2517 die if ($@ && $@ ne "alarm\n"); # propagate errors
2530 sub pick_root_displayer {
2533 foreach my $cmd (@root_displayers) {
2535 my ($name) = m/^([^ ]+)/;
2536 push @names, "\"$name\"";
2537 LOG ($verbose_exec, "looking for $name...");
2538 foreach my $dir (split (/:/, $ENV{PATH})) {
2539 LOG ($verbose_exec, " checking $dir/$name");
2540 return $cmd if (-x "$dir/$name");
2544 $names[$#names] = "or " . $names[$#names];
2545 error "none of: " . join (", ", @names) . " were found on \$PATH.";
2549 my $ppm_to_root_window_cmd = undef;
2552 sub x_or_pbm_output {
2553 my ($window_id) = @_;
2555 # Check for our helper program, to see whether we need to use PPM pipelines.
2557 $_ = "webcollage-helper";
2558 if (defined ($webcollage_helper) || which ($_)) {
2559 $webcollage_helper = $_ unless (defined($webcollage_helper));
2560 LOG ($verbose_pbm, "found \"$webcollage_helper\"");
2561 $webcollage_helper .= " -v";
2563 LOG (($verbose_pbm || $verbose_load), "no $_ program");
2566 # make sure the various programs we execute exist, right up front.
2568 my @progs = ("ppmmake"); # always need this one
2570 if (!defined($webcollage_helper)) {
2571 # Only need these others if we don't have the helper.
2573 "giftopnm", "pngtopnm", "djpeg",
2574 "pnmpaste", "pnmscale", "pnmcut");
2578 which ($_) || error "$_ not found on \$PATH.";
2581 # find a root-window displayer program.
2583 $ppm_to_root_window_cmd = pick_root_displayer();
2585 if (defined ($window_id)) {
2586 error ("-window-id only works if xscreensaver-getimage is installed")
2587 unless ($ppm_to_root_window_cmd =~ m/^xscreensaver-getimage\b/);
2589 error ("unparsable window id: $window_id")
2590 unless ($window_id =~ m/^\d+$|^0x[\da-f]+$/i);
2591 $ppm_to_root_window_cmd =~ s/--?root\b/$window_id/ ||
2592 error ("unable to munge displayer: $ppm_to_root_window_cmd");
2595 if (!$img_width || !$img_height) {
2597 if (!defined ($window_id) &&
2598 defined ($ENV{XSCREENSAVER_WINDOW})) {
2599 $window_id = $ENV{XSCREENSAVER_WINDOW};
2602 if (!defined ($window_id)) {
2604 which ($_) || error "$_ not found on \$PATH.";
2606 ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
2607 if (!defined($img_height)) {
2608 error "xdpyinfo failed.";
2610 } else { # we have a window id
2612 which ($_) || error "$_ not found on \$PATH.";
2613 $_ .= " -id $window_id";
2615 ($img_width, $img_height) = m/^\s*Width:\s*(\d+)\n\s*Height:\s*(\d+)\n/m;
2617 if (!defined($img_height)) {
2618 error "xwininfo failed.";
2623 my $bgcolor = "#000000";
2624 my $bgimage = undef;
2627 if ($background =~ m/^\#[0-9a-f]+$/i) {
2628 $bgcolor = $background;
2630 } elsif (-r $background) {
2631 $bgimage = $background;
2633 } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
2634 error "not a color or readable file: $background";
2637 # default to assuming it's a color
2638 $bgcolor = $background;
2642 # Create the sold-colored base image.
2644 $_ = "ppmmake '$bgcolor' $img_width $img_height";
2645 LOG ($verbose_pbm, "creating base image: $_");
2646 nontrapping_system "$_ > $image_ppm";
2648 # Paste the default background image in the middle of it.
2655 open(IMG, "<$bgimage") || error "couldn't open $bgimage: $!";
2657 while (<IMG>) { $body .= $_; }
2660 if ((@_ = gif_size ($body))) {
2662 $cmd = "giftopnm |";
2664 } elsif ((@_ = jpeg_size ($body))) {
2668 } elsif ((@_ = png_size ($body))) {
2670 $cmd = "pngtopnm |";
2672 } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
2678 error "$bgimage is not a GIF, JPEG, PNG, or PPM.";
2681 my $x = int (($img_width - $iw) / 2);
2682 my $y = int (($img_height - $ih) / 2);
2684 "pasting $bgimage (${iw}x$ih) into base image at $x,$y");
2686 $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
2687 open (IMG, "| $cmd") || error "running $cmd: $!";
2691 LOG ($verbose_exec, "subproc exited normally.");
2692 rename ($image_tmp1, $image_ppm) ||
2693 error "renaming $image_tmp1 to $image_ppm: $!";
2699 my ($base, $img) = pick_image();
2700 my $source = $current_state;
2701 $current_state = "loadimage";
2703 my ($headers, $body) = get_document ($img, $base);
2705 paste_image ($base, $img, $body, $source);
2709 $current_state = "idle";
2710 $load_method = "none";
2712 unlink $image_tmp1, $image_tmp2;
2718 my ($base, $img, $body, $source) = @_;
2720 $current_state = "paste";
2722 $suppress_audit = 0;
2724 LOG ($verbose_pbm, "got $img (" . length($body) . ")");
2728 # If we are using the webcollage-helper, then we do not need to convert this
2729 # image to a PPM. But, if we're using a filter command, we still must, since
2730 # that's what the filters expect (webcollage-helper can read PPMs, so that's
2733 if (defined ($webcollage_helper) &&
2734 !defined ($filter_cmd)) {
2736 ($iw, $ih) = image_size ($body);
2738 LOG (($verbose_pbm || $verbose_load),
2739 "not a GIF, JPG, or PNG" .
2740 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
2741 ? " (looks like HTML)" : "") .
2743 $suppress_audit = 1;
2749 open (OUT, ">$image_tmp1") || error ("writing $image_tmp1: $!");
2750 print OUT $body || error ("writing $image_tmp1: $!");
2751 close OUT || error ("writing $image_tmp1: $!");
2754 ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
2757 LOG ($verbose_pbm, "unable to make PBM from $img");
2762 record_success ($load_method, $img, $base);
2765 my $ow = $iw; # used only for error messages
2768 # don't just tack this onto the front of the pipeline -- we want it to
2769 # be able to change the size of the input image.
2772 LOG ($verbose_pbm, "running $filter_cmd");
2774 my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
2776 LOG(($verbose_pbm || $verbose_load), "failed command: \"$filter_cmd\"");
2777 LOG(($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2780 rename ($image_tmp2, $image_tmp1);
2782 # re-get the width/height in case the filter resized it.
2784 open(IMG, "<$image_tmp1") || return 0;
2787 ($iw, $ih) = m/^(\d+) (\d+)$/;
2789 return 0 unless ($iw && $ih);
2792 my $target_w = $img_width; # max rectangle into which the image must fit
2793 my $target_h = $img_height;
2799 # Usually scale the image to fit on the screen -- but sometimes scale it
2800 # to fit on half or a quarter of the screen. (We do this by reducing the
2801 # size of the target rectangle.) Note that the image is not merely scaled
2802 # to fit; we instead cut the image in half repeatedly until it fits in the
2803 # target rectangle -- that gives a wider distribution of sizes.
2805 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; } # reduce target rect
2806 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; }
2808 if ($iw > $target_w || $ih > $target_h) {
2809 while ($iw > $target_w ||
2815 if ($iw <= 10 || $ih <= 10) {
2816 LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus.");
2820 LOG ($verbose_pbm, "scaling to ${iw}x$ih ($scale)");
2822 $cmd .= " | pnmscale -xsize $iw -ysize $ih";
2826 my $src = $image_tmp1;
2828 my $crop_x = 0; # the sub-rectangle of the image
2829 my $crop_y = 0; # that we will actually paste.
2833 # The chance that we will randomly crop out a section of an image starts
2834 # out fairly low, but goes up for images that are very large, or images
2835 # that have ratios that make them look like banners (we try to avoid
2836 # banner images entirely, but they slip through when the IMG tags didn't
2837 # have WIDTH and HEIGHT specified.)
2839 my $crop_chance = 0.2;
2840 if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
2841 $crop_chance += 0.2;
2843 if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
2844 $crop_chance += 0.2;
2846 if ($min_ratio && ($iw * $min_ratio) > $ih) {
2847 $crop_chance += 0.7;
2850 if ($crop_chance > 0.1) {
2851 LOG ($verbose_pbm, "crop chance: $crop_chance");
2854 if (rand() < $crop_chance) {
2859 if ($crop_w > $min_width) {
2860 # if it's a banner, select the width linearly.
2861 # otherwise, select a bell.
2862 my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
2865 $crop_w = $min_width + int ($r * ($crop_w - $min_width));
2866 $crop_x = int (rand() * ($ow - $crop_w));
2868 if ($crop_h > $min_height) {
2869 # height always selects as a bell.
2870 $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
2871 $crop_y = int (rand() * ($oh - $crop_h));
2874 if ($crop_x != 0 || $crop_y != 0 ||
2875 $crop_w != $iw || $crop_h != $ih) {
2877 "randomly cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2881 # Where the image should logically land -- this might be negative.
2883 my $x = int((rand() * ($img_width + $crop_w/2)) - $crop_w*3/4);
2884 my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
2886 # if we have chosen to paste the image outside of the rectangle of the
2887 # screen, then we need to crop it.
2891 $x + $crop_w > $img_width ||
2892 $y + $crop_h > $img_height) {
2895 "cropping for effective paste of ${crop_w}x$crop_h \@ $x,$y");
2897 if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
2898 if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
2900 if ($x + $crop_w >= $img_width) { $crop_w = $img_width - $x - 1; }
2901 if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
2904 # If any cropping needs to happen, add pnmcut.
2906 if ($crop_x != 0 || $crop_y != 0 ||
2907 $crop_w != $iw || $crop_h != $ih) {
2910 $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
2911 LOG ($verbose_pbm, "cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2914 LOG ($verbose_pbm, "pasting ${iw}x$ih \@ $x,$y in $image_ppm");
2916 $cmd .= " | pnmpaste - $x $y $image_ppm";
2918 $cmd =~ s@^ *\| *@@;
2920 if (defined ($webcollage_helper)) {
2921 $cmd = "$webcollage_helper $image_tmp1 $image_ppm " .
2922 "$scale $opacity " .
2923 "$crop_x $crop_y $x $y " .
2928 # use a PPM pipeline
2930 $_ .= " < $image_tmp1 > $image_tmp2";
2934 $_ = "($_) 2>&1 | sed s'/^/" . blurb() . "/'";
2936 $_ .= " 2> /dev/null";
2939 my $rc = nontrapping_system ($_);
2941 if (defined ($webcollage_helper) && -z $image_ppm) {
2942 LOG (1, "failed command: \"$cmd\"");
2943 print STDERR "\naudit log:\n\n\n";
2944 print STDERR ("#" x 78) . "\n";
2945 print STDERR blurb() . "$image_ppm has zero size\n";
2947 print STDERR "\n\n";
2952 LOG (($verbose_pbm || $verbose_load), "failed command: \"$cmd\"");
2953 LOG (($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2957 if (!defined ($webcollage_helper)) {
2958 rename ($image_tmp2, $image_ppm) || return;
2961 my $target = "$image_ppm";
2963 # don't just tack this onto the end of the pipeline -- we don't want it
2964 # to end up in $image_ppm, because we don't want the results to be
2967 if ($post_filter_cmd) {
2971 $target = $image_tmp1;
2972 if (!defined ($webcollage_helper)) {
2973 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2975 # Blah, my scripts need the JPEG data, but some other folks need
2976 # the PPM data -- what to do? Ignore the problem, that's what!
2977 # $cmd = "djpeg < $image_ppm | ($post_filter_cmd) > $target";
2978 $cmd = "($post_filter_cmd) < $image_ppm > $target";
2981 $rc = nontrapping_system ($cmd);
2983 LOG ($verbose_pbm, "filter failed: \"$post_filter_cmd\"\n");
2988 if (!$no_output_p) {
2989 my $tsize = (stat($target))[7];
2991 $cmd = "$ppm_to_root_window_cmd $target";
2993 # xv seems to hate being killed. it tends to forget to clean
2994 # up after itself, and leaves windows around and colors allocated.
2995 # I had this same problem with vidwhacker, and I'm not entirely
2996 # sure what I did to fix it. But, let's try this: launch xv
2997 # in the background, so that killing this process doesn't kill it.
2998 # it will die of its own accord soon enough. So this means we
2999 # start pumping bits to the root window in parallel with starting
3000 # the next network retrieval, which is probably a better thing
3005 $rc = nontrapping_system ($cmd);
3008 LOG (($verbose_pbm || $verbose_load), "display failed: \"$cmd\"");
3013 LOG ($verbose_pbm, "$target size is $tsize");
3017 $source .= "-" . stats_of($source);
3018 print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n"
3019 if ($verbose_imgmap);
3020 if ($imagemap_base) {
3021 update_imagemap ($base, $x, $y, $iw, $ih,
3022 $image_ppm, $img_width, $img_height);
3031 sub update_imagemap {
3032 my ($url, $x, $y, $w, $h, $image_ppm, $image_width, $image_height) = @_;
3034 $current_state = "imagemap";
3036 my $max_areas = 200;
3038 $url = html_quote ($url);
3041 my $area = "<AREA SHAPE=RECT COORDS=\"$x,$y,$x2,$y2\" HREF=\"$url\">";
3042 unshift @imagemap_areas, $area; # put one on the front
3043 if ($#imagemap_areas >= $max_areas) {
3044 pop @imagemap_areas; # take one off the back.
3047 LOG ($verbose_pbm, "area: $x,$y,$x2,$y2 (${w}x$h)");
3049 my $map_name = $imagemap_base;
3050 $map_name =~ s@^.*/@@;
3051 $map_name = 'collage' if ($map_name eq '');
3053 my $imagemap_html = $imagemap_base . ".html";
3054 my $imagemap_jpg = $imagemap_base . ".jpg";
3056 if (!defined ($imagemap_html_tmp)) {
3057 $imagemap_html_tmp = $imagemap_html . sprintf (".%08x", rand(0xffffffff));
3058 $imagemap_jpg_tmp = $imagemap_jpg . sprintf (".%08x", rand(0xffffffff));
3061 # Read the imagemap html file (if any) to get a template.
3063 my $template_html = '';
3066 if (open (IN, "<$imagemap_html")) {
3067 while (<IN>) { $template_html .= $_; }
3069 LOG ($verbose_pbm, "read template $imagemap_html");
3072 if ($template_html =~ m/^\s*$/s) {
3073 $template_html = ("<MAP NAME=\"$map_name\"></MAP>\n" .
3074 "<IMG SRC=\"$imagemap_base.jpg\"" .
3075 " USEMAP=\"$map_name\">\n");
3076 LOG ($verbose_pbm, "created dummy template");
3080 # Write the jpg to a tmp file
3084 if (defined ($webcollage_helper)) {
3085 $cmd = "cp -p $image_ppm $imagemap_jpg_tmp";
3087 $cmd = "cjpeg < $image_ppm > $imagemap_jpg_tmp";
3089 my $rc = nontrapping_system ($cmd);
3091 error ("imagemap jpeg failed: \"$cmd\"\n");
3095 # Write the html to a tmp file
3098 my $body = $template_html;
3099 my $areas = join ("\n\t", @imagemap_areas);
3100 my $map = ("<MAP NAME=\"$map_name\">\n\t$areas\n</MAP>");
3101 my $img = ("<IMG SRC=\"$imagemap_base.jpg\" " .
3103 "WIDTH=$image_width HEIGHT=$image_height " .
3104 "USEMAP=\"#$map_name\">");
3105 $body =~ s@(<MAP\s+NAME=\"[^\"]*\"\s*>).*?(</MAP>)@$map@is;
3106 $body =~ s@<IMG\b[^<>]*\bUSEMAP\b[^<>]*>@$img@is;
3108 # if there are magic webcollage spans in the html, update those too.
3111 my @st = stat ($imagemap_jpg_tmp);
3112 my $date = strftime("%d-%b-%Y %l:%M:%S %p %Z", localtime($st[9]));
3113 my $size = int(($st[7] / 1024) + 0.5) . "K";
3114 $body =~ s@(<SPAN\s+CLASS=\"webcollage_date\">).*?(</SPAN>)@$1$date$2@si;
3115 $body =~ s@(<SPAN\s+CLASS=\"webcollage_size\">).*?(</SPAN>)@$1$size$2@si;
3119 open (OUT, ">$imagemap_html_tmp") || error ("$imagemap_html_tmp: $!");
3120 print OUT $body || error ("$imagemap_html_tmp: $!");
3121 close OUT || error ("$imagemap_html_tmp: $!");
3122 LOG ($verbose_pbm, "wrote $imagemap_html_tmp");
3125 # Rename the two tmp files to the real files
3127 rename ($imagemap_html_tmp, $imagemap_html) ||
3128 error "renaming $imagemap_html_tmp to $imagemap_html";
3129 LOG ($verbose_pbm, "wrote $imagemap_html");
3130 rename ($imagemap_jpg_tmp, $imagemap_jpg) ||
3131 error "renaming $imagemap_jpg_tmp to $imagemap_jpg";
3132 LOG ($verbose_pbm, "wrote $imagemap_jpg");
3138 $SIG{HUP} = \&signal_cleanup;
3139 $SIG{INT} = \&signal_cleanup;
3140 $SIG{QUIT} = \&signal_cleanup;
3141 $SIG{ABRT} = \&signal_cleanup;
3142 $SIG{KILL} = \&signal_cleanup;
3143 $SIG{TERM} = \&signal_cleanup;
3145 # Need this so that if giftopnm dies, we don't die.
3146 $SIG{PIPE} = 'IGNORE';
3149 END { exit_cleanup(); }
3158 my $driftnet_cmd = 0;
3160 $current_state = "init";
3161 $load_method = "none";
3164 my $window_id = undef;
3166 # historical suckage: the environment variable name is lower case.
3167 $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
3169 while ($_ = $ARGV[0]) {
3171 if ($_ eq "-display" ||
3177 $ENV{DISPLAY} = shift @ARGV;
3178 } elsif ($_ eq "-root") {
3180 } elsif ($_ eq "-window-id" || $_ eq "--window-id") {
3181 $window_id = shift @ARGV;
3183 } elsif ($_ eq "-no-output") {
3185 } elsif ($_ eq "-urls-only") {
3188 } elsif ($_ eq "-imagemap") {
3189 $imagemap_base = shift @ARGV;
3191 } elsif ($_ eq "-verbose") {
3193 } elsif (m/^-v+$/) {
3194 $verbose += length($_)-1;
3195 } elsif ($_ eq "-delay") {
3196 $delay = shift @ARGV;
3197 } elsif ($_ eq "-timeout") {
3198 $http_timeout = shift @ARGV;
3199 } elsif ($_ eq "-filter") {
3200 $filter_cmd = shift @ARGV;
3201 } elsif ($_ eq "-filter2") {
3202 $post_filter_cmd = shift @ARGV;
3203 } elsif ($_ eq "-background" || $_ eq "-bg") {
3204 $background = shift @ARGV;
3205 } elsif ($_ eq "-size") {
3207 if (m@^(\d+)x(\d+)$@) {
3211 error "argument to \"-size\" must be of the form \"640x400\"";
3213 } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
3214 $http_proxy = shift @ARGV;
3215 } elsif ($_ eq "-dictionary" || $_ eq "-dict") {
3216 $dict = shift @ARGV;
3217 } elsif ($_ eq "-opacity") {
3218 $opacity = shift @ARGV;
3219 error ("opacity must be between 0.0 and 1.0")
3220 if ($opacity <= 0 || $opacity > 1);
3221 } elsif ($_ eq "-driftnet" || $_ eq "--driftnet") {
3222 @search_methods = ( 100, "driftnet", \&pick_from_driftnet );
3223 if (! ($ARGV[0] =~ m/^-/)) {
3224 $driftnet_cmd = shift @ARGV;
3226 $driftnet_cmd = $default_driftnet_cmd;
3228 } elsif ($_ eq "-debug" || $_ eq "--debug") {
3229 my $which = shift @ARGV;
3230 my @rest = @search_methods;
3233 my $pct = shift @rest;
3234 my $name = shift @rest;
3235 my $tfn = shift @rest;
3237 if ($name eq $which) {
3238 @search_methods = (100, $name, $tfn);
3243 error "no such search method as \"$which\"" unless ($ok);
3244 LOG (1, "DEBUG: using only \"$which\"");
3247 print STDERR "$copyright\nusage: $progname " .
3248 "[-root] [-display dpy] [-verbose] [-debug which]\n" .
3249 "\t\t [-timeout secs] [-delay secs] [-size WxH]\n" .
3250 "\t\t [-no-output] [-urls-only] [-imagemap filename]\n" .
3251 "\t\t [-filter cmd] [-filter2 cmd] [-background color]\n" .
3252 "\t\t [-dictionary dictionary-file] [-http-proxy host[:port]]\n" .
3253 "\t\t [-driftnet [driftnet-program-and-args]]\n" .
3259 if ($http_proxy && $http_proxy eq "") {
3260 $http_proxy = undef;
3262 if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
3263 # historical suckage: allow "http://host:port" as well as "host:port".
3267 if (!$root_p && !$no_output_p) {
3268 print STDERR $copyright;
3269 error "the -root argument is mandatory (for now.)";
3272 if (!$no_output_p && !$ENV{DISPLAY}) {
3273 error "\$DISPLAY is not set.";
3277 if ($verbose == 1) {
3278 $verbose_imgmap = 1;
3279 $verbose_warnings = 1;
3281 } elsif ($verbose == 2) {
3282 $verbose_imgmap = 1;
3283 $verbose_warnings = 1;
3286 } elsif ($verbose == 3) {
3287 $verbose_imgmap = 1;
3288 $verbose_warnings = 1;
3290 $verbose_filter = 1;
3292 } elsif ($verbose == 4) {
3293 $verbose_imgmap = 1;
3294 $verbose_warnings = 1;
3296 $verbose_filter = 1;
3299 } elsif ($verbose == 5) {
3300 $verbose_imgmap = 1;
3301 $verbose_warnings = 1;
3303 $verbose_filter = 1;
3307 } elsif ($verbose == 6) {
3308 $verbose_imgmap = 1;
3309 $verbose_warnings = 1;
3311 $verbose_filter = 1;
3316 } elsif ($verbose >= 7) {
3317 $verbose_imgmap = 1;
3318 $verbose_warnings = 1;
3320 $verbose_filter = 1;
3328 error ("$dict does not exist") unless (-f $dict);
3336 spawn_driftnet ($driftnet_cmd) if ($driftnet_cmd);
3341 x_or_pbm_output ($window_id);