From 3c991f53d91182d146eb4a95b655249cca7d3f9c Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Thu, 16 Jun 2016 18:17:39 +0200
Subject: added orphans command to rhrds-schedules


diff --git a/utils/rhrd-schedules b/utils/rhrd-schedules
index 4f07db5..fbd8227 100755
--- a/utils/rhrd-schedules
+++ b/utils/rhrd-schedules
@@ -22,8 +22,6 @@
 
 use strict;
 
-use lib '../lib';
-
 use RHRD::rddb;
 use RHRD::utils;
 use DateTime;
@@ -33,7 +31,8 @@ use DateTime::Format::Strptime;
 sub print_usage
 {
   print STDERR "Usage: rhrd-schedules generate YYYY-MM-DD\n" .
-               "       rhrd-schedules show (W1|W2|W3|W4|ALL) (MO|TU|WE|TH|FR|SA|SU|ALL)\n";
+               "       rhrd-schedules show (W1|W2|W3|W4|ALL) (MO|TU|WE|TH|FR|SA|SU|ALL)\n" .
+               "       rhrd-schedules orphans\n";
 }
 
 sub generate
@@ -250,6 +249,67 @@ sub show
   return 0;
 }
 
+
+sub orphans
+{
+  my ($ctx) = @_;
+
+  my @dates = gen_dates("ALL", "ALL");
+  unless(@dates) {
+    return -1;
+  }
+
+  my @showids = RHRD::rddb::list_showids($ctx);
+  if(!defined $showids[0] && defined $showids[1]) {
+    print STDERR "$showids[1]: $showids[2]";
+    return -1;
+  }
+
+  my %shows;
+  for my $showid (@showids) {
+    $shows{$showid} = 0;
+  }
+
+  for my $date (@dates) {
+    my ($year, $month, $day) = @{$date};
+    my ($ret, $data) = RHRD::utils::fetch_parse_json("https://pv.helsinki.at/export/day_schedule/$year/$month/$day/", "rhrd-schedules");
+    if(!$ret) {
+      print STDERR "Error fetching export from PV: $data\n";
+      return 1;
+    }
+
+    for my $entry (@{$data}) {
+      my $start = DateTime::Format::Strptime::strptime("%Y-%m-%d_%H:%M:%S", ${$entry}{'start'});
+      my $pvid = ${$entry}{'id'};
+      my $showid = ${$entry}{'automation-id'};
+
+      next if $pvid == 1; # 'Unmoderiertes Musikprogramm'
+      # maybe only the end is on this day - we don't need this to include in the log since only
+      # start times matter here.
+      next if ($start->year != $year || $start->month != $month || $start->day != $day);
+
+      if($showid < 0) {
+        next;
+      }
+      $shows{$showid}++;
+    }
+  }
+
+  for my $showid (sort keys %shows) {
+    if($shows{$showid} == 0) {
+
+      my ($show, $status, $errorstring) = RHRD::rddb::get_show_info($ctx, $showid);
+      if(!defined $show) {
+        print "$status: $errorstring\n";
+      } else {
+        print "$showid: " . ${$show}{'TITLE'} . "\n";
+      }
+    }
+  }
+
+  return 0;
+}
+
 my $num_args = $#ARGV + 1;
 if($num_args < 1) {
   print_usage();
@@ -274,6 +334,13 @@ if(defined $ctx) {
     } else {
       $ret = show($ctx, $ARGV[1], $ARGV[2]);
     }
+  } elsif($cmd eq "orphans") {
+    if($num_args != 1) {
+      print_usage();
+      $ret = 1;
+    } else {
+      $ret = orphans($ctx);
+    }
   } else {
     print_usage();
     $ret = 1;
-- 
cgit v0.10.2