From d795f36964b11c6251d8d152f0e0222cd833485c Mon Sep 17 00:00:00 2001 From: Oklahomer Date: Mon, 21 Oct 2019 23:58:50 +0900 Subject: [PATCH 1/2] Support caption file upload --- lib/Facebook/OpenGraph.pm | 11 +++++--- t/003_publish/03_post_movie.t | 50 +++++++++++++++++++++++++++++++++++ t/resource/captions.ja_JP.srt | 3 +++ 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 t/resource/captions.ja_JP.srt diff --git a/lib/Facebook/OpenGraph.pm b/lib/Facebook/OpenGraph.pm index 34f9c5d..0349f92 100644 --- a/lib/Facebook/OpenGraph.pm +++ b/lib/Facebook/OpenGraph.pm @@ -451,8 +451,11 @@ sub request { my $content = q{}; if ($method eq 'POST') { - if ($param_ref->{source} || $param_ref->{file} || $param_ref->{upload_phase} ) { - # post image or video file + if ($param_ref->{source} + || $param_ref->{file} + || $param_ref->{upload_phase} + || $param_ref->{captions_file}) { + # post image, video or caption file # https://developers.facebook.com/docs/reference/api/video/ # When posting a video, use graph-video.facebook.com . @@ -576,7 +579,6 @@ sub prep_param { $param_ref->{permissions} = ref $perms ? join q{,}, @$perms : $perms; } - # Source, file and video_file_chunk parameter contains file path. # It must be an array ref to work with HTTP::Request::Common. if (my $path = $param_ref->{source}) { $param_ref->{source} = ref $path ? $path : [$path]; @@ -587,6 +589,9 @@ sub prep_param { if (my $path = $param_ref->{video_file_chunk}) { $param_ref->{video_file_chunk} = ref $path ? $path : [$path]; } + if (my $path = $param_ref->{captions_file}) { + $param_ref->{captions_file} = ref $path ? $path : [$path]; + } # use Field Expansion if (my $field_ref = $param_ref->{fields}) { diff --git a/t/003_publish/03_post_movie.t b/t/003_publish/03_post_movie.t index 532944d..0e858ff 100644 --- a/t/003_publish/03_post_movie.t +++ b/t/003_publish/03_post_movie.t @@ -188,4 +188,54 @@ subtest 'transfer chunked file content with Resumable' => sub { }; +subtest 'post captioned movie' => sub { + + $Mock_furl_http->mock( + request => sub { + my ($mock, %args) = @_; + + ok delete $args{content}, 'content'; # too huge to compare, so just check if it's given + is_deeply( + \%args, + +{ + url => 'https://graph.facebook.com/video_id_12345/captions', + method => 'POST', + headers => [ + 'Authorization' => 'OAuth 12345qwerty', + 'Content-Length' => 254, + 'Content-Type' => 'multipart/form-data; boundary=xYzZY', + ], + }, + 'args' + ); + + return ( + 1, + 200, + 'OK', + ['Content-Type' => 'text/javascript; charset=UTF-8'], + encode_json(+{ + success => JSON::true, + }), + ); + + }, + ); + + my $fb = Facebook::OpenGraph->new(+{ + app_id => 12345678, + access_token => '12345qwerty', + }); + my $response = $fb->publish( + '/video_id_12345/captions', + +{ + captions_file => './t/resource/captions.ja_JP.srt', + default_locale => 'ja_JP', + } + ); + + is_deeply $response, +{ success => JSON::true }, 'response'; + +}; + done_testing; diff --git a/t/resource/captions.ja_JP.srt b/t/resource/captions.ja_JP.srt new file mode 100644 index 0000000..8a9815d --- /dev/null +++ b/t/resource/captions.ja_JP.srt @@ -0,0 +1,3 @@ +1 +00:00:00,000 --> 00:00:03,000 +Hello, world! From 18952ba799b6ec90a096c1a6438067769cf431b0 Mon Sep 17 00:00:00 2001 From: Oklahomer Date: Tue, 22 Oct 2019 00:10:52 +0900 Subject: [PATCH 2/2] Minor refactoring for file-related parameter check --- lib/Facebook/OpenGraph.pm | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/Facebook/OpenGraph.pm b/lib/Facebook/OpenGraph.pm index 0349f92..1a9b5c1 100644 --- a/lib/Facebook/OpenGraph.pm +++ b/lib/Facebook/OpenGraph.pm @@ -579,18 +579,11 @@ sub prep_param { $param_ref->{permissions} = ref $perms ? join q{,}, @$perms : $perms; } + # Source, file, video_file_chunk and captions_file parameter contains file path. # It must be an array ref to work with HTTP::Request::Common. - if (my $path = $param_ref->{source}) { - $param_ref->{source} = ref $path ? $path : [$path]; - } - if (my $path = $param_ref->{file}) { - $param_ref->{file} = ref $path ? $path : [$path]; - } - if (my $path = $param_ref->{video_file_chunk}) { - $param_ref->{video_file_chunk} = ref $path ? $path : [$path]; - } - if (my $path = $param_ref->{captions_file}) { - $param_ref->{captions_file} = ref $path ? $path : [$path]; + for my $file (qw/source file video_file_chunk captions_file/) { + next unless my $path = $param_ref->{$file}; + $param_ref->{$file} = ref $path ? $path : [$path]; } # use Field Expansion