rsstootalizer/cronjob.pl

139 lines
3.8 KiB
Perl
Raw Normal View History

2017-04-23 18:49:07 +00:00
#!/usr/bin/perl -w -I.
use strict;
use Data::Dumper;
use URI::Escape;
use JSON;
2017-04-23 18:51:48 +00:00
use RSSTootalizer::Feed;
2017-04-24 06:17:30 +00:00
use RSSTootalizer::Filter;
2017-04-23 18:51:48 +00:00
use RSSTootalizer::User;
use RSSTootalizer::Entry;
2017-04-26 07:51:03 +00:00
use RSSTootalizer::DB;
2017-04-23 18:49:07 +00:00
2018-03-25 07:39:52 +00:00
my $VERBOSE = 1;
2017-04-23 18:49:07 +00:00
our $config = "";
2017-04-23 18:51:48 +00:00
open CONFIG, "rsstootalizer.conf.json" or die "Cannot open rsstootalizer.conf.json";
2017-04-23 18:49:07 +00:00
{
2017-04-23 19:24:03 +00:00
local $/ = undef;
2017-04-23 18:49:07 +00:00
$config = <CONFIG>;
}
close CONFIG;
$config = decode_json($config);
sub Error {{{
my $errormessage = "\nStack Trace:\n";
my $i=0;
while ((my @call_details = (caller($i++))) ){
$errormessage .= $call_details[1].":".$call_details[2]." in function ".$call_details[3]."\n";
}
print STDERR $errormessage;
exit(1);
}}}
# Force Unicode output
binmode STDERR, ":utf8";
binmode STDOUT, ":utf8";
2018-03-25 07:39:52 +00:00
if ($VERBOSE) {print STDOUT "Checking for new entries\n";}
my $new_entries = 0;
2017-04-23 18:51:48 +00:00
my @feeds = RSSTootalizer::Feed->all();
2018-05-05 08:00:10 +00:00
# For each Feed stored in database
2017-04-23 18:49:07 +00:00
FEED: foreach my $feed (@feeds){
2017-05-01 12:02:24 +00:00
next FEED unless $feed;
2017-04-23 18:49:07 +00:00
next FEED unless $feed->{data}->{enabled};
2018-05-05 08:00:10 +00:00
# If enabled, fetch the RSS xml
2017-04-23 18:49:07 +00:00
my $entries = $feed->fetch_entries();
2017-04-28 18:12:31 +00:00
next FEED unless $entries;
2018-05-05 08:00:10 +00:00
# For each entry in the xml file
2017-04-23 18:49:07 +00:00
ENTRY: foreach my $entry ($entries->items){
2018-05-05 08:00:10 +00:00
# Does the entry already exist in the database?
2017-04-23 18:49:07 +00:00
my @seen_entries = $feed->entry_by("entry_link", $entry->link());
next ENTRY if ((scalar @seen_entries) > 0);
my %entry;
$entry{title} = $entry->title();
$entry{link} = $entry->link();
$entry{content} = $entry->content()->body;
$entry{author} = $entry->author();
$entry{issued} = $entry->issued();
$entry{modified} = $entry->modified();
$entry{id} = $entry->id();
$entry{tags} = join(", ", $entry->tags());
2017-04-24 06:17:30 +00:00
my $do_post = 0;
my @filters = $feed->filters();
foreach my $filter (@filters){
if ($filter->apply($entry)){
if ($filter->{data}->{type} eq "white"){
$do_post = 1;
} else {
$do_post = 0;
}
}
}
if ($do_post){
my $user = $feed->user();
my $status = $feed->{data}->{format};
$status =~ s/{ID}/$entry{id}/g;
2017-05-01 12:02:24 +00:00
if (defined($entry{title})){
$status =~ s/{Title}/$entry{title}/g;
} else {
$status =~ s/{Title}/No Title/g;
}
2017-04-24 06:17:30 +00:00
$status =~ s/{Link}/$entry{link}/g;
$status =~ s/{Content}/$entry{content}/g;
$status =~ s/{Author}/$entry{author}/g;
$status =~ s/{Issued}/$entry{issued}/g;
$status =~ s/{Modified}/$entry{modified}/g;
$status =~ s/{Tags}/$entry{tags}/g;
2017-04-23 18:49:07 +00:00
my %data;
if (length($status) > 500){
2017-04-28 06:22:23 +00:00
$status =~ s/^(.{497}).*$/$1.../g;
}
$data{status} = $status;
# Visibility of a toot can be 'direct', 'private', 'unlisted' or 'public'
# 'direct' and 'unlisted' are irrelevant
# 'private' posts only to followers [default]
# 'public' posts to public timelines [ethical issue?]
# [* Should be set per feed in the sql db *]
# [* Hardcoded here temporarily for testing *]
my $visibility = 'public';
$data{visibility} = $visibility;
$ENV{status} = encode_json({%data});
2017-04-23 18:49:07 +00:00
2018-03-25 07:48:02 +00:00
# encode_json breaks '\n' chars - turns them into '\\n'
# Fix them
$ENV{status} =~ s/\\\\n/\\n/g;
open(DATA, "./post_status.bash '$user->{data}->{access_token}' '$user->{data}->{instance}' |");
2017-04-24 06:17:30 +00:00
my $reply = "";
{
local $/ = undef;
$reply = <DATA>;
}
2018-03-25 07:39:52 +00:00
$new_entries += 1;
2017-04-23 18:49:07 +00:00
}
my %ne;
$ne{feed_id} = $feed->{data}->{ID};
$ne{entry_link} = $entry{link};
2017-04-23 18:51:48 +00:00
RSSTootalizer::Entry->create(%ne);
2017-04-23 18:49:07 +00:00
}
}
2017-04-26 07:51:03 +00:00
RSSTootalizer::DB->doUPDATE("UPDATE `users` SET session_id = 'invalid' WHERE TIME_TO_SEC(NOW()) - TIME_TO_SEC(`valid_from`) > 60*60*4;"); # invalidate old sessions
2018-03-25 07:39:52 +00:00
if ($VERBOSE) {
$new_entries ? ($new_entries > 1 ? print "$new_entries new entries\n" : print "$new_entries new entry\n") : print "No new entries\n";
print STDOUT "Done\n";
}