/[couchdb]/scripts/reblog2couchdb.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /scripts/reblog2couchdb.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations)
Fri Aug 8 19:19:12 2008 UTC (15 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 2555 byte(s)
- import just published items from reblog
- better messages
1 dpavlin 2 #!/usr/bin/perl -w
2    
3     use strict;
4     use DBI;
5     use CouchDB::Client;
6     use Data::Dump qw/dump/;
7    
8     $|++;
9    
10     my $database = 'reblog';
11    
12 dpavlin 6 my $dbi = "DBI:mysql:database=$database";
13     $dbi .= ";host=127.0.0.1;port=13306"; # XXX over ssh
14 dpavlin 2
15     my $sql = qq{
16     select
17 dpavlin 4 items.id as _id,
18     items.*,
19     feeds.url as feed_url,
20     feeds.title as feed_title,
21     feeds.link as feed_link,
22     feeds.description as feed_description
23 dpavlin 10 -- t.value_long as tags
24 dpavlin 2 from items
25 dpavlin 4 join items_userdata on items.id = item_id
26     join feeds on items.feed_id = feeds.id
27 dpavlin 10 -- left outer join items_userdata as t on items.id = t.item_id and t.label='tags'
28     where items.id > ? and items_userdata.label = 'published' and items_userdata.value_numeric = 1
29 dpavlin 4 order by items.id asc
30 dpavlin 10 limit 1000
31 dpavlin 2 };
32    
33     my $c = CouchDB::Client->new(uri => 'http://localhost:5984/');
34    
35     $c->testConnection or die "The server cannot be reached";
36 dpavlin 6 print "CouchDB version " . $c->serverInfo->{version} . "\n";
37 dpavlin 2 my $db = $c->newDB( $database );
38     $db->create unless $c->dbExists( $database );
39    
40 dpavlin 6 my $status = $db->newDoc( '_sync' );
41     eval { $status->retrieve };
42     $status->create if $@;
43 dpavlin 2
44 dpavlin 6 print "status ",dump( $status->{data} ), "\n";
45    
46     my $last_row = $status->{data}->{last_row_id} || 0;
47    
48 dpavlin 7 sub commit_last_row {
49     warn "commit_last_row $last_row\n";
50     $status->{data}->{last_row_id} = $last_row;
51     $status->update;
52     }
53    
54 dpavlin 6 my $dbh = DBI->connect($dbi,"","") || die $DBI::errstr;
55    
56     print "Fetching items from $dbi id > $last_row\n";
57    
58     my $sth = $dbh->prepare($sql) || die $dbh->errstr();
59     $sth->execute( $last_row ) || die $sth->errstr();
60    
61     warn dump( $sth->{NAME} );
62    
63 dpavlin 10 print "found ",$sth->rows," items to process...\n";
64 dpavlin 6
65 dpavlin 2 my $pk = 'id';
66    
67 dpavlin 6 my $count = 0;
68    
69 dpavlin 2 while (my $row = $sth->fetchrow_hashref() ) {
70 dpavlin 4 my $_id = $row->{_id} || die "row needs _id";
71 dpavlin 2 my $doc = $db->newDoc( $_id );
72 dpavlin 4
73     sub row2doc {
74     my ( $row, $doc ) = @_;
75     my $a = delete( $row->{xml} );
76     $doc->addAttachment( 'item.xml', 'application/xhtml+xml', $a ) if $a;
77     my $a = delete( $row->{content} );
78     $doc->addAttachment( 'content.html', 'text/html', $a ) if $a;
79     $doc->{data} = $row;
80     return $doc;
81     }
82    
83     row2doc( $row, $doc );
84    
85 dpavlin 2 eval { $doc->create };
86     if ( $@ ) {
87 dpavlin 4 $doc->retrieve;
88     row2doc( $row, $doc )->update;
89     # eval { $doc->update };
90 dpavlin 10 warn $@ ? "$count ERROR $_id $@\n" : "$count updated $_id\n";
91 dpavlin 2 } else {
92 dpavlin 10 warn "$count created $_id\n";
93 dpavlin 2 }
94 dpavlin 6
95 dpavlin 7 $last_row = $row->{id};
96 dpavlin 6 $count++;
97 dpavlin 7
98     commit_last_row if $count % 100 == 0 # checkpoint every 100 records
99 dpavlin 2 }
100    
101 dpavlin 7 commit_last_row;
102 dpavlin 6
103 dpavlin 2 __END__
104    
105     $sql = qq{
106     update items_userdata
107     set value_numeric = 1
108     where label = 'read' and item_id in ($ids)
109     };
110    
111     $dbh->do( $sql );
112    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26