Skip to content

Commit

Permalink
- Added support for comments (read-only)
Browse files Browse the repository at this point in the history
- Added support for subscribing to a channel (-S=channelID)
- Added the "--subs-order" option for youtube-viewer to change the order of subscriptions.
  • Loading branch information
trizen committed Oct 1, 2015
1 parent a2e86ec commit 8ae4e3d
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 162 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -6,6 +6,7 @@ lib/WWW/YoutubeViewer.pm
lib/WWW/YoutubeViewer/Activities.pm
lib/WWW/YoutubeViewer/Authentication.pm
lib/WWW/YoutubeViewer/Channels.pm
lib/WWW/YoutubeViewer/CommentThreads.pm
lib/WWW/YoutubeViewer/GetCaption.pm
lib/WWW/YoutubeViewer/GuideCategories.pm
lib/WWW/YoutubeViewer/Itags.pm
Expand Down
8 changes: 6 additions & 2 deletions META.json
Expand Up @@ -57,7 +57,7 @@
"provides" : {
"WWW::YoutubeViewer" : {
"file" : "lib/WWW/YoutubeViewer.pm",
"version" : "0.05"
"version" : "0.06"
},
"WWW::YoutubeViewer::Activities" : {
"file" : "lib/WWW/YoutubeViewer/Activities.pm",
Expand All @@ -71,6 +71,10 @@
"file" : "lib/WWW/YoutubeViewer/Channels.pm",
"version" : "0.01"
},
"WWW::YoutubeViewer::CommentThreads" : {
"file" : "lib/WWW/YoutubeViewer/CommentThreads.pm",
"version" : "0.01"
},
"WWW::YoutubeViewer::GetCaption" : {
"file" : "lib/WWW/YoutubeViewer/GetCaption.pm",
"version" : "0.02"
Expand Down Expand Up @@ -130,6 +134,6 @@
"http://dev.perl.org/licenses/"
]
},
"version" : "0.05",
"version" : "0.06",
"x_serialization_backend" : "JSON::PP version 2.27300"
}
7 changes: 5 additions & 2 deletions META.yml
Expand Up @@ -16,7 +16,7 @@ name: WWW-YoutubeViewer
provides:
WWW::YoutubeViewer:
file: lib/WWW/YoutubeViewer.pm
version: '0.05'
version: '0.06'
WWW::YoutubeViewer::Activities:
file: lib/WWW/YoutubeViewer/Activities.pm
version: '0.01'
Expand All @@ -26,6 +26,9 @@ provides:
WWW::YoutubeViewer::Channels:
file: lib/WWW/YoutubeViewer/Channels.pm
version: '0.01'
WWW::YoutubeViewer::CommentThreads:
file: lib/WWW/YoutubeViewer/CommentThreads.pm
version: '0.01'
WWW::YoutubeViewer::GetCaption:
file: lib/WWW/YoutubeViewer/GetCaption.pm
version: '0.02'
Expand Down Expand Up @@ -92,5 +95,5 @@ requires:
URI::Escape: '0'
resources:
license: http://dev.perl.org/licenses/
version: '0.05'
version: '0.06'
x_serialization_backend: 'CPAN::Meta::YAML version 0.016'
67 changes: 40 additions & 27 deletions bin/gtk-youtube-viewer
Expand Up @@ -18,7 +18,7 @@
#-------------------------------------------------------
# GTK Youtube Viewer
# Created on: 12 September 2010
# Latest edit on: 05 August 2015
# Latest edit on: 01 October 2015
# Website: http://github.com/trizen/youtube-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -129,11 +129,12 @@ my %symbols = (
my %CONFIG = (

# Combobox values
active_resolution_combobox => 0,
active_safeSearch_combobox => 1,
active_more_options_expander => 0,
active_panel_account_combobox => 0,
active_channel_type_combobox => 0,
active_resolution_combobox => 0,
active_safeSearch_combobox => 1,
active_more_options_expander => 0,
active_panel_account_combobox => 0,
active_channel_type_combobox => 0,
active_subscriptions_order_combobox => 0,

video_players => {
vlc => {
Expand Down Expand Up @@ -290,6 +291,7 @@ my %objects = (
'comboboxtext4' => \my $definition_combobox,
'comboboxtext5' => \my $safesearch_combobox,
'comboboxtext1' => \my $published_within_combobox,
'comboboxtext13' => \my $subscriptions_order_combobox,
'panel_user_entry' => \my $panel_user_entry,
'comboboxtext6' => \my $panel_account_type_combobox,
'comboboxtext2' => \my $order_combobox,
Expand Down Expand Up @@ -671,6 +673,7 @@ sub apply_configuration {
$clear_search_list_checkbox->set_active($CONFIG{clear_search_list});
$panel_account_type_combobox->set_active($CONFIG{active_panel_account_combobox});
$channel_type_combobox->set_active($CONFIG{active_channel_type_combobox});
$subscriptions_order_combobox->set_active($CONFIG{active_subscriptions_order_combobox});

$published_within_combobox->set_active(0);

Expand Down Expand Up @@ -1130,7 +1133,7 @@ sub set_comments {
return unless $videoID =~ /$valid_video_id_re/;

$feeds_liststore->clear;
print_comments($yv_obj->get_video_comments($videoID));
display_comments($yv_obj->comments_from_video_id($videoID));
}

# Feeds window
Expand All @@ -1142,8 +1145,7 @@ sub show_feeds_window {
$feeds_window->show;
$feeds_statusbar->pop(0);

## NEEDS WORK!!!
#print_comments($yv_obj->get_video_comments($videoID));
display_comments($yv_obj->comments_from_video_id($videoID));

return 1;
}
Expand Down Expand Up @@ -1220,6 +1222,11 @@ sub combobox_caption_changed {
$yv_obj->set_videoCaption($text);
}

sub combobox_subscriptions_order_changed {
$CONFIG{active_subscriptions_order_combobox} = $subscriptions_order_combobox->get_active;
$yv_obj->set_subscriptions_order($subscriptions_order_combobox->get_active_text);
}

sub combobox_panel_account_changed {
my $text = $panel_account_type_combobox->get_active_text;
$CONFIG{active_panel_account_combobox} = $panel_account_type_combobox->get_active;
Expand Down Expand Up @@ -2351,7 +2358,7 @@ sub comments_row_activated {
$feeds_liststore->remove($iter);
my $results = $yv_obj->next_page($value, comments => 1);
if ($yv_utils->has_entries($results)) {
print_comments($results);
display_comments($results);
}
else {
die "This is the last page of comments.\n";
Expand Down Expand Up @@ -2395,12 +2402,11 @@ sub favorite_video {
}

sub subscribe_channel {
my $user = get_channel_id_for_selected_video();
$feeds_statusbar->push(
0, $yv_obj->subscribe_channel($user)
? "Successfully subscribed to channel: $user."
: 'Error!'
);
my $channel_id = get_channel_id_for_selected_video();
$feeds_statusbar->push(0,
$yv_obj->subscribe_channel($channel_id)
? "Successfully subscribed to channel: $channel_id."
: 'Error!');
}

sub like_selected_video {
Expand All @@ -2426,34 +2432,41 @@ sub send_comment_to_video {
my $comment = get_text($gui->get_object('comment_textview'));

$feeds_statusbar->push(0,
length($comment) && $yv_obj->send_comment_to_video($videoID, $comment)
length($comment) && $yv_obj->comment_to_video_id($comment, $videoID)
? 'Video comment has been posted!'
: 'Error!');
}

sub print_comments {
my ($results, %options) = @_;
sub display_comments {
my ($results) = @_;

my $url = $results->{url};
my $comments = $results->{results};
my $res = $results->{results} // {};
my $comments = $res->{items} // [];

my $i = 0;
foreach my $comment (@{$comments}) {
my $snippet = (($comment->{snippet} // next)->{topLevelComment} // next)->{snippet};

my $iter = $feeds_liststore->append;
$feeds_liststore->set(
$iter,
0,
"<big><b>$comment->{name}</b> ("
. $yv_utils->format_date($comment->{published})
"<big><b>"
. encode_entities($snippet->{authorDisplayName})
. "</b> ("
. $yv_utils->format_date($snippet->{publishedAt})
. ") said:</big>\n\t"
. encode_entities($comment->{content})
. encode_entities($snippet->{textDisplay} // 'Empty comment...')
);
}

my $iter = $feeds_liststore->append;
$feeds_liststore->set($iter, 0, "\n<big><b>=&gt;&gt; NEXT PAGE</b></big>\n");
$feeds_liststore->set($iter, 1, $url);
#
## This needs work!!!
#
#my $iter = $feeds_liststore->append;
#$feeds_liststore->set($iter, 0, "\n<big><b>=&gt;&gt; NEXT PAGE</b></big>\n");
#$feeds_liststore->set($iter, 1, $url);

return 1;
}

Expand Down
54 changes: 38 additions & 16 deletions bin/youtube-viewer
Expand Up @@ -18,7 +18,7 @@
#-------------------------------------------------------
# Appname: youtube-viewer
# Created on: 02 June 2010
# Latest edit on: 06 August 2015
# Latest edit on: 01 October 2015
# Website: https://github.com/trizen/youtube-viewer
#-------------------------------------------------------
#
Expand Down Expand Up @@ -247,6 +247,8 @@ my %CONFIG = (
publishedAfter => undef,
order => undef,

subscriptions_order => 'relevance',

hl => 'en_US',
cats_region => 'us',

Expand Down Expand Up @@ -643,11 +645,14 @@ usage: $execname [options] ([url] | [keywords])
-R --recommended : show the recommended videos for you *
-S --subscriptions:s : show the subscribed channels *
-SV --subs-videos:s : show the subscription videos *
--subs-order=s : change the subscription order
valid values: alphabetical, relevance, unread
-L --likes : show the videos that you liked on YouTube *
--dislikes : show the videos that you disliked on YouTube *
* [POST] Personal
--subscribe=s : subscribe to a channel *
--user-subscribe=s : subscribe to a channel via username *
--favorite=s : favorite a YouTube video by URL or ID *
--like=s : send a 'like' rating to a video URL or ID *
--dislike=s : send a 'dislike' rating to a video URL or ID *
Expand Down Expand Up @@ -1024,8 +1029,8 @@ sub apply_configuration {
videoEmbeddable videoLicense
videoSyndicated channelId
publishedAfter publishedBefore
safeSearch regionCode
debug http_proxy hl page
safeSearch regionCode debug hl
http_proxy page subscriptions_order
)
) {

Expand Down Expand Up @@ -1096,6 +1101,10 @@ sub apply_configuration {
subscribe_to_channels(split(/[,\s]+/, delete $opt->{subscribe_channel}));
}

if (defined $opt->{subscribe_username}) {
subscribe_to_usernames(split(/[,\s]+/, delete $opt->{subscribe_username}));
}

if (defined $opt->{favorite_video}) {
favorite_videos(split(/[,\s]+/, delete $opt->{favorite_video}));
}
Expand Down Expand Up @@ -1317,11 +1326,13 @@ sub parse_arguments {

'subscriptions|S:s' => \$opt{subscriptions},
'subs-videos|SV:s' => \$opt{subscription_videos},
'subs-order=s' => \$opt{subscriptions_order},
'favorites|fv|favorited-videos|F:s' => \$opt{favorites},
'recommended|recommendations|R:s' => \$opt{recommendations},
'likes|L:s' => \$opt{likes},
'dislikes' => \$opt{dislikes},
'subscribe=s' => \$opt{subscribe_channel},
'user-subscribe=s' => \$opt{subscribe_username},
'favorite|favorite-video|fav=s' => \$opt{favorite_video},
'channel-suggestions' => \$opt{channel_suggestions},
'cv|channel|channel-videos=s' => \$opt{channel_id_videos},
Expand Down Expand Up @@ -1867,7 +1878,7 @@ sub get_and_print_related_videos {
sub get_and_print_comments {
foreach my $id (@_) {
my $videoID = get_valid_video_id($id) // next;
my $comments = $yv_obj->get_video_comments($videoID);
my $comments = $yv_obj->comments_from_video_id($videoID);
print_comments($comments, $videoID);
}
return 1;
Expand Down Expand Up @@ -1976,12 +1987,14 @@ sub get_and_print_videos_from_playlist {
return 1;
}

sub subscribe_to_channels {
sub subscribe_to {
my ($is_channel, @ids) = @_;

return if not authenticated();

foreach my $channel (@_) {
foreach my $channel (@ids) {
if ($channel =~ /$valid_username_re/) {
if ($yv_obj->subscribe_channel($channel)) {
if ($is_channel ? $yv_obj->subscribe_channel($channel) : $yv_obj->subscribe_channel_from_username($channel)) {
print "** Successfully subscribed to channel: $channel\n";
}
else {
Expand All @@ -1992,6 +2005,14 @@ sub subscribe_to_channels {
return 1;
}

sub subscribe_to_channels {
subscribe_to(1, @_);
}

sub subscribe_to_usernames {
subscribe_to(0, @_);
}

sub _bold_color {
my ($text) = @_;
return colored($text, 'bold');
Expand Down Expand Up @@ -2538,25 +2559,26 @@ sub print_shows {
sub print_comments {
my ($results, $videoID) = @_;

...; # NEEDS WORK!!!

if (not @{$results->{results}}) {
warn colored("\n[!] No comments found...", 'bold red') . "\n";
if (not $yv_utils->has_entries($results)) {
warn_no_results("comments");
}

my $url = $results->{url};
my $comments = $results->{results};
my $res = $results->{results} // {};
my $comments = $res->{items} // [];

my $i = 0;
foreach my $comment (@{$comments}) {
my $snippet = (($comment->{snippet} // next)->{topLevelComment} // next)->{snippet};

printf(
"\n%s on %s said:\n%s\n",
colored($comment->{name}, 'bold'),
$yv_utils->format_date($comment->{published}),
colored($snippet->{authorDisplayName}, 'bold'),
$yv_utils->format_date($snippet->{publishedAt}),
wrap_text(
i_tab => q{ } x 4,
s_tab => q{ } x 4,
text => [$comment->{content} // 'Empty comment...']
text => [$snippet->{textDisplay} // 'Empty comment...']
),
);
}
Expand Down Expand Up @@ -2593,7 +2615,7 @@ sub print_comments {
my $comment = do { local (@ARGV, $/) = $filename; <> };
$comment =~ s/[^\s[:^cntrl:]]+//g; # remove control characters

if (length($comment) and $yv_obj->send_comment_to_video($videoID, $comment)) {
if (length($comment) and $yv_obj->comment_to_video_id($comment, $videoID)) {
print "\n** Comment posted!\n";
}
else {
Expand Down

0 comments on commit 8ae4e3d

Please sign in to comment.