/[Grep]/lib/Grep/Import/ScrapBook.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/Grep/Import/ScrapBook.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 154 by dpavlin, Sun Jun 10 18:41:00 2007 UTC revision 179 by dpavlin, Mon Sep 3 16:06:28 2007 UTC
# Line 17  You can symlink your ScrapBook directory Line 17  You can symlink your ScrapBook directory
17    
18  or modify L<ScrapBookDir> path (relative to Grep installation static root).  or modify L<ScrapBookDir> path (relative to Grep installation static root).
19    
 =head1 SEE ALSO  
   
 L<http://amb.vis.ne.jp/mozilla/scrapbook/> - ScrapBook FireFox extension  
   
20  =cut  =cut
21    
22  use XML::Simple;  use XML::Simple;
23  use File::Slurp;  use File::Slurp;
24    use HTML::ResolveLink;
25    use HTML::TreeBuilder;
26  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
27    
28  sub import {  sub import {
29          my $self = shift;          my $self = shift;
30          my $search = shift or die "need search";  
31          die "search is ", ref($search), " and not Grep::Search" unless ($search->isa('Grep::Search'));          my $config = Jifty->config->app('Import')->{'ScrapBook'};
32    
33            if (! $config ) {
34                    Jifty->log->warn("skipping ScrapBook importer, no application->Import->ScrapBook config");
35                    return;
36            }
37    
38            # required parametars in config.yml
39            foreach my $param ( qw/Dir OwnerEmail/ ) {
40                    Jifty->log->die("can't find $param in Scrapbook config") unless defined ( $config->{$param} );
41            };
42    
43          my $dir =          my $dir =
44                  Jifty::Util->app_root . '/' .                  Jifty::Util->app_root . '/' .
45                  Jifty->config->framework('Web')->{'StaticRoot'} . '/' .                  Jifty->config->framework('Web')->{'StaticRoot'} . '/' . $config->{'Dir'};
                 Jifty->config->app('ScrapBookDir');  
46    
47          my $path = $dir . '/scrapbook.rdf';          my $path = $dir . '/scrapbook.rdf';
48          $path =~ s!//+!/!g;          $path =~ s!//+!/!g;
# Line 52  sub import { Line 59  sub import {
59    
60  #       warn "## original rdf -> ", dump( $rdf );  #       warn "## original rdf -> ", dump( $rdf );
61    
62          my $feed = Grep::Model::Feed->new();          my $owner = Grep::Model::User->new();
63            $owner->load_by_cols( email => $config->{OwnerEmail} );
64            die "can't find ScrapBookOwner ", $config->{OwnerEmail} unless ( $owner->id );
65    
66            Jifty->log->info( "Using user ", $owner->id, " from ", $owner->email, " for import" );
67    
68            my $feed = Grep::Model::Feed->new( current_user => $owner );
69    
70          $feed->load_or_create(          $feed->load_or_create(
71                  uri => 'file://' . $path,                  uri => 'file://' . $path,
72                  title => 'ScrapBook',                  title => 'ScrapBook',
73                  #source => 'Grep::Source',                  #source => 'Grep::Source',
74                    owner => $owner,
75          );          );
76    
77            my $search = Grep::Search->new;
78    
79            my $stats;
80    
81          foreach my $item ( @{ $rdf->{'RDF:Description'} } ) {          foreach my $item ( @{ $rdf->{'RDF:Description'} } ) {
82    
83                  warn "## item = ",dump( $item );                  $stats->{total}++;
84    
85                    #warn "## item = ",dump( $item );
86    
87                  my $hash;                  my $hash;
88                  foreach my $k ( keys %$item ) {                  foreach my $k ( keys %$item ) {
# Line 72  sub import { Line 93  sub import {
93                          $hash->{$n} = $item->{$k};                          $hash->{$n} = $item->{$k};
94                  }                  }
95                    
96                  warn "## hash = ", dump( $hash );                  #warn "## hash = ", dump( $hash );
97    
98    
99                  # fetch full-text content and import it                  # fetch full-text content and import it
100    
101                  my $content_path = $dir . '/data/' . $hash->{id} . '/index.html';                  my $rel_path = '/data/' . $hash->{id} . '/index.html';
102    
103                    my $content_path = $dir . $rel_path;
104                  if ( ! -r $content_path ) {                  if ( ! -r $content_path ) {
105                          Jifty->log->warn("can't import $content_path: $!");                          Jifty->log->warn("can't import $content_path: $!");
106                            $stats->{failure}++;
107                          next;                          next;
108                  }                  }
                 my $content = read_file( $content_path ) or  
                         die "can't read $content_path: $!";  
   
109    
110                  # create date from id                  # create date from id
111    
112                  my $dt;                  my $dt;
113                  if ( $hash->{id} =~ m/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ ) {                  if ( $hash->{id} =~ m/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ ) {
114                          $dt = DateTime->new(                          $dt = Jifty::DateTime->new(
115                                  year    => $1,                                  year    => $1,
116                                  month   => $2,                                  month   => $2,
117                                  day             => $3,                                  day             => $3,
# Line 100  sub import { Line 121  sub import {
121                                  #time_zone => 'UTC',                                  #time_zone => 'UTC',
122                          );                          );
123                  } else {                  } else {
124                          warn "can't parse date from ", $hash->{id};                          die "can't parse date from ", $hash->{id};
125                  }                  }
126    
127                    my $i = Grep::Model::Item->new( current_user => $owner );
128    
129                  my $i = Grep::Model::Item->new();                  if ( $i->load_by_cols(
                 my ($id,$msg) = $i->load_or_create(  
130                          in_feed => $feed,                          in_feed => $feed,
131                          title => $hash->{title},                          title => $hash->{title},
132                          link => $hash->{source},                          link => $hash->{source},
133                          content => $content,                  ) ) {
134                          issued => $hash->{id},                          $stats->{old}++;
135                  );                          Jifty->log->info("existing ", $i->id ," ", $i->link);
136                    } else {
137    
138                            my $content = read_file( $content_path ) or
139                                    die "can't read $content_path: $!";
140    
141                            my $tree = HTML::TreeBuilder->new or die "can't create html tree";
142                            $tree->parse( $content ) or die "can't parse fetched content";
143    
144                            my $body = $tree->look_down( '_tag', 'body' );
145    
146                            my $resolver = HTML::ResolveLink->new( base => '/static/' . $config->{Dir} . $rel_path );
147                            $content = $resolver->resolve( $body->as_HTML );
148    
149                            my ($ok,$msg) = $i->create(
150                                    in_feed => $feed,
151                                    created_on => $dt,
152                                    title => $hash->{title},
153                                    link => $hash->{source},
154                                    content => $content,
155                            );
156    
157                  warn ">> item $id $msg\n";  #                       $i->set_created_on( $dt );
158    
159                            if ( ! $ok ) {
160                                    Jifty->log->error( $msg );
161                                    $stats->{failure}++;
162                                    next;
163                            }
164    
165                            if ( $msg && $msg !~ m/^Found/ ) {
166                                    $stats->{new}++;
167                                    Jifty->log->info("created ", $i->id ," ", $i->link, " ", length( $content ), " bytes");
168                                    $search->add( $i, $owner->id );
169                            } else {
170                                    Jifty->log->info("skipped ", $i->id ," ", $i->link, " ", length( $content ), " bytes");
171                            }
172                    }
173          }          }
174    
175          return 1;          $search->finish;
176    
177            return $stats;
178  }  }
179    
180    =head1 SEE ALSO
181    
182    L<http://amb.vis.ne.jp/mozilla/scrapbook/> - ScrapBook FireFox extension
183    
184    =cut
185    
186  1;  1;

Legend:
Removed from v.154  
changed lines
  Added in v.179

  ViewVC Help
Powered by ViewVC 1.1.26