From http://www.jwz.org/xscreensaver/xscreensaver-5.23.tar.gz
[xscreensaver] / hacks / webcollage
index 9bfe771ffb6baf703d2c4fee796ae7fee8039e06..e1055979fc140b4bd729c07031508efc5a3432c1 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 #
-# webcollage, Copyright (c) 1999-2011 by Jamie Zawinski <jwz@jwz.org>
+# webcollage, Copyright © 1999-2013 by Jamie Zawinski <jwz@jwz.org>
 # This program decorates the screen with random images from the web.
 # One satisfied customer described it as "a nonstop pop culture brainbath."
 #
@@ -60,24 +60,25 @@ use bytes;  # Larry can take Unicode and shove it up his ass sideways.
 
 
 my $progname = $0; $progname =~ s@.*/@@g;
-my $version = q{ $Revision: 1.156 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
+my $version = q{ $Revision: 1.160 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
 my $copyright = "WebCollage $version, Copyright (c) 1999-2011" .
     " Jamie Zawinski <jwz\@jwz.org>\n" .
     "            http://www.jwz.org/webcollage/\n";
 
 
 
-my @search_methods = ( 24, "googlephotos",  \&pick_from_google_image_photos,
-                       13, "googleimgs",    \&pick_from_google_images,
-                       13, "googlenums",    \&pick_from_google_image_numbers,
-                       16, "flickr_recent", \&pick_from_flickr_recent,
-                       13, "flickr_random", \&pick_from_flickr_random,
-                       10, "twitpic",       \&pick_from_twitpic_images,
-                        8, "livejournal",   \&pick_from_livejournal_images,
-                        3, "yahoorand",     \&pick_from_yahoo_random_link,
+my @search_methods = ( 26, "googlephotos",  \&pick_from_google_image_photos,
+                       15, "googleimgs",    \&pick_from_google_images,
+                       15, "googlenums",    \&pick_from_google_image_numbers,
+                       17, "flickr_recent", \&pick_from_flickr_recent,
+                       14, "flickr_random", \&pick_from_flickr_random,
+# twitpic went stale. don't have time to fix it right now.
+#                       10, "twitpic",       \&pick_from_twitpic_images,
+                        9, "livejournal",   \&pick_from_livejournal_images,
+                        4, "yahoorand",     \&pick_from_yahoo_random_link,
 
                      # This one doesn't work very well: too many non-img links.
-                       0, "twitter",       \&pick_from_twitter_images,
+                        0, "twitter",       \&pick_from_twitter_images,
 
                      # This is a cute way to search for a certain webcams.
                      # Not included in default methods, since these images
@@ -230,6 +231,7 @@ my %warningless_sites = (
   "ljplus.ru"               => 1,
   "yandex.ru"               => 1,
   "imgur.com"               => 1,
+  "yfrog.com"               => 1,
 
   "yimg.com"                => 1,  # This is where dailynews.yahoo.com stores
   "eimg.com"                => 1,  # its images, so pick_from_yahoo_news_text()
@@ -248,44 +250,53 @@ my %warningless_sites = (
 #
 my %entity_table = (
    "apos"   => '\'',
-   "quot"   => '"', "amp"    => '&', "lt"     => '<', "gt"     => '>',
-   "nbsp"   => ' ', "iexcl"  => '¡', "cent"   => '¢', "pound"  => '£',
-   "curren" => '¤', "yen"    => '¥', "brvbar" => '¦', "sect"   => '§',
-   "uml"    => '¨', "copy"   => '©', "ordf"   => 'ª', "laquo"  => '«',
-   "not"    => '¬', "shy"    => '­', "reg"    => '®', "macr"   => '¯',
-   "deg"    => '°', "plusmn" => '±', "sup2"   => '²', "sup3"   => '³',
-   "acute"  => '´', "micro"  => 'µ', "para"   => '¶', "middot" => '·',
-   "cedil"  => '¸', "sup1"   => '¹', "ordm"   => 'º', "raquo"  => '»',
-   "frac14" => '¼', "frac12" => '½', "frac34" => '¾', "iquest" => '¿',
-   "Agrave" => 'À', "Aacute" => 'Á', "Acirc"  => 'Â', "Atilde" => 'Ã',
-   "Auml"   => 'Ä', "Aring"  => 'Å', "AElig"  => 'Æ', "Ccedil" => 'Ç',
-   "Egrave" => 'È', "Eacute" => 'É', "Ecirc"  => 'Ê', "Euml"   => 'Ë',
-   "Igrave" => 'Ì', "Iacute" => 'Í', "Icirc"  => 'Î', "Iuml"   => 'Ï',
-   "ETH"    => 'Ð', "Ntilde" => 'Ñ', "Ograve" => 'Ò', "Oacute" => 'Ó',
-   "Ocirc"  => 'Ô', "Otilde" => 'Õ', "Ouml"   => 'Ö', "times"  => '×',
-   "Oslash" => 'Ø', "Ugrave" => 'Ù', "Uacute" => 'Ú', "Ucirc"  => 'Û',
-   "Uuml"   => 'Ü', "Yacute" => 'Ý', "THORN"  => 'Þ', "szlig"  => 'ß',
-   "agrave" => 'à', "aacute" => 'á', "acirc"  => 'â', "atilde" => 'ã',
-   "auml"   => 'ä', "aring"  => 'å', "aelig"  => 'æ', "ccedil" => 'ç',
-   "egrave" => 'è', "eacute" => 'é', "ecirc"  => 'ê', "euml"   => 'ë',
-   "igrave" => 'ì', "iacute" => 'í', "icirc"  => 'î', "iuml"   => 'ï',
-   "eth"    => 'ð', "ntilde" => 'ñ', "ograve" => 'ò', "oacute" => 'ó',
-   "ocirc"  => 'ô', "otilde" => 'õ', "ouml"   => 'ö', "divide" => '÷',
-   "oslash" => 'ø', "ugrave" => 'ù', "uacute" => 'ú', "ucirc"  => 'û',
-   "uuml"   => 'ü', "yacute" => 'ý', "thorn"  => 'þ', "yuml"   => 'ÿ',
+   "quot"   => '"',    "amp"    => '&',    "lt"     => '<',
+   "gt"     => '>',    "nbsp"   => ' ',    "iexcl"  => '',
+   "cent"   => "\xA2", "pound"  => "\xA3", "curren" => "\xA4",
+   "yen"    => "\xA5", "brvbar" => "\xA6", "sect"   => "\xA7",
+   "uml"    => "\xA8", "copy"   => "\xA9", "ordf"   => "\xAA",
+   "laquo"  => "\xAB", "not"    => "\xAC", "shy"    => "\xAD",
+   "reg"    => "\xAE", "macr"   => "\xAF", "deg"    => "\xB0",
+   "plusmn" => "\xB1", "sup2"   => "\xB2", "sup3"   => "\xB3",
+   "acute"  => "\xB4", "micro"  => "\xB5", "para"   => "\xB6",
+   "middot" => "\xB7", "cedil"  => "\xB8", "sup1"   => "\xB9",
+   "ordm"   => "\xBA", "raquo"  => "\xBB", "frac14" => "\xBC",
+   "frac12" => "\xBD", "frac34" => "\xBE", "iquest" => "\xBF",
+   "Agrave" => "\xC0", "Aacute" => "\xC1", "Acirc"  => "\xC2",
+   "Atilde" => "\xC3", "Auml"   => "\xC4", "Aring"  => "\xC5",
+   "AElig"  => "\xC6", "Ccedil" => "\xC7", "Egrave" => "\xC8",
+   "Eacute" => "\xC9", "Ecirc"  => "\xCA", "Euml"   => "\xCB",
+   "Igrave" => "\xCC", "Iacute" => "\xCD", "Icirc"  => "\xCE",
+   "Iuml"   => "\xCF", "ETH"    => "\xD0", "Ntilde" => "\xD1",
+   "Ograve" => "\xD2", "Oacute" => "\xD3", "Ocirc"  => "\xD4",
+   "Otilde" => "\xD5", "Ouml"   => "\xD6", "times"  => "\xD7",
+   "Oslash" => "\xD8", "Ugrave" => "\xD9", "Uacute" => "\xDA",
+   "Ucirc"  => "\xDB", "Uuml"   => "\xDC", "Yacute" => "\xDD",
+   "THORN"  => "\xDE", "szlig"  => "\xDF", "agrave" => "\xE0",
+   "aacute" => "\xE1", "acirc"  => "\xE2", "atilde" => "\xE3",
+   "auml"   => "\xE4", "aring"  => "\xE5", "aelig"  => "\xE6",
+   "ccedil" => "\xE7", "egrave" => "\xE8", "eacute" => "\xE9",
+   "ecirc"  => "\xEA", "euml"   => "\xEB", "igrave" => "\xEC",
+   "iacute" => "\xED", "icirc"  => "\xEE", "iuml"   => "\xEF",
+   "eth"    => "\xF0", "ntilde" => "\xF1", "ograve" => "\xF2",
+   "oacute" => "\xF3", "ocirc"  => "\xF4", "otilde" => "\xF5",
+   "ouml"   => "\xF6", "divide" => "\xF7", "oslash" => "\xF8",
+   "ugrave" => "\xF9", "uacute" => "\xFA", "ucirc"  => "\xFB",
+   "uuml"   => "\xFC", "yacute" => "\xFD", "thorn"  => "\xFE",
+   "yuml"   => "\xFF",
 
    # HTML 4 entities that do not have 1:1 Latin1 mappings.
-   "bull"  => "*",   "hellip"=> "...",  "prime" => "'",  "Prime" => "\"",
-   "frasl" => "/",   "trade" => "[tm]", "larr"  => "<-", "rarr"  => "->",
-   "harr"  => "<->", "lArr"  => "<=",   "rArr"  => "=>", "hArr"  => "<=>",
-   "empty" => "Ø",   "minus" => "-",    "lowast"=> "*",  "sim"   => "~",
-   "cong"  => "=~",  "asymp" => "~",    "ne"    => "!=", "equiv" => "==",
-   "le"    => "<=",  "ge"    => ">=",   "lang"  => "<",  "rang"  => ">",
-   "loz"   => "<>",  "OElig" => "OE",   "oelig" => "oe", "Yuml"  => "Y",
-   "circ"  => "^",   "tilde" => "~",    "ensp"  => " ",  "emsp"  => " ",
-   "thinsp"=> " ",   "ndash" => "-",    "mdash" => "--", "lsquo" => "`",
-   "rsquo" => "'",   "sbquo" => "'",    "ldquo" => "\"", "rdquo" => "\"",
-   "bdquo" => "\"",  "lsaquo"=> "<",    "rsaquo"=> ">",
+   "bull"  => "*",    "hellip"=> "...",  "prime" => "'",  "Prime" => "\"",
+   "frasl" => "/",    "trade" => "[tm]", "larr"  => "<-", "rarr"  => "->",
+   "harr"  => "<->",  "lArr"  => "<=",   "rArr"  => "=>", "hArr"  => "<=>",
+   "empty" => "\xD8", "minus" => "-",    "lowast"=> "*",  "sim"   => "~",
+   "cong"  => "=~",   "asymp" => "~",    "ne"    => "!=", "equiv" => "==",
+   "le"    => "<=",   "ge"    => ">=",   "lang"  => "<",  "rang"  => ">",
+   "loz"   => "<>",   "OElig" => "OE",   "oelig" => "oe", "Yuml"  => "Y",
+   "circ"  => "^",    "tilde" => "~",    "ensp"  => " ",  "emsp"  => " ",
+   "thinsp"=> " ",    "ndash" => "-",    "mdash" => "--", "lsquo" => "`",
+   "rsquo" => "'",    "sbquo" => "'",    "ldquo" => "\"", "rdquo" => "\"",
+   "bdquo" => "\"",   "lsaquo"=> "<",    "rsaquo"=> ">",
 );
 
 
@@ -1974,6 +1985,8 @@ sub pick_from_twitpic_images($) {
 
     $img = $src;
 
+    $img = "http:$img" if ($img =~ m@^//@s);  # Oh come on
+
     # Sometimes these images are hosted on twitpic, sometimes on Amazon.
     if ($img =~ m@^/@) {
       $base =~ s@^(https?://[^/]+)/.*@$1@s;
@@ -2004,9 +2017,14 @@ sub pick_from_twitpic_images($) {
 # of images only updates once a minute; so we'll remember the last N entries
 # on it and randomly select from those, to get a wider variety each time.
 
-my $twitter_img_url = "http://twitter.com/statuses/public_timeline.rss";
+my $twitter_img_url = "http://api.twitter.com/1/statuses/" .
+                     "public_timeline.json" .
+                     "?include_entities=true" .
+                     "&include_rts=true" .
+                     "&count=200";
 
 my $twitter_cache_size = 1000;
+
 my @twitter_cache = (); # fifo, for ordering by age
 my %twitter_cache = (); # hash, for detecting dups
 
@@ -2018,30 +2036,30 @@ sub pick_from_twitter_images($) {
   $last_search = $twitter_img_url;   # for warnings
 
   my ( $base, $body ) = get_document ($twitter_img_url, undef, $timeout);
-
   # Update the cache.
 
   if ($body) {
-    $body =~ s/\n/ /gs;
-    $body =~ s/(<item)\b/\n$1/gsi;
+    $body =~ s/[\r\n]+/ /gs;
 
-    my @items = split (/\n/, $body);
-    shift @items;
+    # Parsing JSON is a pain in the ass.  So we halfass it as usual.
+    $body =~ s/^\[|\]$//s;
+    $body =~ s/(\[.*?\])/{ $_ = $1; s@\},@\} @gs; $_; }/gsexi;
+    my @items = split (/},{/, $body);
     foreach (@items) {
-      next unless (m@<link>([^<>]*)</link>@si);
-      my $page = html_unquote ($1);
+      my ($name) = m@"screen_name":"([^\"]+)"@si;
+      my ($img)  = m@"media_url":"([^\"]+)"@si;
+      my ($page) = m@"display_url":"([^\"]+)"@si;
+      next unless ($name && $img && $page);
+      foreach ($img, $page) {
+        s/\\//gs;
+        $_ = "http://$_" unless (m/^http/si);
+      }
 
       next if ($twitter_cache{$page}); # already have it
 
-      my ($title) = m@<title\b[^<>]*>(.*?)</title>@si;
-      next unless $title;
-
-      my ($url) = ($title =~ m@\b(https?://[^\s\[\]()<>\"\']+[a-z\d/])@si);
-      next unless $url;
-
-      LOG ($verbose_filter, "  candidate: $page - $url");
+      LOG ($verbose_filter, "  candidate: $page - $img");
       push @twitter_cache, $page;
-      $twitter_cache{$page} = $url;
+      $twitter_cache{$page} = $img;
     }
   }
 
@@ -2071,21 +2089,7 @@ sub pick_from_twitter_images($) {
 
   $suppress_audit = 1;
 
-  my ( $base2, $body2 ) = get_document ($url, $base, $timeout);
-
-  if (!$base2 || !$body2) {
-    $body2 = undef;
-    return ();
-  }
-
-  my $img = pick_image_from_body ($base2, $body2);
-  $body2 = undef;
-
-  if ($img) {
-    return ($base2, $img);
-  } else {
-    return ();
-  }
+  return ($page, $url);
 }
 
 \f
@@ -2095,7 +2099,7 @@ sub pick_from_twitter_images($) {
 #
 ############################################################################
 
-my $flickr_img_url = "http://www.flickr.com/photos/";
+my $flickr_img_url = "http://www.flickr.com/explore/";
 
 # Like LiveJournal, the Flickr page of images tends to update slowly,
 # so remember the last N entries on it and randomly select from those.
@@ -2126,13 +2130,15 @@ sub pick_from_flickr_recent($) {
   my $count = 0;
   my $count2 = 0;
   foreach (split (/\n/, $body)) {
+
     my ($page, $thumb) = m@<A \s [^<>]* \b HREF=\"([^<>\"]+)\" [^<>]* > \s*
-                           <IMG \s [^<>]* \b SRC=\"([^<>\"]+)\" @xsi;
+                           <IMG \s [^<>]* \b
+                               data-defer-src = \"([^<>\"]+)\" @xsi;
     next unless defined ($thumb);
     $page = html_unquote ($page);
     $thumb = html_unquote ($thumb);
 
-    next unless ($thumb =~ m@^http://farm\d*\.static\.flickr\.com/@);
+    next unless ($thumb =~ m@^http://farm\d*\.static\.?flickr\.com/@);
 
     my $base = "http://www.flickr.com/";
     $page  =~ s@^/@$base@;
@@ -2272,9 +2278,8 @@ sub get_driftnet_file($) {
   error ("\$driftnet_dir unset?") unless ($driftnet_dir);
 
   my $id = $driftnet_magic;
-  my $re = qr/$driftnet_dir/;
   error ("$id: $file not in $driftnet_dir?")
-    unless ($file =~ m@^$re@o);
+    unless ($file =~ m@^\Q$driftnet_dir@o);
 
   open (my $in, '<', $file) || error ("$id: $file: $!");
   my $body = '';
@@ -2343,6 +2348,7 @@ sub pick_from_local_dir($) {
 
   my $v = ($verbose_exec ? "-v" : "");
   my $pick = `xscreensaver-getimage-file $v "$dir"`;
+  $pick =~ s/\s+$//s;
   $pick = "$dir/$pick" unless ($pick =~ m@^/@s);       # relative path
 
   LOG ($verbose_load, "picked file $pick ($id)");
@@ -2356,9 +2362,8 @@ sub get_local_file($) {
   error ("\$local_dir unset?") unless ($local_dir);
 
   my $id = $local_magic;
-  my $re = qr/$local_dir/;
   error ("$id: $file not in $local_dir?")
-    unless ($file =~ m@^$re@o);
+    unless ($file =~ m@^\Q$local_dir@o);
 
   open (my $in, '<', $file) || error ("$id: $file: $!");
   local $/ = undef;  # read entire file