| 1 | package Plagger::Rule::Fresh; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Rule ); | ||
| 4 | |||
| 5 | sub init { | ||
| 6 | my $self = shift; | ||
| 7 | |||
| 8 | if (my $config = $self->{mtime}) { | ||
| 9 | my $path = $config->{path}; | ||
| 10 | |||
| 11 | # If autoupdate is set, automatically touch the file | ||
| 12 | my $now = time; | ||
| 13 | my $mtime = (stat($path))[9]; | ||
| 14 | |||
| 15 | if ($config->{autoupdate}) { | ||
| 16 | if ($mtime) { | ||
| 17 | utime $now, $now, $path or Plagger->context->error("$path: $!"); | ||
| 18 | } else { | ||
| 19 | open my $fh, ">", $path or Plagger->context->error("$path: $!"); | ||
| 20 | } | ||
| 21 | } else { | ||
| 22 | $mtime or Plagger->context->error("$path: $!") | ||
| 23 | } | ||
| 24 | $self->{timestamp} = $mtime || 0; | ||
| 25 | } else { | ||
| 26 | eval { | ||
| 27 | $self->{duration} = $self->init_duration(); | ||
| 28 | $self->{timestamp} = time - $self->{duration}; | ||
| 29 | }; | ||
| 30 | if ($@) { | ||
| 31 | Plagger->context->error("Parse duration error: $@"); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | $self->{compare_dt} = Plagger::Date->from_epoch(epoch => $self->{timestamp}); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub init_duration { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | my $duration = $self->{duration} || 120; # minutes | ||
| 42 | |||
| 43 | if ($duration =~ /^\d+$/) { | ||
| 44 | # if it's all digit, the unit is minutes | ||
| 45 | return $duration * 60; | ||
| 46 | } | ||
| 47 | |||
| 48 | eval { require Time::Duration::Parse }; | ||
| 49 | if ($@) { | ||
| 50 | Plagger->context->error("You need to install Time::Duration::Parse to use human readable timespec"); | ||
| 51 | } | ||
| 52 | |||
| 53 | Time::Duration::Parse::parse_duration($self->{duration}); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub id { | ||
| 57 | my $self = shift; | ||
| 58 | return "fresh:$self->{duration}sec"; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub as_title { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | my $duration = $self->{duration} | ||
| 65 | ? "within " . $self->duration_friendly | ||
| 66 | : "since " . $self->{compare_dt}->strftime('%Y/%m/%d %H:%M'); | ||
| 67 | |||
| 68 | return "updated " . $duration; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub duration_friendly { | ||
| 72 | my $self = shift; | ||
| 73 | eval { require Time::Duration }; | ||
| 74 | return $@ ? "$self->{duration} seconds" | ||
| 75 | : Time::Duration::duration($self->{duration}); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub dispatch { | ||
| 79 | my($self, $args) = @_; | ||
| 80 | |||
| 81 | my $date; | ||
| 82 | if ($args->{entry}) { | ||
| 83 | $date = $args->{entry}->date; | ||
| 84 | } elsif ($args->{feed}) { | ||
| 85 | $date = $args->{feed}->updated; | ||
| 86 | } else { | ||
| 87 | Plagger->context->error("No entry nor feed object in this plugin phase"); | ||
| 88 | } | ||
| 89 | |||
| 90 | # no date field ... should be Fresh, ugh. | ||
| 91 | $date ? $date >= $self->{compare_dt} : 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | __END__ |