28 |
sub import { |
sub import { |
29 |
my $self = shift; |
my $self = shift; |
30 |
|
|
31 |
|
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; |
60 |
# warn "## original rdf -> ", dump( $rdf ); |
# warn "## original rdf -> ", dump( $rdf ); |
61 |
|
|
62 |
my $owner = Grep::Model::User->new(); |
my $owner = Grep::Model::User->new(); |
63 |
$owner->load_by_cols( email => Jifty->config->app('ScrapBookOwner') ); |
$owner->load_by_cols( email => $config->{OwnerEmail} ); |
64 |
die "can't find ScrapBookOwner ", Jifty->config->app('ScrapBookOwner') unless ( $owner->id ); |
die "can't find ScrapBookOwner ", $config->{OwnerEmail} unless ( $owner->id ); |
65 |
|
|
66 |
Jifty->log->info( "Using user ", $owner->id, " from ", $owner->email, " for import" ); |
Jifty->log->info( "Using user ", $owner->id, " from ", $owner->email, " for import" ); |
67 |
|
|
68 |
my $feed = Grep::Model::Feed->new( current_user => $owner ); |
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', |
106 |
$stats->{failure}++; |
$stats->{failure}++; |
107 |
next; |
next; |
108 |
} |
} |
|
my $content = read_file( $content_path ) or |
|
|
die "can't read $content_path: $!"; |
|
|
|
|
|
my $tree = HTML::TreeBuilder->new or die "can't create html tree"; |
|
|
$tree->parse( $content ) or die "can't parse fetched content"; |
|
|
|
|
|
my $body = $tree->look_down( '_tag', 'body' ); |
|
|
|
|
|
my $resolver = HTML::ResolveLink->new( base => '/static/' . Jifty->config->app('ScrapBookDir') . $rel_path ); |
|
|
$content = $resolver->resolve( $body->as_HTML ); |
|
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, |
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 ); |
my $i = Grep::Model::Item->new( current_user => $owner ); |
128 |
my ($ok,$msg) = $i->load_or_create( |
|
129 |
|
if ( $i->load_by_cols( |
130 |
in_feed => $feed, |
in_feed => $feed, |
131 |
title => $hash->{title}, |
title => $hash->{title}, |
132 |
link => $hash->{source}, |
link => $hash->{source}, |
133 |
content => $content, |
) ) { |
|
issued => $hash->{id}, |
|
|
); |
|
|
|
|
|
if ( ! $ok ) { |
|
|
Jifty->log->error( $msg ); |
|
|
$stats->{failure}++; |
|
|
next; |
|
|
} |
|
|
|
|
|
if ( $msg && $msg =~ m/^Found/ ) { |
|
134 |
$stats->{old}++; |
$stats->{old}++; |
135 |
|
Jifty->log->info("existing ", $i->id ," ", $i->link); |
136 |
} else { |
} else { |
|
$stats->{new}++; |
|
|
Jifty->log->info("created ", $i->id ," ", $i->link, " ", length( $content ), " bytes"); |
|
|
$search->add( $i, $owner->id ); |
|
|
} |
|
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 |
|
# $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 |
$search->finish; |
$search->finish; |