#!/usr/bin/env perl
use 5.016;
use strict;
use warnings;
use Path::Class;
use JSON;
use Net::Twitter;
use ElasticSearch;
use DateTime::Format::Strptime;
use Data::Visitor::Callback;
use Data::Printer;

my $conf = JSON->new->utf8->decode(
    file(__FILE__)->parent->file('tweet-archive.conf')
        ->slurp(iomode=>'<:raw')
            // '{}'
    );

my $cb = Data::Visitor::Callback->new(
    'JSON::Boolean' => sub { say "converting a boolean"; return 0+$_ },
);
my $dt = DateTime::Format::Strptime->new(pattern => '%a %b %d %T %z %Y');

my $nt = Net::Twitter->new(
    traits => [qw/API::REST OAuth/],
    consumer_key    => $conf->{consumer_key},
    consumer_secret => $conf->{consumer_secret},
);

$nt->access_token($conf->{access_token});
$nt->access_token_secret($conf->{access_token_secret});

my $es = ElasticSearch->new();

my $tl = $nt->home_timeline({
    include_entities => 1,
    trim_user => 1,
    exclude_replies => 0,
});

for my $tweet (@$tl) {
    $cb->visit($tweet);
    say p $tweet;
    my $result = $es->index(
        index => 'tweet-archive',
        type => 'tweet',
        id => $tweet->{id_str},
        timestamp => $dt->parse_datetime($tweet->{created_at})->iso8601,
        data => $tweet,
    );
    say p $result;
}
