flickrgallery.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Copyright (C) 2005 Fletcher T. Penney <fletcher@freeshell.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('flickrgallery.pl', 'FlickrGallery Module');
  21. # NOTE: This API key for Flickr is NOT to be used in any other products
  22. # INCLUDING derivative works. The rest of the code can be used as licensed
  23. my $FlickrAPIKey = "a8d5ba0d878e08847ccc8b150e52a859";
  24. our (%RuleOrder, @MyRules, @MyMarkdownRules);
  25. our ($FlickrBaseUrl, $FlickrHeaderTemplate, $FlickrFooterTemplate, $FlickrImageTemplate, $FlickrExtension, $FlickrLabel);
  26. use LWP;
  27. $FlickrBaseUrl = "http://www.flickr.com/services/rest/" unless defined $FlickrBaseUrl;
  28. $FlickrHeaderTemplate = '<h3>$title</h3>
  29. <p>$description</p>
  30. <div class="gallery">' unless defined $FlickrHeaderTemplate;
  31. $FlickrFooterTemplate = '<div class="gallery close"></div></div>' unless defined $FlickrFooterTemplate;
  32. $FlickrImageTemplate = '<div class="image"><a href="$imageurl" title="$title"><img src="http://static.flickr.com/$server/$id_$secret$FlickrExtension.jpg" width="$width" height="$height" alt="$title"/></a><div class="text"><p>$cleanTitle<br/><br/>$description</p></div></div>' unless defined $FlickrImageTemplate;
  33. $FlickrLabel = "Square" unless defined $FlickrLabel;
  34. $FlickrLabel = ucfirst($FlickrLabel);
  35. my %FlickrExtensions = (
  36. 'Square' => '_s',
  37. 'Thumbnail' => '_t',
  38. 'Small' => '_m',
  39. 'Medium' => '',
  40. 'Original' => '_o',
  41. );
  42. $FlickrExtension = $FlickrExtensions{$FlickrLabel};
  43. # Square|Thumbnail|Small|Medium|Original
  44. my $size = "Square|Thumbnail|Small|medium|Original";
  45. push (@MyRules, \&FlickrGalleryRule);
  46. # Allow compatibility with Markdown Module
  47. push (@MyMarkdownRules, \&MarkdownFlickrGalleryRule);
  48. $RuleOrder{\&FlickrGalleryRule} = -10;
  49. sub FlickrGalleryRule {
  50. # This code is used when Markdown is not available
  51. if (/\G^([\n\r]*\&lt;\s*FlickrSet:\s*(\d+)\s*\&gt;\s*)$/cgim) {
  52. my $oldpos = pos;
  53. my $oldstr = $_;
  54. print FlickrGallery($2);
  55. pos = $oldpos;
  56. $oldstr =~ s/\&lt;\s*FlickrSet:\s*(\d+)\s*\&gt;//is;
  57. $_ = $oldstr;
  58. return '';
  59. }
  60. if (/\G^([\n\r]*\&lt;\s*FlickrPhoto:\s*(\d+)\s*([a-z0-9]*?)\s*($size)?\s*\&gt;\s*)$/cgim) {
  61. my $oldpos = pos;
  62. my $oldstr = $_;
  63. print GetFlickrPhoto($2,$3,$4);
  64. pos = $oldpos;
  65. $oldstr =~ s/\&lt;\s*FlickrPhoto:\s*(\d+)\s*([a-z0-9]*?)\s*($size)?\s*\&gt;//is;
  66. $_ = $oldstr;
  67. return '';
  68. }
  69. return;
  70. }
  71. sub MarkdownFlickrGalleryRule {
  72. # for Markdown only
  73. my $text = shift;
  74. $text =~ s{
  75. ^&lt;FlickrSet:\s*(\d+)\s*\>
  76. }{
  77. FlickrGallery($1);
  78. }egimx;
  79. $text =~ s{
  80. ^&lt;FlickrPhoto:\s*(\d+)\s*([a-z0-9]*?)\s*($size)?\s*\>
  81. }{
  82. GetFlickrPhoto($1,$2,$3);
  83. }egimx;
  84. return $text
  85. }
  86. sub FlickrGallery {
  87. my $id = shift();
  88. return "&lt;FlickrSet:$id&gt; (error LWP::UserAgent not available)" unless eval {require LWP::UserAgent};
  89. my $ua = LWP::UserAgent->new;
  90. # $ua->timeout(10);
  91. my $result = "";
  92. # Get Title and description
  93. my $url = $FlickrBaseUrl . "?method=flickr.photosets.getInfo&api_key=" .
  94. $FlickrAPIKey . "&photoset_id=" . $id;
  95. # my $response = $ua->get($url);
  96. my $response = $ua->request(HTTP::Request->new(GET=>$url));
  97. $response->content =~ /\<title\>(.*?)\<\/title\>/;
  98. my $title = $1;
  99. $response->content =~ /\<description\>(.*?)\<\/description\>/;
  100. my $description = $1;
  101. $result = $FlickrHeaderTemplate;
  102. $result =~ s/(\$[a-zA-Z\d]+)/"defined $1 ? $1 : ''"/eeg;
  103. # Get list of photos and process them
  104. $url = $FlickrBaseUrl . "?method=flickr.photosets.getPhotos&api_key=" .
  105. $FlickrAPIKey . "&photoset_id=" . $id;
  106. # $response = $ua->get($url);
  107. $response = $ua->request(HTTP::Request->new(GET=>$url));
  108. my $xml = $response->content;
  109. while (
  110. $xml =~ m/\<photo\s+id=\"(\d+)\"\s+secret=\"(.+?)\"\s+server=\"(\d+)\"/g
  111. ) {
  112. $result .= FlickrPhoto($1,$2,$3);
  113. }
  114. my $footer = $FlickrFooterTemplate;
  115. $footer =~ s/(\$[a-zA-Z\d]+)/"defined $1 ? $1 : ''"/eeg;
  116. $result .= $footer;
  117. return $result;
  118. }
  119. sub FlickrPhoto {
  120. my ($id, $secret, $server) = @_;
  121. my $ua = LWP::UserAgent->new;
  122. # $ua->timeout(10);
  123. my $url = $FlickrBaseUrl . "?method=flickr.photos.getInfo&api_key=" .
  124. $FlickrAPIKey . "&photo_id=" . $id . "&secret=" . $secret;
  125. # my $response = $ua->get($url);
  126. my $response = $ua->request(HTTP::Request->new(GET=>$url));
  127. $response->content =~ /\<title\>(.*?)\<\/title\>/;
  128. my $title = $1;
  129. my $cleanTitle = $title;
  130. $response->content =~ /\<description\>(.*?)\<\/description\>/;
  131. my $description = $1;
  132. $response->content =~ /\<url type="photopage"\>(.*?)\<\/url\>/;
  133. my $imageurl = $1;
  134. $url = $FlickrBaseUrl . "?method=flickr.photos.getSizes&api_key=" .
  135. $FlickrAPIKey . "&photo_id=" . $id;
  136. # $response = $ua->get($url);
  137. $response = $ua->request(HTTP::Request->new(GET=>$url));
  138. $response->content =~ /\<size label=\"$FlickrLabel\" width=\"(\d+)\" height=\"(\d+)\"/;
  139. my $width = $1;
  140. my $height = $2;
  141. my $output = $FlickrImageTemplate;
  142. $output =~ s/(\$[a-zA-Z\d]+)/"defined $1 ? $1 : ''"/eeg;
  143. return $output
  144. }
  145. sub GetFlickrPhoto{
  146. my ($id, $secret, $size) = @_;
  147. local $FlickrLabel = ucfirst($size) if ($size);
  148. local $FlickrExtension = $FlickrExtensions{$FlickrLabel};
  149. my $ua = LWP::UserAgent->new;
  150. # $ua->timeout(10);
  151. my $url = $FlickrBaseUrl . "?method=flickr.photos.getInfo&api_key=" .
  152. $FlickrAPIKey . "&photo_id=" . $id;
  153. $url .= "&secret=" . $secret if ($secret);
  154. # my $response = $ua->get($url);
  155. my $response = $ua->request(HTTP::Request->new(GET=>$url));
  156. $response->content =~ m/\<photo\s+id=\"(\d+)\"\s+secret=\"(.+?)\"\s+server=\"(\d+)\"/g;
  157. $secret = $2;
  158. my $server = $3;
  159. return FlickrPhoto($id,$secret,$server);
  160. }