Skip to content

Commit 5359112

Browse files
committed
- Added support for next pages of comments with yt-dlp. (closes #23)
1 parent f29047c commit 5359112

File tree

4 files changed

+94
-25
lines changed

4 files changed

+94
-25
lines changed

bin/gtk-pipe-viewer

+14-10
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ my %CONFIG = (
256256
# yt-dlp comment options
257257
ytdlp_cmd => undef, # auto-detect
258258
ytdlp_comments => 0,
259-
ytdlp_max_comments => 50,
259+
ytdlp_max_comments => 20,
260260
ytdlp_max_comment_depth => 1,
261261

262262
tooltips => 1,
@@ -3431,7 +3431,7 @@ sub get_player_command {
34313431
),
34323432

34333433
# Rest of the arguments
3434-
grep({ defined($_) and /\S/ } values %MPLAYER)
3434+
(grep { defined($_) and /\S/ } values %MPLAYER)
34353435
)
34363436
);
34373437

@@ -3799,7 +3799,9 @@ sub display_comments {
37993799
my $comment_age = $yv_utils->get_publication_age($comment);
38003800

38013801
if ($comment_age) {
3802-
$comment_age = "$comment_age ago";
3802+
if ($comment_age !~ / ago\b/) {
3803+
$comment_age = "$comment_age ago";
3804+
}
38033805
}
38043806
else {
38053807
$comment_age = $yv_utils->get_publication_date($comment) // 'unknown';
@@ -3820,13 +3822,15 @@ sub display_comments {
38203822
)
38213823
);
38223824

3823-
my $iter = $feeds_liststore->append;
3824-
$feeds_liststore->set(
3825-
$iter,
3826-
0 => $comment_text,
3827-
3 => $video_id,
3828-
4 => $comment_id,
3829-
);
3825+
if (not $comment->{_hidden}) {
3826+
my $iter = $feeds_liststore->append;
3827+
$feeds_liststore->set(
3828+
$iter,
3829+
0 => $comment_text,
3830+
3 => $video_id,
3831+
4 => $comment_id,
3832+
);
3833+
}
38303834

38313835
if (exists($comment->{replies}) and ref($comment->{replies}) eq 'ARRAY') {
38323836

bin/pipe-viewer

+11-10
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ my %CONFIG = (
280280
# yt-dlp comment options
281281
ytdlp_cmd => undef, # auto-defined
282282
ytdlp_comments => 0,
283-
ytdlp_max_comments => 20,
283+
ytdlp_max_comments => 10,
284284
ytdlp_max_comment_depth => 1,
285285

286286
# Custom layout
@@ -1657,7 +1657,7 @@ for (my $i = 0 ; $i <= $#ARGV ; $i++) {
16571657
}
16581658
}
16591659

1660-
if (my @keywords = grep chr ord ne q{-}, @ARGV) {
1660+
if (my @keywords = grep { substr($_, 0, 1) ne q{-} } @ARGV) {
16611661
print_videos($yv_obj->search_videos(\@keywords));
16621662
}
16631663
elsif ($opt{interactive} and -t) {
@@ -1851,9 +1851,7 @@ sub parse_options2 {
18511851
my ($args, $keywords) = _parse_options($input);
18521852

18531853
my @args =
1854-
map $args->{$_} eq '0' ? "--no-$_"
1855-
: $args->{$_} eq '1' ? "--$_"
1856-
: "--$_=$args->{$_}" => keys %{$args};
1854+
map { $args->{$_} eq '0' ? "--no-$_" : $args->{$_} eq '1' ? "--$_" : "--$_=$args->{$_}" } keys %{$args};
18571855

18581856
return wantarray ? (\@args, [split q{ }, $keywords]) : \@args;
18591857
}
@@ -2698,7 +2696,9 @@ sub print_comments {
26982696
my $comment_age = $yv_utils->get_publication_age($comment);
26992697

27002698
if ($comment_age) {
2701-
$comment_age = "$comment_age ago";
2699+
if ($comment_age !~ / ago\b/) {
2700+
$comment_age = "$comment_age ago";
2701+
}
27022702
}
27032703
else {
27042704
$comment_age = $yv_utils->get_publication_date($comment) // 'unknown';
@@ -2713,7 +2713,8 @@ sub print_comments {
27132713
s_tab => q{ } x 3,
27142714
text => [$yv_utils->get_comment_content($comment) // 'Empty comment...']
27152715
),
2716-
);
2716+
)
2717+
if not $comment->{_hidden};
27172718

27182719
if (exists($comment->{replies}) and ref($comment->{replies}) eq 'ARRAY') {
27192720
foreach my $reply (@{$comment->{replies}}) {
@@ -3271,7 +3272,7 @@ sub print_playlists {
32713272
}
32723273
}
32733274

3274-
my $contains_keywords = grep /$non_digit_or_opt_re/, @keywords;
3275+
my $contains_keywords = grep { /$non_digit_or_opt_re/ } @keywords;
32753276

32763277
my @for_search;
32773278
foreach my $key (@keywords) {
@@ -3771,7 +3772,7 @@ sub get_player_command {
37713772
),
37723773

37733774
# Rest of the arguments
3774-
grep({ defined($_) and /\S/ } values %MPLAYER)
3775+
(grep { defined($_) and /\S/ } values %MPLAYER)
37753776
)
37763777
);
37773778

@@ -4271,7 +4272,7 @@ sub print_videos {
42714272
state @for_play;
42724273

42734274
my @copy_of_keywords = @keywords;
4274-
my $contains_keywords = grep /$non_digit_or_opt_re/, @keywords;
4275+
my $contains_keywords = grep { /$non_digit_or_opt_re/ } @keywords;
42754276

42764277
while (@keywords) {
42774278
my $key = shift @keywords;

lib/WWW/PipeViewer.pm

+5
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,11 @@ sub next_page_with_token {
14551455
return $token->();
14561456
}
14571457

1458+
if ($token =~ /^ytdlp:comments:(.*?):(\d+):(.*?):(.*)/) {
1459+
my ($video_id, $page, $prev_root_comment_id, $prev_comment_id) = ($1, $2, $3, $4);
1460+
return $self->comments_from_ytdlp($video_id, $page, $prev_root_comment_id, $prev_comment_id);
1461+
}
1462+
14581463
if ($token =~ /^yt(search|browse):(\w+):(.*)/) {
14591464
if ($1 eq 'browse') {
14601465
return $self->yt_browse_next_page($url, $3, type => $2, url => $url);

lib/WWW/PipeViewer/CommentThreads.pm

+64-5
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ sub _make_commentThreads_url {
2929
}
3030

3131
sub comments_from_ytdlp {
32-
my ($self, $video_id) = @_;
32+
my ($self, $video_id, $page, $prev_root_comment_id, $prev_comment_id) = @_;
33+
34+
$page //= 1;
3335

3436
my $max_comments = $self->get_ytdlp_max_comments;
3537
my $max_comment_depth = $self->get_ytdlp_max_comment_depth;
3638
my $comments_order = $self->get_comments_order;
3739
my $ytdlp_cmd = $self->get_ytdlp_cmd;
3840

41+
my $max_comments_per_page = $max_comments;
42+
$max_comments = $page * $max_comments;
43+
3944
my @cmd = (
4045
$ytdlp_cmd,
4146
'--write-comments',
@@ -58,7 +63,51 @@ sub comments_from_ytdlp {
5863
(ref($info) eq 'HASH' and exists($info->{comments}) and ref($info->{comments}) eq 'ARRAY')
5964
|| return;
6065

61-
my @comments = @{$info->{comments}};
66+
my @comments = @{$info->{comments}};
67+
my $comment_count = $info->{comment_count} // scalar(@comments);
68+
69+
my $last_comment_id = undef;
70+
my $last_root_comment_id = undef;
71+
72+
if (@comments) {
73+
$last_comment_id = $comments[-1]{id};
74+
}
75+
76+
for (my $i = $#comments ; $i >= 0 ; --$i) {
77+
my $comment = $comments[$i];
78+
if ($comment->{parent} eq 'root') {
79+
$last_root_comment_id = $comment->{id};
80+
last;
81+
}
82+
}
83+
84+
$last_comment_id //= $prev_comment_id // '';
85+
$last_root_comment_id //= $prev_root_comment_id // '';
86+
87+
if ($page > 1) {
88+
my $index = 0;
89+
my $prev_root_comment;
90+
91+
foreach my $i (0 .. $#comments) {
92+
my $comment = $comments[$i];
93+
94+
if ($prev_root_comment_id and $comment->{id} eq $prev_root_comment_id) {
95+
$prev_root_comment = $comment;
96+
}
97+
98+
if ($prev_comment_id and $comment->{id} eq $prev_comment_id) {
99+
$index = $i + 1;
100+
last;
101+
}
102+
}
103+
104+
@comments = splice(@comments, $index);
105+
106+
if (defined($prev_root_comment)) {
107+
$prev_root_comment->{_hidden} = 1;
108+
unshift @comments, $prev_root_comment;
109+
}
110+
}
62111

63112
my %table;
64113
foreach my $comment (@comments) {
@@ -78,12 +127,22 @@ sub comments_from_ytdlp {
78127
}
79128
}
80129

130+
my $url = undef;
131+
my $continuation = undef;
132+
133+
if ($comment_count >= $max_comments) {
134+
my $next_page = $page + 1;
135+
$url = 'https://yt-dlp';
136+
$continuation = join(':', 'ytdlp:comments', $video_id, $next_page, $last_root_comment_id, $last_comment_id);
137+
}
138+
81139
scalar {
82140
results => {
83-
comments => \@formatted_comments,
84-
videoId => $video_id,
141+
comments => \@formatted_comments,
142+
videoId => $video_id,
143+
continuation => $continuation,
85144
},
86-
url => undef,
145+
url => $url,
87146
};
88147
}
89148

0 commit comments

Comments
 (0)