#!/usr/bin/perl

#use like: time ./makefetchcmds.pl 201505 201911 "%22trump%20is%22" CNN


$START = $ARGV[0]; $STARTYEAR = substr($START, 0, 4);
$END = $ARGV[1]; $ENDYEAR = substr($END, 0, 4);
$QUERY = $ARGV[2];
$STATION = $ARGV[3];

use Time::Piece;
mkdir("./CACHE/");

open(OUT, ">./run.sh");
print OUT "#!/bin/sh\n";
foreach $year ($STARTYEAR...$ENDYEAR) {
    foreach $mon (1...12) {
	$dateym = sprintf("%04d%02d", $year, $mon);
	if ($dateym >= $START && $dateym <= $END) { } else { next; }
	$t = Time::Piece->strptime($dateym, "%Y%m");
	$lastday = $t->month_last_day;
	$startdate = sprintf("%04d%02d%02d", $year, $mon, 1);
	$enddate = sprintf("%04d%02d%02d", $year, $mon, $lastday);
	
	#foreach $day (1...$lastday) { 
	#$date = sprintf("%04d%02d%02d", $year, $mon, $day);
	
	print OUT "curl \"https://api.gdeltproject.org/api/v2/tv/tv?startdatetime=${startdate}000000&enddatetime=${enddate}235959&datanorm=raw&query=$QUERY%20station:$STATION%20&mode=clipgallery&format=csv&maxrecords=5000\" -o ./CACHE/$STATION.$dateym.csv; sleep 5;\n";
	
    }
}
close(OUT);
chmod(0755, "./run.sh");
