File indexing completed on 2024-05-12 05:10:35

0001 #!/usr/bin/perl -w
0002 
0003 # tcm2kcal.pl -- converts a Turner Classic Movies (TCM) calendar
0004 #                web page into a KDE calendar file.
0005 ############################################################################
0006 # SPDX-FileCopyrightText: 2003-2004 Allen Winter <winter@kde.org>          #
0007 # SPDX-License-Identifier: GPL-2.0-or-later                                #
0008 ############################################################################
0009 #
0010 # This script is used to help test konsolekalendar.
0011 #
0012 # Modify $konkal, $cal, and $TCM as necessary for your needs.
0013 #
0014 # Requirements:
0015 #   lynx
0016 #   konsolekalendar (from KDE).
0017 #   Date-Calc perl module (from CPAN).
0018 #
0019 # Options: -d, delete entries from KDE calendar file
0020 #          -c, change entries in KDE calendar by re-writing movie titles in all caps.
0021 #          -m <month>, where month is an integer 1 thru 12
0022 #                      default is current month
0023 #          -y <year>, where year is an integer YYYY
0024 #                     default is current year
0025 #          -f <file>, where file is location of output KDE calendar
0026 #                     default is $HOME/.kde/share/apps/korganizer/tcm.ics
0027 #      -w <konsolekalendar> where konsolekalendar is located
0028 
0029 use strict;
0030 use Env qw(HOME);
0031 use Getopt::Std;
0032 use Date::Calc qw(Today Month_to_Text
0033                   Add_Delta_DHMS Add_Delta_Days
0034                   Day_of_Week Day_of_Week_to_Text
0035                   Month_to_Text);
0036 
0037 #location of konsolekalendar program
0038 my($konkal)="/usr/bin/konsolekalendar";
0039 
0040 #location of new TCM KDE calendar
0041 my($cal)="$HOME/.kde/share/apps/korganizer/tcm.ics";
0042 
0043 #default program execution mode
0044 my($mode)="--add";
0045 
0046 #current date information
0047 my($year,$month,$d) = Today();
0048 
0049 #parse command line
0050 &parse();
0051 
0052 my($mmonth)=Month_to_Text($month);
0053 
0054 #location of TCM monthly calendar web page
0055 my($TCM)="http://www.turnerclassicmovies.com/Schedule/Print/0,,$month-$year|0|,00.html";
0056 print "Processing $TCM ...\n";
0057 
0058 if( open(GET, "lynx -dump -nolist -connect_timeout=3 '$TCM'|") ) {
0059 } else {
0060   die "Failed to get the TCM calendar page.\n";
0061 }
0062 
0063 if( $mode eq "--add" ) {
0064   if( ! -r $cal ) {
0065     if( system("$konkal --create --file $cal") ) {
0066       print "Failure Condition Encountered.  Unable to Create KDE Calendar.\n"; exit 1;
0067     }
0068   }
0069 }
0070 
0071 my($tday,$mday);
0072 my($day)=1;
0073 my($lastampm)="AM";
0074 my($line)="";
0075 my($event)="";
0076 my($num)=0;
0077 while($line=<GET>) {
0078 
0079   #prepare line
0080   chomp($line);
0081   $line=&rmleadwhite($line);
0082 
0083   #skip junk lines
0084   next if( ! $line );
0085   next if( $line =~ m/^\[/ );
0086   next if( $line =~ m/^All Times Eastern/ );
0087   next if( $line =~ m/^$mmonth[[:space:]]*$year/ );
0088 
0089   #start new day if line begins with a DD Sunday|Monday|...|Friday|Saturday
0090   ($tday,$mday) = split(" ",$line);
0091   if( $tday =~ m/^[0-9]*$/ ) {
0092     if( $tday >= 1 && $tday <= 31 ) {
0093       if( $mday =~ m/day$/ ) {
0094     &process() if( $event );
0095 #        print "New day starting: $tday $mday\n";
0096         $day = $tday;
0097     $event = "";
0098     next;
0099       }
0100     }
0101   }
0102 
0103   #start a new event if line begins with a HH:MM[[space]]AM|PM
0104   if( $line =~ m/^[0-9]*:[0-9][0-9][[:space:]]*[A,P]M/ ) {
0105     &process() if( $event );
0106     $event = "$line";
0107   } else {
0108     $event = $event . " " . $line;
0109   }
0110 }
0111 
0112 #process remaining event, if there is one.
0113 &process() if( $event );
0114 
0115 close(GET);
0116 print "$num movies processed\n";
0117 
0118 ##### END MAIN #####
0119 
0120 sub rmleadwhite() {
0121   #remove leading white space
0122   my($x)=@_;
0123   ($x)=~s+^[[:space:]]*++;
0124   return($x);
0125 }
0126 
0127 sub parse() {
0128   #parse command line
0129 
0130   our($opt_h, $opt_d, $opt_c, $opt_m, $opt_y, $opt_f, $opt_w);
0131   getopts('hdcm:y:f:w:');
0132 
0133   $mode="--delete" if( $opt_d );
0134   $mode="--change" if( $opt_c );
0135   $month=$opt_m if( $opt_m );
0136   $year=$opt_y if( $opt_y );
0137   $cal=$opt_f if( $opt_f );
0138   $konkal=$opt_w if( $opt_w );
0139 
0140 #  if( $opt_h ) {
0141 #   print "help here!\r\n"; 
0142 #  } // if
0143 
0144 }
0145 
0146 sub process() {
0147   #remove any evil double quotes from the event string
0148   $event =~ s/\"//g;
0149 
0150   ### Compute starting date
0151   my($date) = sprintf("%4d-%02d-%02d",$year,$month,$day);
0152 
0153   ### Compute starting time
0154   my($ttime,$ampm) = split(" ",$event);
0155   my($hour,$min) = split(":",$ttime);
0156 
0157   # adjust the hour by am or pm
0158   $hour += 12 if( $ampm =~ m/[Pp][Mm]/ && $hour < 12 );
0159   $hour -= 12 if( $ampm =~ m/[Aa][Mm]/ && $hour == 12 );
0160 
0161   my($time) = sprintf("%02d:%02d",$hour,$min);
0162 
0163   # advance day (for the enddate) if we have moved from pm to am
0164   if($lastampm =~ m/[Pp][Mm]/ && $ampm =~ m/[Aa][Mm]/ ) {
0165     ($year, $month, $day) = Add_Delta_Days($year,$month, $day, 1);
0166   }
0167 
0168   # format start date and time for "greping" later
0169   my($gdate) = sprintf("\"%s %02d %s %4d\"",
0170                        Day_of_Week_to_Text(Day_of_Week($year,$month,$day)),
0171                        $day,
0172                        Month_to_Text($month),
0173                        $year);
0174   my($ghour) = $hour;
0175   if( $ghour == 12 ) {
0176     $ampm = "pm";
0177   }
0178   if( $ghour > 12 ) {
0179     $ghour -= 12;
0180     $ampm = "pm"
0181   }
0182   if( $ghour == 0 ) {
0183     $ghour = 12;
0184     $ampm = "am";
0185   }
0186   my($gtime) = sprintf("\"%02d:%02d %s\"",$ghour,$min,lc($ampm));
0187 
0188   ### Compute Movie End Datetime by adding Movie Duration to Start Datetime
0189 
0190   # derive duration
0191   my($duration) = $event;
0192   $duration =~ s/CC//g;
0193   $duration =~ s/LBX//g;
0194   $duration =~ s/DVS//g;
0195   my(@d) = reverse(split(" ",$duration));
0196   $duration=$d[0];
0197   $duration =~ s/m\.$//g;
0198   #print "DURATION COMPUTATION ERROR\n" if( $duration < 1 || $duration > 300);
0199 
0200   my($endyear,$endmonth,$endday,$endhh, $endmm, $endss) = Add_Delta_DHMS(
0201        $year,$month,$day,$hour,$min,0,
0202                     0,   0,    $duration, 0);
0203   # format end datetime
0204   my($enddate) = sprintf("%4d-%02d-%02d",$endyear,$endmonth,$endday);
0205   my($endtime) = sprintf("%02d:%02d",$endhh,$endmm);
0206   my($genddate) = sprintf("\"%s %02d %s %4d\"",
0207                           Day_of_Week_to_Text(Day_of_Week($endyear,$endmonth,$endday)),
0208                           $endday,
0209                           Month_to_Text($endmonth),
0210                           $endyear);
0211   $ampm = "am";
0212   if( $endhh == 12 ) {
0213     $ampm = "pm";
0214   }
0215   if( $endhh > 12 ) {
0216     $endhh -= 12;
0217     $ampm = "pm";
0218   }
0219   if( $endhh == 0 ) {
0220     $endhh = 12;
0221     $ampm = "am";
0222   }
0223   my($gendtime) = sprintf("\"%02d:%02d %s\"",$endhh,$endmm,lc($ampm));
0224 
0225   # Derive Movie Title
0226   my($tmp) = split("[)]",$event);
0227   my($tmp2,$title) = split("^[0-9]*:[0-9][0-9][[:space:]]*[A,P]M",$tmp);
0228   $title=&rmleadwhite($title);
0229   $title=$title . ")";
0230   if( $title =~ m/\([[:space:]]\)/ ) {
0231     print "SKIPPING MOVIE WITHOUT A TITLE\n";
0232     return;
0233   }
0234 
0235   my($gtitle) = "\"" . $title . "\"";
0236 
0237   # "Grep line".
0238   #  Due to events across multiple days, search for ending date/time only.
0239 #  my($UID)=&find_uid("$gdate,$gtime,$genddate,$gendtime,$gtitle");
0240   my($UID)=&find_uid(",$genddate,$gendtime,$gtitle");
0241 
0242   # New Title for Change Mode
0243   $title=uc($title) if( $mode eq "--change" );
0244 
0245 #  print "EVENT start datetime=$date, $time, title=$title, end datetime=$enddate, $endtime, duration=$duration\n";
0246   $lastampm=$ampm;
0247 
0248   # Run konsolekalendar to insert/change/delete the event
0249   if( system("$konkal $mode $UID --file $cal --date $date --time $time --end-date $enddate --end-time $endtime --summary \"$title\" --description \"$event\"") ) {
0250     $mode =~ s/--//;
0251     print "Failure Condition Encountered.  Unable to $mode Event.\n"; exit 1;
0252   }
0253 
0254   $num++;
0255 }
0256 
0257 sub find_uid() {
0258 
0259   my($line);
0260   my($grepline) = shift;
0261   my($UID)="";
0262 
0263   if( $mode ne "--add" ) {
0264     if( open(VIEW, "$konkal --view --all --export-type csv --file $cal |") ) {
0265       while($line=<VIEW>) {
0266     if( index($line,$grepline) >= 0) {
0267       my(@u) = reverse(split(",",$line));
0268       chomp($u[0]);
0269       $UID="--uid=$u[0]";
0270       last;
0271     }
0272       }
0273       if( $UID eq "" ) {
0274     print "Failure Condition Encoutered.  Unable to locate Event $grepline in calendar.\n"; exit 1;
0275       }
0276     } else {
0277       die "Failed to view cal $cal.\n";
0278     }
0279     close(VIEW);
0280   }
0281   return($UID);
0282 }
0283 
0284 __END__