diff --git a/lib/sites/youtube.pm b/lib/sites/youtube.pm index 31ddec1a8d62b1b177775ac7bf533fc55c3ba2e6..e0901f9793e27f50d2a4868ab155eb3bb24861bf 100644 --- a/lib/sites/youtube.pm +++ b/lib/sites/youtube.pm @@ -1,22 +1,47 @@ 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 $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 $yt = new WebService::GData::YouTube(); - my $video = $yt->get_video_by_id($id); - + 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; }