3 # webcollage, Copyright (c) 1999-2012 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.158 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
64 my $copyright = "WebCollage $version, Copyright (c) 1999-2011" .
65 " Jamie Zawinski <jwz\@jwz.org>\n" .
66 " http://www.jwz.org/webcollage/\n";
70 my @search_methods = ( 24, "googlephotos", \&pick_from_google_image_photos,
71 13, "googleimgs", \&pick_from_google_images,
72 13, "googlenums", \&pick_from_google_image_numbers,
73 16, "flickr_recent", \&pick_from_flickr_recent,
74 13, "flickr_random", \&pick_from_flickr_random,
75 10, "twitpic", \&pick_from_twitpic_images,
76 8, "livejournal", \&pick_from_livejournal_images,
77 3, "yahoorand", \&pick_from_yahoo_random_link,
79 # This one doesn't work very well: too many non-img links.
80 0, "twitter", \&pick_from_twitter_images,
82 # This is a cute way to search for a certain webcams.
83 # Not included in default methods, since these images
84 # aren't terribly interesting by themselves.
85 # See also "SurveillanceSaver".
87 0, "securitycam", \&pick_from_security_camera,
89 # Nonfunctional as of June 2011.
90 # 0, "altavista", \&pick_from_alta_vista_random_link,
92 # In Apr 2002, Google asked me to stop searching them.
93 # I asked them to add a "random link" url. They said
94 # "that would be easy, we'll think about it" and then
95 # never wrote back. Booo Google! Booooo! So, screw
96 # those turkeys, I've turned Google searching back on.
97 # I'm sure they can take it. (Jan 2005.)
99 # Jan 2005: Yahoo fucked up their search form so that
100 # it's no longer possible to do "or" searches on news
101 # images, so we rarely get any hits there any more.
103 # 0, "yahoonews", \&pick_from_yahoo_news_text,
105 # Dec 2004: the ircimages guy's server can't take the
106 # heat, so he started banning the webcollage user agent.
107 # I tried to convince him to add a lighter-weight page to
108 # support webcollage better, but he doesn't care.
110 # 0, "ircimages", \&pick_from_ircimages,
112 # Dec 2002: Alta Vista has a new "random link" URL now.
113 # They added it specifically to better support webcollage!
114 # That was super cool of them. This is how we used to do
117 # 0, "avimages", \&pick_from_alta_vista_images,
118 # 0, "avtext", \&pick_from_alta_vista_text,
120 # This broke in 2004. Eh, Lycos sucks anyway.
122 # 0, "lycos", \&pick_from_lycos_text,
124 # This broke in 2003, I think. I suspect Hotbot is
125 # actually the same search engine data as Lycos.
127 # 0, "hotbot", \&pick_from_hotbot_text,
130 # programs we can use to write to the root window (tried in ascending order.)
132 my @root_displayers = (
133 "xscreensaver-getimage -root -file",
134 "chbg -once -xscreensaver -max_size 100",
135 "xv -root -quit -viewonly +noresetroot -quick24 -rmode 5" .
136 " -rfg black -rbg black",
137 "xli -quiet -onroot -center -border black",
138 "xloadimage -quiet -onroot -center -border black",
140 # this lame program wasn't built with vroot.h:
141 # "xsri -scale -keep-aspect -center-horizontal -center-vertical",
145 # Some sites need cookies to work properly. These are they.
148 "www.altavista.com" => "AV_ALL=1", # request uncensored searches
149 "web.altavista.com" => "AV_ALL=1",
151 # log in as "cipherpunk"
152 "www.nytimes.com" => 'NYT-S=18cHMIlJOn2Y1bu5xvEG3Ufuk6E1oJ.' .
153 'FMxWaQV0igaB5Yi/Q/guDnLeoL.pe7i1oakSb' .
154 '/VqfdUdb2Uo27Vzt1jmPn3cpYRlTw9',
156 "ircimages.com" => 'disclaimer=1',
160 # If this is set, it's a helper program to use for pasting images together:
161 # this is a lot faster and more efficient than using PPM pipelines, which is
162 # what we do if this program doesn't exist. (We check for "webcollage-helper"
163 # on $PATH at startup, and set this variable appropriately.)
165 my $webcollage_helper = undef;
168 # If we have the webcollage-helper program, then it will paste the images
169 # together with transparency! 0.0 is invisible, 1.0 is totally opaque.
174 # Some sites have managed to poison the search engines. These are they.
175 # (We auto-detect sites that have poisoned the search engines via excessive
176 # keywords or dictionary words, but these are ones that slip through
179 # This can contain full host names, or 2 or 3 component domains.
182 "die.net" => 1, # 'l33t h4ck3r d00dz.
183 "genforum.genealogy.com" => 1, # Cluttering avtext with human names.
184 "rootsweb.com" => 1, # Cluttering avtext with human names.
185 "akamai.net" => 1, # Lots of sites have their images on Akamai.
186 "akamaitech.net" => 1, # But those are pretty much all banners.
187 # Since Akamai is super-expensive, let's
188 # go out on a limb and assume that all of
189 # their customers are rich-and-boring.
190 "bartleby.com" => 1, # Dictionary, cluttering avtext.
191 "encyclopedia.com" => 1, # Dictionary, cluttering avtext.
192 "onlinedictionary.datasegment.com" => 1, # Dictionary, cluttering avtext.
193 "hotlinkpics.com" => 1, # Porn site that has poisoned avimages
194 # (I don't see how they did it, though!)
195 "alwayshotels.com" => 1, # Poisoned Lycos pretty heavily.
196 "nextag.com" => 1, # Poisoned Alta Vista real good.
197 "ghettodriveby.com" => 1, # Poisoned Google Images.
198 "crosswordsolver.org" => 1, # Poisoned Google Images.
199 "xona.com" => 1, # Poisoned Google Images.
200 "freepatentsonline.com" => 1, # Poisoned Google Images.
201 "herbdatanz.com" => 1, # Poisoned Google Images.
205 # When verbosity is turned on, we warn about sites that we seem to be hitting
206 # a lot: usually this means some new poisoner has made it into the search
207 # engines. But sometimes, the warning is just because that site has a lot
208 # of stuff on it. So these are the sites that are immune to the "frequent
209 # site" diagnostic message.
211 my %warningless_sites = (
212 "home.earthlink.net" => 1,
213 "www.angelfire.com" => 1,
214 "members.aol.com" => 1,
215 "img.photobucket.com" => 1,
216 "pics.livejournal.com" => 1,
222 "wikimedia.org" => 1,
224 "amazonaws.com" => 1,
226 "photoshelter.com" => 1,
227 "myspacecdn.com" => 1,
228 "feedburner.com" => 1,
235 "yimg.com" => 1, # This is where dailynews.yahoo.com stores
236 "eimg.com" => 1, # its images, so pick_from_yahoo_news_text()
237 # hits this every time.
239 "images.quizfarm.com" => 1, # damn those LJ quizzes...
240 "images.quizilla.com" => 1,
241 "images.quizdiva.net" => 1,
243 "driftnet" => 1, # builtin...
244 "local-directory" => 1, # builtin...
248 # For decoding HTML-encoded character entities to URLs.
252 "quot" => '"', "amp" => '&', "lt" => '<', "gt" => '>',
253 "nbsp" => ' ', "iexcl" => '¡', "cent" => '¢', "pound" => '£',
254 "curren" => '¤', "yen" => '¥', "brvbar" => '¦', "sect" => '§',
255 "uml" => '¨', "copy" => '©', "ordf" => 'ª', "laquo" => '«',
256 "not" => '¬', "shy" => '', "reg" => '®', "macr" => '¯',
257 "deg" => '°', "plusmn" => '±', "sup2" => '²', "sup3" => '³',
258 "acute" => '´', "micro" => 'µ', "para" => '¶', "middot" => '·',
259 "cedil" => '¸', "sup1" => '¹', "ordm" => 'º', "raquo" => '»',
260 "frac14" => '¼', "frac12" => '½', "frac34" => '¾', "iquest" => '¿',
261 "Agrave" => 'À', "Aacute" => 'Á', "Acirc" => 'Â', "Atilde" => 'Ã',
262 "Auml" => 'Ä', "Aring" => 'Å', "AElig" => 'Æ', "Ccedil" => 'Ç',
263 "Egrave" => 'È', "Eacute" => 'É', "Ecirc" => 'Ê', "Euml" => 'Ë',
264 "Igrave" => 'Ì', "Iacute" => 'Í', "Icirc" => 'Î', "Iuml" => 'Ï',
265 "ETH" => 'Ð', "Ntilde" => 'Ñ', "Ograve" => 'Ò', "Oacute" => 'Ó',
266 "Ocirc" => 'Ô', "Otilde" => 'Õ', "Ouml" => 'Ö', "times" => '×',
267 "Oslash" => 'Ø', "Ugrave" => 'Ù', "Uacute" => 'Ú', "Ucirc" => 'Û',
268 "Uuml" => 'Ü', "Yacute" => 'Ý', "THORN" => 'Þ', "szlig" => 'ß',
269 "agrave" => 'à', "aacute" => 'á', "acirc" => 'â', "atilde" => 'ã',
270 "auml" => 'ä', "aring" => 'å', "aelig" => 'æ', "ccedil" => 'ç',
271 "egrave" => 'è', "eacute" => 'é', "ecirc" => 'ê', "euml" => 'ë',
272 "igrave" => 'ì', "iacute" => 'í', "icirc" => 'î', "iuml" => 'ï',
273 "eth" => 'ð', "ntilde" => 'ñ', "ograve" => 'ò', "oacute" => 'ó',
274 "ocirc" => 'ô', "otilde" => 'õ', "ouml" => 'ö', "divide" => '÷',
275 "oslash" => 'ø', "ugrave" => 'ù', "uacute" => 'ú', "ucirc" => 'û',
276 "uuml" => 'ü', "yacute" => 'ý', "thorn" => 'þ', "yuml" => 'ÿ',
278 # HTML 4 entities that do not have 1:1 Latin1 mappings.
279 "bull" => "*", "hellip"=> "...", "prime" => "'", "Prime" => "\"",
280 "frasl" => "/", "trade" => "[tm]", "larr" => "<-", "rarr" => "->",
281 "harr" => "<->", "lArr" => "<=", "rArr" => "=>", "hArr" => "<=>",
282 "empty" => "Ø", "minus" => "-", "lowast"=> "*", "sim" => "~",
283 "cong" => "=~", "asymp" => "~", "ne" => "!=", "equiv" => "==",
284 "le" => "<=", "ge" => ">=", "lang" => "<", "rang" => ">",
285 "loz" => "<>", "OElig" => "OE", "oelig" => "oe", "Yuml" => "Y",
286 "circ" => "^", "tilde" => "~", "ensp" => " ", "emsp" => " ",
287 "thinsp"=> " ", "ndash" => "-", "mdash" => "--", "lsquo" => "`",
288 "rsquo" => "'", "sbquo" => "'", "ldquo" => "\"", "rdquo" => "\"",
289 "bdquo" => "\"", "lsaquo"=> "<", "rsaquo"=> ">",
293 ##############################################################################
295 # Various global flags set by command line parameters, or computed
297 ##############################################################################
300 my $current_state = "???"; # for diagnostics
303 my $image_succeeded = -1;
304 my $suppress_audit = 0;
306 my $verbose_imgmap = 0; # print out rectangles and URLs only (stdout)
307 my $verbose_warnings = 0; # print out warnings when things go wrong
308 my $verbose_load = 0; # diagnostics about loading of URLs
309 my $verbose_filter = 0; # diagnostics about page selection/rejection
310 my $verbose_net = 0; # diagnostics about network I/O
311 my $verbose_pbm = 0; # diagnostics about PBM pipelines
312 my $verbose_http = 0; # diagnostics about all HTTP activity
313 my $verbose_exec = 0; # diagnostics about executing programs
315 my $report_performance_interval = 60 * 15; # print some stats every 15 minutes
317 my $http_proxy = undef;
318 my $http_timeout = 20;
319 my $cvt_timeout = 10;
325 my $min_gif_area = (120 * 120);
331 my $imagemap_base = undef;
333 my @pids_to_kill = (); # forked pids we should kill when we exit, if any.
335 my $driftnet_magic = 'driftnet';
336 my $driftnet_dir = undef;
337 my $default_driftnet_cmd = "driftnet -a -m 100";
339 my $local_magic = 'local-directory';
340 my $local_dir = undef;
345 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
346 "arrhenius", "arteriole", "blanket", "brainchild",
347 "burdensome", "carnival", "cherub", "chord", "clever",
348 "dedicate", "dilogarithm", "dolan", "dryden",
352 ##############################################################################
356 ##############################################################################
358 # returns three values: the HTTP response line; the document headers;
359 # and the document body.
361 sub get_document_1($$$) {
362 my ($url, $referer, $timeout) = @_;
364 if (!defined($timeout)) { $timeout = $http_timeout; }
365 if ($timeout > $http_timeout) { $timeout = $http_timeout; }
368 LOG (($verbose_net || $verbose_load), "timed out for $url");
372 LOG ($verbose_net, "get_document_1 $url " . ($referer ? $referer : ""));
374 if (! ($url =~ m@^http://@i)) {
375 LOG ($verbose_net, "not an HTTP URL: $url");
379 my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
380 $path = "" unless $path;
382 if (!$url_proto || !$serverstring) {
383 LOG (($verbose_net || $verbose_load), "unparsable URL: $url");
387 my ($them,$port) = split(/:/, $serverstring);
388 $port = 80 unless $port;
393 $serverstring = $http_proxy if $http_proxy;
394 $serverstring =~ s@^[a-z]+://@@;
395 ($them2,$port2) = split(/:/, $serverstring);
396 $port2 = 80 unless $port2;
399 my ($remote, $iaddr, $paddr, $proto, $line);
401 if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
403 LOG (($verbose_net || $verbose_load), "unrecognised port in $url");
406 $iaddr = inet_aton($remote);
408 LOG (($verbose_net || $verbose_load), "host not found: $remote");
411 $paddr = sockaddr_in($port2, $iaddr);
419 local $SIG{ALRM} = sub {
420 LOG (($verbose_net || $verbose_load), "timed out ($timeout) for $url");
425 $proto = getprotobyname('tcp');
426 if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
427 LOG (($verbose_net || $verbose_load), "socket: $!");
430 if (!connect(S, $paddr)) {
431 LOG (($verbose_net || $verbose_load), "connect($serverstring): $!");
435 select(S); $| = 1; select(STDOUT);
437 my $cookie = $cookies{$them};
439 my $user_agent = "$progname/$version";
441 if ($url =~ m@^http://www\.altavista\.com/@ ||
442 $url =~ m@^http://random\.yahoo\.com/@ ||
443 $url =~ m@^http://images\.google\.com/@ ||
444 $url =~ m@^http://www\.google\.com/@) {
445 # block this, you turkeys.
446 $user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.7)" .
447 " Gecko/20070914 Firefox/2.0.0.7";
449 # 28-Jun-2007: Google Images now emits the entire page in JS if
450 # you claim to be Gecko. They also still block "webcollage".
451 # They serve non-JS for unrecognised agents, so let's try this...
452 $user_agent = "NoJavascriptPlease/1.0"
453 if ($url =~ m@^http://[a-z]+\.google\.com/@);
456 my $hdrs = "GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
458 "User-Agent: $user_agent\r\n";
460 $hdrs .= "Referer: $referer\r\n";
463 my @cc = split(/\r?\n/, $cookie);
464 $hdrs .= "Cookie: " . join('; ', @cc) . "\r\n";
468 foreach (split('\r?\n', $hdrs)) {
469 LOG ($verbose_http, " ==> $_");
472 my $http = <S> || "";
474 # Kludge: the Yahoo Random Link is now returning as its first
475 # line "Status: 301" instead of "HTTP/1.0 301 Found". Fix it...
477 $http =~ s@^Status:\s+(\d+)\b@HTTP/1.0 $1@i;
481 LOG ($verbose_http, " <== $_");
487 LOG ($verbose_http, " <== $_");
489 if (m@^Set-cookie:\s*([^;\r\n]+)@i) {
490 set_cookie($them, $1)
501 " <== [ body ]: $lines lines, " . length($body) . " bytes");
506 LOG (($verbose_net || $verbose_load), "null response: $url");
510 $SIG{ALRM} = 'DEFAULT'; # seem to be suffering a race?
511 return ( $http, $head, $body );
513 die if ($@ && $@ ne "alarm\n"); # propagate errors
515 if ($@ && $@ ne "alarm\n") {
516 print STDERR blurb() . "DIE " . join(" ", $@) . "\n";
534 # returns two values: the document headers; and the document body.
535 # if the given URL did a redirect, returns the redirected-to document.
537 sub get_document($$;$) {
538 my ($url, $referer, $timeout) = @_;
541 if (defined($referer) && $referer eq $driftnet_magic) {
542 return get_driftnet_file ($url);
545 if (defined($referer) && $referer eq $local_magic) {
546 return get_local_file ($url);
551 my $max_loop_count = 4;
554 if (defined($timeout) && $timeout <= 0) {
555 LOG (($verbose_net || $verbose_load), "timed out for $url");
560 my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
562 if (defined ($timeout)) {
564 my $elapsed = $now - $start;
565 $timeout -= $elapsed;
569 return () unless $http; # error message already printed
571 $http =~ s/[\r\n]+$//s;
573 if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
576 my ( $location ) = m@^location:[ \t]*(.*)$@im;
578 $location =~ s/[\r\n]$//;
580 LOG ($verbose_net, "redirect from $url to $location");
585 $referer =~ m@^(http://[^/]+)@i;
587 } elsif (! ($url =~ m@^[a-z]+:@i)) {
589 s@[^/]+$@@g if m@^http://[^/]+/@i;
590 $_ .= "/" if m@^http://[^/]+$@i;
595 LOG ($verbose_net, "no Location with \"$http\"");
596 return ( $url, $body );
599 if ($loop_count++ > $max_loop_count) {
601 "too many redirects ($max_loop_count) from $orig_url");
606 } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
608 LOG (($verbose_net || $verbose_load), "failed: $1 ($url)");
610 # http errors -- return nothing.
616 LOG (($verbose_net || $verbose_load), "document contains no data: $url");
622 return ( $url, $body );
628 # If we already have a cookie defined for this site, and the site is trying
629 # to overwrite that very same cookie, let it do so. This is because nytimes
630 # expires its cookies - it lets you upgrade to a new cookie without logging
631 # in again, but you have to present the old cookie to get the new cookie.
632 # So, by doing this, the built-in cypherpunks cookie will never go "stale".
635 my ($host, $cookie) = @_;
636 my $oc = $cookies{$host};
639 my ($oc_name, $oc_value) = m@^([^= \t\r\n]+)=(.*)$@;
641 my ($nc_name, $nc_value) = m@^([^= \t\r\n]+)=(.*)$@;
643 if ($oc_name eq $nc_name &&
644 $oc_value ne $nc_value) {
645 $cookies{$host} = $cookie;
646 LOG ($verbose_net, "overwrote ${host}'s $oc_name cookie");
651 ############################################################################
653 # Extracting image URLs from HTML
655 ############################################################################
657 # given a URL and the body text at that URL, selects and returns a random
658 # image from it. returns () if no suitable images found.
660 sub pick_image_from_body($$) {
661 my ($url, $body) = @_;
666 # if there's at least one slash after the host, take off the last
668 if ( m@^http://[^/]+/@io ) {
669 $base =~ s@[^/]+$@@go;
672 # if there are no slashes after the host at all, put one on the end.
673 if ( m@^http://[^/]+$@io ) {
679 # strip out newlines, compress whitespace
686 # There are certain web sites that list huge numbers of dictionary
687 # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
688 # Porn sites tend not to be reputable!)
690 # I do not want webcollage to filter on content: I want it to select
691 # randomly from the set of images on the web. All the logic here for
692 # rejecting some images is really a set of heuristics for rejecting
693 # images that are not really images: for rejecting *text* that is in
694 # GIF/JPEG/PNG form. I don't want text, I want pictures, and I want
695 # the content of the pictures to be randomly selected from among all
696 # the available content.
698 # So, filtering out "dirty" pictures by looking for "dirty" keywords
699 # would be wrong: dirty pictures exist, like it or not, so webcollage
700 # should be able to select them.
702 # However, picking a random URL is a hard thing to do. The mechanism I'm
703 # using is to search for a selection of random words. This is not
704 # perfect, but works ok most of the time. The way it breaks down is when
705 # some URLs get precedence because their pages list *every word* as
706 # related -- those URLs come up more often than others.
708 # So, after we've retrieved a URL, if it has too many keywords, reject
709 # it. We reject it not on the basis of what those keywords are, but on
710 # the basis that by having so many, the page has gotten an unfair
711 # advantage against our randomizer.
714 foreach my $trip (@tripwire_words) {
715 $trip_count++ if m/$trip/i;
718 if ($trip_count >= $#tripwire_words - 2) {
719 LOG (($verbose_filter || $verbose_load),
720 "there is probably a dictionary in \"$url\": rejecting.");
721 $rejected_urls{$url} = -1;
731 foreach (split(/ *</)) {
732 if ( m/^meta.*["']keywords["']/i ) {
734 # Likewise, reject any web pages that have a KEYWORDS meta tag
739 LOG (($verbose_filter || $verbose_load),
740 "excessive keywords ($L bytes) in $url: rejecting.");
741 $rejected_urls{$url} = $L;
746 LOG ($verbose_filter, " keywords ($L bytes) in $url (ok)");
749 } elsif (m/^ (IMG|A) \b .* (SRC|HREF) \s* = \s* ["']? (.*?) [ "'<>] /six ||
750 m/^ (LINK|META) \b .* (REL|PROPERTY) \s* = \s*
751 ["']? (image_src|og:image) ["']? /six) {
753 my $was_inline = (lc($1) eq 'img');
754 my $was_meta = (lc($1) eq 'link' || lc($1) eq 'meta');
757 # For <link rel="image_src" href="...">
758 # and <meta property="og:image" content="...">
761 next unless (m/ (HREF|CONTENT) \s* = \s* ["']? (.*?) [ "'<>] /six);
765 my ( $width ) = m/width ?=[ \"]*(\d+)/oi;
766 my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
771 ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
773 } elsif ( ! m@^[^/:?]+:@ ) {
776 1 while (s@/[^/]+/\.\./@/@g);
780 if ( ! m@^http://@io ) {
785 if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg|png)$@io ) {
789 # skip really short or really narrow images
790 if ( $width && $width < $min_width) {
791 if (!$height) { $height = "?"; }
792 LOG ($verbose_filter, " skip narrow image $_ (${width}x$height)");
796 if ( $height && $height < $min_height) {
797 if (!$width) { $width = "?"; }
798 LOG ($verbose_filter, " skip short image $_ (${width}x$height)");
802 # skip images with ratios that make them look like banners.
803 if ($min_ratio && $width && $height &&
804 ($width * $min_ratio ) > $height) {
805 if (!$height) { $height = "?"; }
806 LOG ($verbose_filter, " skip bad ratio $_ (${width}x$height)");
810 # skip GIFs with a small number of pixels -- those usually suck.
811 if ($width && $height &&
813 ($width * $height) < $min_gif_area) {
814 LOG ($verbose_filter, " skip small GIF $_ (${width}x$height)");
818 # skip images with a URL that indicates a Yahoo thumbnail.
819 if (m@\.yimg\.com/.*/t/@) {
820 if (!$width) { $width = "?"; }
821 if (!$height) { $height = "?"; }
822 LOG ($verbose_filter, " skip yahoo thumb $_ (${width}x$height)");
828 if ($unique_urls{$url}) {
829 LOG ($verbose_filter, " skip duplicate image $_");
833 LOG ($verbose_filter,
835 ($width && $height ? " (${width}x${height})" : "") .
836 ($was_meta ? " (meta)" : $was_inline ? " (inline)" : ""));
842 $weight = 20; # meta tag images are far preferable to inline images.
844 if ($url !~ m@[.](gif|png)$@io ) {
845 $weight += 2; # JPEGs are preferable to GIFs and PNGs.
848 $weight += 4; # pointers to images are preferable to inlined images.
852 $unique_urls{$url}++;
853 for (my $i = 0; $i < $weight; $i++) {
854 $urls[++$#urls] = $url;
859 my $fsp = ($body =~ m@<frameset@i);
864 @urls = depoison (@urls);
867 LOG ($verbose_load, "no images on $base" . ($fsp ? " (frameset)" : ""));
871 # pick a random element of the table
872 my $i = int(rand($#urls+1));
875 LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
881 # Given a URL and the RSS feed from that URL, pick a random image from
882 # the feed. This is a lot simpler than extracting images out of a page:
883 # we already know we have reasonable images, so we just pick one.
884 # Returns: the real URL of the page (preferably not the RSS version),
887 sub pick_image_from_rss($$) {
888 my ( $url, $body ) = @_;
889 my @suitable = ($body =~ m/<enclosure url="(.*?)"/g);
891 my ($base) = ($body =~ m@<link>([^<>]+)</link>@i);
892 $base = $url unless $base;
894 # pick a random element of the table
896 my $i = int(rand(scalar @suitable));
897 my $url = $suitable[$i];
898 LOG ($verbose_load, "picked image " .($i+1) . "/" .
899 ($#suitable+1) . ": $url");
900 return ($base, $url);
906 ############################################################################
908 # Subroutines for getting pages and images out of search engines
910 ############################################################################
913 sub pick_dictionary() {
914 my @dicts = ("/usr/dict/words",
915 "/usr/share/dict/words",
916 "/usr/share/lib/dict/words",
917 "/usr/share/dict/cracklib-small",
918 "/usr/share/dict/cracklib-words"
920 foreach my $f (@dicts) {
926 error ("$dicts[0] does not exist") unless defined($wordlist);
929 # returns a random word from the dictionary
933 return undef unless open (my $in, '<', $wordlist);
935 my $size = (stat($in))[7];
940 error ("looping ($count) while reading $wordlist")
943 my $pos = int (rand ($size));
944 if (seek ($in, $pos, 0)) {
945 $word = <$in>; # toss partial line
946 $word = <$in>; # keep next line
950 next if ($word =~ m/^[-\']/);
954 $word =~ s/^[^a-z]+//s;
955 $word =~ s/[^a-z]+$//s;
961 $word =~ s/ally$/al/s;
962 $word =~ s/izes$/ize/s;
963 $word =~ s/esses$/ess/s;
964 $word =~ s/(.{5})ing$/$1/s;
966 next if (length ($word) > 14);
972 if ( $word =~ s/\s/\+/gs ) { # convert intra-word spaces to "+".
973 $word = "\%22$word\%22"; # And put quotes (%22) around it.
980 sub random_words($) {
982 return (random_word() . $sep .
983 random_word() . $sep .
984 random_word() . $sep .
985 random_word() . $sep .
992 $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
999 $s =~ s/%([a-z0-9]{2})/chr(hex($1))/ige;
1008 $s =~ s/\"/"/gi;
1012 sub html_unquote($) {
1014 $s =~ s/(&([a-z]+);)/{ $entity_table{$2} || $1; }/gexi; # e.g., '
1015 $s =~ s/(&\#(\d+);)/{ chr($2) }/gexi; # e.g., '
1020 # Loads the given URL (a search on some search engine) and returns:
1021 # - the total number of hits the search engine claimed it had;
1022 # - a list of URLs from the page that the search engine returned;
1023 # Note that this list contains all kinds of internal search engine
1024 # junk URLs too -- caller must prune them.
1026 sub pick_from_search_engine($$$) {
1027 my ( $timeout, $search_url, $words ) = @_;
1032 print STDERR "\n\n" if ($verbose_load);
1034 LOG ($verbose_load, "words: $_");
1035 LOG ($verbose_load, "URL: $search_url");
1037 $last_search = $search_url; # for warnings
1040 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
1041 if (defined ($timeout)) {
1042 $timeout -= (time - $start);
1043 if ($timeout <= 0) {
1045 LOG (($verbose_net || $verbose_load),
1046 "timed out (late) for $search_url");
1047 $suppress_audit = 1;
1052 return () if (! $body);
1057 my $search_count = "?";
1058 if ($body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
1060 } elsif ($body =~ m@<NOBR>((\d{1,3})(,\d{3})*) @i) {
1062 } elsif ($body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
1064 } elsif ($body =~ m@found about ((\d{1,3})(,\d{3})*|\d+) results@) {
1066 } elsif ($body =~ m@\b\d+ - \d+ of (\d+)\b@i) { # avimages
1068 } elsif ($body =~ m@About ((\d{1,3})(,\d{3})*) images@i) { # avimages
1070 } elsif ($body =~ m@We found ((\d{1,3})(,\d{3})*|\d+) results@i) { # *vista
1072 } elsif ($body =~ m@ of about <B>((\d{1,3})(,\d{3})*)<@i) { # googleimages
1074 } elsif ($body =~ m@<B>((\d{1,3})(,\d{3})*)</B> Web sites were found@i) {
1075 $search_count = $1; # lycos
1076 } elsif ($body =~ m@WEB.*?RESULTS.*?\b((\d{1,3})(,\d{3})*)\b.*?Matches@i) {
1077 $search_count = $1; # hotbot
1078 } elsif ($body =~ m@no photos were found containing@i) { # avimages
1079 $search_count = "0";
1080 } elsif ($body =~ m@found no document matching@i) { # avtext
1081 $search_count = "0";
1083 1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
1085 # if ($search_count eq "?" || $search_count eq "0") {
1086 # my $file = "/tmp/wc.html";
1087 # open (my $out, '>', $file) || error ("writing $file: $!");
1090 # print STDERR blurb() . "###### wrote $file\n";
1094 my $length = length($body);
1103 foreach (split(/\n/)) {
1105 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
1108 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
1109 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
1111 if ( $rejected_urls{$u} ) {
1112 LOG ($verbose_filter, " pre-rejecting candidate: $u");
1116 LOG ($verbose_http, " HREF: $u");
1118 $subpages[++$#subpages] = $u;
1121 if ( $#subpages < 0 ) {
1122 LOG ($verbose_filter,
1123 "found nothing on $base ($length bytes, $href_count links).");
1127 LOG ($verbose_filter, "" . $#subpages+1 . " links on $search_url");
1129 return ($search_count, @subpages);
1137 my ($h) = m@^http://([^/: \t\r\n]+)@i;
1139 next unless defined($h);
1141 if ($poisoners{$h}) {
1142 LOG (($verbose_filter), " rejecting poisoner: $_");
1145 if ($h =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
1147 LOG (($verbose_filter), " rejecting poisoner: $_");
1150 if ($h =~ m@([^.]+\.[^.]+)$@ &&
1152 LOG (($verbose_filter), " rejecting poisoner: $_");
1162 # given a list of URLs, picks one at random; loads it; and returns a
1163 # random image from it.
1164 # returns the url of the page loaded; the url of the image chosen.
1166 sub pick_image_from_pages($$$$@) {
1167 my ($base, $total_hit_count, $unfiltered_link_count, $timeout, @pages) = @_;
1169 $total_hit_count = "?" unless defined($total_hit_count);
1171 @pages = depoison (@pages);
1173 "" . ($#pages+1) . " candidates of $unfiltered_link_count links" .
1174 " ($total_hit_count total)");
1176 return () if ($#pages < 0);
1178 my $i = int(rand($#pages+1));
1179 my $page = $pages[$i];
1181 LOG ($verbose_load, "picked page $page");
1183 $suppress_audit = 1;
1185 my ( $base2, $body2 ) = get_document ($page, $base, $timeout);
1187 if (!$base2 || !$body2) {
1192 my $img = pick_image_from_body ($base2, $body2);
1196 return ($base2, $img);
1203 ############################################################################
1205 # Pick images from random pages returned by the Yahoo Random Link
1207 ############################################################################
1210 my $yahoo_random_link = "http://random.yahoo.com/fast/ryl";
1213 # Picks a random page; picks a random image on that page;
1214 # returns two URLs: the page containing the image, and the image.
1215 # Returns () if nothing found this time.
1217 sub pick_from_yahoo_random_link($) {
1220 print STDERR "\n\n" if ($verbose_load);
1221 LOG ($verbose_load, "URL: $yahoo_random_link");
1223 $last_search = $yahoo_random_link; # for warnings
1225 $suppress_audit = 1;
1227 my ( $base, $body ) = get_document ($yahoo_random_link, undef, $timeout);
1228 if (!$base || !$body) {
1233 LOG ($verbose_load, "redirected to: $base");
1235 my $img = pick_image_from_body ($base, $body);
1239 return ($base, $img);
1246 ############################################################################
1248 # Pick images from random pages returned by the Alta Vista Random Link
1249 # Note: this seems to have gotten a *lot* less random lately (2007).
1251 ############################################################################
1254 my $alta_vista_random_link = "http://www.altavista.com/image/randomlink";
1257 # Picks a random page; picks a random image on that page;
1258 # returns two URLs: the page containing the image, and the image.
1259 # Returns () if nothing found this time.
1261 sub pick_from_alta_vista_random_link($) {
1264 print STDERR "\n\n" if ($verbose_load);
1265 LOG ($verbose_load, "URL: $alta_vista_random_link");
1267 $last_search = $alta_vista_random_link; # for warnings
1269 $suppress_audit = 1;
1271 my ( $base, $body ) = get_document ($alta_vista_random_link,
1273 if (!$base || !$body) {
1278 LOG ($verbose_load, "redirected to: $base");
1280 my $img = pick_image_from_body ($base, $body);
1284 return ($base, $img);
1291 ############################################################################
1293 # Pick images by feeding random words into Alta Vista Image Search
1295 ############################################################################
1298 my $alta_vista_images_url = "http://www.altavista.com/image/results" .
1299 "?ipht=1" . # photos
1300 "&igrph=1" . # graphics
1303 "&micat=1" . # no partner sites
1304 "&sc=on" . # "site collapse"
1308 sub pick_from_alta_vista_images($) {
1311 my $words = random_word();
1312 my $page = (int(rand(9)) + 1);
1313 my $search_url = $alta_vista_images_url . $words;
1316 $search_url .= "&pgno=" . $page; # page number
1317 $search_url .= "&stq=" . (($page-1) * 12); # first hit result on page
1320 my ($search_hit_count, @subpages) =
1321 pick_from_search_engine ($timeout, $search_url, $words);
1323 my @candidates = ();
1324 foreach my $u (@subpages) {
1326 # avimages is encoding their URLs now.
1327 next unless ($u =~ s/^.*\*\*(http%3a.*$)/$1/gsi);
1328 $u = url_unquote($u);
1330 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1331 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
1332 next if ($u =~ m@[/.]yahoo\.com\b@i); # yahoo and av in cahoots?
1333 next if ($u =~ m@[/.]doubleclick\.net\b@i); # you cretins
1334 next if ($u =~ m@[/.]clicktomarket\.com\b@i); # more cretins
1336 next if ($u =~ m@[/.]viewimages\.com\b@i); # stacked deck
1337 next if ($u =~ m@[/.]gettyimages\.com\b@i);
1339 LOG ($verbose_filter, " candidate: $u");
1340 push @candidates, $u;
1343 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1344 $timeout, @candidates);
1349 ############################################################################
1351 # Pick images from Aptix security cameras
1352 # Cribbed liberally from google image search code.
1353 # By Jason Sullivan <jasonsul@us.ibm.com>
1355 ############################################################################
1357 my $aptix_images_url = ("http://www.google.com/search" .
1358 "?q=inurl:%22jpg/image.jpg%3Fr%3D%22");
1361 sub pick_from_security_camera($) {
1364 my $page = (int(rand(9)) + 1);
1365 my $num = 20; # 20 images per page
1366 my $search_url = $aptix_images_url;
1369 $search_url .= "&start=" . $page*$num; # page number
1370 $search_url .= "&num=" . $num; #images per page
1373 my ($search_hit_count, @subpages) =
1374 pick_from_search_engine ($timeout, $search_url, '');
1376 my @candidates = ();
1378 foreach my $u (@subpages) {
1379 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins (most links)
1380 next unless ($u =~ m@jpg/image.jpg\?r=@i); # All pics contain this
1382 LOG ($verbose_filter, " candidate: $u");
1383 push @candidates, $u;
1387 @candidates = depoison (@candidates);
1388 return () if ($#candidates < 0);
1389 my $i = int(rand($#candidates+1));
1390 my $img = $candidates[$i];
1391 my $ref = $referers{$img};
1393 LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1394 return ($ref, $img);
1398 ############################################################################
1400 # Pick images by feeding random words into Google Image Search.
1401 # By Charles Gales <gales@us.ibm.com>
1403 ############################################################################
1406 my $google_images_url = "http://images.google.com/images" .
1407 "?site=images" . # photos
1408 "&btnG=Search" . # graphics
1409 "&safe=off" . # no screening
1414 sub pick_from_google_images($;$$) {
1415 my ($timeout, $words, $max_page) = @_;
1417 if (!defined($words)) {
1418 $words = random_word(); # only one word for Google
1421 my $page = (int(rand(9)) + 1);
1422 my $num = 20; # 20 images per page
1423 my $search_url = $google_images_url . $words;
1426 $search_url .= "&start=" . $page*$num; # page number
1427 $search_url .= "&num=" . $num; #images per page
1430 my ($search_hit_count, @subpages) =
1431 pick_from_search_engine ($timeout, $search_url, $words);
1433 my @candidates = ();
1435 foreach my $u (@subpages) {
1436 next unless ($u =~ m@imgres\?imgurl@i); # All pics start with this
1437 next if ($u =~ m@[/.]google\.com\b@i); # skip google builtins
1439 $u = html_unquote($u);
1440 if ($u =~ m@^/imgres\?imgurl=(.*?)&imgrefurl=(.*?)\&@) {
1443 $img = "http://$img" unless ($img =~ m/^http:/i);
1445 $ref = url_decode($ref);
1446 $img = url_decode($img);
1448 LOG ($verbose_filter, " candidate: $ref");
1449 push @candidates, $img;
1450 $referers{$img} = $ref;
1454 @candidates = depoison (@candidates);
1455 return () if ($#candidates < 0);
1456 my $i = int(rand($#candidates+1));
1457 my $img = $candidates[$i];
1458 my $ref = $referers{$img};
1460 LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1461 return ($ref, $img);
1466 ############################################################################
1468 # Pick images by feeding random numbers into Google Image Search.
1469 # By jwz, suggested by Ian O'Donnell.
1471 ############################################################################
1475 sub pick_from_google_image_numbers($) {
1479 my $number = int(rand($max));
1481 $number = sprintf("%04d", $number)
1484 pick_from_google_images ($timeout, "$number");
1489 ############################################################################
1491 # Pick images by feeding random digital camera file names into
1492 # Google Image Search.
1493 # By jwz, inspired by the excellent Random Personal Picture Finder
1494 # at http://www.diddly.com/random/
1496 ############################################################################
1500 # Common digital camera file name formats, as described at
1501 # http://www.diddly.com/random/about.html
1503 sub { sprintf ("dcp%05d.jpg", int(rand(4000))); }, # Kodak
1504 sub { sprintf ("dsc%05d.jpg", int(rand(4000))); }, # Nikon
1505 sub { sprintf ("dscn%04d.jpg", int(rand(4000))); }, # Nikon
1506 sub { sprintf ("mvc-%03d.jpg", int(rand(999))); }, # Sony Mavica
1507 sub { sprintf ("mvc%05d.jpg", int(rand(9999))); }, # Sony Mavica
1508 sub { sprintf ("P101%04d.jpg", int(rand(9999))); }, # Olympus w/ date=101
1509 sub { sprintf ("P%x%02d%04d.jpg", # Olympus
1510 int(rand(0xC)), int(rand(30))+1,
1512 sub { sprintf ("IMG_%03d.jpg", int(rand(999))); }, # ?
1513 sub { sprintf ("IMAG%04d.jpg", int(rand(9999))); }, # RCA and Samsung
1514 sub { my $n = int(rand(9999)); # Canon
1515 sprintf ("1%02d-%04d.jpg", int($n/100), $n); },
1516 sub { my $n = int(rand(9999)); # Canon
1517 sprintf ("1%02d-%04d_IMG.jpg",
1518 int($n/100), $n); },
1519 sub { sprintf ("IMG_%04d.jpg", int(rand(9999))); }, # Canon
1520 sub { sprintf ("dscf%04d.jpg", int(rand(9999))); }, # Fuji Finepix
1521 sub { sprintf ("pdrm%04d.jpg", int(rand(9999))); }, # Toshiba PDR
1522 sub { sprintf ("IM%06d.jpg", int(rand(9999))); }, # HP Photosmart
1523 sub { sprintf ("EX%06d.jpg", int(rand(9999))); }, # HP Photosmart
1524 # sub { my $n = int(rand(3)); # Kodak DC-40,50,120
1525 # sprintf ("DC%04d%s.jpg", int(rand(9999)),
1526 # $n == 0 ? 'S' : $n == 1 ? 'M' : 'L'); },
1527 sub { sprintf ("pict%04d.jpg", int(rand(9999))); }, # Minolta Dimage
1528 sub { sprintf ("P%07d.jpg", int(rand(9999))); }, # Kodak DC290
1529 # sub { sprintf ("%02d%02d%04d.jpg", # Casio QV3000, QV4000
1530 # int(rand(12))+1, int(rand(31))+1,
1531 # int(rand(999))); },
1532 # sub { sprintf ("%02d%x%02d%04d.jpg", # Casio QV7000
1533 # int(rand(6)), # year
1534 # int(rand(12))+1, int(rand(31))+1,
1535 # int(rand(999))); },
1536 sub { sprintf ("IMGP%04d.jpg", int(rand(9999))); }, # Pentax Optio S
1537 sub { sprintf ("PANA%04d.jpg", int(rand(9999))); }, # Panasonic vid still
1538 sub { sprintf ("HPIM%04d.jpg", int(rand(9999))); }, # HP Photosmart
1539 sub { sprintf ("PCDV%04d.jpg", int(rand(9999))); }, # ?
1544 sub pick_from_google_image_photos($) {
1547 my $i = int(rand($#photomakers + 1));
1548 my $fn = $photomakers[$i];
1550 my $words .= $file . "%20filetype:jpg";
1552 pick_from_google_images ($timeout, $words);
1557 ############################################################################
1559 # Pick images by feeding random words into Alta Vista Text Search
1561 ############################################################################
1564 my $alta_vista_url = "http://www.altavista.com/web/results" .
1568 "&sc=on" . # "site collapse"
1573 sub pick_from_alta_vista_text($) {
1576 my $words = random_words('%20');
1577 my $page = (int(rand(9)) + 1);
1578 my $search_url = $alta_vista_url . $words;
1581 $search_url .= "&pgno=" . $page;
1582 $search_url .= "&stq=" . (($page-1) * 10);
1585 my ($search_hit_count, @subpages) =
1586 pick_from_search_engine ($timeout, $search_url, $words);
1588 my @candidates = ();
1589 foreach my $u (@subpages) {
1591 # Those altavista fuckers are playing really nasty redirection games
1592 # these days: the filter your clicks through their site, but use
1593 # onMouseOver to make it look like they're not! Well, it makes it
1594 # easier for us to identify search results...
1596 next unless ($u =~ s/^.*\*\*(http%3a.*$)/$1/gsi);
1597 $u = url_unquote($u);
1599 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1600 next if ($u =~ m@[/.]altavista\.com\b@i); # skip altavista builtins
1601 next if ($u =~ m@[/.]yahoo\.com\b@i); # yahoo and av in cahoots?
1603 LOG ($verbose_filter, " candidate: $u");
1604 push @candidates, $u;
1607 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1608 $timeout, @candidates);
1613 ############################################################################
1615 # Pick images by feeding random words into Hotbot
1617 ############################################################################
1619 my $hotbot_search_url =("http://hotbot.lycos.com/default.asp" .
1621 "&descriptiontype=0" .
1628 "&cobrand=undefined" .
1631 sub pick_from_hotbot_text($) {
1634 $last_search = $hotbot_search_url; # for warnings
1636 # lycos seems to always give us back dictionaries and word lists if
1637 # we search for more than one word...
1639 my $words = random_word();
1641 my $start = int(rand(8)) * 10 + 1;
1642 my $search_url = $hotbot_search_url . $words . "&first=$start&page=more";
1644 my ($search_hit_count, @subpages) =
1645 pick_from_search_engine ($timeout, $search_url, $words);
1647 my @candidates = ();
1648 foreach my $u (@subpages) {
1650 # Hotbot plays redirection games too
1652 # next unless ($u =~ m@/director.asp\?.*\btarget=([^&]+)@);
1653 # $u = url_decode($1);
1655 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1656 next if ($u =~ m@[/.]hotbot\.com\b@i); # skip hotbot builtins
1657 next if ($u =~ m@[/.]lycos\.com\b@i); # skip hotbot builtins
1658 next if ($u =~ m@[/.]inktomi\.com\b@i); # skip hotbot builtins
1660 LOG ($verbose_filter, " candidate: $u");
1661 push @candidates, $u;
1664 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1665 $timeout, @candidates);
1670 ############################################################################
1672 # Pick images by feeding random words into Lycos
1674 ############################################################################
1676 my $lycos_search_url = "http://search.lycos.com/default.asp" .
1682 sub pick_from_lycos_text($) {
1685 $last_search = $lycos_search_url; # for warnings
1687 # lycos seems to always give us back dictionaries and word lists if
1688 # we search for more than one word...
1690 my $words = random_word();
1692 my $start = int(rand(8)) * 10 + 1;
1693 my $search_url = $lycos_search_url . $words . "&first=$start&page=more";
1695 my ($search_hit_count, @subpages) =
1696 pick_from_search_engine ($timeout, $search_url, $words);
1698 my @candidates = ();
1699 foreach my $u (@subpages) {
1701 # Lycos plays redirection games.
1703 # next unless ($u =~ m@^http://click.lycos.com/director.asp
1708 # $u = url_decode($1);
1710 next unless ($u =~ m@^http://@i); # skip non-HTTP or relative URLs
1711 next if ($u =~ m@[/.]hotbot\.com\b@i); # skip lycos builtins
1712 next if ($u =~ m@[/.]lycos\.com\b@i); # skip lycos builtins
1713 next if ($u =~ m@[/.]terralycos\.com\b@i); # skip lycos builtins
1714 next if ($u =~ m@[/.]inktomi\.com\b@i); # skip lycos builtins
1717 LOG ($verbose_filter, " candidate: $u");
1718 push @candidates, $u;
1721 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1722 $timeout, @candidates);
1727 ############################################################################
1729 # Pick images by feeding random words into news.yahoo.com
1731 ############################################################################
1733 my $yahoo_news_url = "http://news.search.yahoo.com/search/news" .
1738 sub pick_from_yahoo_news_text($) {
1741 $last_search = $yahoo_news_url; # for warnings
1743 my $words = random_word();
1744 my $search_url = $yahoo_news_url . $words;
1746 my ($search_hit_count, @subpages) =
1747 pick_from_search_engine ($timeout, $search_url, $words);
1749 my @candidates = ();
1750 foreach my $u (@subpages) {
1752 # de-redirectize the URLs
1753 $u =~ s@^http://rds\.yahoo\.com/.*-http%3A@http:@s;
1755 # only accept URLs on Yahoo's news site
1756 next unless ($u =~ m@^http://dailynews\.yahoo\.com/@i ||
1757 $u =~ m@^http://story\.news\.yahoo\.com/@i);
1758 next unless ($u =~ m@&u=/@);
1760 LOG ($verbose_filter, " candidate: $u");
1761 push @candidates, $u;
1764 return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1765 $timeout, @candidates);
1770 ############################################################################
1772 # Pick images from LiveJournal's list of recently-posted images.
1774 ############################################################################
1776 my $livejournal_img_url = "http://www.livejournal.com/stats/latest-img.bml";
1778 # With most of our image sources, we get a random page and then select
1779 # from the images on it. However, in the case of LiveJournal, the page
1780 # of images tends to update slowly; so we'll remember the last N entries
1781 # on it and randomly select from those, to get a wider variety each time.
1783 my $lj_cache_size = 1000;
1784 my @lj_cache = (); # fifo, for ordering by age
1785 my %lj_cache = (); # hash, for detecting dups
1788 sub pick_from_livejournal_images($) {
1791 $last_search = $livejournal_img_url; # for warnings
1793 my ( $base, $body ) = get_document ($livejournal_img_url, undef, $timeout);
1794 return () unless $body;
1797 $body =~ s/(<recent-image)\b/\n$1/gsi;
1799 foreach (split (/\n/, $body)) {
1800 next unless (m/^<recent-image\b/);
1801 next unless (m/\bIMG=[\'\"]([^\'\"]+)[\'\"]/si);
1802 my $img = html_unquote ($1);
1804 next if ($lj_cache{$img}); # already have it
1806 next unless (m/\bURL=[\'\"]([^\'\"]+)[\'\"]/si);
1807 my $page = html_unquote ($1);
1808 my @pair = ($img, $page);
1809 LOG ($verbose_filter, " candidate: $img");
1810 push @lj_cache, \@pair;
1811 $lj_cache{$img} = \@pair;
1814 return () if ($#lj_cache == -1);
1816 my $n = $#lj_cache+1;
1817 my $i = int(rand($n));
1818 my ($img, $page) = @{$lj_cache[$i]};
1820 # delete this one from @lj_cache and from %lj_cache.
1822 @lj_cache = ( @lj_cache[0 .. $i-1],
1823 @lj_cache[$i+1 .. $#lj_cache] );
1824 delete $lj_cache{$img};
1826 # Keep the size of the cache under the limit by nuking older entries
1828 while ($#lj_cache >= $lj_cache_size) {
1829 my $pairP = shift @lj_cache;
1830 my $img = $pairP->[0];
1831 delete $lj_cache{$img};
1834 LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
1836 return ($page, $img);
1840 ############################################################################
1842 # Pick images from ircimages.com (images that have been in the /topic of
1843 # various IRC channels.)
1845 ############################################################################
1847 my $ircimages_url = "http://ircimages.com/";
1850 sub pick_from_ircimages($) {
1853 $last_search = $ircimages_url; # for warnings
1855 my $n = int(rand(2900));
1856 my $search_url = $ircimages_url . "page-$n";
1858 my ( $base, $body ) = get_document ($search_url, undef, $timeout);
1859 return () unless $body;
1861 my @candidates = ();
1864 $body =~ s/(<A)\b/\n$1/gsi;
1866 foreach (split (/\n/, $body)) {
1868 my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
1871 if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; } # quoted string
1872 elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; } # or token
1874 next unless ($u =~ m/^http:/i);
1875 next if ($u =~ m@^http://(searchirc\.com\|ircimages\.com)@i);
1876 next unless ($u =~ m@[.](gif|jpg|jpeg|pjpg|pjpeg|png)$@i);
1878 LOG ($verbose_http, " HREF: $u");
1879 push @candidates, $u;
1882 LOG ($verbose_filter, "" . $#candidates+1 . " links on $search_url");
1884 return () if ($#candidates == -1);
1886 my $i = int(rand($#candidates+1));
1887 my $img = $candidates[$i];
1889 LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#candidates+1) .
1892 $search_url = $img; # hmm...
1893 return ($search_url, $img);
1897 ############################################################################
1899 # Pick images from Twitpic's list of recently-posted images.
1901 ############################################################################
1903 my $twitpic_img_url = "http://twitpic.com/public_timeline/feed.rss";
1905 # With most of our image sources, we get a random page and then select
1906 # from the images on it. However, in the case of Twitpic, the page
1907 # of images tends to update slowly; so we'll remember the last N entries
1908 # on it and randomly select from those, to get a wider variety each time.
1910 my $twitpic_cache_size = 1000;
1911 my @twitpic_cache = (); # fifo, for ordering by age
1912 my %twitpic_cache = (); # hash, for detecting dups
1915 sub pick_from_twitpic_images($) {
1918 $last_search = $twitpic_img_url; # for warnings
1920 my ( $base, $body ) = get_document ($twitpic_img_url, undef, $timeout);
1926 $body =~ s/(<item)\b/\n$1/gsi;
1928 my @items = split (/\n/, $body);
1931 next unless (m@<link>([^<>]*)</link>@si);
1932 my $page = html_unquote ($1);
1937 next if ($twitpic_cache{$page}); # already have it
1939 LOG ($verbose_filter, " candidate: $page");
1940 push @twitpic_cache, $page;
1941 $twitpic_cache{$page} = $page;
1945 # Pull from the cache.
1947 return () if ($#twitpic_cache == -1);
1949 my $n = $#twitpic_cache+1;
1950 my $i = int(rand($n));
1951 my $page = $twitpic_cache[$i];
1953 # delete this one from @twitpic_cache and from %twitpic_cache.
1955 @twitpic_cache = ( @twitpic_cache[0 .. $i-1],
1956 @twitpic_cache[$i+1 .. $#twitpic_cache] );
1957 delete $twitpic_cache{$page};
1959 # Keep the size of the cache under the limit by nuking older entries
1961 while ($#twitpic_cache >= $twitpic_cache_size) {
1962 my $page = shift @twitpic_cache;
1963 delete $twitpic_cache{$page};
1966 ( $base, $body ) = get_document ($page, undef, $timeout);
1968 $body = '' unless defined($body);
1970 foreach (split (/<img\s+/, $body)) {
1971 my ($src) = m/\bsrc=[\"\'](.*?)[\"\']/si;
1974 next if m@/images/@s;
1978 $img = "http:$img" if ($img =~ m@^//@s); # Oh come on
1980 # Sometimes these images are hosted on twitpic, sometimes on Amazon.
1981 if ($img =~ m@^/@) {
1982 $base =~ s@^(https?://[^/]+)/.*@$1@s;
1983 $img = $base . $img;
1989 LOG ($verbose_load, "no matching images on $page\n");
1993 LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
1995 return ($page, $img);
1999 ############################################################################
2001 # Pick images from Twitter's list of recently-posted updates.
2003 ############################################################################
2005 # With most of our image sources, we get a random page and then select
2006 # from the images on it. However, in the case of Twitter, the page
2007 # of images only updates once a minute; so we'll remember the last N entries
2008 # on it and randomly select from those, to get a wider variety each time.
2010 my $twitter_img_url = "http://api.twitter.com/1/statuses/" .
2011 "public_timeline.json" .
2012 "?include_entities=true" .
2013 "&include_rts=true" .
2016 my $twitter_cache_size = 1000;
2018 my @twitter_cache = (); # fifo, for ordering by age
2019 my %twitter_cache = (); # hash, for detecting dups
2023 sub pick_from_twitter_images($) {
2026 $last_search = $twitter_img_url; # for warnings
2028 my ( $base, $body ) = get_document ($twitter_img_url, undef, $timeout);
2032 $body =~ s/[\r\n]+/ /gs;
2034 # Parsing JSON is a pain in the ass. So we halfass it as usual.
2035 $body =~ s/^\[|\]$//s;
2036 $body =~ s/(\[.*?\])/{ $_ = $1; s@\},@\} @gs; $_; }/gsexi;
2037 my @items = split (/},{/, $body);
2039 my ($name) = m@"screen_name":"([^\"]+)"@si;
2040 my ($img) = m@"media_url":"([^\"]+)"@si;
2041 my ($page) = m@"display_url":"([^\"]+)"@si;
2042 next unless ($name && $img && $page);
2043 foreach ($img, $page) {
2045 $_ = "http://$_" unless (m/^http/si);
2048 next if ($twitter_cache{$page}); # already have it
2050 LOG ($verbose_filter, " candidate: $page - $img");
2051 push @twitter_cache, $page;
2052 $twitter_cache{$page} = $img;
2056 # Pull from the cache.
2058 return () if ($#twitter_cache == -1);
2060 my $n = $#twitter_cache+1;
2061 my $i = int(rand($n));
2062 my $page = $twitter_cache[$i];
2063 my $url = $twitter_cache{$page};
2065 # delete this one from @twitter_cache and from %twitter_cache.
2067 @twitter_cache = ( @twitter_cache[0 .. $i-1],
2068 @twitter_cache[$i+1 .. $#twitter_cache] );
2069 delete $twitter_cache{$page};
2071 # Keep the size of the cache under the limit by nuking older entries
2073 while ($#twitter_cache >= $twitter_cache_size) {
2074 my $page = shift @twitter_cache;
2075 delete $twitter_cache{$page};
2078 LOG ($verbose_load, "picked page $url");
2080 $suppress_audit = 1;
2082 return ($page, $url);
2086 ############################################################################
2088 # Pick images from Flickr's page of recently-posted photos.
2090 ############################################################################
2092 my $flickr_img_url = "http://www.flickr.com/photos/";
2094 # Like LiveJournal, the Flickr page of images tends to update slowly,
2095 # so remember the last N entries on it and randomly select from those.
2097 # I know that Flickr has an API (http://www.flickr.com/services/api/)
2098 # but it was easy enough to scrape the HTML, so I didn't bother exploring.
2100 my $flickr_cache_size = 1000;
2101 my @flickr_cache = (); # fifo, for ordering by age
2102 my %flickr_cache = (); # hash, for detecting dups
2106 sub pick_from_flickr_recent($) {
2109 my $start = 16 * int(rand(100));
2111 $last_search = $flickr_img_url; # for warnings
2112 $last_search .= "?start=$start" if ($start > 0);
2114 my ( $base, $body ) = get_document ($last_search, undef, $timeout);
2115 return () unless $body;
2117 $body =~ s/[\r\n]/ /gs;
2118 $body =~ s/(<a)\b/\n$1/gsi;
2122 foreach (split (/\n/, $body)) {
2123 my ($page, $thumb) = m@<A \s [^<>]* \b HREF=\"([^<>\"]+)\" [^<>]* > \s*
2124 <IMG \s [^<>]* \b SRC=\"([^<>\"]+)\" @xsi;
2125 next unless defined ($thumb);
2126 $page = html_unquote ($page);
2127 $thumb = html_unquote ($thumb);
2129 next unless ($thumb =~ m@^http://farm\d*\.static\.?flickr\.com/@);
2131 my $base = "http://www.flickr.com/";
2132 $page =~ s@^/@$base@;
2133 $thumb =~ s@^/@$base@;
2136 $img =~ s/_[a-z](\.[a-z\d]+)$/$1/si; # take off "thumb" suffix
2139 next if ($flickr_cache{$img}); # already have it
2141 my @pair = ($img, $page, $start);
2142 LOG ($verbose_filter, " candidate: $img");
2143 push @flickr_cache, \@pair;
2144 $flickr_cache{$img} = \@pair;
2148 return () if ($#flickr_cache == -1);
2150 my $n = $#flickr_cache+1;
2151 my $i = int(rand($n));
2152 my ($img, $page) = @{$flickr_cache[$i]};
2154 # delete this one from @flickr_cache and from %flickr_cache.
2156 @flickr_cache = ( @flickr_cache[0 .. $i-1],
2157 @flickr_cache[$i+1 .. $#flickr_cache] );
2158 delete $flickr_cache{$img};
2160 # Keep the size of the cache under the limit by nuking older entries
2162 while ($#flickr_cache >= $flickr_cache_size) {
2163 my $pairP = shift @flickr_cache;
2164 my $img = $pairP->[0];
2165 delete $flickr_cache{$img};
2168 LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
2170 return ($page, $img);
2174 ############################################################################
2176 # Pick images from a random RSS feed on Flickr.
2178 ############################################################################
2180 my $flickr_rss_base = ("http://www.flickr.com/services/feeds/photos_public.gne".
2181 "?format=rss_200_enc&tagmode=any&tags=");
2183 # Picks a random RSS feed; picks a random image from that feed;
2184 # returns 2 URLs: the page containing the image, and the image.
2185 # Mostly by Joe Mcmahon <mcmahon@yahoo-inc.com>
2188 sub pick_from_flickr_random($) {
2189 my $timeout = shift;
2191 my $words = random_words(',');
2192 my $rss = $flickr_rss_base . $words;
2193 $last_search = $rss;
2198 print STDERR "\n\n" if ($verbose_load);
2199 LOG ($verbose_load, "words: $_");
2200 LOG ($verbose_load, "URL: $last_search");
2202 $suppress_audit = 1;
2204 my ( $base, $body ) = get_document ($last_search, undef, $timeout);
2205 if (!$base || !$body) {
2211 ($base, $img) = pick_image_from_rss ($base, $body);
2213 return () unless defined ($img);
2215 LOG ($verbose_load, "redirected to: $base");
2216 return ($base, $img);
2220 ############################################################################
2222 # Pick images by waiting for driftnet to populate a temp dir with files.
2223 # Requires driftnet version 0.1.5 or later.
2224 # (Driftnet is a program by Chris Lightfoot that sniffs your local ethernet
2225 # for images being downloaded by others.)
2226 # Driftnet/webcollage integration by jwz.
2228 ############################################################################
2231 sub pick_from_driftnet($) {
2234 my $id = $driftnet_magic;
2235 my $dir = $driftnet_dir;
2239 error ("\$driftnet_dir unset?") unless ($dir);
2242 error ("$dir unreadable") unless (-d "$dir/.");
2244 $timeout = $http_timeout unless ($timeout);
2247 while ($now = time, $now < $start + $timeout) {
2248 opendir (my $dir, $dir) || error ("$dir: $!");
2249 while (my $file = readdir($dir)) {
2250 next if ($file =~ m/^\./);
2251 $file = "$dir/$file";
2253 LOG ($verbose_load, "picked file $file ($id)");
2254 return ($id, $file);
2258 LOG (($verbose_net || $verbose_load), "timed out for $id");
2263 sub get_driftnet_file($) {
2266 error ("\$driftnet_dir unset?") unless ($driftnet_dir);
2268 my $id = $driftnet_magic;
2269 error ("$id: $file not in $driftnet_dir?")
2270 unless ($file =~ m@^\Q$driftnet_dir@o);
2272 open (my $in, '<', $file) || error ("$id: $file: $!");
2274 local $/ = undef; # read entire file
2276 close ($in) || error ("$id: $file: $!");
2277 unlink ($file) || error ("$id: $file: rm: $!");
2278 return ($id, $body);
2282 sub spawn_driftnet($) {
2285 # make a directory to use.
2287 my $tmp = $ENV{TEMPDIR} || "/tmp";
2288 $driftnet_dir = sprintf ("$tmp/driftcollage-%08x", rand(0xffffffff));
2289 LOG ($verbose_exec, "mkdir $driftnet_dir");
2290 last if mkdir ($driftnet_dir, 0700);
2293 if (! ($cmd =~ m/\s/)) {
2294 # if the command didn't have any arguments in it, then it must be just
2295 # a pointer to the executable. Append the default args to it.
2296 my $dargs = $default_driftnet_cmd;
2297 $dargs =~ s/^[^\s]+//;
2301 # point the driftnet command at our newly-minted private directory.
2303 $cmd .= " -d $driftnet_dir";
2304 $cmd .= ">/dev/null" unless ($verbose_exec);
2307 if ($pid < 0) { error ("fork: $!\n"); }
2310 push @pids_to_kill, $pid;
2311 LOG ($verbose_exec, "forked for \"$cmd\"");
2314 nontrapping_system ($cmd) || error ("exec: $!");
2317 # wait a bit, then make sure the process actually started up.
2320 error ("pid $pid failed to start \"$cmd\"")
2321 unless (1 == kill (0, $pid));
2325 sub pick_from_local_dir($) {
2328 my $id = $local_magic;
2331 my $dir = $local_dir;
2332 error ("\$local_dir unset?") unless ($dir);
2335 error ("$dir unreadable") unless (-d "$dir/.");
2337 my $v = ($verbose_exec ? "-v" : "");
2338 my $pick = `xscreensaver-getimage-file $v "$dir"`;
2340 $pick = "$dir/$pick" unless ($pick =~ m@^/@s); # relative path
2342 LOG ($verbose_load, "picked file $pick ($id)");
2343 return ($id, $pick);
2347 sub get_local_file($) {
2350 error ("\$local_dir unset?") unless ($local_dir);
2352 my $id = $local_magic;
2353 error ("$id: $file not in $local_dir?")
2354 unless ($file =~ m@^\Q$local_dir@o);
2356 open (my $in, '<', $file) || error ("$id: $file: $!");
2357 local $/ = undef; # read entire file
2359 close ($in) || error ("$id: $file: $!");
2360 return ($id, $body);
2365 ############################################################################
2367 # Pick a random image in a random way
2369 ############################################################################
2372 # Picks a random image on a random page, and returns two URLs:
2373 # the page containing the image, and the image.
2374 # Returns () if nothing found this time.
2377 sub pick_image(;$) {
2380 $current_state = "select";
2381 $load_method = "none";
2383 my $n = int(rand(100));
2386 my @rest = @search_methods;
2389 my $pct = shift @rest;
2390 my $name = shift @rest;
2391 my $tfn = shift @rest;
2393 if ($total > $n && !defined($fn)) {
2395 $current_state = $name;
2396 $load_method = $current_state;
2400 if ($total != 100) {
2401 error ("internal error: \@search_methods totals to $total%!");
2404 record_attempt ($current_state);
2405 return $fn->($timeout);
2410 ############################################################################
2412 # Statistics and logging
2414 ############################################################################
2417 return strftime ("%H:%M:%S: ", localtime);
2421 return "$progname: " . timestr() . "$current_state: ";
2426 print STDERR blurb() . "$err\n";
2432 print STDERR "$progname: stack trace:\n";
2434 my ($package, $filename, $line, $subroutine) = caller($i++);
2435 last unless defined($package);
2436 $filename =~ s@^.*/@@;
2437 print STDERR " $filename#$line, $subroutine\n";
2449 my $head = "$progname: DEBUG: ";
2450 foreach (split (/\n/, $lastlog)) {
2451 print STDERR "$head$_\n";
2457 my ($print, $msg) = @_;
2458 my $blurb = timestr() . "$current_state: ";
2459 $lastlog .= "$blurb$msg\n";
2460 print STDERR "$progname: $blurb$msg\n" if $print;
2465 my %stats_successes;
2468 my $last_state = undef;
2469 sub record_attempt($) {
2473 record_failure($last_state) unless ($image_succeeded > 0);
2475 $last_state = $name;
2478 report_performance();
2481 $image_succeeded = 0;
2482 $suppress_audit = 0;
2485 sub record_success($$$) {
2486 my ($name, $url, $base) = @_;
2487 if (defined($stats_successes{$name})) {
2488 $stats_successes{$name}++;
2490 $stats_successes{$name} = 1;
2493 stop_timer ($name, 1);
2494 my $o = $current_state;
2495 $current_state = $name;
2496 save_recent_url ($url, $base);
2497 $current_state = $o;
2498 $image_succeeded = 1;
2503 sub record_failure($) {
2506 return if $image_succeeded;
2508 stop_timer ($name, 0);
2509 if ($verbose_load && !$verbose_exec) {
2511 if ($suppress_audit) {
2512 print STDERR "$progname: " . timestr() . "(audit log suppressed)\n";
2516 my $o = $current_state;
2517 $current_state = "DEBUG";
2519 my $line = "#" x 78;
2520 print STDERR "\n\n\n";
2521 print STDERR ("#" x 78) . "\n";
2522 print STDERR blurb() . "failed to get an image. Full audit log:\n";
2525 print STDERR ("-" x 78) . "\n";
2526 print STDERR "\n\n";
2528 $current_state = $o;
2530 $image_succeeded = 0;
2537 my $i = $stats_successes{$name};
2538 my $j = $stats_attempts{$name};
2541 return "" . ($j ? int($i * 100 / $j) : "0") . "%";
2545 my $current_start_time = 0;
2547 sub start_timer($) {
2549 $current_start_time = time;
2551 if (defined($stats_attempts{$name})) {
2552 $stats_attempts{$name}++;
2554 $stats_attempts{$name} = 1;
2556 if (!defined($stats_elapsed{$name})) {
2557 $stats_elapsed{$name} = 0;
2561 sub stop_timer($$) {
2562 my ($name, $success) = @_;
2563 $stats_elapsed{$name} += time - $current_start_time;
2567 my $last_report_time = 0;
2568 sub report_performance() {
2570 return unless $verbose_warnings;
2573 return unless ($now >= $last_report_time + $report_performance_interval);
2574 my $ot = $last_report_time;
2575 $last_report_time = $now;
2577 return if ($ot == 0);
2579 my $blurb = "$progname: " . timestr();
2582 print STDERR "${blurb}Current standings:\n";
2584 foreach my $name (sort keys (%stats_attempts)) {
2585 my $try = $stats_attempts{$name};
2586 my $suc = $stats_successes{$name} || 0;
2587 my $pct = int($suc * 100 / $try);
2588 my $secs = $stats_elapsed{$name};
2589 my $secs_link = $secs / $try;
2590 print STDERR sprintf ("$blurb %-14s %4s (%d/%d);" .
2591 " \t %.1f secs/link\n",
2592 "$name:", "$pct%", $suc, $try, $secs_link);
2598 my $max_recent_images = 400;
2599 my $max_recent_sites = 20;
2600 my @recent_images = ();
2601 my @recent_sites = ();
2603 sub save_recent_url($$) {
2604 my ($url, $base) = @_;
2606 return unless ($verbose_warnings);
2609 my ($site) = m@^http://([^ \t\n\r/:]+)@;
2610 return unless defined ($site);
2612 if ($base eq $driftnet_magic || $base eq $local_magic) {
2614 @recent_images = ();
2618 foreach (@recent_images) {
2620 print STDERR blurb() . "WARNING: recently-duplicated image: $url" .
2621 " (on $base via $last_search)\n";
2627 # suppress "duplicate site" warning via %warningless_sites.
2629 if ($warningless_sites{$site}) {
2631 } elsif ($site =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
2632 $warningless_sites{$1}) {
2634 } elsif ($site =~ m@([^.]+\.[^.]+)$@ &&
2635 $warningless_sites{$1}) {
2640 foreach (@recent_sites) {
2642 print STDERR blurb() . "WARNING: recently-duplicated site: $site" .
2643 " ($url on $base via $last_search)\n";
2649 push @recent_images, $url;
2650 push @recent_sites, $site;
2651 shift @recent_images if ($#recent_images >= $max_recent_images);
2652 shift @recent_sites if ($#recent_sites >= $max_recent_sites);
2657 ##############################################################################
2661 ##############################################################################
2668 s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
2673 # Given the raw body of a GIF document, returns the dimensions of the image.
2677 my $type = substr($body, 0, 6);
2679 return () unless ($type =~ /GIF8[7,9]a/);
2680 $s = substr ($body, 6, 10);
2681 my ($a,$b,$c,$d) = unpack ("C"x4, $s);
2682 return () unless defined ($d);
2683 return (($b<<8|$a), ($d<<8|$c));
2686 # Given the raw body of a JPEG document, returns the dimensions of the image.
2691 my $L = length($body);
2693 my $c1 = substr($body, $i, 1); $i++;
2694 my $c2 = substr($body, $i, 1); $i++;
2695 return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
2698 while (ord($ch) != 0xDA && $i < $L) {
2699 # Find next marker, beginning with 0xFF.
2700 while (ord($ch) != 0xFF) {
2701 return () if (length($body) <= $i);
2702 $ch = substr($body, $i, 1); $i++;
2704 # markers can be padded with any number of 0xFF.
2705 while (ord($ch) == 0xFF) {
2706 return () if (length($body) <= $i);
2707 $ch = substr($body, $i, 1); $i++;
2710 # $ch contains the value of the marker.
2711 my $marker = ord($ch);
2713 if (($marker >= 0xC0) &&
2714 ($marker <= 0xCF) &&
2715 ($marker != 0xC4) &&
2716 ($marker != 0xCC)) { # it's a SOFn marker
2718 return () if (length($body) <= $i);
2719 my $s = substr($body, $i, 4); $i += 4;
2720 my ($a,$b,$c,$d) = unpack("C"x4, $s);
2721 return (($c<<8|$d), ($a<<8|$b));
2724 # We must skip variables, since FFs in variable names aren't
2725 # valid JPEG markers.
2726 return () if (length($body) <= $i);
2727 my $s = substr($body, $i, 2); $i += 2;
2728 my ($c1, $c2) = unpack ("C"x2, $s);
2729 my $length = ($c1 << 8) | $c2;
2730 return () if ($length < 2);
2737 # Given the raw body of a PNG document, returns the dimensions of the image.
2741 return () unless ($body =~ m/^\211PNG\r/);
2742 my ($bits) = ($body =~ m/^.{12}(.{12})/s);
2743 return () unless defined ($bits);
2744 return () unless ($bits =~ /^IHDR/);
2745 my ($ign, $w, $h) = unpack("a4N2", $bits);
2750 # Given the raw body of a GIF, JPEG, or PNG document, returns the dimensions
2755 my ($w, $h) = gif_size ($body);
2756 if ($w && $h) { return ($w, $h); }
2757 ($w, $h) = jpeg_size ($body);
2758 if ($w && $h) { return ($w, $h); }
2759 return png_size ($body);
2763 # returns the full path of the named program, or undef.
2767 foreach (split (/:/, $ENV{PATH})) {
2768 if (-x "$_/$prog") {
2776 # Like rand(), but chooses numbers with a bell curve distribution.
2779 $_ = 1.0 unless defined($_);
2781 return (rand($_) + rand($_) + rand($_));
2785 sub exit_cleanup() {
2787 print STDERR "$progname: exiting\n" if ($verbose_warnings);
2788 if (@pids_to_kill) {
2789 print STDERR blurb() . "killing: " . join(' ', @pids_to_kill) . "\n";
2790 kill ('TERM', @pids_to_kill);
2794 sub signal_cleanup($) {
2796 print STDERR blurb() . (defined($sig)
2797 ? "caught signal $sig."
2800 if ($verbose_exec || $verbose_warnings);
2806 ##############################################################################
2808 # Generating a list of urls only
2810 ##############################################################################
2812 sub url_only_output() {
2814 my ($base, $img) = pick_image;
2818 print "$img $base\n";
2823 ##############################################################################
2825 # Running as an xscreensaver module, or as a web page imagemap
2827 ##############################################################################
2829 my $image_ppm = sprintf ("%s/webcollage-%08x.ppm",
2830 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
2832 my $image_tmp1 = sprintf ("%s/webcollage-1-%08x.ppm",
2833 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
2835 my $image_tmp2 = sprintf ("%s/webcollage-2-%08x.ppm",
2836 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
2839 my $filter_cmd = undef;
2840 my $post_filter_cmd = undef;
2841 my $background = undef;
2843 my @imagemap_areas = ();
2844 my $imagemap_html_tmp = undef;
2845 my $imagemap_jpg_tmp = undef;
2848 my $img_width; # size of the image being generated.
2854 unlink $image_ppm, $image_tmp1, $image_tmp2;
2855 unlink $imagemap_html_tmp, $imagemap_jpg_tmp
2856 if (defined ($imagemap_html_tmp));
2860 # Like system, but prints status about exit codes, and kills this process
2861 # with whatever signal killed the sub-process, if any.
2863 sub nontrapping_system(@) {
2867 s/\"[^\"]+\"/\"...\"/g;
2869 LOG ($verbose_exec, "executing \"$_\"");
2874 LOG ($verbose_exec, "subproc exited normally.");
2875 } elsif (($rc & 0xff) == 0) {
2877 LOG ($verbose_exec, "subproc exited with status $rc.");
2880 LOG ($verbose_exec, "subproc dumped core.");
2883 LOG ($verbose_exec, "subproc died with signal $rc.");
2884 # die that way ourselves.
2892 # Given the URL of a GIF, JPEG, or PNG image, and the body of that image,
2893 # writes a PPM to the given output file. Returns the width/height of the
2894 # image if successful.
2896 sub image_to_pnm($$$) {
2897 my ($url, $body, $output) = @_;
2898 my ($cmd, $cmd2, $w, $h);
2900 if ((@_ = gif_size ($body))) {
2903 } elsif ((@_ = jpeg_size ($body))) {
2906 } elsif ((@_ = png_size ($body))) {
2910 LOG (($verbose_pbm || $verbose_load),
2911 "not a GIF, JPG, or PNG" .
2912 (($body =~ m@<(base|html|head|body|script|table|a href)\b@i)
2913 ? " (looks like HTML)" : "") .
2915 $suppress_audit = 1;
2919 $cmd2 = "exec $cmd"; # yes, this really is necessary. if we don't
2920 # do this, the process doesn't die properly.
2921 if (!$verbose_pbm) {
2923 # We get a "giftopnm: got a 'Application Extension' extension"
2924 # warning any time it's an animgif.
2926 # Note that "giftopnm: EOF / read error on image data" is not
2927 # always a fatal error -- sometimes the image looks fine anyway.
2929 $cmd2 .= " 2>/dev/null";
2932 # There exist corrupted GIF and JPEG files that can make giftopnm and
2933 # djpeg lose their minds and go into a loop. So this gives those programs
2934 # a small timeout -- if they don't complete in time, kill them.
2940 local $SIG{ALRM} = sub {
2942 "timed out ($cvt_timeout) for $cmd on \"$url\" in pid $pid");
2943 kill ('TERM', $pid) if ($pid);
2948 if (($pid = open (my $pipe, "| $cmd2 > $output"))) {
2955 LOG ($verbose_exec, "awaiting $pid");
2957 LOG ($verbose_exec, "$pid completed");
2959 my $size = (stat($output))[7];
2960 $size = -1 unless defined($size);
2962 LOG ($verbose_pbm, "$cmd on ${w}x$h \"$url\" failed ($size bytes)");
2966 LOG ($verbose_pbm, "created ${w}x$h $output ($cmd)");
2969 print STDERR blurb() . "$cmd failed: $!\n";
2973 die if ($@ && $@ ne "alarm\n"); # propagate errors
2987 # Same as the "ppmmake" command: creates a solid-colored PPM.
2988 # Does not understand the rgb.txt color names except "black" and "white".
2991 my ($outfile, $bgcolor, $w, $h) = @_;
2994 if ($bgcolor =~ m/^\#?([\dA-F][\dA-F])([\dA-F][\dA-F])([\dA-F][\dA-F])$/i ||
2995 $bgcolor =~ m/^\#?([\dA-F])([\dA-F])([\dA-F])$/i) {
2996 ($r, $g, $b) = (hex($1), hex($2), hex($3));
2997 } elsif ($bgcolor =~ m/^black$/i) {
2998 ($r, $g, $b) = (0, 0, 0);
2999 } elsif ($bgcolor =~ m/^white$/i) {
3000 ($r, $g, $b) = (0xFF, 0xFF, 0xFF);
3002 error ("unparsable color name: $bgcolor");
3005 my $pixel = pack('CCC', $r, $g, $b);
3006 my $bits = "P6\n$w $h\n255\n" . ($pixel x ($w * $h));
3008 open (my $out, '>', $outfile) || error ("$outfile: $!");
3014 sub pick_root_displayer() {
3018 # see "xscreensaver/hacks/webcollage-cocoa.m"
3019 return "echo COCOA LOAD ";
3022 foreach my $cmd (@root_displayers) {
3024 my ($name) = m/^([^ ]+)/;
3025 push @names, "\"$name\"";
3026 LOG ($verbose_exec, "looking for $name...");
3027 foreach my $dir (split (/:/, $ENV{PATH})) {
3028 LOG ($verbose_exec, " checking $dir/$name");
3029 return $cmd if (-x "$dir/$name");
3033 $names[$#names] = "or " . $names[$#names];
3034 error "none of: " . join (", ", @names) . " were found on \$PATH.";
3038 my $ppm_to_root_window_cmd = undef;
3041 sub x_or_pbm_output($) {
3042 my ($window_id) = @_;
3044 # Check for our helper program, to see whether we need to use PPM pipelines.
3046 $_ = "webcollage-helper";
3047 if (defined ($webcollage_helper) || which ($_)) {
3048 $webcollage_helper = $_ unless (defined($webcollage_helper));
3049 LOG ($verbose_pbm, "found \"$webcollage_helper\"");
3050 $webcollage_helper .= " -v";
3052 LOG (($verbose_pbm || $verbose_load), "no $_ program");
3055 if ($cocoa_p && !defined ($webcollage_helper)) {
3056 error ("webcollage-helper not found in Cocoa-mode!");
3060 # make sure the various programs we execute exist, right up front.
3064 if (!defined($webcollage_helper)) {
3065 # Only need these others if we don't have the helper.
3067 "giftopnm", "pngtopnm", "djpeg",
3068 "pnmpaste", "pnmscale", "pnmcut");
3072 which ($_) || error "$_ not found on \$PATH.";
3075 # find a root-window displayer program.
3077 if (!$no_output_p) {
3078 $ppm_to_root_window_cmd = pick_root_displayer();
3081 if (defined ($window_id)) {
3082 error ("-window-id only works if xscreensaver-getimage is installed")
3083 unless ($ppm_to_root_window_cmd =~ m/^xscreensaver-getimage\b/);
3085 error ("unparsable window id: $window_id")
3086 unless ($window_id =~ m/^\d+$|^0x[\da-f]+$/i);
3087 $ppm_to_root_window_cmd =~ s/--?root\b/$window_id/ ||
3088 error ("unable to munge displayer: $ppm_to_root_window_cmd");
3091 if (!$img_width || !$img_height) {
3093 if (!defined ($window_id) &&
3094 defined ($ENV{XSCREENSAVER_WINDOW})) {
3095 $window_id = $ENV{XSCREENSAVER_WINDOW};
3098 if (!defined ($window_id)) {
3100 which ($_) || error "$_ not found on \$PATH.";
3102 ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
3103 if (!defined($img_height)) {
3104 error "xdpyinfo failed.";
3106 } else { # we have a window id
3108 which ($_) || error "$_ not found on \$PATH.";
3109 $_ .= " -id $window_id";
3111 ($img_width, $img_height) = m/^\s*Width:\s*(\d+)\n\s*Height:\s*(\d+)\n/m;
3113 if (!defined($img_height)) {
3114 error "xwininfo failed.";
3119 my $bgcolor = "#000000";
3120 my $bgimage = undef;
3123 if ($background =~ m/^\#[0-9a-f]+$/i) {
3124 $bgcolor = $background;
3126 } elsif (-r $background) {
3127 $bgimage = $background;
3129 } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
3130 error "not a color or readable file: $background";
3133 # default to assuming it's a color
3134 $bgcolor = $background;
3138 # Create the sold-colored base image.
3140 LOG ($verbose_pbm, "creating base image: ${img_width}x${img_height}");
3141 $_ = ppmmake ($image_ppm, $bgcolor, $img_width, $img_height);
3143 # Paste the default background image in the middle of it.
3149 open (my $imgf, '<', $bgimage) || error "couldn't open $bgimage: $!";
3150 local $/ = undef; # read entire file
3155 if ((@_ = gif_size ($body))) {
3157 $cmd = "giftopnm |";
3159 } elsif ((@_ = jpeg_size ($body))) {
3163 } elsif ((@_ = png_size ($body))) {
3165 $cmd = "pngtopnm |";
3167 } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
3173 error "$bgimage is not a GIF, JPEG, PNG, or PPM.";
3176 my $x = int (($img_width - $iw) / 2);
3177 my $y = int (($img_height - $ih) / 2);
3179 "pasting $bgimage (${iw}x$ih) into base image at $x,$y");
3181 $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
3182 open ($imgf, "| $cmd") || error "running $cmd: $!";
3186 LOG ($verbose_exec, "subproc exited normally.");
3187 rename ($image_tmp1, $image_ppm) ||
3188 error "renaming $image_tmp1 to $image_ppm: $!";
3194 my ($base, $img) = pick_image();
3195 my $source = $current_state;
3196 $current_state = "loadimage";
3198 my ($headers, $body) = get_document ($img, $base);
3200 paste_image ($base, $img, $body, $source);
3204 $current_state = "idle";
3205 $load_method = "none";
3207 unlink $image_tmp1, $image_tmp2;
3212 sub paste_image($$$$) {
3213 my ($base, $img, $body, $source) = @_;
3215 $current_state = "paste";
3217 $suppress_audit = 0;
3219 LOG ($verbose_pbm, "got $img (" . length($body) . ")");
3223 # If we are using the webcollage-helper, then we do not need to convert this
3224 # image to a PPM. But, if we're using a filter command, we still must, since
3225 # that's what the filters expect (webcollage-helper can read PPMs, so that's
3228 if (defined ($webcollage_helper) &&
3229 !defined ($filter_cmd)) {
3231 ($iw, $ih) = image_size ($body);
3233 LOG (($verbose_pbm || $verbose_load),
3234 "not a GIF, JPG, or PNG" .
3235 (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
3236 ? " (looks like HTML)" : "") .
3238 $suppress_audit = 1;
3243 open (my $out, '>', $image_tmp1) || error ("writing $image_tmp1: $!");
3244 (print $out $body) || error ("writing $image_tmp1: $!");
3245 close ($out) || error ("writing $image_tmp1: $!");
3248 ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
3251 LOG ($verbose_pbm, "unable to make PBM from $img");
3256 record_success ($load_method, $img, $base);
3259 my $ow = $iw; # used only for error messages
3262 # don't just tack this onto the front of the pipeline -- we want it to
3263 # be able to change the size of the input image.
3266 LOG ($verbose_pbm, "running $filter_cmd");
3268 my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
3270 LOG(($verbose_pbm || $verbose_load), "failed command: \"$filter_cmd\"");
3271 LOG(($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
3274 rename ($image_tmp2, $image_tmp1);
3276 # re-get the width/height in case the filter resized it.
3277 open (my $imgf, '<', $image_tmp1) || return 0;
3280 ($iw, $ih) = m/^(\d+) (\d+)$/;
3282 return 0 unless ($iw && $ih);
3285 my $target_w = $img_width; # max rectangle into which the image must fit
3286 my $target_h = $img_height;
3292 # Usually scale the image to fit on the screen -- but sometimes scale it
3293 # to fit on half or a quarter of the screen. (We do this by reducing the
3294 # size of the target rectangle.) Note that the image is not merely scaled
3295 # to fit; we instead cut the image in half repeatedly until it fits in the
3296 # target rectangle -- that gives a wider distribution of sizes.
3298 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; } # reduce target rect
3299 if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; }
3301 if ($iw > $target_w || $ih > $target_h) {
3302 while ($iw > $target_w ||
3308 if ($iw <= 10 || $ih <= 10) {
3309 LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus.");
3313 LOG ($verbose_pbm, "scaling to ${iw}x$ih ($scale)");
3315 $cmd .= " | pnmscale -xsize $iw -ysize $ih";
3319 my $src = $image_tmp1;
3321 my $crop_x = 0; # the sub-rectangle of the image
3322 my $crop_y = 0; # that we will actually paste.
3326 # The chance that we will randomly crop out a section of an image starts
3327 # out fairly low, but goes up for images that are very large, or images
3328 # that have ratios that make them look like banners (we try to avoid
3329 # banner images entirely, but they slip through when the IMG tags didn't
3330 # have WIDTH and HEIGHT specified.)
3332 my $crop_chance = 0.2;
3333 if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
3334 $crop_chance += 0.2;
3336 if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
3337 $crop_chance += 0.2;
3339 if ($min_ratio && ($iw * $min_ratio) > $ih) {
3340 $crop_chance += 0.7;
3343 if ($crop_chance > 0.1) {
3344 LOG ($verbose_pbm, "crop chance: $crop_chance");
3347 if (rand() < $crop_chance) {
3352 if ($crop_w > $min_width) {
3353 # if it's a banner, select the width linearly.
3354 # otherwise, select a bell.
3355 my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
3358 $crop_w = $min_width + int ($r * ($crop_w - $min_width));
3359 $crop_x = int (rand() * ($ow - $crop_w));
3361 if ($crop_h > $min_height) {
3362 # height always selects as a bell.
3363 $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
3364 $crop_y = int (rand() * ($oh - $crop_h));
3367 if ($crop_x != 0 || $crop_y != 0 ||
3368 $crop_w != $iw || $crop_h != $ih) {
3370 "randomly cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
3374 # Where the image should logically land -- this might be negative.
3376 my $x = int((rand() * ($img_width + $crop_w/2)) - $crop_w*3/4);
3377 my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
3379 # if we have chosen to paste the image outside of the rectangle of the
3380 # screen, then we need to crop it.
3384 $x + $crop_w > $img_width ||
3385 $y + $crop_h > $img_height) {
3388 "cropping for effective paste of ${crop_w}x$crop_h \@ $x,$y");
3390 if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
3391 if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
3393 if ($x + $crop_w >= $img_width) { $crop_w = $img_width - $x - 1; }
3394 if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
3397 # If any cropping needs to happen, add pnmcut.
3399 if ($crop_x != 0 || $crop_y != 0 ||
3400 $crop_w != $iw || $crop_h != $ih) {
3403 $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
3404 LOG ($verbose_pbm, "cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
3407 LOG ($verbose_pbm, "pasting ${iw}x$ih \@ $x,$y in $image_ppm");
3409 $cmd .= " | pnmpaste - $x $y $image_ppm";
3411 $cmd =~ s@^ *\| *@@;
3413 if (defined ($webcollage_helper)) {
3414 $cmd = "$webcollage_helper $image_tmp1 $image_ppm " .
3415 "$scale $opacity " .
3416 "$crop_x $crop_y $x $y " .
3421 # use a PPM pipeline
3423 $_ .= " < $image_tmp1 > $image_tmp2";
3427 $_ = "($_) 2>&1 | sed s'/^/" . blurb() . "/'";
3429 $_ .= " 2> /dev/null";
3432 my $rc = nontrapping_system ($_);
3434 if (defined ($webcollage_helper) && -z $image_ppm) {
3435 LOG (1, "failed command: \"$cmd\"");
3436 print STDERR "\naudit log:\n\n\n";
3437 print STDERR ("#" x 78) . "\n";
3438 print STDERR blurb() . "$image_ppm has zero size\n";
3440 print STDERR "\n\n";
3445 LOG (($verbose_pbm || $verbose_load), "failed command: \"$cmd\"");
3446 LOG (($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
3450 if (!defined ($webcollage_helper)) {
3451 rename ($image_tmp2, $image_ppm) || return;
3454 my $target = "$image_ppm";
3456 # don't just tack this onto the end of the pipeline -- we don't want it
3457 # to end up in $image_ppm, because we don't want the results to be
3460 if ($post_filter_cmd) {
3464 $target = $image_tmp1;
3465 if (!defined ($webcollage_helper)) {
3466 $cmd = "($post_filter_cmd) < $image_ppm > $target";
3468 # Blah, my scripts need the JPEG data, but some other folks need
3469 # the PPM data -- what to do? Ignore the problem, that's what!
3470 # $cmd = "djpeg < $image_ppm | ($post_filter_cmd) > $target";
3471 $cmd = "($post_filter_cmd) < $image_ppm > $target";
3474 $rc = nontrapping_system ($cmd);
3476 LOG ($verbose_pbm, "filter failed: \"$post_filter_cmd\"\n");
3481 if (!$no_output_p) {
3482 my $tsize = (stat($target))[7];
3484 $cmd = "$ppm_to_root_window_cmd $target";
3486 # xv seems to hate being killed. it tends to forget to clean
3487 # up after itself, and leaves windows around and colors allocated.
3488 # I had this same problem with vidwhacker, and I'm not entirely
3489 # sure what I did to fix it. But, let's try this: launch xv
3490 # in the background, so that killing this process doesn't kill it.
3491 # it will die of its own accord soon enough. So this means we
3492 # start pumping bits to the root window in parallel with starting
3493 # the next network retrieval, which is probably a better thing
3496 $cmd .= " &" unless ($cocoa_p);
3498 $rc = nontrapping_system ($cmd);
3501 LOG (($verbose_pbm || $verbose_load), "display failed: \"$cmd\"");
3506 LOG ($verbose_pbm, "$target size is $tsize");
3510 $source .= "-" . stats_of($source);
3511 print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n"
3512 if ($verbose_imgmap);
3513 if ($imagemap_base) {
3514 update_imagemap ($base, $x, $y, $iw, $ih,
3515 $image_ppm, $img_width, $img_height);
3524 sub update_imagemap($$$$$$$$) {
3525 my ($url, $x, $y, $w, $h, $image_ppm, $image_width, $image_height) = @_;
3527 $current_state = "imagemap";
3529 my $max_areas = 200;
3531 $url = html_quote ($url);
3534 my $area = "<AREA SHAPE=RECT COORDS=\"$x,$y,$x2,$y2\" HREF=\"$url\">";
3535 unshift @imagemap_areas, $area; # put one on the front
3536 if ($#imagemap_areas >= $max_areas) {
3537 pop @imagemap_areas; # take one off the back.
3540 LOG ($verbose_pbm, "area: $x,$y,$x2,$y2 (${w}x$h)");
3542 my $map_name = $imagemap_base;
3543 $map_name =~ s@^.*/@@;
3544 $map_name = 'collage' if ($map_name eq '');
3546 my $imagemap_html = $imagemap_base . ".html";
3547 my $imagemap_jpg = $imagemap_base . ".jpg";
3549 if (!defined ($imagemap_html_tmp)) {
3550 $imagemap_html_tmp = $imagemap_html . sprintf (".%08x", rand(0xffffffff));
3551 $imagemap_jpg_tmp = $imagemap_jpg . sprintf (".%08x", rand(0xffffffff));
3554 # Read the imagemap html file (if any) to get a template.
3556 my $template_html = '';
3558 if (open (my $in, '<', $imagemap_html)) {
3559 local $/ = undef; # read entire file
3560 $template_html = <$in>;
3562 LOG ($verbose_pbm, "read template $imagemap_html");
3565 if ($template_html =~ m/^\s*$/s) {
3566 $template_html = ("<MAP NAME=\"$map_name\"></MAP>\n" .
3567 "<IMG SRC=\"$imagemap_base.jpg\"" .
3568 " USEMAP=\"$map_name\">\n");
3569 LOG ($verbose_pbm, "created dummy template");
3573 # Write the jpg to a tmp file
3577 if (defined ($webcollage_helper)) {
3578 $cmd = "cp -p $image_ppm $imagemap_jpg_tmp";
3580 $cmd = "cjpeg < $image_ppm > $imagemap_jpg_tmp";
3582 my $rc = nontrapping_system ($cmd);
3584 error ("imagemap jpeg failed: \"$cmd\"\n");
3588 # Write the html to a tmp file
3591 my $body = $template_html;
3592 my $areas = join ("\n\t", @imagemap_areas);
3593 my $map = ("<MAP NAME=\"$map_name\">\n\t$areas\n</MAP>");
3594 my $img = ("<IMG SRC=\"$imagemap_base.jpg\" " .
3596 "WIDTH=$image_width HEIGHT=$image_height " .
3597 "USEMAP=\"#$map_name\">");
3598 $body =~ s@(<MAP\s+NAME=\"[^\"]*\"\s*>).*?(</MAP>)@$map@is;
3599 $body =~ s@<IMG\b[^<>]*\bUSEMAP\b[^<>]*>@$img@is;
3601 # if there are magic webcollage spans in the html, update those too.
3604 my @st = stat ($imagemap_jpg_tmp);
3605 my $date = strftime("%d-%b-%Y %l:%M:%S %p %Z", localtime($st[9]));
3606 my $size = int(($st[7] / 1024) + 0.5) . "K";
3607 $body =~ s@(<SPAN\s+CLASS=\"webcollage_date\">).*?(</SPAN>)@$1$date$2@si;
3608 $body =~ s@(<SPAN\s+CLASS=\"webcollage_size\">).*?(</SPAN>)@$1$size$2@si;
3611 open (my $out, '>', $imagemap_html_tmp) || error ("$imagemap_html_tmp: $!");
3612 (print $out $body) || error ("$imagemap_html_tmp: $!");
3613 close ($out) || error ("$imagemap_html_tmp: $!");
3614 LOG ($verbose_pbm, "wrote $imagemap_html_tmp");
3617 # Rename the two tmp files to the real files
3619 rename ($imagemap_html_tmp, $imagemap_html) ||
3620 error "renaming $imagemap_html_tmp to $imagemap_html";
3621 LOG ($verbose_pbm, "wrote $imagemap_html");
3622 rename ($imagemap_jpg_tmp, $imagemap_jpg) ||
3623 error "renaming $imagemap_jpg_tmp to $imagemap_jpg";
3624 LOG ($verbose_pbm, "wrote $imagemap_jpg");
3628 # Figure out what the proxy server should be, either from environment
3629 # variables or by parsing the output of the (MacOS) program "scutil",
3630 # which tells us what the system-wide proxy settings are.
3634 if (! $http_proxy) {
3635 # historical suckage: the environment variable name is lower case.
3636 $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
3639 if (defined ($http_proxy)) {
3640 if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
3641 # historical suckage: allow "http://host:port" as well as "host:port".
3646 my $proxy_data = `scutil --proxy 2>/dev/null`;
3647 my ($server) = ($proxy_data =~ m/\bHTTPProxy\s*:\s*([^\s]+)/s);
3648 my ($port) = ($proxy_data =~ m/\bHTTPPort\s*:\s*([^\s]+)/s);
3649 # Note: this ignores the "ExceptionsList".
3651 $http_proxy = $server;
3652 $http_proxy .= ":$port" if $port;
3657 LOG ($verbose_net, "proxy server: $http_proxy");
3662 sub init_signals() {
3664 $SIG{HUP} = \&signal_cleanup;
3665 $SIG{INT} = \&signal_cleanup;
3666 $SIG{QUIT} = \&signal_cleanup;
3667 $SIG{ABRT} = \&signal_cleanup;
3668 $SIG{KILL} = \&signal_cleanup;
3669 $SIG{TERM} = \&signal_cleanup;
3671 # Need this so that if giftopnm dies, we don't die.
3672 $SIG{PIPE} = 'IGNORE';
3675 END { exit_cleanup(); }
3684 my $driftnet_cmd = 0;
3686 $current_state = "init";
3687 $load_method = "none";
3690 my $window_id = undef;
3692 while ($_ = $ARGV[0]) {
3694 if ($_ eq "-display" ||
3700 $ENV{DISPLAY} = shift @ARGV;
3701 } elsif ($_ eq "-root") {
3703 } elsif ($_ eq "-window-id" || $_ eq "--window-id") {
3704 $window_id = shift @ARGV;
3706 } elsif ($_ eq "-no-output") {
3708 } elsif ($_ eq "-urls-only") {
3711 } elsif ($_ eq "-cocoa") {
3713 } elsif ($_ eq "-imagemap") {
3714 $imagemap_base = shift @ARGV;
3716 } elsif ($_ eq "-verbose") {
3718 } elsif (m/^-v+$/) {
3719 $verbose += length($_)-1;
3720 } elsif ($_ eq "-delay") {
3721 $delay = shift @ARGV;
3722 } elsif ($_ eq "-timeout") {
3723 $http_timeout = shift @ARGV;
3724 } elsif ($_ eq "-filter") {
3725 $filter_cmd = shift @ARGV;
3726 } elsif ($_ eq "-filter2") {
3727 $post_filter_cmd = shift @ARGV;
3728 } elsif ($_ eq "-background" || $_ eq "-bg") {
3729 $background = shift @ARGV;
3730 } elsif ($_ eq "-size") {
3732 if (m@^(\d+)x(\d+)$@) {
3736 error "argument to \"-size\" must be of the form \"640x400\"";
3738 } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
3739 $http_proxy = shift @ARGV;
3740 } elsif ($_ eq "-dictionary" || $_ eq "-dict") {
3741 $dict = shift @ARGV;
3742 } elsif ($_ eq "-opacity") {
3743 $opacity = shift @ARGV;
3744 error ("opacity must be between 0.0 and 1.0")
3745 if ($opacity <= 0 || $opacity > 1);
3746 } elsif ($_ eq "-driftnet" || $_ eq "--driftnet") {
3747 @search_methods = ( 100, "driftnet", \&pick_from_driftnet );
3748 if (! ($ARGV[0] =~ m/^-/)) {
3749 $driftnet_cmd = shift @ARGV;
3751 $driftnet_cmd = $default_driftnet_cmd;
3753 } elsif ($_ eq "-directory" || $_ eq "--directory") {
3754 @search_methods = ( 100, "local", \&pick_from_local_dir );
3755 if (! ($ARGV[0] =~ m/^-/)) {
3756 $local_dir = shift @ARGV;
3758 error ("local directory path must be set")
3760 } elsif ($_ eq "-fps") {
3761 # -fps only works on MacOS, via "webcollage-cocoa.m".
3762 # Ignore it if passed to this script in an X11 context.
3763 } elsif ($_ eq "-debug" || $_ eq "--debug") {
3764 my $which = shift @ARGV;
3765 my @rest = @search_methods;
3768 my $pct = shift @rest;
3769 my $name = shift @rest;
3770 my $tfn = shift @rest;
3772 if ($name eq $which) {
3773 @search_methods = (100, $name, $tfn);
3778 error "no such search method as \"$which\"" unless ($ok);
3779 LOG (1, "DEBUG: using only \"$which\"");
3782 print STDERR "$copyright\nusage: $progname " .
3783 "[-root] [-display dpy] [-verbose] [-debug which]\n" .
3784 "\t\t [-timeout secs] [-delay secs] [-size WxH]\n" .
3785 "\t\t [-no-output] [-urls-only] [-imagemap filename]\n" .
3786 "\t\t [-background color] [-opacity f]\n" .
3787 "\t\t [-filter cmd] [-filter2 cmd]\n" .
3788 "\t\t [-dictionary dictionary-file] [-http-proxy host[:port]]\n" .
3789 "\t\t [-driftnet [driftnet-program-and-args]]\n" .
3790 "\t\t [-directory local-image-directory]\n" .
3796 if (!$root_p && !$no_output_p && !$cocoa_p) {
3797 print STDERR $copyright;
3798 error "the -root argument is mandatory (for now.)";
3801 if (!$no_output_p && !$cocoa_p && !$ENV{DISPLAY}) {
3802 error "\$DISPLAY is not set.";
3806 if ($verbose == 1) {
3807 $verbose_imgmap = 1;
3808 $verbose_warnings = 1;
3810 } elsif ($verbose == 2) {
3811 $verbose_imgmap = 1;
3812 $verbose_warnings = 1;
3815 } elsif ($verbose == 3) {
3816 $verbose_imgmap = 1;
3817 $verbose_warnings = 1;
3819 $verbose_filter = 1;
3821 } elsif ($verbose == 4) {
3822 $verbose_imgmap = 1;
3823 $verbose_warnings = 1;
3825 $verbose_filter = 1;
3828 } elsif ($verbose == 5) {
3829 $verbose_imgmap = 1;
3830 $verbose_warnings = 1;
3832 $verbose_filter = 1;
3836 } elsif ($verbose == 6) {
3837 $verbose_imgmap = 1;
3838 $verbose_warnings = 1;
3840 $verbose_filter = 1;
3845 } elsif ($verbose >= 7) {
3846 $verbose_imgmap = 1;
3847 $verbose_warnings = 1;
3849 $verbose_filter = 1;
3857 error ("$dict does not exist") unless (-f $dict);
3863 if ($imagemap_base && !($img_width && $img_height)) {
3864 error ("-size WxH is required with -imagemap");
3867 if (defined ($local_dir)) {
3868 $_ = "xscreensaver-getimage-file";
3869 which ($_) || error "$_ not found on \$PATH.";
3875 spawn_driftnet ($driftnet_cmd) if ($driftnet_cmd);
3880 x_or_pbm_output ($window_id);