/[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 155 by dpavlin, Sun Jun 10 19:20:59 2007 UTC revision 177 by dpavlin, Sat Aug 11 17:41:42 2007 UTC
# Line 21  or modify L<ScrapBookDir> path (relative Line 21  or modify L<ScrapBookDir> path (relative
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 48  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          $feed->load_or_create(          $feed->load_or_create(
70                  uri => 'file://' . $path,                  uri => 'file://' . $path,
71                  title => 'ScrapBook',                  title => 'ScrapBook',
72                  #source => 'Grep::Source',                  #source => 'Grep::Source',
73                    owner => $owner,
74          );          );
75    
76            my $search = Grep::Search->new;
77    
78          my $stats;          my $stats;
79    
80          foreach my $item ( @{ $rdf->{'RDF:Description'} } ) {          foreach my $item ( @{ $rdf->{'RDF:Description'} } ) {
# Line 77  sub import { Line 97  sub import {
97    
98                  # fetch full-text content and import it                  # fetch full-text content and import it
99    
100                  my $content_path = $dir . '/data/' . $hash->{id} . '/index.html';                  my $rel_path = '/data/' . $hash->{id} . '/index.html';
101    
102                    my $content_path = $dir . $rel_path;
103                  if ( ! -r $content_path ) {                  if ( ! -r $content_path ) {
104                          Jifty->log->warn("can't import $content_path: $!");                          Jifty->log->warn("can't import $content_path: $!");
105                          $stats->{failure}++;                          $stats->{failure}++;
# Line 86  sub import { Line 108  sub import {
108                  my $content = read_file( $content_path ) or                  my $content = read_file( $content_path ) or
109                          die "can't read $content_path: $!";                          die "can't read $content_path: $!";
110    
111                    my $tree = HTML::TreeBuilder->new or die "can't create html tree";
112                    $tree->parse( $content ) or die "can't parse fetched content";
113    
114                    my $body = $tree->look_down( '_tag', 'body' );
115    
116                    my $resolver = HTML::ResolveLink->new( base => '/static/' . $config->{Dir} . $rel_path );
117                    $content = $resolver->resolve( $body->as_HTML );
118    
119                  # create date from id                  # create date from id
120    
# Line 101  sub import { Line 130  sub import {
130                                  #time_zone => 'UTC',                                  #time_zone => 'UTC',
131                          );                          );
132                  } else {                  } else {
133                          warn "can't parse date from ", $hash->{id};                          die "can't parse date from ", $hash->{id};
134                  }                  }
135    
136                  my $i = Grep::Model::Item->new();                  my $i = Grep::Model::Item->new( current_user => $owner );
137                  my ($ok,$msg) = $i->load_or_create(                  my ($ok,$msg) = $i->load_or_create(
138                          in_feed => $feed,                          in_feed => $feed,
139                          title => $hash->{title},                          title => $hash->{title},
140                          link => $hash->{source},                          link => $hash->{source},
141                          content => $content,                          content => $content,
142                          issued => $hash->{id},  #                       created_on => $dt,
143                  );                  );
144    
145                    $i->set_created_on( $dt );
146    
147                  if ( ! $ok ) {                  if ( ! $ok ) {
148                          Jifty->log->error( $msg );                          Jifty->log->error( $msg );
149                          $stats->{failure}++;                          $stats->{failure}++;
# Line 123  sub import { Line 154  sub import {
154                          $stats->{old}++;                          $stats->{old}++;
155                  } else {                  } else {
156                          $stats->{new}++;                          $stats->{new}++;
157                          Jifty->log->info("imported ", $i->id ," ", $i->link, " ", length( $content ), " bytes");                          Jifty->log->info("created ", $i->id ," ", $i->link, " ", length( $content ), " bytes");
158                          $search->add( $i, $i->in_feed->owner->id );                          $search->add( $i, $owner->id );
159                  }                  }
160    
161          }          }
162    
163            $search->finish;
164    
165          return $stats;          return $stats;
166  }  }
167    

Legend:
Removed from v.155  
changed lines
  Added in v.177

  ViewVC Help
Powered by ViewVC 1.1.26