/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 162 - (show annotations)
Mon Jun 11 10:18:09 2007 UTC (16 years, 11 months ago) by dpavlin
File size: 3846 byte(s)
cleanup config
1 #!/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 HTML::ResolveLink;
25 use HTML::TreeBuilder;
26 use Data::Dump qw/dump/;
27
28 sub import {
29 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 =
44 Jifty::Util->app_root . '/' .
45 Jifty->config->framework('Web')->{'StaticRoot'} . '/' . $config->{'Dir'};
46
47 my $path = $dir . '/scrapbook.rdf';
48 $path =~ s!//+!/!g;
49
50 if ( ! -e $dir || ! -e $path ) {
51 Jifty->log->warn("Skipping ScrapBook import $path: $!");
52 return 1;
53 }
54
55 my $rdf = XMLin(
56 $path,
57 # KeyAttr => [ qw/RDF:about/ ],
58 ) || die "can't open $path: $!";
59
60 # warn "## original rdf -> ", dump( $rdf );
61
62 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(
70 uri => 'file://' . $path,
71 title => 'ScrapBook',
72 #source => 'Grep::Source',
73 owner => $owner,
74 );
75
76 my $search = Grep::Search->new;
77
78 my $stats;
79
80 foreach my $item ( @{ $rdf->{'RDF:Description'} } ) {
81
82 $stats->{total}++;
83
84 #warn "## item = ",dump( $item );
85
86 my $hash;
87 foreach my $k ( keys %$item ) {
88 next if $k =~ m/^RDF:/;
89 next if ( $item->{$k} eq '' );
90 my $n = $k;
91 $n =~ s/^\w+://; # strip namespace
92 $hash->{$n} = $item->{$k};
93 }
94
95 #warn "## hash = ", dump( $hash );
96
97
98 # fetch full-text content and import it
99
100 my $rel_path = '/data/' . $hash->{id} . '/index.html';
101
102 my $content_path = $dir . $rel_path;
103 if ( ! -r $content_path ) {
104 Jifty->log->warn("can't import $content_path: $!");
105 $stats->{failure}++;
106 next;
107 }
108 my $content = read_file( $content_path ) or
109 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
120
121 my $dt;
122 if ( $hash->{id} =~ m/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ ) {
123 $dt = DateTime->new(
124 year => $1,
125 month => $2,
126 day => $3,
127 hour => $4,
128 minute => $5,
129 second => $6,
130 #time_zone => 'UTC',
131 );
132 } else {
133 warn "can't parse date from ", $hash->{id};
134 }
135
136 my $i = Grep::Model::Item->new( current_user => $owner );
137 my ($ok,$msg) = $i->load_or_create(
138 in_feed => $feed,
139 title => $hash->{title},
140 link => $hash->{source},
141 content => $content,
142 issued => $hash->{id},
143 );
144
145 if ( ! $ok ) {
146 Jifty->log->error( $msg );
147 $stats->{failure}++;
148 next;
149 }
150
151 if ( $msg && $msg =~ m/^Found/ ) {
152 $stats->{old}++;
153 } else {
154 $stats->{new}++;
155 Jifty->log->info("created ", $i->id ," ", $i->link, " ", length( $content ), " bytes");
156 $search->add( $i, $owner->id );
157 }
158
159 }
160
161 $search->finish;
162
163 return $stats;
164 }
165
166 =head1 SEE ALSO
167
168 L<http://amb.vis.ne.jp/mozilla/scrapbook/> - ScrapBook FireFox extension
169
170 =cut
171
172 1;

  ViewVC Help
Powered by ViewVC 1.1.26