| 1 |
package Plagger::Plugin::Publish::2chdat; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use Encode; |
|---|
| 6 |
use File::Spec; |
|---|
| 7 |
|
|---|
| 8 |
sub register { |
|---|
| 9 |
my($self, $context) = @_; |
|---|
| 10 |
$context->register_hook( |
|---|
| 11 |
$self, |
|---|
| 12 |
'publish.feed' => \&feed, |
|---|
| 13 |
'publish.finalize' => \&finalize, |
|---|
| 14 |
); |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
sub init { |
|---|
| 18 |
my $self = shift; |
|---|
| 19 |
$self->SUPER::init(@_); |
|---|
| 20 |
|
|---|
| 21 |
my $dir = $self->conf->{dir}; |
|---|
| 22 |
unless (-e $dir && -d _) { |
|---|
| 23 |
mkdir $dir, 0755 or Plagger->context->error("$dir: $!"); |
|---|
| 24 |
mkdir File::Spec->catfile($dir, 'dat'), 0755 or Plagger->context->error("$dir/dat: $!"); |
|---|
| 25 |
} |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
sub feed { |
|---|
| 29 |
my($self, $context, $args) = @_; |
|---|
| 30 |
|
|---|
| 31 |
my $feed = $args->{feed}; |
|---|
| 32 |
my $out = File::Spec->catfile($self->conf->{dir}, 'dat', $feed->id_safe . ".dat"); |
|---|
| 33 |
$context->log(info => "Writing dat output to $out"); |
|---|
| 34 |
|
|---|
| 35 |
my $anonymous = decode("utf-8", $self->conf->{default_anonymous} || "名無しさん"); |
|---|
| 36 |
|
|---|
| 37 |
open my $fh, ">:encoding(shift_jis)", $out or $context->error("$out: $!"); |
|---|
| 38 |
printf $fh "%s<><>%s ID:%s<> %s<>%s\n", |
|---|
| 39 |
($feed->author || $feed->entries->[0]->author || $anonymous), |
|---|
| 40 |
$self->format_date( Plagger::Date->from_epoch(0) ), |
|---|
| 41 |
substr($feed->id_safe), 0, 8), |
|---|
| 42 |
$self->format_body($feed->description) . "<BR>" . $feed->link, |
|---|
| 43 |
$feed->title; |
|---|
| 44 |
|
|---|
| 45 |
for my $entry (reverse $feed->entries) { |
|---|
| 46 |
printf $fh "%s<><>%s ID:%s<> %s\n", |
|---|
| 47 |
($entry->author || $anonymous), |
|---|
| 48 |
$self->format_date( $entry->date || Plagger::Date->now ), |
|---|
| 49 |
substr($entry->id_safe, 0, 8), |
|---|
| 50 |
$self->format_body($entry->body) . "<P>" . $entry->link . "</P>"; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
close $fh; |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
if (my $mtime = $feed->entries->[0]->date) { |
|---|
| 57 |
utime $mtime->epoch, $mtime->epoch, $out; |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
sub finalize { |
|---|
| 62 |
my($self, $context, $args) = @_; |
|---|
| 63 |
|
|---|
| 64 |
my $out = File::Spec->catfile($self->conf->{dir}, 'subject.txt'); |
|---|
| 65 |
open my $fh, ">:encoding(shift_jis)", $out or $context->erorr("$out: $!"); |
|---|
| 66 |
for my $feed ($context->update->feeds) { |
|---|
| 67 |
printf $fh "%s.dat<>%s (%d)\n", $feed->id_safe, $feed->title, $feed->count; |
|---|
| 68 |
} |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
sub format_date { |
|---|
| 72 |
my($self, $date) = @_; |
|---|
| 73 |
my $clone = $date->clone; |
|---|
| 74 |
$clone->set_locale("ja_JP"); |
|---|
| 75 |
return $clone->strftime("%Y/%m/%d(%a) %H:%M:%S"); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
sub format_body { |
|---|
| 79 |
my($self, $body) = @_; |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
$body =~ s!<img .*?src="(.*?)".*?>!$1!ig; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
$body =~ s{<a .*?href="(.*?)".*?>(.*?)</a>} |
|---|
| 86 |
{my($url, $label) = ($1, $2); |
|---|
| 87 |
$label =~ m!^https?://! ? $url : "$label ($url)"}ieg; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
$body =~ s{(<pre>)(.*?)(</pre>)}{$1 . nl2br($2) . $3}seig; |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
$body =~ tr/\r\n//d; |
|---|
| 94 |
|
|---|
| 95 |
$body; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
sub nl2br { |
|---|
| 99 |
my $str = shift; |
|---|
| 100 |
$str =~ s/\r?\n/<BR>/g; |
|---|
| 101 |
$str; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
1; |
|---|
| 105 |
|
|---|
| 106 |
__END__ |
|---|
| 107 |
|
|---|
| 108 |
=head1 |
|---|
| 109 |
|
|---|
| 110 |
Plagger::Plugin::Publish::2chdat - Publish 2ch data files for 2ch browsers |
|---|
| 111 |
|
|---|
| 112 |
=head1 SYNOPSYS |
|---|
| 113 |
|
|---|
| 114 |
- module: Publish::2chdat |
|---|
| 115 |
config: |
|---|
| 116 |
dir: /home/miyagawa/public_html/2chdat |
|---|
| 117 |
|
|---|
| 118 |
=head1 DESCRIPTION |
|---|
| 119 |
|
|---|
| 120 |
This plugin publishes feed updates as 2ch browser compatible data |
|---|
| 121 |
files e.g. C<subject.txt> and C<*.dat> files. |
|---|
| 122 |
|
|---|
| 123 |
Note that this plugin might not work very well with Subscription |
|---|
| 124 |
plugins that manages unread management, e.g. Bloglines and Livedoor |
|---|
| 125 |
Reader. |
|---|
| 126 |
|
|---|
| 127 |
=head1 CONFIG |
|---|
| 128 |
|
|---|
| 129 |
=head2 dir |
|---|
| 130 |
|
|---|
| 131 |
Directory to save data files in. |
|---|
| 132 |
|
|---|
| 133 |
=head1 AUTHOR |
|---|
| 134 |
|
|---|
| 135 |
Tatsuhiko Miyagawa |
|---|
| 136 |
|
|---|
| 137 |
=head1 SEE ALSO |
|---|
| 138 |
|
|---|
| 139 |
L<Plagger>, L<http://age.s22.xrea.com/talk2ch/> |
|---|
| 140 |
|
|---|
| 141 |
=cut |
|---|