2ch/Plugin/Jbbs.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 111 CC 11 SCAR 24.7
CC
11
Cov
0%
parse_date
30.0
gen_conf
12.0
permalink
12.0
CRAP 11.8
1 package WWW::2ch::Plugin::Jbbs;
2 use strict;
3 our $VERSION = '0.03';
4  
5 use base qw( WWW::2ch::Plugin::Base );
6  
7 use POSIX;
8  
9 sub encoding { 'euc-jp' }
10  
11 sub gen_conf {
12 my $self = shift;
13 my $conf = shift;
14  
15 my $url = $conf->{url};
16 my ($host, $bbs, $key);
17 if ($url =~ m|^http://jbbs\.livedoor\.jp/bbs/read.cgi/([^/]+)/([^/]+)/(\d+)/|i) {
18 ($host, $bbs, $key) = ($1, $2, $3);
19 } elsif ($url =~ m|^http://jbbs\.livedoor\.jp/([^/]+)/([^/]+)/|i) {
20 ($host, $bbs) = ($1, $2);
21 } else {
22 die 'url format error.';
23 }
24 $self->config(+{
25 host => $host,
26 domain => 'jbbs.livedoor.jp',
27 bbs => $bbs,
28 key => $key,
29 setting => "http://jbbs.livedoor.jp/$host/$bbs/",
30 subject => "http://jbbs.livedoor.jp/$host/$bbs/subject.txt",
31 dat => "http://jbbs.livedoor.jp/bbs/rawmode.cgi/$host/$bbs/$key/",
32 local_path => "jbbs.livedoor.jp/$host/$bbs/",
33 });
34 $self->config;
35 }
36  
37 sub daturl {
38 my ($self, $key) = @_;
39 'http://' . $self->config->{domain} . '/bbs/rawmode.cgi/' . $self->config->{host} . '/' . $self->config->{bbs} . "/$key/";
40 }
41  
42 sub permalink {
43 my ($self, $key, $resid) = @_;
44 if ($key) {
45 if ($resid) {
46 return 'http://' . $self->config->{domain} . '/bbs/read.cgi/' . $self->config->{host} . '/' . $self->config->{bbs} . "/$key/$resid";
47 } else {
48 return 'http://' . $self->config->{domain} . '/bbs/read.cgi/' . $self->config->{host} . '/' . $self->config->{bbs} . "/$key/";
49 }
50 } else {
51 return $self->config->{setting};
52 }
53 }
54  
55 sub get_dat {
56 my ($self, $c) = @_;
57  
58 my $res = $c->c->ua->diff_request($c->url);
59 return unless $res->is_success;
60 my $data = $res->content;
61 $c->set_cache($data, $res);
62 $data;
63 }
64  
65 sub parse_setting {
66 my ($self, $data) = @_;
67  
68 my $config;
69 $data =~ m|<title>(.*?)</title>|;
70 $config->{title} = $1;
71 $config;
72 }
73  
74 sub parse_subject {
75 my ($self, $data) = @_;
76  
77 my @subject;
78 foreach (split(/\n/, $data)) {
79 /^(\d+).cgi,(.+?)\((\d+)\)$/;
80 push(@subject, +{
81 key => $1,
82 title => $2,
83 resnum => $3,
84 });
85 }
86 return \@subject;
87 }
88  
89 sub parse_dat {
90 my ($self, $data) = @_;
91  
92 my @dat;
93 foreach (split(/\n/, $data)) {
94 if (/^(.*?)<>(.*?)<>(.*?)<>(.*?)<>(.*?)<>(.*?)<>(.*?)$/i) {
95 my $res ={
96 resid => $1,
97 name => $2,
98 mail => $3,
99 date => $4,
100 body => $5,
101 id => $7,
102 };
103 my $date = $self->parse_date($res->{date});
104 $res->{$_} = $date->{$_} foreach (keys %{ $date });
105 push(@dat, $res);
106 }
107 }
108 return \@dat;
109 }
110  
111 sub parse_date {
112 my ($self, $data) = @_;
113  
114 my $ret = {
115 time => time,
116 };
117 my ($y, $m, $d, $h, $i, $s) = (0, 0, 0, 0, 0, 0);
118 if ($data =~ m|(\d+)/(\d+)/(\d+)|) {
119 ($y, $m, $d) = ($1, $2, $3);
120 if ($data =~ m| (\d+):(\d+):(\d+)|) {
121 ($h, $i, $s) = ($1, $2, $3);
122 } elsif ($data =~ m| (\d+):(\d+)|) {
123 ($h, $i, $s) = ($1, $2, 0);
124 }
125 $y += 2000 if $y < 10;
126 $y -= 1900;
127 $m--;
128 $ret->{time} = mktime($s, $i, $h, $d, $m, $y);
129 }
130 $ret;
131 }
132  
133 1;
134  
135 =head1 NAME
136  
137 WWW::2ch::Plugin::Jbbs - Peculiar processing to jbbs
138  
139 =head1 DESCRIPTION
140  
141 It takes charge of peculiar processing to jbbs.
142  
143 =head1 SEE ALSO
144  
145 L<WWW::2ch>, L<WWW::2ch::Plugin::Base>, L<http://jbbs.livedoor.jp/>
146  
147 =head1 AUTHOR
148  
149 Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt>
150  
151 =head1 COPYRIGHT AND LICENSE
152  
153 Copyright (C) 2006 by Kazuhiro Osawa
154  
155 This library is free software; you can redistribute it and/or modify
156 it under the same terms as Perl itself, either Perl version 5.8.5 or,
157 at your option, any later version of Perl 5 you may have available.
158  
159 =cut