2ch/Plugin/Base.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 165 CC 20 SCAR 35.2
CC
20
Cov
0%
get_dat
72.0
parse_date
56.0
gen_conf
12.0
CRAP 33.7
1 package WWW::2ch::Plugin::Base;
2 use strict;
3 our $VERSION = '0.06';
4  
5 use base qw( Class::Accessor::Fast );
6 __PACKAGE__->mk_accessors( qw( conf config ) );
7  
8 use POSIX;
9  
10 sub new {
11 my $class = shift;
12 my $conf = shift;
13  
14 my $self = bless {}, $class;
15 $conf = { $conf } unless ref($conf) eq 'HASH';
16 $self->conf($conf);
17
18 $self;
19 }
20  
21 sub encoding { 'shiftjis' }
22  
23 sub gen_conf {
24 my $self = shift;
25 my $conf = shift;
26  
27 my $url = $conf->{url};
28 my ($host, $bbs, $key);
29 if ($url =~ m|^http://([\w\d]+)\.2ch\.net/test/read.cgi/([^/]+)/(\d+)/|i) {
30 ($host, $bbs, $key) = ($1, $2, $3);
31 } elsif ($url =~ m|^http://([\w\d]+)\.2ch\.net/([^/]+)/|i) {
32 ($host, $bbs) = ($1, $2);
33 } else {
34 die 'url format error.';
35 }
36  
37 $self->config(+{
38 host => $host,
39 domain => "$host.2ch.net",
40 bbs => $bbs,
41 key => $key,
42 setting => "http://$host.2ch.net/$bbs/SETTING.TXT",
43 subject => "http://$host.2ch.net/$bbs/subject.txt",
44 dat => "http://$host.2ch.net/$bbs/dat/$key.dat",
45 local_path => "$host.2ch.net/$bbs/",
46 });
47 $self->config;
48 }
49  
50 sub daturl {
51 my ($self, $key) = @_;
52 'http://' . $self->config->{domain} . '/' . $self->config->{bbs} . "/dat/$key.dat";
53 }
54  
55 sub permalink {
56 my ($self, $key, $resid) = @_;
57 if ($key) {
58 if ($resid) {
59 return 'http://' . $self->config->{domain} . '/test/read.cgi/' . $self->config->{bbs} . "/$key/$resid";
60 } else {
61 return 'http://' . $self->config->{domain} . '/test/read.cgi/' . $self->config->{bbs} . "/$key/";
62 }
63 } else {
64 return 'http://' . $self->config->{domain} . '/' . $self->config->{bbs} . '/';
65 }
66 }
67  
68 sub get_dat {
69 my ($self, $c) = @_;
70  
71 my ($res, $data);
72 my $cache = $c->get_cache;
73 if ($cache->{data}) {
74 $res = $c->c->ua->diff_request($c->url, time => $cache->{time}, size => (length($cache->{data}) - 1));
75 $data = $res->content;
76 if ($res->code eq '206') {
77 if ($data =~ s/^\n//) {
78 $data = $cache->{data} . $data;
79 $c->set_cache($data, $res);
80 } else {
81 $cache = undef;
82 }
83 } elsif ($res->code eq '304') {
84 $data = $cache->{data};
85 } elsif ($res->code eq '416') {
86 $cache = undef;
87 }
88 }
89 unless ($cache->{data}) {
90 $res = $c->c->ua->diff_request($c->url);
91 return unless $res->is_success;
92 my $data = $data = $res->content;
93 $c->set_cache($data, $res);
94 }
95 $data;
96 }
97  
98 sub parse_setting {
99 my ($self, $data) = @_;
100  
101 my $config;
102 my @list = split(/\n/, $data);
103 shift @list;
104 foreach (@list) {
105 my ($a, $b) = split(/=/, $_);
106 $config->{$a} = $b;
107 }
108  
109 $config->{title} = $config->{BBS_TITLE};
110 $config->{noname} = $config->{BBS_NONAME_NAME};
111 $config->{image} = $config->{BBS_TITLE_PICTURE};
112  
113 $config;
114 }
115  
116 sub parse_subject {
117 my ($self, $data) = @_;
118  
119 my @subject;
120 foreach (split(/\n/, $data)) {
121 /^(\d+).dat<>(.+?) \((\d+)\)$/;
122 push(@subject, +{
123 key => $1,
124 title => $2,
125 resnum => $3,
126 });
127 }
128 return \@subject;
129 }
130  
131 sub parse_dat {
132 my ($self, $data) = @_;
133  
134 my @dat;
135 my $i = 0;
136 foreach (split(/\n/, $data)) {
137 if (/^(.*?)<>(.*?)<>(.*?)<>(.*?)<>.*?$/) {
138 $i++;
139 my $res ={
140 name => $1,
141 mail => $2,
142 date => $3,
143 body => $4,
144 resid => $i,
145 };
146 my $date = $self->parse_date($res->{date});
147 $res->{$_} = $date->{$_} foreach (keys %{ $date });
148 push(@dat, $res);
149 }
150 }
151 return \@dat;
152 }
153  
154 sub parse_date {
155 my ($self, $data) = @_;
156  
157 my $ret = {
158 time => time,
159 id => '',
160 be => '',
161 };
162  
163 my ($y, $m, $d, $h, $i, $s) = (0, 0, 0, 0, 0, 0);
164 if ($data =~ m|(\d+)/(\d+)/(\d+)|) {
165 ($y, $m, $d) = ($1, $2, $3);
166 if ($data =~ m| (\d+):(\d+):(\d+)|) {
167 ($h, $i, $s) = ($1, $2, $3);
168 } elsif ($data =~ m| (\d+):(\d+)|) {
169 ($h, $i, $s) = ($1, $2, 0);
170 }
171 $y += 2000 if $y < 10;
172 $y -= 1900;
173 $m--;
174 $ret->{time} = mktime($s, $i, $h, $d, $m, $y);
175 }
176 if ($data =~ / ID:([^ ]+) ?/) {
177 $ret->{id} = $1;
178 }
179 if ($data =~ / BE:([^\- ]+)[\- ]?/) {
180 $ret->{be} = $1;
181 }
182 $ret;
183 }
184  
185 1;
186 __END__