diff options
Diffstat (limited to 'lib/WWW/FairViewer/Channels.pm')
-rw-r--r-- | lib/WWW/FairViewer/Channels.pm | 91 |
1 files changed, 41 insertions, 50 deletions
diff --git a/lib/WWW/FairViewer/Channels.pm b/lib/WWW/FairViewer/Channels.pm index 3ee44d4..55598d0 100644 --- a/lib/WWW/FairViewer/Channels.pm +++ b/lib/WWW/FairViewer/Channels.pm @@ -25,12 +25,18 @@ sub _make_channels_url { sub videos_from_channel_id { my ($self, $channel_id) = @_; - return $self->_get_results($self->_make_feed_url("channels/$channel_id/videos")); + + if (my $results = $self->yt_channel_uploads($channel_id)) { + return $results; + } + + my $url = $self->_make_feed_url("channels/$channel_id/videos"); + return $self->_get_results($url); } sub videos_from_username { my ($self, $channel_id) = @_; - return $self->_get_results($self->_make_feed_url("channels/$channel_id/videos")); + $self->videos_from_channel_id($channel_id); } =head2 popular_videos($channel_id) @@ -46,7 +52,12 @@ sub popular_videos { return $self->_get_results($self->_make_feed_url('popular')); } - return $self->_get_results($self->_make_feed_url("channels/$channel_id/videos", sort_by => 'popular')); + if (my $results = $self->yt_channel_uploads($channel_id, sort_by => 'popular')) { + return $results; + } + + my $url = $self->_make_feed_url("channels/$channel_id/videos", sort_by => 'popular'); + return $self->_get_results($url); } =head2 channels_from_categoryID($category_id) @@ -94,65 +105,34 @@ For all functions, C<$channels->{results}{items}> contains: } } -=head2 my_channel() - -Returns info about the channel of the current authenticated user. - -=cut - -sub my_channel { - my ($self) = @_; - $self->get_access_token() // return; - return $self->_get_results($self->_make_channels_url(part => 'snippet', mine => 'true')); -} - -=head2 my_channel_id() +=head2 channel_id_from_username($username) -Returns the channel ID of the current authenticated user. +Return the channel ID for an username. =cut -sub my_channel_id { - my ($self) = @_; +sub channel_id_from_username { + my ($self, $username) = @_; state $cache = {}; - if (exists $cache->{id}) { - return $cache->{id}; + if (exists $cache->{username}) { + return $cache->{username}; } - $cache->{id} = undef; - my $channel = $self->my_channel() // return; - $cache->{id} = $channel->{results}{items}[0]{id} // return; -} - -=head2 channels_my_subscribers() - -Retrieve a list of channels that subscribed to the authenticated user's channel. - -=cut - -sub channels_my_subscribers { - my ($self) = @_; - $self->get_access_token() // return; - return $self->_get_results($self->_make_channels_url(mySubscribers => 'true')); -} - -=head2 channel_id_from_username($username) - -Return the channel ID for an username. - -=cut - -sub channel_id_from_username { - my ($self, $username) = @_; + if (defined(my $id = $self->yt_channel_id($username))) { + if (ref($id) eq '' and $id =~ /\S/) { + $cache->{$username} = $id; + return $id; + } + } # A channel's username (if it doesn't include spaces) is also valid in place of ucid. if ($username =~ /\w/ and not $username =~ /\s/) { return $username; } - # TODO: resolve channel name to channel ID + # Unable to resolve channel name to channel ID (return as it is) return $username; } @@ -165,11 +145,22 @@ Return the channel title for a given channel ID. sub channel_title_from_id { my ($self, $channel_id) = @_; - if ($channel_id eq 'mine') { - $channel_id = $self->my_channel_id(); + $channel_id // return; + + state $cache = {}; + + if (exists $cache->{channel_id}) { + return $cache->{channel_id}; + } + + if (defined(my $title = $self->yt_channel_title($channel_id))) { + if (ref($title) eq '' and $title =~ /\S/) { + $cache->{$channel_id} = $title; + return $title; + } } - my $info = $self->channels_info($channel_id // return) // return; + my $info = $self->channels_info($channel_id) // return; ( ref($info) eq 'HASH' and ref($info->{results}) eq 'HASH' |