#!/usr/bin/perl -w # # # rhautoimport # # Copyright (C) 2009-2015 Christian Pointner # # This file is part of rhautoimport. # # rhautoimport 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. # # rhautoimport 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 rhautoimport. If not, see . # use strict; package rhautoimport; use File::Basename; use IPC::Open3; use IO::Handle; use LWP::Simple; use XML::Feed; use RHRD::rddb; use constant { RD_USER => 'autoimport', }; sub get_dropboxes { my ($groupname) = @_; my ($dbh, $status, $errorstring) = RHRD::rddb::opendb(); if(!defined $dbh) { return ($dbh, $errorstring); } my @allowed_dbs = RHRD::rddb::get_dropboxes($dbh, RD_USER, $groupname); RHRD::rddb::closedb($dbh); return @allowed_dbs; } sub fetch_parse_rss { my ($url, $ua_str) = @_; my $uri = URI->new($url); $ua_str = "Radio Helsinki - Automatic Import" unless $ua_str; my $ua = LWP::UserAgent->new; $ua->agent($ua_str); $ua->env_proxy; my $res = URI::Fetch->fetch($uri, UserAgent => $ua) or die URI::Fetch->errstr; die "This feed has been permanently removed" if $res->status == URI::Fetch::URI_GONE(); my $xml = $res->content; return XML::Feed->parse(\$xml); } # TODO: use rhimportd for this? sub check_file_extension { my ($file) = @_; my $ext = uc((fileparse($file, qr/\.[^.]*/))[2]); foreach (".MP3", ".OGG", ".FLAC", ".WAV") { if($ext eq $_) { return 1; } } return 0; } sub clear_carts { my ($show_id) = @_; return (1, "clear_carts not implemented...\n"); } sub import_uri { my ($show_id, $uri) = @_; return (1, "import_uri not implemented...\n"); } # TODO: use rhimportd for this? sub pv_add_note { my ( $title, $text, $id, $date, $type, $index ) = @_; my @script = ('python', '/srv/pv/pv/manage.py', 'addnote', $id, $date, $type); push(@script , $index) unless (!defined $index); my ($reader, $writer, $error ) = ( new IO::Handle, new IO::Handle, new IO::Handle ); $writer->autoflush(1); local $SIG{CHLD} = 'DEFAULT'; my $pid = open3($writer, $reader, $error, @script); binmode($reader, ":utf8"); binmode($writer, ":utf8"); binmode($error, ":utf8"); print $writer $title . "\n" . $text; close $writer; waitpid $pid, 0; my $err_out = join('', <$error>); my $read_out = join('', <$reader>); if ( $? >> 8 ) { print "\n\nPV: adding note returned non-zero value\n"; print "STDERR:\n" . $err_out . "\n" unless $err_out eq ''; print "STDOUT:\n" . $read_out . "\n" unless $read_out eq ''; print "Ignoring failed headline import!\n"; } else { print $read_out . "\n" unless $read_out eq ''; print $err_out . "\n" unless $err_out eq ''; } } 1;