Changeset 29211 for lang/perl/HappySync

Show
Ignore:
Timestamp:
01/29/09 09:45:46 (4 years ago)
Author:
unau
Message:

changed local sile formats.

Location:
lang/perl/HappySync/trunk/lib
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/HappySync/trunk/lib/HappySync.pm

    r29105 r29211  
    4343    $self->{client} = $client; 
    4444    $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); 
    4947    return 1; 
    5048} 
     
    7270            if ($entry->onSiteIsNewer()) { 
    7371                $entry->buildLocalPath(); 
    74                 $entry->get(); 
     72                $entry->getRemoteXML(); 
     73                $entry->writeInto(); 
    7574            } 
    7675        } 
     
    8786 
    8887    my $entry = undef; 
     88    print STDERR "$tag\n"; 
    8989    if (my ($year, $type, $rest, $blogRest, $blogDate, $blogName, $draftName) = 
    9090        $tag =~ $self->{pattern4Tag}) { 
    9191        if ($blogRest) { 
    9292            $entry = $self->{blogEntry}; 
    93             $entry->setDateAndAnchorName($blogDate, $blogName); 
     93            $entry->setDateAndEntryID($blogDate, $blogName); 
    9494        } 
    9595        else { 
    9696            $entry = $self->{draftEntry}; 
    97             $entry->setDateAndAnchorName(q{},       $draftName); 
     97            $entry->setDateAndEntryID(q{},       $draftName); 
    9898        } 
    9999    } 
    100      
    101100    $entry->setOnSiteMetaData($meta); 
    102101 
  • lang/perl/HappySync/trunk/lib/HappySync/Entry.pm

    r29106 r29211  
    99use File::Path; 
    1010use Data::Dumper; 
     11use Encode; 
    1112 
    12 sub new($$$) { 
    13     my ($class, $client, $xmlSimpleInstance, $syncpoint) = @_; 
    14     bless { 
    15            'client'      => $client, 
    16            'xs'          => $xmlSimpleInstance, 
    17            'syncpoint'   => $syncpoint, 
     13sub new($) { 
     14    my ($parent) = @_; 
     15    return { 
     16           client      => $parent->{client}, 
     17           xs          => $parent->{xs}, 
     18           syncpoint   => $parent->{syncpoint}, 
    1819       }; 
    1920} 
    2021 
    21 sub get($) {} 
     22sub new_Blog($) { 
     23    my ($parent) = @_; 
     24    bless new($parent), 'HappySync::Entry::Blog'; 
     25} 
    2226 
    23 sub setDateAndAnchorName() { 
     27sub new_Draft($) { 
     28    my ($parent) = @_; 
     29    bless new($parent), 'HappySync::Entry::Draft'; 
     30} 
     31 
     32sub getRemoteXML($) { 
    2433    my $self = shift; 
    2534 
    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 
     50sub 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 
     59sub 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 
     73sub setDateAndEntryID() { 
     74    my $self = shift; 
     75 
     76    $self->{date}  = shift; 
     77    $self->{entry_id} = shift; 
    2878} 
    2979 
     
    3282    my $meta = shift; 
    3383 
    34     $self->setURI($meta); 
     84    $self->{uri} = $meta->{link}[0]->{href}; 
    3585    $self->{updatedOnSite} = $meta->{updated}; 
    3686} 
    37  
    38 sub setURI($$) {}; 
    3987 
    4088sub buildLocalPath($) { 
     
    4593    mkpath($dirpath, 0, 0755); 
    4694    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} 
    47105 
     106sub setError($$) { 
     107    my ($self, $value) = @_; 
     108    $self->{files}->{err}->{value} = $value; 
     109} 
     110sub hasError($) { 
     111    return shift->{files}->{err}->{value}; 
    48112} 
    49113 
     
    54118} 
    55119 
     120sub extractTitle($) { 
     121    return shift->{title} || q{}; 
     122} 
     123 
     124sub extractSource($) { 
     125    return shift->{'hatena:syntax'}->{content} || q{}; 
     126} 
     127 
     128sub extractHTML($) { 
     129    return shift->{content}->{content} || q{}; 
     130} 
     131 
     132 
    56133### 
    57134package HappySync::Entry::Blog; 
    58135use 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 } 
    80136 
    81137sub prepareLocalPath($) { 
     
    85141    my $dirpath = $self->{syncpoint}."d/$year$month"; 
    86142    $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}; 
    89144} 
    90145 
    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 } 
    125146 
    126147### 
     
    128149use base qw/HappySync::Entry/; 
    129150 
    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  
    152151sub prepareLocalPath($) { 
    153152    my $self = shift; 
    154153    my $dirpath = $self->{syncpoint}."d/draft"; 
    155154    $self->{dirpath}  = $dirpath; 
    156     $self->{filepath} = "$dirpath/".$self->{name}.".txt"; 
     155    $self->{basepath} = "$dirpath/".$self->{entry_id}; 
    157156} 
    158157 
    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 } 
    183158 
    1841591;