Skip to content

Commit

Permalink
- Added support for including streams in subscription videos. (implem…
Browse files Browse the repository at this point in the history
…ents and closes #185)

Implemented via the config-option `subscription_results`, which by default is set to "uploads". When set to "uploads,streams", livestreams will also be included in the subscription list. However, this requires 2x more GET requests, and therefore will load 2x slower.

Related conversation: #201 (comment)
  • Loading branch information
trizen committed Aug 11, 2024
1 parent 709e088 commit 2a3df2a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 54 deletions.
22 changes: 16 additions & 6 deletions bin/gtk-pipe-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# GTK Pipe Viewer
# Fork: 30 October 2020
# Edit: 04 March 2024
# Edit: 11 August 2024
# https://github.com/trizen/pipe-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -295,6 +295,7 @@ my %CONFIG = (
watch_history_file => $watched_file,

# Subscribed channels
subscription_results => 'uploads', # valid values: uploads, streams, shorts (comma-separated)
subscriptions_limit => 10_000,
subscriptions_lifetime => 600,
saved_channels_file => $saved_channels_file,
Expand Down Expand Up @@ -2716,6 +2717,7 @@ sub videos_from_data_file {
}

sub refresh_subscription_video_results {
my ($method) = @_;

my @channels = $yv_utils->read_channels_from_file($CONFIG{subscribed_channels_file});

Expand Down Expand Up @@ -2758,7 +2760,7 @@ sub refresh_subscription_video_results {
);
}
},
'fetch_subscriptions',
'fetch_' . $method,
\@request_args,
priority => REQUEST_PRIORITY_SEARCH,
);
Expand Down Expand Up @@ -2853,7 +2855,12 @@ sub display_subscription_videos {

$t0 = time + 1;

refresh_subscription_video_results() if $refresh;
if ($refresh) {
foreach my $method (grep { /^\w+\z/ } map { split(/,/, $_) } split(' ', $CONFIG{subscription_results})) {
say STDERR ":: Fetching: $method" if $DEBUG;
refresh_subscription_video_results($method);
}
}

return;
}
Expand Down Expand Up @@ -2973,11 +2980,14 @@ sub get_thumbnail_info {

($thumb{width}, $thumb{height}) = fit_to_dimensions($thumb{width}, $thumb{height}, $xsize, $ysize);

# Clean URL of trackers and other junk
$thumb{url} =~ s/\.(?:jpg|png|webp)\K\?.*//;
# Clean URL of trackers and other junk (breaks some thumbnails)
# $thumb{url} =~ s/\.(?:jpg|png|webp)\K\?.*//;

# Prefer JPEG format over WebP if the later is not supported.
$thumb{url} =~ s/\.webp\z/.jpg/ && $thumb{url} =~ s{/vi_webp/}{/vi/} unless $webp_supported;
if (not $webp_supported) {
$thumb{url} =~ s/\.webp\z/.jpg/
and $thumb{url} =~ s{/vi_webp/}{/vi/};
}

return \%thumb;
}
Expand Down
73 changes: 41 additions & 32 deletions bin/pipe-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# pipe-viewer
# Fork: 30 October 2020
# Edit: 04 March 2024
# Edit: 11 August 2024
# https://github.com/trizen/pipe-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -298,6 +298,7 @@ my %CONFIG = (
watch_history_file => $watched_file,

# Subscribed channels
subscription_results => 'uploads', # valid values: uploads, streams, shorts (comma-separated)
subscriptions_limit => 10_000,
subscriptions_lifetime => 600,
saved_channels_file => $saved_channels_file,
Expand Down Expand Up @@ -1611,6 +1612,7 @@ sub parse_arguments {
'lp|local-playlists:s' => \$opt{local_playlist},
'wv|watched-videos' => \$opt{watched_videos},
'ls|local-subs|sub-videos|SV' => \$opt{local_subscriptions},
'subscription-results=s' => \$opt{subscription_results},

#'save-video|save=s' => \$opt{save_video},
'save|save-channel=s' => \$opt{save_channel},
Expand Down Expand Up @@ -3060,6 +3062,33 @@ sub print_subscription_videos {
print_videos_from_data_file($subscription_videos_data_file);
}

sub fetch_channel_latest_videos {
my ($channel_id) = @_;

my @results;
foreach my $method (grep { /^\w+\z/ } map { split(/,/, $_) } split(' ', $opt{subscription_results})) {

my $uploads = $yv_obj->$method($channel_id) // next;
my $videos = $uploads->{results} // [];

if (ref($videos) eq 'HASH' and exists $videos->{videos}) {
$videos = $videos->{videos};
}

if (ref($videos) eq 'HASH' and exists $videos->{entries}) {
$videos = $videos->{entries};
}

if (ref($videos) ne 'ARRAY') {
next;
}

push @results, @$videos;
}

@results ? \@results : undef;
}

sub print_local_subscription_videos {

state $t0 = time;
Expand Down Expand Up @@ -3118,24 +3147,10 @@ sub print_local_subscription_videos {
my $pid = $pm->start($id) // next;
next if ($pid != 0);

my $uploads = $yv_obj->uploads($id) // do {
my $videos = fetch_channel_latest_videos($id) // do {
$pm->finish(1, []);
next;
};
my $videos = $uploads->{results} // [];

if (ref($videos) eq 'HASH' and exists $videos->{videos}) {
$videos = $videos->{videos};
}

if (ref($videos) eq 'HASH' and exists $videos->{entries}) {
$videos = $videos->{entries};
}

if (ref($videos) ne 'ARRAY') {
$pm->finish(1, []);
next;
}

foreach my $video (@$videos) {
$video->{timestamp} = [@$time];
Expand All @@ -3152,22 +3167,8 @@ sub print_local_subscription_videos {
local $| = 1;
printf("[%d/%d] Retrieving info for $channels[$i][1]...\r", $i + 1, $#channels + 1);

my $id = $channels[$i][0] // next;
my $uploads = $yv_obj->uploads($id) // next;

my $videos = $uploads->{results} // [];

if (ref($videos) eq 'HASH' and exists $videos->{videos}) {
$videos = $videos->{videos};
}

if (ref($videos) eq 'HASH' and exists $videos->{entries}) {
$videos = $videos->{entries};
}

if (ref($videos) ne 'ARRAY') {
next;
}
my $id = $channels[$i][0] // next;
my $videos = fetch_channel_latest_videos($id) // next;

foreach my $video (@$videos) {
$video->{timestamp} = [@$time];
Expand Down Expand Up @@ -5157,6 +5158,14 @@ Absolute path to the file where to store saved channels (C<:save=i>).
Absolute path to the file where to store subscribed channels (C<:sub=i>).
=head2 subscription_results
Comma-separated list of methods used to retrieve videos from subscribed channels.
subscription_results => "uploads,streams", # include streams in feed
Valid values: uploads, streams, shorts.
=head2 subscriptions_lifetime
Amount of time, in seconds, before rescanning the subscribed channels for new videos (during C<-ls>).
Expand Down
36 changes: 20 additions & 16 deletions lib/WWW/PipeViewer/Worker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,27 @@ sub _handle_fetch_one_thumbnail {
return;
}

# Create the `_handle_fetch_uploads`, `_handle_fetch_streams` and `_handle_fetch_shorts` methods.
#<<<
sub _handle_fetch_subscriptions {
my ($self, $request) = @_;
my $id = $request->{id};
my $channels = $request->{args};
my $keep_alive = @$channels;
my @new_requests = map {
scalar {
id => $id,
priority => $request->{priority},
method => 'uploads',
args => [@$channels[$_]],
keep_alive => --$keep_alive,
}
} 0 .. $keep_alive - 1;
unshift @{$self->{_pending_requests}}, @new_requests;
return;
foreach my $method ('uploads', 'streams', 'shorts') {
no strict 'refs';
*{__PACKAGE__ . '::' . '_handle_fetch_' . $method} = sub {
my ($self, $request) = @_;
my $id = $request->{id};
my $channels = $request->{args};
my $keep_alive = @$channels;
my @new_requests = map {
scalar {
id => $id,
priority => $request->{priority},
method => $method,
args => [@$channels[$_]],
keep_alive => --$keep_alive,
}
} 0 .. $keep_alive - 1;
unshift @{$self->{_pending_requests}}, @new_requests;
return;
};
}
#>>>

Expand Down

0 comments on commit 2a3df2a

Please sign in to comment.