#!/usr/bin/perl # # rhrdweb # # Copyright (C) 2016 Christian Pointner # # 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 . # 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) { $href->{'START'}->set_time_zone("UTC"); my %show; $show{'start'} = $href->{'START'}->iso8601() . "Z"; $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);