Changeset 29211 for lang/perl/HappySync
- Timestamp:
- 01/29/09 09:45:46 (4 years ago)
- Location:
- lang/perl/HappySync/trunk/lib
- Files:
-
- 2 modified
-
HappySync.pm (modified) (3 diffs)
-
HappySync/Entry.pm (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/HappySync/trunk/lib/HappySync.pm
r29105 r29211 43 43 $self->{client} = $client; 44 44 $self->{xs} = $xs; 45 $self->{blogEntry} = new HappySync::Entry::Blog($client, $xs, 46 $self->{syncpoint}); 47 $self->{draftEntry} = new HappySync::Entry::Draft($client, $xs, 48 $self->{syncpoint}); 45 $self->{blogEntry} = HappySync::Entry::new_Blog($self); 46 $self->{draftEntry} = HappySync::Entry::new_Draft($self); 49 47 return 1; 50 48 } … … 72 70 if ($entry->onSiteIsNewer()) { 73 71 $entry->buildLocalPath(); 74 $entry->get(); 72 $entry->getRemoteXML(); 73 $entry->writeInto(); 75 74 } 76 75 } … … 87 86 88 87 my $entry = undef; 88 print STDERR "$tag\n"; 89 89 if (my ($year, $type, $rest, $blogRest, $blogDate, $blogName, $draftName) = 90 90 $tag =~ $self->{pattern4Tag}) { 91 91 if ($blogRest) { 92 92 $entry = $self->{blogEntry}; 93 $entry->setDateAnd AnchorName($blogDate, $blogName);93 $entry->setDateAndEntryID($blogDate, $blogName); 94 94 } 95 95 else { 96 96 $entry = $self->{draftEntry}; 97 $entry->setDateAnd AnchorName(q{}, $draftName);97 $entry->setDateAndEntryID(q{}, $draftName); 98 98 } 99 99 } 100 101 100 $entry->setOnSiteMetaData($meta); 102 101 -
lang/perl/HappySync/trunk/lib/HappySync/Entry.pm
r29106 r29211 9 9 use File::Path; 10 10 use Data::Dumper; 11 use Encode; 11 12 12 sub new($ $$) {13 my ($ class, $client, $xmlSimpleInstance, $syncpoint) = @_;14 bless{15 'client' => $client,16 'xs' => $xmlSimpleInstance,17 'syncpoint' => $syncpoint,13 sub new($) { 14 my ($parent) = @_; 15 return { 16 client => $parent->{client}, 17 xs => $parent->{xs}, 18 syncpoint => $parent->{syncpoint}, 18 19 }; 19 20 } 20 21 21 sub get($) {} 22 sub new_Blog($) { 23 my ($parent) = @_; 24 bless new($parent), 'HappySync::Entry::Blog'; 25 } 22 26 23 sub setDateAndAnchorName() { 27 sub new_Draft($) { 28 my ($parent) = @_; 29 bless new($parent), 'HappySync::Entry::Draft'; 30 } 31 32 sub getRemoteXML($) { 24 33 my $self = shift; 25 34 26 $self->{'date'} = shift; 27 $self->{'name'} = shift; 35 my $xml = q{}; 36 { 37 my $feed = $self->{client}->getFeed($self->{uri}); 38 if (! $feed) { 39 $self->setError("an empty feed: ".$self->{uri}."\n"); 40 return; 41 } 42 $xml = $self->{files}->{xml}->{value} = $feed->as_xml; 43 } 44 my $body = $self->{xs}->XMLin($xml); 45 $self->{files}->{html}->{value} = extractHTML($body); 46 $self->{files}->{source}->{value} = 47 extractTitle($body)."\n".extractSource($body); 48 } 49 50 sub writeIntoFile($) { 51 my $pair = shift; 52 my $path = $pair->{path}; 53 open my $fh, '>', $path or die $!; 54 print STDERR "\t->$path\n"; 55 print $fh encode('UTF-8', $pair->{value})."\n"; 56 close($fh); 57 } 58 59 sub writeInto($) { 60 my $self = shift; 61 my $files = $self->{files}; 62 if ($self->hasError()) { 63 writeIntoFile($files->{err}); 64 } 65 else { 66 writeIntoFile($files->{updated}); 67 writeIntoFile($files->{source}); 68 writeIntoFile($files->{html}) if $files->{html}->{value}; 69 writeIntoFile($files->{xml}); 70 } 71 } 72 73 sub setDateAndEntryID() { 74 my $self = shift; 75 76 $self->{date} = shift; 77 $self->{entry_id} = shift; 28 78 } 29 79 … … 32 82 my $meta = shift; 33 83 34 $self-> setURI($meta);84 $self->{uri} = $meta->{link}[0]->{href}; 35 85 $self->{updatedOnSite} = $meta->{updated}; 36 86 } 37 38 sub setURI($$) {};39 87 40 88 sub buildLocalPath($) { … … 45 93 mkpath($dirpath, 0, 0755); 46 94 die "dir error $dirpath" if (! -d $dirpath || ! -x $dirpath); 95 my $base = $self->{basepath}; 96 $self->{files} = 97 { 98 updated => { path => "$base.updated", value => $self->{updatedOnSite} }, 99 source => { path => "$base.txt", value => q{} }, 100 html => { path => "$base.html", value => q{} }, 101 xml => { path => "$base.xml", value => q{} }, 102 err => { path => "$base.err", value => q{} }, 103 }; 104 } 47 105 106 sub setError($$) { 107 my ($self, $value) = @_; 108 $self->{files}->{err}->{value} = $value; 109 } 110 sub hasError($) { 111 return shift->{files}->{err}->{value}; 48 112 } 49 113 … … 54 118 } 55 119 120 sub extractTitle($) { 121 return shift->{title} || q{}; 122 } 123 124 sub extractSource($) { 125 return shift->{'hatena:syntax'}->{content} || q{}; 126 } 127 128 sub extractHTML($) { 129 return shift->{content}->{content} || q{}; 130 } 131 132 56 133 ### 57 134 package HappySync::Entry::Blog; 58 135 use base qw/HappySync::Entry/; 59 60 use XML::Simple;61 use Encode;62 use Data::Dumper;63 64 sub new($$$) {65 my ($class, $client, $xmlSimpleInstance, $syncpoint) = @_;66 bless {67 'client' => $client,68 'xs' => $xmlSimpleInstance,69 'syncpoint' => $syncpoint,70 };71 }72 73 74 sub setURI($$) {75 my $self = shift;76 my $meta = shift;77 78 $self->{uri} = $meta->{link}[0]->{href};79 }80 136 81 137 sub prepareLocalPath($) { … … 85 141 my $dirpath = $self->{syncpoint}."d/$year$month"; 86 142 $self->{dirpath} = $dirpath; 87 $self->{filepath} = "$dirpath/$date-".$self->{name}.".txt"; 88 $self->{htmlpath} = "$dirpath/$date-".$self->{name}.".html"; 143 $self->{basepath} = "$dirpath/$date-".$self->{entry_id}; 89 144 } 90 145 91 sub 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 146 126 147 ### … … 128 149 use base qw/HappySync::Entry/; 129 150 130 use XML::Simple;131 use Encode;132 use Data::Dumper;133 134 sub new($$$) {135 my ($class, $client, $xmlSimpleInstance, $syncpoint) = @_;136 bless {137 'client' => $client,138 'xs' => $xmlSimpleInstance,139 'syncpoint' => $syncpoint,140 };141 }142 143 sub 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 152 151 sub prepareLocalPath($) { 153 152 my $self = shift; 154 153 my $dirpath = $self->{syncpoint}."d/draft"; 155 154 $self->{dirpath} = $dirpath; 156 $self->{ filepath} = "$dirpath/".$self->{name}.".txt";155 $self->{basepath} = "$dirpath/".$self->{entry_id}; 157 156 } 158 157 159 sub 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 158 184 159 1;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)