1 |
dpavlin |
154 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
|
|
package Grep::Import::ScrapBook; |
7 |
|
|
|
8 |
|
|
=head1 NAME |
9 |
|
|
|
10 |
|
|
Grep::Import::ScrapBook - importer for local ScrapBook pages |
11 |
|
|
|
12 |
|
|
=head1 CONFIGURATION |
13 |
|
|
|
14 |
|
|
You can symlink your ScrapBook directory |
15 |
|
|
|
16 |
|
|
~/Grep/share/web/static$ ln -sf /home/dpavlin/private/ScrapBook scrapbook |
17 |
|
|
|
18 |
|
|
or modify L<ScrapBookDir> path (relative to Grep installation static root). |
19 |
|
|
|
20 |
|
|
=cut |
21 |
|
|
|
22 |
|
|
use XML::Simple; |
23 |
|
|
use File::Slurp; |
24 |
|
|
use Data::Dump qw/dump/; |
25 |
|
|
|
26 |
|
|
sub import { |
27 |
|
|
my $self = shift; |
28 |
|
|
my $search = shift or die "need search"; |
29 |
|
|
die "search is ", ref($search), " and not Grep::Search" unless ($search->isa('Grep::Search')); |
30 |
|
|
|
31 |
|
|
my $dir = |
32 |
|
|
Jifty::Util->app_root . '/' . |
33 |
|
|
Jifty->config->framework('Web')->{'StaticRoot'} . '/' . |
34 |
|
|
Jifty->config->app('ScrapBookDir'); |
35 |
|
|
|
36 |
|
|
my $path = $dir . '/scrapbook.rdf'; |
37 |
|
|
$path =~ s!//+!/!g; |
38 |
|
|
|
39 |
|
|
if ( ! -e $dir || ! -e $path ) { |
40 |
|
|
Jifty->log->warn("Skipping ScrapBook import $path: $!"); |
41 |
|
|
return 1; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
my $rdf = XMLin( |
45 |
|
|
$path, |
46 |
|
|
# KeyAttr => [ qw/RDF:about/ ], |
47 |
|
|
) || die "can't open $path: $!"; |
48 |
|
|
|
49 |
|
|
# warn "## original rdf -> ", dump( $rdf ); |
50 |
|
|
|
51 |
dpavlin |
157 |
my $owner = Grep::Model::User->new(); |
52 |
|
|
$owner->load_by_cols( email => Jifty->config->app('ScrapBookOwner') ); |
53 |
|
|
die "can't find ScrapBookOwner ", Jifty->config->app('ScrapBookOwner') unless ( $owner->id ); |
54 |
|
|
|
55 |
|
|
Jifty->log->info( "Using user ", $owner->id, " from ", $owner->email, " for import" ); |
56 |
|
|
|
57 |
|
|
my $feed = Grep::Model::Feed->new( current_user => $owner ); |
58 |
dpavlin |
154 |
$feed->load_or_create( |
59 |
|
|
uri => 'file://' . $path, |
60 |
|
|
title => 'ScrapBook', |
61 |
|
|
#source => 'Grep::Source', |
62 |
dpavlin |
157 |
owner => $owner, |
63 |
dpavlin |
154 |
); |
64 |
|
|
|
65 |
dpavlin |
155 |
my $stats; |
66 |
|
|
|
67 |
dpavlin |
154 |
foreach my $item ( @{ $rdf->{'RDF:Description'} } ) { |
68 |
|
|
|
69 |
dpavlin |
155 |
$stats->{total}++; |
70 |
dpavlin |
154 |
|
71 |
dpavlin |
155 |
#warn "## item = ",dump( $item ); |
72 |
|
|
|
73 |
dpavlin |
154 |
my $hash; |
74 |
|
|
foreach my $k ( keys %$item ) { |
75 |
|
|
next if $k =~ m/^RDF:/; |
76 |
|
|
next if ( $item->{$k} eq '' ); |
77 |
|
|
my $n = $k; |
78 |
|
|
$n =~ s/^\w+://; # strip namespace |
79 |
|
|
$hash->{$n} = $item->{$k}; |
80 |
|
|
} |
81 |
|
|
|
82 |
dpavlin |
155 |
#warn "## hash = ", dump( $hash ); |
83 |
dpavlin |
154 |
|
84 |
|
|
|
85 |
|
|
# fetch full-text content and import it |
86 |
|
|
|
87 |
|
|
my $content_path = $dir . '/data/' . $hash->{id} . '/index.html'; |
88 |
|
|
if ( ! -r $content_path ) { |
89 |
|
|
Jifty->log->warn("can't import $content_path: $!"); |
90 |
dpavlin |
155 |
$stats->{failure}++; |
91 |
dpavlin |
154 |
next; |
92 |
|
|
} |
93 |
|
|
my $content = read_file( $content_path ) or |
94 |
|
|
die "can't read $content_path: $!"; |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
# create date from id |
98 |
|
|
|
99 |
|
|
my $dt; |
100 |
|
|
if ( $hash->{id} =~ m/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ ) { |
101 |
|
|
$dt = DateTime->new( |
102 |
|
|
year => $1, |
103 |
|
|
month => $2, |
104 |
|
|
day => $3, |
105 |
|
|
hour => $4, |
106 |
|
|
minute => $5, |
107 |
|
|
second => $6, |
108 |
|
|
#time_zone => 'UTC', |
109 |
|
|
); |
110 |
|
|
} else { |
111 |
|
|
warn "can't parse date from ", $hash->{id}; |
112 |
|
|
} |
113 |
|
|
|
114 |
dpavlin |
157 |
my $i = Grep::Model::Item->new( current_user => $owner ); |
115 |
dpavlin |
155 |
my ($ok,$msg) = $i->load_or_create( |
116 |
dpavlin |
154 |
in_feed => $feed, |
117 |
|
|
title => $hash->{title}, |
118 |
|
|
link => $hash->{source}, |
119 |
|
|
content => $content, |
120 |
|
|
issued => $hash->{id}, |
121 |
|
|
); |
122 |
|
|
|
123 |
dpavlin |
155 |
if ( ! $ok ) { |
124 |
|
|
Jifty->log->error( $msg ); |
125 |
|
|
$stats->{failure}++; |
126 |
|
|
next; |
127 |
|
|
} |
128 |
dpavlin |
154 |
|
129 |
dpavlin |
155 |
if ( $msg && $msg =~ m/^Found/ ) { |
130 |
|
|
$stats->{old}++; |
131 |
|
|
} else { |
132 |
|
|
$stats->{new}++; |
133 |
dpavlin |
157 |
Jifty->log->info("created ", $i->id ," ", $i->link, " ", length( $content ), " bytes"); |
134 |
|
|
$search->add( $i, $owner->id ); |
135 |
dpavlin |
155 |
} |
136 |
dpavlin |
154 |
|
137 |
|
|
} |
138 |
|
|
|
139 |
dpavlin |
155 |
return $stats; |
140 |
dpavlin |
154 |
} |
141 |
|
|
|
142 |
dpavlin |
155 |
=head1 SEE ALSO |
143 |
dpavlin |
154 |
|
144 |
dpavlin |
155 |
L<http://amb.vis.ne.jp/mozilla/scrapbook/> - ScrapBook FireFox extension |
145 |
|
|
|
146 |
|
|
=cut |
147 |
|
|
|
148 |
dpavlin |
154 |
1; |