#!/usr/bin/perl

#(9/27/2023): Accepts an SRT captioning file and outputs a JSON file of explicit legislative mentions per: https://blog.gdeltproject.org/hyperlinking-television-connecting-our-nations-legislation-to-the-legislative-process-via-deep-linking-cspan/

use POSIX qw(floor);
use URI::Escape;
use JSON::XS;

if ($ARGV[0] eq '' || $ARGV[1] eq '') { print "FATAL: Invalid Parameters...\n"; exit; }
$IAID = $ARGV[0]; $IAID=~s/^.*\///; $IAID=~s/\.captioning\.main\.srt//;

##################################################################
#LOAD TRANSCRIPT...

#load from file...
open(FILE, $ARGV[0]); binmode(FILE, ":utf8");
while(<FILE>) {
    $_=~s/[\x{FEFF}\x{FFFF}]/ /g; #first transcode a few characters...
    $_=~s/\s+$//; $_=~s/^\s+//; #remove leading/trailing spaces...
    
    #skip formatting lines...
    if ($_=~/^\d+$/ || $_ eq '') { next; };
    if ($_=~/^[\d:,\-\> ]+$/) { next; };
    #remove speaker notations...
    $_=~s/^>>\s*//;    
    #$mid = ord(substr($_,0,1)); #codify oddities...
    
    $TRANSCRIPT .= $_ . ' ';
    
}
close(FILE);

#clean up extra spaces...
$TRANSCRIPT=~s/\s+/ /gs;

#compute Congress Number...
$CONGRESSNUMBER = 0;
($CHANNEL, $year, $mon, $day, $hour, $min, $sec, $PROGRAM) = $IAID=~/(CSPAN[23]*)_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)_*(.*)/;
$PROGRAM=~s/_/ /g;
$SHOWJSONDATE = "${year}-${mon}-${day}T${hour}:${min}:${sec}.000Z";
$startyear = $year % 2 == 0 ? $year - 1 : $year;
$CONGRESSNUMBER = floor(($startyear - 1789) / 2) + 1;
#print "CongressNumber: $CONGRESSNUMBER\n";
##################################################################

##################################################################
#EXTRACT LEGISLATION MENTIONS...

#$TRANSCRIPT = "THE BILL, H.R. 2494, TO MAKE THE ASSAULT and house resolution ZERO was";

#lookup to remap spelled-out numbers...
%remap = ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6, 'seven' => 7,
	'eight' => 8, 'nine' => 9, 'zero' => 0);
	
#scan for legislation mentions...
while ($TRANSCRIPT=~/(hr|h\.r\.|house bill|s\.|senate bill|h\.res\.|h \.res\.|house resolution|house res\.|s\.res\.|s\. res\.|senate res\.|senate resolution|h\.con\.res\.|house concurrent resolution|s\.con\.res\.|senate concurrent resolution|h\.j\.res\.|house joint resolution|s\.j\.res\.|senate joint resolution|h\.amdt\.|house amendment|s\.amdt\.|senate amendment)\s+([0-9]{1,4}|zero|one|two|three|four|five|six|seven|eight|nine)/gi) {
    my $billtype = $1, $billnum = $2;
    #extract our pre/post contextual text... BEFORE the other regexes below...
    $pre = substr($`, -75, 75); $post = substr($', 0, 75);
    $pre=~s/^[^\s]+\s//;$post=~s/\s[^\s]+$//; #chop to the nearest word (must do AFTER the regexs above)...
    
    ######################
    #remap spelled-out bill numbers...
    $billnum=~s/\b(one|two|three|four|five|six|seven|eight|nine|zero)\b/$remap{lc($1)}/gi;
    
    #remap spelled-out types ("house resolution" => "h.res.") since the search only supports in this form...
    $billtype=~s/\bhouse bill\b/h.r./i;
    $billtype=~s/\bsenate bill\b/s./i;

    $billtype=~s/\bhouse resolution\b/h.res./i;
    $billtype=~s/\bhouse res\./h.res./i;
    $billtype=~s/\bsenate resolution\b/s.res./i;
    $billtype=~s/\bsenate res\./s.res./i;
    
    $billtype=~s/\bhouse concurrent resolution\b/h.con.res./i;
    $billtype=~s/\bsenate concurrent resolution\b/s.con.res./i;
    
    $billtype=~s/\bhouse amendment\b/h.amdt./i;
    $billtype=~s/\bsenate amendment\b/s.amdt./i;

    $billtype=~s/\bhouse joint resolution\b/h.j.res./i;
    $billtype=~s/\bsenate joint resolution\b/s.j.res./i;
    ######################

    ######################
    #craft the Congress.gov search link...
    $billlink = 'https://www.congress.gov/quick-search/legislation?wordsPhrases="' .
   	uri_escape("$billtype$billnum") . '"&wordVariants=on&congresses%5B%5D=' . $CONGRESSNUMBER;
    ######################

    ######################
    #write our record...
    my $rec;
    $rec->{'IAID'} = $IAID;
    $rec->{'IAURL'} = 'https://archive.org/details/' . $IAID;
    $rec->{'TVEURL'} = 'https://api.gdeltproject.org/api/v2/tvv/tvv?id=' . $IAID;
    $rec->{'broadcastDate'} = $SHOWJSONDATE;
    $rec->{'channel'} = $CHANNEL;
    $rec->{'program'} = $PROGRAM;
    $rec->{'congressNum'} = $CONGRESSNUMBER + 0;
    $rec->{'preText'} = $pre;
    $rec->{'legislation'} = "$billtype$billnum";
    $rec->{'legislationType'} = $billtype;
    $rec->{'legislationNum'} = $billnum + 0;
    $rec->{'congressGovLink'} = $billlink;
    $rec->{'postText'} = $post;    
    $JSONWRITE .= JSON::XS->new->allow_nonref(1)->utf8->encode( $rec ) . "\n";
    ######################

}
##################################################################


##################################################################
#WRITE OUR RESULTS (IF ANY)...
if ($JSONWRITE ne '') { 
    open(OUT, ">$ARGV[1]"); print OUT $JSONWRITE; close(OUT);
}
##################################################################


