summaryrefslogtreecommitdiffstats
path: root/bin/pinboard_export
blob: 5876809f1a5ef9a3614d194c30c533901c362a28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!perl
use strict;
use warnings;

use DBI;
use Getopt::Long qw(:config pass_through);
use WWW::Pinboard;
use Term::ProgressBar;

my ($dsn, $token, $quiet);
GetOptions(
    'dsn=s'   => \$dsn,
    'token=s' => \$token,
    'quiet'   => \$quiet,
);
die "--dsn is required" unless $dsn;
die "--token is required" unless $token;

my $dbh = DBI->connect($dsn, '', '', { RaiseError => 1, AutoCommit => 0 });
my $fromdt = '1970-01-01T00:00:00Z';
if (!$dbh->tables(undef, undef, 'posts')) {
    $dbh->do(<<'');
    CREATE TABLE `posts` (
        href TEXT NOT NULL,
        description TEXT NOT NULL,
        extended TEXT NOT NULL,
        tags TEXT NOT NULL,
        time TEXT NOT NULL,
        toread TEXT NOT NULL
    );

}
else {
    ($fromdt) = $dbh->selectrow_array(
        'SELECT time FROM posts ORDER BY strftime("%s", time) DESC LIMIT 1'
    );
}

my $api = WWW::Pinboard->new(token => $token);

if ($fromdt ge $api->update->{update_time}) {
    $dbh->disconnect;
    exit(0);
}

my $progress;

my $sth = $dbh->prepare(
    'INSERT INTO posts (href, description, extended, tags, time, toread) VALUES (?, ?, ?, ?, ?, ?)'
);

my $posts = $api->all(fromdt => $fromdt, progress => sub {
    my ($chunk, $res) = @_;
    if (!$progress && !$quiet && defined $res->{headers}{'content-length'}) {
        $progress = Term::ProgressBar->new({
            count => $res->{headers}{'content-length'},
            ETA   => 'linear',
        });
        $progress->message("downloading new posts...");
    }
    $res->{content} .= $chunk;
    $progress->update(length($res->{content})) if $progress;
});

if (!$quiet) {
    $progress = Term::ProgressBar->new({
        count => scalar(@$posts),
        ETA   => 'linear',
    });
    $progress->message('importing posts...');
}

for my $post (@$posts) {
    $sth->execute(
        $post->{href},
        $post->{description},
        $post->{extended},
        $post->{tags},
        $post->{time},
        $post->{toread},
    );
    $progress->update if $progress;
}

$dbh->commit;
$dbh->disconnect;