- #!/usr/bin/env perl
- use Mojolicious::Lite;
- use URI::Title qw( title );
- get '/' => sub {
- my $c = shift;
- $c->render(template => 'index');
- };
- get '/comment' => sub {
- my $c = shift;
- my $v = $c->param('v');
- if (! $v) {
- $c->render(template => 'error', error => 'No video specified!');
- } elsif ($v =~ m! (?: \bv i? [/=] | be/ ) ([a-zA-Z_\-]+) !x) {
- $c = $c->redirect_to("/comment?v=$1");
- } elsif ($v !~ /.........../) {
- $c->render(template => 'error', error => 'Invalid video ID!');
- } else {
- my $title = title('http://youtube.com/watch?v='.$v);
- if (! $title) {
- $c->render(template => 'error', error => 'Invalid video, or could not fetch video.');
- } else {
- $title =~ s/(.*) - YouTube/$1/;
- $c->stash(title => $title);
- $c->stash(v => $v);
- $c->render(template => 'comment');
- }
- }
- };
- app->start;
- __DATA__
- @@ index.html.ep
- % layout 'default';
- % title 'Hello hello!';
- <!DOCTYPE html>
- <html>
- <head><title><%= title %></title></head>
- <body>
- <h1>OpinionsAllowed</h1>
- <p>Voice your opinion for any video, comments are <strong>never</strong> disabled!</p>
- %= form_for 'comment' => (method => 'GET') => begin
- %= label_for v => 'Video Link: '
- %= text_field 'v'
- % end
- <hr><p>OpinionsAllowed 2015 - Written in Mojolicious for Perl 5</p>
- </body>
- </html>
- @@ comment.html.ep
- <!DOCTYPE html>
- <html>
- <head><title><%= $title %> - OpinionsAllowed</title></head>
- <body>
- <div align='center'>
- <h1>Video</h1><h2><%= $title %></h2>
- <p><iframe id="ytplayer" type="text/html" width="640" height="390"
- src="http://www.youtube.com/embed/<%= $v %>?autoplay=1"
- frameborder="0"/></p>
- </div>
- <hr><p>OpinionsAllowed 2015 - Written in Mojolicious for Perl 5</p>
- </body>
- </html>
- @@ error.html.ep
- <!DOCTYPE html>
- <html>
- <head><title>Error: <%= $error %></title></head><body><h1>Error</h1>
- <p><%= $error %> <a href="/">Home.</a></p></body>
- <hr><p>OpinionsAllowed 2015 - Written in Mojolicious for Perl 5</p>
- </html>
Stikked
