Sélectionner une révision Git
Bifurcation depuis
Alexandre MORIGNOT / PlayBot
Le projet source a une visibilité limitée.
-
Alexandre Morignot a rédigéAlexandre Morignot a rédigé
youtube.pm 1,24 Kio
package PlayBot::sites::youtube;
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use FindBin;
my $conf;
my $endpoint = "https://www.googleapis.com/youtube/v3";
BEGIN {
chdir "$FindBin::Bin/";
local $/;
open CONF, '<', 'playbot.conf';
my $json = <CONF>;
$conf = decode_json($json);
}
sub get {
my $id = shift;
my $ua = LWP::UserAgent->new(
timeout => 30,
env_proxy => 1,
);
my $response = $ua->get($endpoint
.'/videos?id='.$id
.'&key='.$conf->{'youtube_api_key'}
.'&part=snippet,contentDetails');
die($response->status_line) unless ($response->is_success);
my $content = decode_json($response->decoded_content);
die "video not found" if (not scalar @{ $content->{items} });
my $video = $content->{'items'}->[0];
my %infos;
$infos{'title'} = $video->{'snippet'}->{'title'};
$video->{'contentDetails'}->{'duration'} =~ /PT((?<h>\d+)H)?((?<m>\d+)M)?((?<s>\d+)S)?/;
# default values
my ($h) = $+{'h'} || 0;
my ($m) = $+{'m'} || 0;
my ($s) = $+{'s'} || 0;
# there is one more second, don't know why
$infos{'duration'} = $h*3600 + $m*60 + $s - 1;
$infos{'author'} = $video->{'snippet'}->{'channelTitle'};
return %infos;
}
1;