Skip to content

Commit 433b552

Browse files
committed
- Added the *AGE* token, which represents the age of a video.
Possible values: 1 second 2 seconds ... 1 minute 2 minutes ... 1 hour 2 hours ... 1 day 2 days ... 1 month 2 months ... 1 year 2 years ... Implements and closes #283
1 parent 24d1de4 commit 433b552

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

bin/gtk-youtube-viewer

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,8 @@ $accel->connect(ord('g'), ['control-mask'], ['visible'], \&show_user_favorited_v
13271327
$accel->connect(ord('m'), ['control-mask'], ['visible'], \&show_videos_from_selected_author);
13281328
$accel->connect(ord('k'), ['control-mask'], ['visible'], \&show_playlists_from_selected_author);
13291329
$accel->connect(ord('w'), ['control-mask'], ['visible'], \&show_warnings_window);
1330-
$accel->connect(0xffff, ['lock-mask'], ['visible'], \&delete_selected_row);
1331-
$accel->connect(0xffc8, ['lock-mask'], ['visible'], \&maximize_unmaximize_mainw);
1330+
$accel->connect(0xffff, ['lock-mask'], ['visible'], \&delete_selected_row);
1331+
$accel->connect(0xffc8, ['lock-mask'], ['visible'], \&maximize_unmaximize_mainw);
13321332
$mainw->add_accel_group($accel);
13331333

13341334
# Support for navigating back and forth using the side buttons of the mouse
@@ -1357,7 +1357,7 @@ $accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_feeds_window);
13571357
$feeds_window->add_accel_group($accel);
13581358

13591359
$accel = Gtk2::AccelGroup->new;
1360-
$accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_preferences_window);
1360+
$accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_preferences_window);
13611361
$accel->connect(ord('s'), ['control-mask'], ['visible'], \&save_configuration);
13621362
$prefernces_window->add_accel_group($accel);
13631363

@@ -1568,7 +1568,7 @@ sub save_configuration {
15681568
my $hash_ref = eval $config;
15691569

15701570
print STDERR $@ if $@;
1571-
die $@ if $@;
1571+
die $@ if $@;
15721572

15731573
%CONFIG = (%CONFIG, %{$hash_ref});
15741574
dump_configuration();
@@ -2454,7 +2454,7 @@ sub set_thumbnail {
24542454
my ($entry, $liststore, $iter) = @{$_[0]};
24552455

24562456
my $thumb_url = $yv_utils->get_thumbnail_url($entry, $CONFIG{thumbnail_type});
2457-
my $pixbuf = get_pixbuf_thumbnail($thumb_url, ($yv_utils->is_channel($entry) ? (160, 160) : ()));
2457+
my $pixbuf = get_pixbuf_thumbnail($thumb_url, ($yv_utils->is_channel($entry) ? (160, 160) : ()));
24582458
$liststore->set_value($iter, 1, $pixbuf);
24592459

24602460
return 0;

bin/youtube-viewer

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ my %CONFIG = (
338338
convert_to => undef,
339339

340340
custom_layout => 0,
341-
custom_layout_format => [{width => 3, text => "*NO*.", color => 'bold', align => 'right'},
341+
custom_layout_format => [{width => 3, text => "*NO*.", color => 'bold', align => 'right'},
342342
{width => '60%', text => "*TITLE*"},
343343
{width => '20%', text => "*AUTHOR*", color => 'green', align => 'right'},
344344
{width => 8, text => "*TIME*", color => 'blue', align => 'right'},
@@ -1901,7 +1901,7 @@ sub favorite_videos {
19011901
sub select_and_save_to_playlist {
19021902
return if not authenticated();
19031903

1904-
my $request = $yv_obj->my_playlists() // last;
1904+
my $request = $yv_obj->my_playlists() // last;
19051905
my $playlistID = print_playlists($request, return_playlist_id => 1);
19061906

19071907
if (defined($playlistID)) {
@@ -3132,6 +3132,7 @@ sub save_watched_video {
31323132
my ($video_id) = @_;
31333133

31343134
if ($opt{remember_watched} and not exists($watched_videos{$video_id})) {
3135+
$watched_videos{$video_id} = 1;
31353136
open my $fh, '>>', $opt{watched_file} or return;
31363137
say {$fh} $video_id;
31373138
close $fh;

lib/WWW/YoutubeViewer/Utils.pm

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,53 @@ sub format_date {
148148
return $date;
149149
}
150150

151+
=head2 date_to_time($date)
152+
153+
Return the (approximated) age for a given date of the form "2010-05-04T00:25:55.000Z".
154+
155+
=cut
156+
157+
sub date_to_age {
158+
my ($self, $date) = @_;
159+
160+
$date =~ m{^
161+
(?<year>\d{4})
162+
-
163+
(?<month>\d{2})
164+
-
165+
(?<day>\d{2})
166+
[a-zA-Z]
167+
(?<hour>\d{2})
168+
:
169+
(?<min>\d{2})
170+
:
171+
(?<sec>\d{2})
172+
}x || return undef;
173+
174+
my ($sec, $min, $hour, $day, $month, $year) = gmtime(time);
175+
176+
$year += 1900;
177+
$month += 1;
178+
179+
if ($year == $+{year}) {
180+
if ($month == $+{month}) {
181+
if ($day == $+{day}) {
182+
if ($hour == $+{hour}) {
183+
if ($min == $+{min}) {
184+
return join(' ', $sec - $+{sec}, 'seconds');
185+
}
186+
return join(' ', $min - $+{min}, 'minutes');
187+
}
188+
return join(' ', $hour - $+{hour}, 'hours');
189+
}
190+
return join(' ', $day - $+{day}, 'days');
191+
}
192+
return join(' ', $month - $+{month}, 'months');
193+
}
194+
195+
return join(' ', $year - $+{year}, 'years');
196+
}
197+
151198
=head2 has_entries($request)
152199
153200
Returns true if a given request has entries.
@@ -221,6 +268,7 @@ sub format_text {
221268
FTITLE => sub { $self->normalize_video_title($self->get_title($info), $fat32safe) },
222269
CAPTION => sub { $self->get_caption($info) },
223270
PUBLISHED => sub { $self->get_publication_date($info) },
271+
AGE => sub { $self->get_publication_age($info) },
224272
DESCRIPTION => sub { $self->get_description($info) },
225273
226274
RATING => sub {
@@ -424,6 +472,18 @@ sub get_publication_date {
424472
$self->format_date($info->{snippet}{publishedAt});
425473
}
426474

475+
sub get_publication_age {
476+
my ($self, $info) = @_;
477+
478+
my $age = $self->date_to_age($info->{snippet}{publishedAt});
479+
480+
if ($age =~ /^1\s/) { # singular mode
481+
$age =~ s/s\z//;
482+
}
483+
484+
return $age;
485+
}
486+
427487
sub get_duration {
428488
my ($self, $info) = @_;
429489
$self->format_duration($info->{contentDetails}{duration});

0 commit comments

Comments
 (0)