blob: c22abcb543c7c14980e46e6f56eee267008f28fe (
plain) (
tree)
|
|
package Lifestream::Worker;
use Moose;
extends 'Tatsumaki::Service';
use Digest::SHA qw(sha256_hex);
use Lifestream::Schema;
use Tatsumaki::HTTPClient;
use Try::Tiny;
use XML::Feed;
has config => (
is => "rw",
isa => "HashRef"
);
has schema => (
is => 'ro',
isa => 'Lifestream::Schema',
lazy => 1,
default => sub {
my $self = shift;
return Lifestream::Schema->connect(
@{ $self->config->{connect_info} } );
}
);
sub start {
my $self = shift;
my $t;
$t = AE::timer 0, 15, sub {
$self->fetch_feeds;
};
#$self->fetch_feeds();
}
sub fetch_feeds {
my $self = shift;
my $feeds = $self->schema->resultset('Feed')->search();
while ( my $feed = $feeds->next ) {
if ( !$feed->favico_url ) {
my $uri = URI->new( $feed->profile_url );
$uri->path('favicon.ico');
$self->schema->txn_do(
sub {
$feed->update( { favico_url => $uri->as_string } );
}
);
}
$self->work_feed( $feed->feed_url, $feed->id );
}
}
sub work_feed {
my ( $self, $url, $id ) = @_;
warn "fetching $url\n";
Tatsumaki::HTTPClient->new->get(
$url,
sub {
my $res = shift;
if ( !$res->is_success ) {
warn "can't fetch $url\n";
return;
}
my $feed = XML::Feed->parse( \$res->content );
for my $entry ( $feed->entries ) {
my $entry_id = sha256_hex( $entry->link );
next if $self->schema->resultset('Entry')->find($id);
try {
$self->schema->txn_do(
sub {
$self->schema->resultset('Entry')->find_or_create(
{
id => $entry_id,
permalink => $entry->link,
title => $entry->title,
date => $entry->issued,
feedid => $id,
}
);
}
);
}
catch { warn "can't insert meme : $_\n"; };
}
}
);
}
1;
|