File Coverage

blib/lib/Web/oEmbed/Common.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             =head1 PACKAGE
2              
3             Web::oEmbed::Common - Define several well-known oEmbed providers.
4              
5              
6             =head1 SYNOPSIS
7              
8             my $consumer = Web::oEmbed::Common->new();
9             $consumer->set_embedly_api_key( '0123ABCD0123ABCD0123ABCD' );
10              
11             my $response = $consumer->embed( $link_url );
12             if ( $response ) {
13             print $response->title;
14             print $response->render;
15             }
16              
17              
18             =head1 DESCRIPTION
19              
20             Web::oEmbed::Common provides a subclass of L that is pre-configured with the oEmbed API endpoints for dozens of popular web sites.
21              
22             The interface mirrors that of L: create an oEmbed client object and call its C method for each URL you'd like more information about, then extract the response information using the L.
23              
24              
25             =head2 Defined Providers
26              
27             When you create a new instance of Web::oEmbed::Common, it is initialized with a default set of well-known providers.
28              
29             Endpoints are currently defined for the following content sites: Blip.tv, DailyMotion, 5min, Flickr, FunnyOrDie.com, Hulu, PhotoBucket, PollDaddy.com, Qik, Revision3, Scribd, SmugMug, Viddler, Vimeo, WordPress.tv, YouTube.
30              
31             Endpoints are also defined for the oEmbed proxy / adaptor services from Embed.ly and oohEmbed.com, each of which supports over a dozen content sites. Use of the Embed.ly service is subject to a daily rate limit, so you should consider obtaining your own API key by registering on their site.
32              
33              
34             =head2 Registering Additional Providers
35              
36             You can add your own definitions using the C method.
37              
38             As with L, each provider definition should include a C parameter with the oEmbed endpoint URL, but there are minor differences in how you specify the target URLs which that service can handle.
39              
40             The provider definition's C option can contain a whitespace-separated list of multiple URL patterns to match against. These URLs can contain optional portions or alternatives in parentheses.
41              
42              
43             =head1 SEE ALSO
44              
45             L, L
46              
47              
48             =head1 AUTHOR
49              
50             Developed by Matthew Simon Cavalletto. You may contact the author
51             directly at C or C.
52              
53             I found some of these oEmbed endpoint URLs defined in similar libraries
54             in other languages, including wp-includes/class-oembed, django-oembed, and ruby-oembed.
55              
56              
57             =head1 LICENSE
58              
59             Copyright 2010, 2011 Matthew Simon Cavalletto.
60              
61             You may use, modify, and distribute this software under the same terms as Perl.
62              
63             See http://dev.perl.org/licenses/ for more information.
64              
65             =cut
66              
67              
68             package Web::oEmbed::Common;
69              
70 2     2   55512 use strict;
  2         4  
  2         70  
71 2     2   47 use 5.006;
  2         7  
  2         72  
72              
73 2     2   9 use Carp;
  2         8  
  2         210  
74 2     2   3632 use Any::Moose;
  2         103246  
  2         28  
75              
76             our $VERSION = '0.05';
77              
78             ########################################################################
79              
80 2     2   2221 use Web::oEmbed;
  0            
  0            
81             extends 'Web::oEmbed';
82              
83             {
84             # Embed.ly passes back a non-standard "description" field.
85             package Web::oEmbed::Response;
86             has 'description', is => 'rw';
87             }
88              
89             ########################################################################
90              
91             # Bulk registration
92             sub register_providers {
93             my($self, @providers) = @_;
94              
95             foreach my $provider ( @providers ) {
96             $self->register_provider( $provider )
97             }
98             }
99              
100             sub _compile_url {
101             my($self, $incoming) = @_;
102            
103             # Pass multiple space-separated URLs in a single string
104             my @incoming = grep length($_), split ' ', $incoming;
105            
106             # Generate alternatives for parenthesized optional elements in the pattern
107             # to facilitate the frequent case of supporting both x.com and www.x.com.
108             my @urls;
109             while ( @incoming ) {
110             my $url = shift @incoming;
111            
112             unless ( $url =~ m{\(([^\)]*)\)} ) {
113             push @urls, $url;
114             next;
115             }
116             my @alts = split '\|', $1;
117             if ( @alts == 1 ) {
118             unshift @alts, '';
119             }
120             unshift @incoming, map {
121             my $version = $url;
122             $version =~ s{\(([^\)]*)\)}{$_};
123             $version;
124             } @alts
125             }
126            
127             # The URL lists from oohEmbed includes patterns like *yfrog.com
128             # so we over-ride the default Web::oEmbed behavior to allow * to match . in
129             # hostnames, so the above line will match both www.yfrog.com and yfrog.com.
130             join '|', map {
131             # warn "Working on URL: $_\n";
132             # no 'warnings';
133             my $uri = URI->new( $_ );
134             $uri->scheme . '://'
135             . Web::oEmbed::_run_regexp($uri->host, '[0-9a-zA-Z\-\.]*')
136             . Web::oEmbed::_run_regexp($uri->path, "[$URI::uric]+" )
137             } @urls
138             }
139              
140             # Anchor right end of regexp.
141             sub provider_for {
142             my ($self, $uri) = @_;
143             foreach my $provider ( @{$self->providers} ) {
144             if ($uri =~ m!^$provider->{regexp}(\#|$)!) {
145             return $provider;
146             }
147             }
148             return;
149             }
150              
151             ########################################################################
152              
153             sub BUILD {
154             my $self = shift;
155             $self->register_common();
156             }
157              
158             sub register_common {
159             (shift)->register_providers(
160             {
161             name => 'Flickr',
162             api => 'http://www.flickr.com/services/oembed/',
163             url => 'http://(www.)flickr.com/photos/*',
164             },
165             {
166             name => 'YouTube',
167             api => 'http://www.youtube.com/oembed',
168             url => 'http://*youtube.com/watch* http://youtu.be/*',
169             },
170             {
171             name => 'Viddler',
172             api => 'http://lab.viddler.com/services/oembed/',
173             url => 'http://(www.)viddler.com/*',
174             },
175             {
176             name => 'Qik',
177             api => 'http://qik.com/api/oembed.json',
178             url => 'http://qik.com/*',
179             },
180             {
181             name => 'Vimeo',
182             api => 'http://vimeo.com/api/oembed.json',
183             url => 'http://(www.)vimeo.com/*',
184             },
185             {
186             name => 'Revision3',
187             api => 'http://revision3.com/api/oembed/',
188             url => 'http://*revision3.com/*',
189             },
190             {
191             name => 'Scribd',
192             api => 'http://www.scribd.com/services/oembed',
193             url => 'http://(www.)scribd.com/*',
194             },
195             {
196             name => '5min',
197             api => 'http://api.5min.com/oembed.xml',
198             url => 'http://www.5min.com/video/*',
199             },
200             {
201             name => 'Blip.tv',
202             api => 'http://blip.tv/oembed/',
203             url => 'http://blip.tv/file/*',
204             },
205             {
206             name => 'DailyMotion',
207             api => 'http://www.dailymotion.com/api/oembed',
208             url => 'http://*dailymotion.com/*',
209             },
210             {
211             name => 'SmugMug',
212             api => 'http://api.smugmug.com/services/oembed/',
213             url => 'http://*smugmug.com/*',
214             },
215             {
216             name => 'Hulu',
217             api => 'http://www.hulu.com/api/oembed.json',
218             url => 'http://*hulu.com/watch/*',
219             },
220             {
221             name => 'PhotoBucket',
222             api => 'http://photobucket.com/oembed',
223             url => 'http://i*.photobucket.com/albums/* http://gi*.photobucket.com/groups/*',
224             },
225             {
226             name => 'WordPress.tv',
227             api => 'http://wordpress.tv/oembed/',
228             url => 'http://wordpress.tv/*',
229             },
230             {
231             name => 'PollDaddy.com',
232             api => 'http://polldaddy.com/oembed/',
233             url => 'http://*polldaddy.com/*',
234             },
235             {
236             name => 'FunnyOrDie.com',
237             api => 'http://www.funnyordie.com/oembed',
238             url => 'http://*funnyordie.com/videos/*',
239             },
240             {
241             name => 'Pownce',
242             api => 'http://api.pownce.com/2.1/oembed.json',
243             url => 'http://*.pownce.com/*',
244             },
245             {
246             name => 'Poll Everywhere',
247             api => 'http://www.polleverywhere.com/services/oembed/',
248             url => 'http://www.polleverywhere.com/*polls/*',
249             },
250             {
251             name => 'My Opera',
252             api => 'http://my.opera.com/service/oembed',
253             url => 'http://my.opera.com/*',
254             },
255             {
256             name => 'Clearspring',
257             api => 'http://widgets.clearspring.com/widget/v1/oembed/',
258             url => 'http://www.clearspring.com/widgets/*',
259             },
260             {
261             name => 'Embed.ly',
262             api =>
263             'http://api.embed.ly/1/oembed?key=6f4159f0be0011e0aa814040d3dc5c07',
264             url => 'http://*/*',
265             },
266             {
267             name => 'oohEmbed',
268             api => 'http://oohembed.com/oohembed/',
269             url => 'http://*.5min.com/Video/* http://*.amazon.(com|co.uk|de|ca|jp)/*/(gp/product|o/ASIN|obidos/ASIN|dp)/* http://*.blip.tv/* http://*.collegehumor.com/video:* http://*.thedailyshow.com/video/* http://*.dailymotion.com/* http://dotsub.com/view/* http://*.flickr.com/photos/* http://*.funnyordie.com/videos/* http://video.google.com/videoplay?* http://www.hulu.com/watch/* http://*.livejournal.com/ http://*.metacafe.com/watch/* http://*.nfb.ca/film/* http://*.phodroid.com/*/*/* http://qik.com/* http://*.revision3.com/* http://*.scribd.com/* http://*.slideshare.net/* http://*.twitpic.com/* http://(www.)twitter.com/*/statuses/* http://*.viddler.com/explore/* http://(www.)vimeo.com/* http://(www.)vimeo.com/groups/*/videos/* http://*.wikipedia.org/wiki/* http://*.wordpress.com/yyyy/mm/dd/* http://(*.)xkcd.com/*/ http://(www.)yfrog.(com|ru|com.tr|it|fr|co.il|co.uk|com.pl|pl|eu|us)/* http://*.youtube.com/watch*',
270             },
271             );
272             }
273              
274             sub set_embedly_api_key {
275             my ( $class, $api_key ) = @_;
276              
277             for ( @{ $class->providers } ) {
278             if ( $_->{name} eq 'Embed.ly' ) {
279             $_->{api} = 'http://api.embed.ly/1/oembed?key=' . $api_key
280             }
281             }
282             }
283              
284             ########################################################################
285              
286             1;