/
Scenefinity.pm
93 lines (73 loc) · 2.3 KB
/
Scenefinity.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use strictures;
package Web::Scenefinity;
# VERSION
# ABSTRACT: web app to endlessly stream random demos
# COPYRIGHT
use Web::Simple;
use Web::SimpleX::Helper::ActionWithRender qw' mm action ';
use File::ShareDir::ProjectDistDir 'dist_dir';
use Text::Xslate;
use JSON qw' from_json to_json ';
use HTTP::Tiny;
use Data::Dumper;
use List::Util qw( shuffle );
sub dispatch_request {
(
"/" => mm( action "infinity" ),
"/youtube.js" => mm( action "youtube_js" ),
"/more_demos" => mm( action "more_demos", "json" ),
);
}
sub default_view { "Xslate" }
sub jget {
my ( $url ) = @_;
my $res = HTTP::Tiny->new->get( $url );
die Dumper( $res ) unless $res->{success};
return from_json( $res->{content} );
}
sub author_all_videos {
my ( $author ) = @_;
my $base =
"http://gdata.youtube.com/feeds/api/videos?author=%s&v=2&format=5&start-index=%d&max-results=%d&alt=json";
my @videos;
my $i = 1;
while ( 1 ) {
my $all = jget sprintf $base, $author, $i, 50;
last if !$all->{feed}{entry};
push @videos, @{ $all->{feed}{entry} };
$i += 50;
}
return @videos;
}
sub more_demos {
my @videos = author_all_videos( "Annikras" );
@videos = shuffle @videos;
@videos = grep $_, @videos[ 0 .. 9 ];
my @ids = map $_->{id}{'$t'}, @videos;
$_ =~ s/.*video:(.*)/$1/ for @ids;
return \@ids;
}
sub infinity { ['infinity.html'] }
sub youtube_js { ["youtube.js"] }
sub render_Xslate {
my ( $self, $result ) = @_;
my $content = Text::Xslate->new( path => dist_dir( "Web-Scenefinity" ) )->render( @{$result} );
[ 200, [ "Content-Type" => "text/html; charset=utf-8" ], [$content], ];
}
sub view_error_Xslate {
my ( $self, $error ) = @_;
my $msg = "An error happened during rendering of the page.<br><pre>$error</pre>";
[ 500, [ "Content-Type" => "text/html; charset=utf-8" ], [$msg], ];
}
sub render_json {
my ( $self, $result ) = @_;
my $content = to_json $result;
[ 200, [ "Content-Type" => "text/html; charset=utf-8" ], [$content], ];
}
sub action_error_json { die $_[1] }
sub view_error_json {
my ( $self, $error ) = @_;
my $msg = "An error happened during rendering of the page.<br><pre>$error</pre>";
[ 500, [ "Content-Type" => "text/html; charset=utf-8" ], [$msg], ];
}
1;