Skip to content

Commit

Permalink
More progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trizen committed Feb 20, 2013
1 parent 65828d9 commit 59b708b
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 244 deletions.
92 changes: 68 additions & 24 deletions WWW-YoutubeViewer/bin/youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#-------------------------------------------------------
# Appname: youtube-viewer
# Created on: 02 June 2010
# Latest edit on: 18 February 2013
# Latest edit on: 20 February 2013
# Websites: http://trizen.googlecode.com
# https://github.com/trizen/youtube-viewer
#-------------------------------------------------------
Expand Down Expand Up @@ -103,6 +103,7 @@ See http://dev.perl.org/licenses/ for more information.
use 5.016;

use lib qw(../lib); # devel only

#use warnings; # debug only

use File::Spec::Functions qw(catdir catfile curdir path rel2abs tmpdir);
Expand Down Expand Up @@ -410,9 +411,14 @@ unless (qr/^--?(?>N|noconfig)\z/ ~~ \@ARGV) {
}

$yv_obj->set_prefer_https(1);
print $yv_obj->search_videos(qw(xxswampxx));

exit;
#pp $yv_obj->search_videos([qw(google)]);

#my $uri = 'https://www.googleapis.com/youtube/v3/search?key=AIzaSyA2PQakgMYbX3U3imqplLMhpfM8mzEn22Q&maxResults=3&part=snippet&q=google&type=video';

#print $yv_obj->lwp_get($uri);

#exit;

#---------------------- YOUTUBE-VIEWER USAGE ----------------------#
sub help {
Expand Down Expand Up @@ -827,12 +833,17 @@ sub apply_configuration {
# ... YOUTUBE OPTIONS ... #
foreach my $option_name (
qw(
caption results duration
author orderby region category
categories_language safe_search
page debug time prefer_https http_proxy
videoCaption maxResults order
videoDefinition videoCategoryId
videoDimension videoDuration
videoEmbeddable videoLicense
videoSyndicated channelId
publishedAfter publishedBefore
regionCode videoCategoryId
page debug prefer_https http_proxy
)
) {

if (defined $opt->{$option_name}) {
my $code = \&{"WWW::YoutubeViewer::set_$option_name"};
my $value = delete $opt->{$option_name};
Expand Down Expand Up @@ -941,7 +952,7 @@ sub apply_configuration {

if (defined $opt->{search_videos}) {
my $value = delete $opt->{search_videos};
print_videos($yv_obj->search($value, @{$keywords}));
print_videos($yv_obj->search_videos([$value, @{$keywords}]));
}

if (defined $opt->{search_channels}) {
Expand Down Expand Up @@ -1174,7 +1185,7 @@ for (my $i = 0 ; $i <= $#ARGV ; $i++) {
}

if (my @keywords = grep chr ord ne q{-}, @ARGV) {
print_videos($yv_obj->search(@keywords));
print_videos($yv_obj->search_videos(\@keywords));
}
elsif (-t and not $opt{non_interactive}) {
first_user_input();
Expand Down Expand Up @@ -1304,7 +1315,7 @@ HELP
}

if (scalar(@for_search) > 0) {
print_videos($yv_obj->search(@for_search));
print_videos($yv_obj->search_videos(\@for_search));
}
else {
__SUB__->();
Expand Down Expand Up @@ -1647,7 +1658,7 @@ sub general_options {
when ($_ ~~ ['n', 'next'] and defined $url) {
if (not $url ~~ $opt{_last_page_urls}) {
my $request = $yv_obj->next_page($url, (defined($mode) ? ($mode => 1) : ()));
if (@{$request->{results}}) {
if (exists $request->{results}{items}) {
$callback->($request, @{$callback_args});
}
else {
Expand Down Expand Up @@ -2545,7 +2556,7 @@ sub play_videos {
}

foreach my $video (@{$videos}) {
my $streaming = get_streaming_url($video->{videoID});
my $streaming = get_streaming_url($video->{id}{videoId});

if (defined $streaming->{info}{status} and not $streaming->{info}{status} =~ /^(?:ok|success)/i) {
warn colored("(x_x) Can't stream: " . sprintf($CONFIG{youtube_video_url}, $video->{videoID}), 'bold red')
Expand Down Expand Up @@ -2667,12 +2678,34 @@ sub print_video_info {
sub print_videos {
my ($results, %args) = @_;

if (not @{$results->{results}}) {
use Data::Dump qw(pp);
pp $results;

=gdata response
{
etag => "\"HmiNsUIWRgiTfkRt2Naw6AaEcm0/QXtOMFRBmq3u8EnxYZemKVIyG-w\"",
items => [
{
etag => "\"HmiNsUIWRgiTfkRt2Naw6AaEcm0/Ilij54Zu3VGxw-U7aC-K2ZJiV6M\"",
id => { kind => "youtube#video", videoId => "V0z7XOOmoM4" },
kind => "youtube#searchResult",
snippet => {
kind => "youtube#searchListResponse",
nextPageToken => "CAEQAA",
pageInfo => { resultsPerPage => 1, totalResults => 1000000 },
=cut

if (not exists $results->{results}{items}) {
warn_no_results("video");
}

my $url = $results->{url};
my $videos = $results->{results};
my $info = $results->{results} // {};
my $videos = $info->{items} // [];

my $num = 0;
foreach my $video (@{$videos}) {
Expand All @@ -2683,7 +2716,7 @@ sub print_videos {
colored('Views' => 'bold') => $yv_utils->set_thousands($video->{views}),
colored('Rating' => 'bold') => sprintf('%.2f', $video->{rating}),
colored('Category' => 'bold') => $video->{category},
colored('Published' => 'bold') => $yv_utils->format_date($video->{published}),
colored('Published' => 'bold') => $yv_utils->format_date($video->{snippet}{publishedAt}),
colored('Duration' => 'bold') => $yv_utils->format_time($video->{duration}),
colored('Author' => 'bold') => $video->{author},
wrap_text(
Expand All @@ -2705,9 +2738,17 @@ sub print_videos {
}
else {
print "\n" if $num == 0;
printf("%s. %s (by %s) (%s)\n",
printf(
"%s. %s (%s)\n%s\n\n",
colored(sprintf('%2d', ++$num), 'bold'),
$video->{title}, $video->{author}, $yv_utils->format_time($video->{duration}));
$video->{snippet}{title},
$yv_utils->format_date($video->{snippet}{publishedAt}),
wrap_text(
i_tab => q{ } x 4,
s_tab => q{ } x 4,
text => [$video->{snippet}{description} || 'No description available...']
),
);
}
}

Expand Down Expand Up @@ -2759,8 +2800,9 @@ sub print_videos {
}
when (['n', 'next']) {
if (not $url ~~ $opt{_last_page_urls}) {
my $request = $yv_obj->next_page($url);
if (@{$request->{results}}) {
say "Here\n";
if (exists $info->{nextPageToken}) {
my $request = $yv_obj->next_page($url, $info->{nextPageToken});
__SUB__->($request, @keywords ? (auto => 1) : ());
}
else {
Expand All @@ -2783,8 +2825,10 @@ sub print_videos {
}
}
when (['b', 'back', 'p', 'prev', 'previous']) {
if ($yv_obj->back_page_is_available($url)) {
__SUB__->($yv_obj->previous_page($url), @keywords ? (auto => 1) : ());

#if ($yv_obj->back_page_is_available($url)) {
if (exists $info->{prevPageToken}) {
__SUB__->($yv_obj->previous_page($url, $info->{prevPageToken}), @keywords ? (auto => 1) : ());
}
else {
warn_first_page();
Expand All @@ -2798,7 +2842,7 @@ sub print_videos {
foreach my $id (@nums) {
my $username = $videos->[$id]{author};
my $request = $yv_obj->get_videos_from_username($username);
if (@{$request->{results}}) {
if (exists $request->{results}{items}) {
__SUB__->($request);
}
else {
Expand All @@ -2815,7 +2859,7 @@ sub print_videos {
foreach my $id (@nums) {
my $username = $videos->[$id]{author};
my $request = $yv_obj->get_playlists_from_username($username);
if (@{$request->{results}}) {
if (exists $request->{results}{items}) {
print_playlists($request);
}
else {
Expand Down Expand Up @@ -2963,7 +3007,7 @@ sub print_videos {
}

if (@for_search) {
__SUB__->($yv_obj->search(splice(@for_search)));
__SUB__->($yv_obj->search_videos([splice(@for_search)]));
}
elsif ($opt{auto_next_page}) {
@keywords = (':next', grep { not $_ ~~ [qw(:n :next :anp)] } @copy_of_keywords);
Expand Down
Loading

0 comments on commit 59b708b

Please sign in to comment.