#!/usr/bin/perl -w # # # rhimport # # Copyright (C) 2009 Christian Pointner # # This file is part of rhimport. # # rhimport is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # rhimport is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with rhimport. If not, see . # use strict; use Switch; use Date::Calc; use Time::localtime; use IO::Handle; use IPC::Open3; use File::Slurp; use lib '/usr/local/share/rhimport/'; use rhimport; my $STAT_FILE = $ENV{'HOME'} . "/rhimport-zffe.stat"; my $ZF_NOTE_FILE = $ENV{'HOME'} . "/rhimport-zf.last_note"; my $ZF_NEW_FILE = $ENV{'HOME'} . "/rhimport-zf.is_new"; my $FE_NOTE_FILE = $ENV{'HOME'} . "/rhimport-fe.last_note"; my $FE_NEW_FILE = $ENV{'HOME'} . "/rhimport-fe.is_new"; my $PV_ID = '359'; binmode(STDIN, ":utf8"); binmode(STDOUT, ":utf8"); binmode(STDERR, ":utf8"); if($#ARGV >= 0 && $ARGV[0] eq 'last') { print "!!!This is the last attempt, there won't be a retry on error!!!\n" } my @today = Date::Calc::Today(); my $dow = Date::Calc::Day_of_Week(@today); my @tomorrow = Date::Calc::Add_Delta_Days($today[0], $today[1], $today[2], 1); my @tmp = Date::Calc::Standard_to_Business(@today); $tmp[2] = 1; my @this_mon = Date::Calc::Business_to_Standard(@tmp); my @next_mon = Date::Calc::Add_Delta_Days($this_mon[0], $this_mon[1], $this_mon[2], 7); my @import_day = @tomorrow; if ($dow == 5) { @import_day = @next_mon; } my $import_date = sprintf("%04d-%02d-%02d", @import_day); my $current_date = `cat $STAT_FILE`; if($current_date eq $import_date) { print "Already created note of today\n"; exit 0; } unless(-e $ZF_NEW_FILE) { print "zip-fm not yet imported - "; if($#ARGV >= 0 && $ARGV[0] eq 'last') { print "giving up, rebroadcasting last show"; } else { print "will retry later\n"; exit 0, } } unless(-e $FE_NEW_FILE) { print "focus europa not yet imported - "; if($#ARGV >= 0 && $ARGV[0] eq 'last') { print "giving up, rebroadcasting last show"; } else { print "will retry later\n"; exit 0, } } sub get_note { my ($file) = @_; my @lines = read_file($file, binmode => ':utf8', err_mode => 'quiet'); return "" unless ($lines[0]); chomp($lines[0]); $lines[0] = "

$lines[0]

\n"; return join('',@lines); }; my $sum_title = "focus europa, zip-fm (WH)"; my $sum_text = get_note($FE_NOTE_FILE) . "
\n"; $sum_text = $sum_text . "
\n" . get_note($ZF_NOTE_FILE) . "
\n"; $sum_text = $sum_text . "

\n"; print "\nsummary:\n" . $sum_title . "\n\n" . $sum_text . "\n"; rhimport::pv_add_note($sum_title, $sum_text, $PV_ID, sprintf("%04d-%02d-%02d", @import_day), "1"); print "\n"; unlink($STAT_FILE); open(my $fhs, '>', $STAT_FILE); print $fhs $import_date; close($fhs); unlink($ZF_NEW_FILE); unlink($FE_NEW_FILE); exit 0;