Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Implemented support for popular videos.
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Oct 5, 2020
1 parent bc431c3 commit 4ef8611
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
4 changes: 4 additions & 0 deletions lib/WWW/StrawViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ sub lwp_get {
return $self->lwp_get($url, %opt, depth => $opt{depth} + 1);
}

# Too many errors. Pick another invidious instance.
$self->pick_and_set_random_instance();

_warn_reponse_error($response, $url);
return;
}
Expand Down Expand Up @@ -681,6 +684,7 @@ sub default_arguments {

sub _make_feed_url {
my ($self, $path, %args) = @_;

my $extra_args = $self->default_arguments(%args);
my $url = $self->get_api_url . $path;

Expand Down
23 changes: 16 additions & 7 deletions lib/WWW/StrawViewer/Channels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ sub videos_from_username {
return $self->_get_results($self->_make_feed_url("channels/$channel_id/videos"));
}

=head2 popular_videos($channel_id)
Get the most popular videos for a given channel ID.
=cut

sub popular_videos {
my ($self, $channel_id) = @_;
return $self->_get_results($self->_make_feed_url("channels/$channel_id/videos", sort_by => 'popular'));
}

=head2 channels_from_categoryID($category_id)
Return the YouTube channels associated with the specified category.
Expand Down Expand Up @@ -131,15 +142,13 @@ Return the channel ID for an username.
sub channel_id_from_username {
my ($self, $username) = @_;

state $username_lookup = {};

if (exists $username_lookup->{$username}) {
return $username_lookup->{$username};
# 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;
}

$username_lookup->{$username} = undef;
my $channel = $self->channels_from_username($username) // return;
$username_lookup->{$username} = $channel->{results}{items}[0]{id} // return;
# TODO: resolve channel name to channel ID
return $username;
}

=head2 channel_title_from_id($channel_id)
Expand Down
18 changes: 0 additions & 18 deletions lib/WWW/StrawViewer/Videos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,6 @@ sub trending_videos_from_category {
return $results;
}

=head2 popular_videos($channel_id)
Get the most popular videos for a given channel ID.
=cut

sub popular_videos {
my ($self, $id) = @_;

my $results = do {
local $self->{channelId} = $id;
local $self->{order} = 'viewCount';
$self->search_videos("");
};

return $results;
}

=head2 my_likes()
Get the videos liked by the authenticated user.
Expand Down

0 comments on commit 4ef8611

Please # to comment.