Skip to content
Extraits de code Groupes Projets
Valider e66358a0 rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

Bump to youtube API v3

parent 545546c0
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
package youtube;
use WebService::GData::YouTube;
use URI::Find;
use Encode;
require Encode::Detect;
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 $yt = new WebService::GData::YouTube();
my $video = $yt->get_video_by_id($id);
my $ua = LWP::UserAgent->new(timeout => 30);
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);
my $video = $content->{'items'}->[0];
my %infos;
#$infos{'title'} = decode("UTF-8", $video->title);
$infos{'title'} = $video->title;
$infos{'author'} = $video->uploader;
$infos{'url'} = $video->base_uri;
$infos{'duration'} = $video->duration;
$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;
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter