summaryrefslogtreecommitdiff
path: root/rhautoimport.pm
blob: 4bf95680dc6f494870fe2c771a104d9ea08ee501 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl -w
#
#
#  rhautoimport
#
#  Copyright (C) 2009-2015 Christian Pointner <equinox@helsinki.at>
#
#  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 <http://www.gnu.org/licenses/>.
#

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;