1 package Newslash::FeedFetcher;
9 use constant CONF_PATH => "/etc/newslash/newslash.conf";
11 use Newslash::Model::Feeds;
13 use DateTime::Format::MySQL;
21 bless {_config => {} }, $class;
25 my ($self, $new_val) = @_;
26 $self->{_config} = $new_val if defined $new_val;
27 return $self->{_config};
31 my ($self, $new_val) = @_;
32 $self->{_feeds} = $new_val if defined $new_val;
33 return $self->{_feeds};
39 # Environment variable
41 if ($ENV{MOJO_HOME}) {
42 $home = $ENV{MOJO_HOME};
45 my $path = $INC{"Newslash/FeedFetcher.pm"};
46 my @terms = split(/[\/\\]/, $path);
48 my $last = pop(@terms);
49 if ($last =~ m/^(lib|blib)$/) {
50 $home = File::Spec->catdir(@terms);
63 $config_file = CONF_PATH;
66 my $home = $self->_detect_home;
68 $config_file = File::Spec->catdir($home, "newslash-feedfetcher.yaml");
69 if (! -e $config_file) {
70 $config_file = File::Spec->catdir($home, "newslash-web.yaml");
75 my $config = eval { LoadFile($config_file) };
76 die qq{Can't parse config "$config_file": $@} if $@;
77 die qq{Invalid config "$config_file"} unless ref $config eq 'HASH';
84 my $log = Mojo::Log->new;
86 if ($ENV{MOJO_LOG_LEVEL}) {
87 $log->level($ENV{MOJO_LOG_LEVEL});
93 my $config = $self->_load_config;
94 $self->config($config);
96 my $feeds = new Newslash::Model::Feeds($self->config);
99 my $rs = $feeds->select;
101 die "cannot retrieve feed infomations from database.";
103 for my $feed (@$rs) {
104 if ($feed->{status} ne "active") {
105 $log->info("$feed->{title} is not active. skip ...");
108 $log->info("fetch $feed->{url} ...");
110 my $f = $feeds->fetch_url(\$feed->{url}, $config->{FeedFetcher} || {});
111 my $now = DateTime::Format::MySQL->format_datetime(DateTime->now);
113 my $error = $feeds->last_error;
114 $log->error("fetch $feed->{url} failed - $error.");
115 $feeds->update(feed_id => $feed->{feed_id},
116 latest_fetch_time => $now,
117 latest_fetch_result => $feeds->last_errorno);
120 $feeds->update(feed_id => $feed->{feed_id},
121 latest_fetch_time => $now,
122 latest_fetch_result => 200);
124 for my $item ($f->entries) {
125 my $content = $item->content->body;
126 my $type = $item->content->type;
127 my $modified = $item->modified || $item->issued;
132 my $id = $feeds->items->create(feed_id => $feed->{feed_id},
133 url => $item->link || "",
134 title => $item->title || "",
136 content_type => $type,
137 modified => $modified,
140 my $error = $feeds->last_error;
141 $log->error("cannot create feed item: $error");
145 $log->debug("update item " . $item->link . " (feed_id: $feed->{feed_id})");
148 $log->debug("create item " . $item->link . " (feed_id: $feed->{feed_id}, item_id: $id)");