summaryrefslogtreecommitdiff
path: root/rh-bin/schedules.json
blob: ca01cfc171eda46ea955e16047a63fa0bf09527b (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
#!/usr/bin/perl
#
#  rhrdweb
#
#  Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
#
#  This file is part of rhrdweb.
#
#  rhrdweb is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  rhrdweb 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 Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with rhrdweb. If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use RHRD::rddb;
use JSON::MaybeXS;
use CGI;
use DateTime;
use DateTime::Format::Strptime;
use DateTime::Duration;

my $status = 'ERROR';
my $errorstring = 'unknown';
my $responsecode = 500;
my @showlist;


my $q = CGI->new;
my $num_days = $q->param('DAYS');
$num_days = 1 unless (defined $num_days);

my $date = DateTime->today();
my $start_date = $q->param('START');
if(defined $start_date) {
  my $strp = DateTime::Format::Strptime->new(pattern   => '%F');
  $date = $strp->parse_datetime($start_date);
}

if(!defined $date) {
  $responsecode = 400;
  $errorstring = "field START contains invalid date"
} if($num_days <= 0) {
  $responsecode = 400;
  $errorstring = "field DAYS must be > 0"
} else {
  (my $ctx, $status, $errorstring) = RHRD::rddb::init();
  if(defined $ctx) {
    for (my $i = 0; $i < $num_days; $i++) {
      my @shows = RHRD::rddb::get_schedule_log($ctx, $date->year, $date->month, $date->day);
      if(!defined $shows[0] && defined $shows[1]) {
        $responsecode = 500;
        $status = $shows[1];
        $errorstring = $shows[2];
      } else {
        for my $href (@shows) {
          my %show;
          $show{'start'} = $href->{'START'}->iso8601();
          $show{'id'} = int($href->{'ID'});
          $show{'title'} = $href->{'TITLE'};
          $show{'len'} = int($href->{'LEN'});
          push @showlist, \%show;
        }
        $responsecode = 200;
        $status = "OK";
        $errorstring = "success";
      }
      $date += DateTime::Duration->new(days => 1);
    }

    RHRD::rddb::destroy($ctx);
  }
}

my %answer;
$answer{'status'} = $status;
$answer{'errorstring'} = $errorstring;
$answer{'shows'} = \@showlist;

my $j = JSON::MaybeXS->new(utf8 => 0);

print "Content-type: application/json; charset=UTF-8\n";
print "Status: $responsecode\n\n";
print $j->encode(\%answer);