X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?p=xscreensaver;a=blobdiff_plain;f=hacks%2Fwebcollage;h=e606655ec8e6b9db09162499ef1bafff151ca45b;hp=733cae5c6ccf531c86712ed6380980939991ec8d;hb=9c9d475ff889ed8be02e8ce8c17da28b93278fca;hpb=e4fa2ac140f7bc56571373a7b7eb585fa4500e38 diff --git a/hacks/webcollage b/hacks/webcollage index 733cae5c..e606655e 100755 --- a/hacks/webcollage +++ b/hacks/webcollage @@ -1,6 +1,6 @@ #!/usr/bin/perl -w # -# webcollage, Copyright (c) 1999-2003 by Jamie Zawinski +# webcollage, Copyright (c) 1999-2004 by Jamie Zawinski # This program decorates the screen with random images from the web. # One satisfied customer described it as "a nonstop pop culture brainbath." # @@ -17,8 +17,15 @@ # # webcollage -root # webcollage -root -filter 'vidwhacker -stdin -stdout' - - +# +# +# You can see this in action at http://www.jwz.org/webcollage/ -- +# it auto-reloads about once a minute. To make a page similar to +# that on your own system, do this: +# +# webcollage -size '800x600' -imagemap $HOME/www/webcollage/index +# +# # If you have the "driftnet" program installed, webcollage can display a # collage of images sniffed off your local ethernet, instead of pulled out # of search engines: in that way, your screensaver can display the images @@ -53,16 +60,17 @@ use bytes; # Larry can take Unicode and shove it up his ass sideways. my $progname = $0; $progname =~ s@.*/@@g; -my $version = q{ $Revision: 1.108 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; -my $copyright = "WebCollage $version, Copyright (c) 1999-2002" . +my $version = q{ $Revision: 1.114 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; +my $copyright = "WebCollage $version, Copyright (c) 1999-2004" . " Jamie Zawinski \n" . - " http://www.jwz.org/xscreensaver/\n"; + " http://www.jwz.org/webcollage/\n"; -my @search_methods = ( 77, "altavista", \&pick_from_alta_vista_random_link, - 14, "yahoorand", \&pick_from_yahoo_random_link, - 9, "yahoonews", \&pick_from_yahoo_news_text, +my @search_methods = ( 72, "altavista", \&pick_from_alta_vista_random_link, + 10, "livejournal", \&pick_from_livejournal_images, + 10, "yahoorand", \&pick_from_yahoo_random_link, + 8, "yahoonews", \&pick_from_yahoo_news_text, # Alta Vista has a new "random link" URL now. # They added it specifically to better support webcollage! @@ -214,6 +222,7 @@ my $min_gif_area = (120 * 120); my $no_output_p = 0; my $urls_only_p = 0; +my $imagemap_base = undef; my @pids_to_kill = (); # forked pids we should kill when we exit, if any. @@ -261,6 +270,11 @@ sub get_document_1 { my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4); $path = "" unless $path; + if (!$url_proto || !$serverstring) { + LOG (($verbose_net || $verbose_load), "unparsable URL: $url"); + return (); + } + my ($them,$port) = split(/:/, $serverstring); $port = 80 unless $port; @@ -548,9 +562,9 @@ sub pick_image_from_body { # randomly from the set of images on the web. All the logic here for # rejecting some images is really a set of heuristics for rejecting # images that are not really images: for rejecting *text* that is in - # GIF/JPEG form. I don't want text, I want pictures, and I want the - # content of the pictures to be randomly selected from among all the - # available content. + # GIF/JPEG/PNG form. I don't want text, I want pictures, and I want + # the content of the pictures to be randomly selected from among all + # the available content. # # So, filtering out "dirty" pictures by looking for "dirty" keywords # would be wrong: dirty pictures exist, like it or not, so webcollage @@ -630,7 +644,7 @@ sub pick_image_from_body { } # skip non-image - if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg)$@io ) { + if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg|png)$@io ) { next; } @@ -679,9 +693,9 @@ sub pick_image_from_body { $urls[++$#urls] = $url; $unique_urls{$url}++; - # jpegs are preferable to gifs. + # JPEGs are preferable to GIFs and PNGs. $_ = $url; - if ( ! m@[.]gif$@io ) { + if ( ! m@[.](gif|png)$@io ) { $urls[++$#urls] = $url; } @@ -797,6 +811,24 @@ sub url_unquote { return $s; } +sub html_quote { + my ($s) = @_; + $s =~ s/&/&/gi; + $s =~ s//>/gi; + $s =~ s/\"/"/gi; + return $s; +} + +sub html_unquote { + my ($s) = @_; + $s =~ s/</@i) ? " (looks like HTML)" : "") . ": $url"); @@ -2155,7 +2262,9 @@ sub x_or_pbm_output { if (!defined($webcollage_helper)) { # Only need these others if we don't have the helper. - @progs = (@progs, "giftopnm", "djpeg", "pnmpaste", "pnmscale", "pnmcut"); + @progs = (@progs, + "giftopnm", "pngtopnm", "djpeg", + "pnmpaste", "pnmscale", "pnmcut"); } foreach (@progs) { @@ -2221,13 +2330,17 @@ sub x_or_pbm_output { ($iw, $ih) = @_; $cmd = "djpeg |"; + } elsif ((@_ = png_size ($body))) { + ($iw, $ih) = @_; + $cmd = "pngtopnm |"; + } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) { $iw = $1; $ih = $2; $cmd = ""; } else { - error "$bgimage is not a GIF, JPEG, or PPM."; + error "$bgimage is not a GIF, JPEG, PNG, or PPM."; } my $x = int (($img_width - $iw) / 2); @@ -2288,7 +2401,7 @@ sub paste_image { ($iw, $ih) = image_size ($body); if (!$iw || !$ih) { LOG (($verbose_pbm || $verbose_load), - "not a GIF or JPG" . + "not a GIF, JPG, or PNG" . (($body =~ m@<(base|html|head|body|script|table|a href)>@i) ? " (looks like HTML)" : "") . ": $img"); @@ -2341,7 +2454,7 @@ sub paste_image { return 0 unless ($iw && $ih); } - my $target_w = $img_width; + my $target_w = $img_width; # max rectangle into which the image must fit my $target_h = $img_height; my $cmd = ""; @@ -2349,25 +2462,27 @@ sub paste_image { # Usually scale the image to fit on the screen -- but sometimes scale it - # to fit on half or a quarter of the screen. Note that we don't merely - # scale it to fit, we instead cut it in half until it fits -- that should - # give a wider distribution of sizes. + # to fit on half or a quarter of the screen. (We do this by reducing the + # size of the target rectangle.) Note that the image is not merely scaled + # to fit; we instead cut the image in half repeatedly until it fits in the + # target rectangle -- that gives a wider distribution of sizes. # - if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; } - if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; } + if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; } # reduce target rect + if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; } if ($iw > $target_w || $ih > $target_h) { while ($iw > $target_w || $ih > $target_h) { $iw = int($iw / 2); $ih = int($ih / 2); + $scale /= 2; } if ($iw <= 10 || $ih <= 10) { LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus."); return 0; } - LOG ($verbose_pbm, "scaling to ${iw}x$ih"); + LOG ($verbose_pbm, "scaling to ${iw}x$ih ($scale)"); $cmd .= " | pnmscale -xsize $iw -ysize $ih"; } @@ -2454,7 +2569,7 @@ sub paste_image { # If any cropping needs to happen, add pnmcut. # if ($crop_x != 0 || $crop_y != 0 || - $crop_w != $iw || $crop_h != $ih) { + $crop_w != $iw || $crop_h != $ih) { $iw = $crop_w; $ih = $crop_h; $cmd .= " | pnmcut $crop_x $crop_y $iw $ih"; @@ -2568,12 +2683,118 @@ sub paste_image { print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n" if ($verbose_imgmap); + if ($imagemap_base) { + update_imagemap ($base, $x, $y, $iw, $ih, + $image_ppm, $img_width, $img_height); + } + clearlog(); return 1; } +sub update_imagemap { + my ($url, $x, $y, $w, $h, $image_ppm, $image_width, $image_height) = @_; + + $current_state = "imagemap"; + + my $max_areas = 200; + + $url = html_quote ($url); + my $x2 = $x + $w; + my $y2 = $y + $h; + my $area = ""; + unshift @imagemap_areas, $area; # put one on the front + if ($#imagemap_areas >= $max_areas) { + pop @imagemap_areas; # take one off the back. + } + + LOG ($verbose_pbm, "area: $x,$y,$x2,$y2 (${w}x$h)"); + + my $map_name = $imagemap_base; + $map_name =~ s@^.*/@@; + $map_name = 'collage' if ($map_name eq ''); + + my $imagemap_html = $imagemap_base . ".html"; + my $imagemap_jpg = $imagemap_base . ".jpg"; + + if (!defined ($imagemap_html_tmp)) { + $imagemap_html_tmp = $imagemap_html . sprintf (".%08x", rand(0xffffffff)); + $imagemap_jpg_tmp = $imagemap_jpg . sprintf (".%08x", rand(0xffffffff)); + } + + # Read the imagemap html file (if any) to get a template. + # + my $template_html = ''; + { + local *IN; + if (open (IN, "<$imagemap_html")) { + while () { $template_html .= $_; } + close IN; + LOG ($verbose_pbm, "read template $imagemap_html"); + } + + if ($template_html =~ m/^\s*$/s) { + $template_html = ("\n" . + "\n"); + LOG ($verbose_pbm, "created dummy template"); + } + } + + # Write the jpg to a tmp file + # + { + #my $cmd = "cjpeg < $image_ppm > $imagemap_jpg_tmp"; + my $cmd = "cp -p $image_ppm $imagemap_jpg_tmp"; + my $rc = nontrapping_system ($cmd); + if ($rc != 0) { + error ("imagemap jpeg failed: \"$cmd\"\n"); + } + } + + # Write the html to a tmp file + # + { + my $body = $template_html; + my $areas = join ("\n\t", @imagemap_areas); + my $map = ("\n\t$areas\n"); + my $img = (""); + $body =~ s@().*?()@$map@is; + $body =~ s@]*\bUSEMAP\b[^<>]*>@$img@is; + + # if there are magic webcollage spans in the html, update those too. + # + { + my @st = stat ($imagemap_jpg_tmp); + my $date = strftime("%d-%b-%Y %l:%M:%S %p %Z", localtime($st[9])); + my $size = int(($st[7] / 1024) + 0.5) . "K"; + $body =~ s@().*?()@$1$date$2@si; + $body =~ s@().*?()@$1$size$2@si; + } + + local *OUT; + open (OUT, ">$imagemap_html_tmp") || error ("$imagemap_html_tmp: $!"); + print OUT $body || error ("$imagemap_html_tmp: $!"); + close OUT || error ("$imagemap_html_tmp: $!"); + LOG ($verbose_pbm, "wrote $imagemap_html_tmp"); + } + + # Rename the two tmp files to the real files + # + rename ($imagemap_html_tmp, $imagemap_html) || + error "renaming $imagemap_html_tmp to $imagemap_html"; + LOG ($verbose_pbm, "wrote $imagemap_html"); + rename ($imagemap_jpg_tmp, $imagemap_jpg) || + error "renaming $imagemap_jpg_tmp to $imagemap_jpg"; + LOG ($verbose_pbm, "wrote $imagemap_jpg"); +} + + sub init_signals { $SIG{HUP} = \&signal_cleanup; @@ -2622,6 +2843,9 @@ sub main { } elsif ($_ eq "-urls-only") { $urls_only_p = 1; $no_output_p = 1; + } elsif ($_ eq "-imagemap") { + $imagemap_base = shift @ARGV; + $no_output_p = 1; } elsif ($_ eq "-verbose") { $verbose++; } elsif (m/^-v+$/) { @@ -2676,8 +2900,9 @@ sub main { } else { print STDERR "$copyright\nusage: $progname " . "[-root] [-display dpy] [-verbose] [-debug which]\n" . - "\t\t [-timeout secs] [-delay secs] [-filter cmd] [-filter2 cmd]\n" . - "\t\t [-no-output] [-urls-only] [-background color] [-size WxH]\n" . + "\t\t [-timeout secs] [-delay secs] [-size WxH]\n" . + "\t\t [-no-output] [-urls-only] [-imagemap filename]\n" . + "\t\t [-filter cmd] [-filter2 cmd] [-background color]\n" . "\t\t [-dictionary dictionary-file] [-http-proxy host[:port]]\n" . "\t\t [-driftnet [driftnet-program-and-args]]\n" . "\n";