root/lang/perl/HappySync/trunk/lib/HappySync/Entry.pm @ 29106

Revision 29106, 4.1 kB (checked in by unau, 6 years ago)
Line 
1package HappySync::Entry;
2use strict;
3use warnings;
4
5use XML::Atom::Entry;
6use XML::Atom::Client;
7use XML::Atom::Feed;
8use XML::Simple;
9use File::Path;
10use Data::Dumper;
11
12sub new($$$) {
13    my ($class, $client, $xmlSimpleInstance, $syncpoint) = @_;
14    bless {
15           'client'      => $client,
16           'xs'          => $xmlSimpleInstance,
17           'syncpoint'   => $syncpoint,
18       };
19}
20
21sub get($) {}
22
23sub setDateAndAnchorName() {
24    my $self = shift;
25
26    $self->{'date'}  = shift;
27    $self->{'name'} = shift;
28}
29
30sub setOnSiteMetaData($$) {
31    my $self = shift;
32    my $meta = shift;
33
34    $self->setURI($meta);
35    $self->{updatedOnSite} = $meta->{updated};
36}
37
38sub setURI($$) {};
39
40sub buildLocalPath($) {
41    my $self = shift;
42
43    $self->prepareLocalPath();
44    my $dirpath = $self->{dirpath};
45    mkpath($dirpath, 0, 0755);
46    die "dir error $dirpath" if (! -d $dirpath || ! -x $dirpath);
47
48}
49
50sub prepareLocalPath() {}
51
52sub onSiteIsNewer($) {
53    1;
54}
55
56###
57package HappySync::Entry::Blog;
58use base qw/HappySync::Entry/;
59
60use XML::Simple;
61use Encode;
62use Data::Dumper;
63
64sub new($$$) {
65    my ($class, $client, $xmlSimpleInstance, $syncpoint) = @_;
66    bless {
67           'client'      => $client,
68           'xs'          => $xmlSimpleInstance,
69           'syncpoint'   => $syncpoint,
70       };
71}
72
73
74sub setURI($$) {
75    my $self = shift;
76    my $meta = shift;
77
78    $self->{uri} = $meta->{link}[0]->{href};
79}
80
81sub prepareLocalPath($) {
82    my $self = shift;
83    my $date = $self->{date};
84    my ($year, $month, $day) = $date =~ /(\d{4})(\d{2})(\d{2})/o;
85    my $dirpath = $self->{syncpoint}."d/$year$month";
86    $self->{dirpath}  = $dirpath;
87    $self->{filepath} = "$dirpath/$date-".$self->{name}.".txt";
88    $self->{htmlpath} = "$dirpath/$date-".$self->{name}.".html";
89}
90
91sub get($) {
92    my $self = shift;
93
94    my $client = $self->{client};
95    my $feed = $client->getFeed($self->{uri});
96    if (! $feed) {
97        print STDERR "ERR URI:\t".$self->{uri}."\n";
98        return;
99    }
100    my $body = XMLin($feed->as_xml);
101
102    my $html   = $body->{content}->{content} || q{};
103    my $source = $body->{'hatena:syntax'}->{content} || q{};
104    my $title   = $body->{title} || q{};
105
106    {
107        my $filepath = $self->{htmlpath};
108        open my $fh, '>', $filepath or die $!;
109        print STDERR $filepath."\n";
110        print $fh encode('UTF-8', $title)."\n";
111        print $fh $self->{updatedOnSite}."\n";
112        print $fh encode('UTF-8', $html)."\n";
113        close($fh);
114    }
115    {
116        my $filepath = $self->{filepath};
117        open my $fh, '>', $filepath or die $!;
118        print STDERR $filepath."\n";
119        print $fh encode('UTF-8', $title)."\n";
120        print $fh $self->{updatedOnSite}."\n";
121        print $fh encode('UTF-8', $source)."\n";
122        close($fh);
123    }
124}
125
126###
127package HappySync::Entry::Draft;
128use base qw/HappySync::Entry/;
129
130use XML::Simple;
131use Encode;
132use Data::Dumper;
133
134sub new($$$) {
135    my ($class, $client, $xmlSimpleInstance, $syncpoint) = @_;
136    bless {
137           'client'      => $client,
138           'xs'          => $xmlSimpleInstance,
139           'syncpoint'   => $syncpoint,
140       };
141}
142
143sub setURI($$) {
144    my $self = shift;
145    my $meta = shift;
146
147#    print STDERR Dumper($meta->{link});
148    $self->{uri} = $meta->{link}[0]->{href};
149#    $self->{uri} = $meta->{link}->{href};
150}
151
152sub prepareLocalPath($) {
153    my $self = shift;
154    my $dirpath = $self->{syncpoint}."d/draft";
155    $self->{dirpath}  = $dirpath;
156    $self->{filepath} = "$dirpath/".$self->{name}.".txt";
157}
158
159sub get($) {
160    my $self = shift;
161
162    my $client = $self->{client};
163    my $feed = $client->getFeed($self->{uri});
164    if (! $feed) {
165        print STDERR "ERR URI:\t".$self->{uri}."\n";
166        return;
167    }
168    my $body = $self->{xs}->XMLin($feed->as_xml);
169
170    my $source  = $body->{content}->{content} || q{};
171    my $title   = $body->{title} || q{};
172
173    {
174        my $filepath = $self->{filepath};
175        open my $fh, '>', $filepath or die $!;
176        print STDERR $filepath."\n";
177        print $fh encode('UTF-8', $title)."\n";
178        print $fh $self->{updatedOnSite}."\n";
179        print $fh encode('UTF-8', $source)."\n";
180        close($fh);
181    }
182}
183
1841;
Note: See TracBrowser for help on using the browser.