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
|
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, 1800, sub {
scalar $t;
$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;
|