use v5.10;
use strict;

use Getopt::Long;
Getopt::Long::Configure('bundling');

my %params = (
	sparse => undef,
	auto_threshold => 120,
	auto_power => '1/1.5',

	agent => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36', 
	id => undef, title => undef, ext => undef, start => undef, end => undef, dry_run => undef, NN => undef, TT => undef, offset => undef, print_title  => 0, load_splash => 0,
);

GetOptions(
	'sparse=s' => \$params{sparse},
	'auto-threshold=i' => \$params{auto_threshold},
	'auto-power=s' => \$params{auto_power},

	'agent=s' => \$params{agent}, 
	'id=i' => \$params{id}, 
	'title=s' => \$params{title}, 
	'ext=s' => \$params{ext}, 
	'start=s' => \$params{start}, 
	'end=s' => \$params{end},
	'dry-run=i' => \$params{dry_run},
	'NN=s' => \$params{NN},
	'TT=s' => \$params{TT},
	'offset=i' => \$params{offset},
	'print-title' => \$params{print_title},
	'load-splash' => \$params{load_splash},
)
or die 'GetOptions';

say ''; say "REM --- $_=$params{$_}" for sort keys %params; say '';

my $headers = join ' ', map {/\S/ ? qq`-H "$_"` : ()} map {chomp; $_} <DATA>;

if ($headers =~ /"User-Agent: (.+?)"/) {$params{agent} = $1}

my $page = qx`curl -L $headers https://livecamrips.to/video/$params{id} 2>nul`;
use File::Slurp;
#--- write_file qq`$params{id}.htm`, $page;
#--- my $page = read_file qq`$params{id}.htm`;
$page !~ m`iframe id="iframe-video" class="embed-responsive-item" src="(https://mdzsmutpcvykb.net/\S+?)"`i && die "$params{id}";

my $mdcore_page = $1;

#--- straight title
$page =~ m`<title>(.+?)</title>`;
$params{title} = $1;
$params{title} =~ s/:/_/g;
$params{title} = lc $params{title};
$params{title} .= " [$params{id}]";

#--- tricky title
$page =~ m`text-secondary">\n\s*(\S+)\n\s*</a>(.+)`;
$params{title} = "$1$2";
$params{title} = lc $params{title};
$params{title} .= " [$params{id}]";

if ($params{print_title}) {
	if ($page =~ /(\d\d)\.(\d\d)\.(\d\d) &middot;\s*\n\s*(\S+)/) {
		$params{title} = qq`($params{title}) - [20$3-$2-$1 id=$params{id} @{[lc$4]}]`;
	}
	say $params{title};
	exit;
}

$page = qx`curl -L $headers $mdcore_page 2>nul`;

my $script = join '', "var MDCore={};", (grep {/^(MDCore|eval)/i} split /\n/, $page),	";console.log(MDCore.wurl);";
$script !~ /mdcore\.ref/i && do {say "no mdcore: $params{id}"; say $page;   exit};
#--- $QQ=chr 34; $S=~s/$QQ/\\$QQ/g;
$script =~ s/"/\\"/g;

if ($params{load_splash}) {
	$script =~ s/;console.+/;console.log(MDCore.poster);/;
	my $url = "https:" . qx`e:\\etc\\jsshell-win64\\js.exe -e "$script"`;
	chomp $url;
	system qq`curl -k -L -A "$params{agent}" "$url" -o "$params{id}.jpg" 2>nul`;
	exit;
}

my $url = "https:" . qx`e:\\etc\\jsshell-win64\\js.exe -e "$script"`;
chomp $url;
say $url;
#--- say (($T) = qx`ffprobe-64 -headers "User-Agent: $ARGV[1]" -i "$U" 2>&1` =~ /Duration: ([:\d\.]+)/);

my $opt_sparse = defined $params{sparse} ? "--sparse=$params{sparse}" : '';
my $opt_auto_threshold = defined $params{auto_threshold} ? "--auto-threshold=$params{auto_threshold}" : '';
my $opt_auto_power = defined $params{auto_power} ? "--auto-power=$params{auto_power}" : '';

my $opt_ext = defined $params{ext} ? "--ext=$params{ext}" : '';
my $opt_start = defined $params{start} ? qq(--start="$params{start}") : '';
my $opt_end = defined $params{end} ? qq(--end="$params{end}") : '';
my $opt_dry_run = defined $params{dry_run} ? "--dry-run=$params{dry_run}" : '';
my $opt_NN = defined $params{NN} ? qq(--NN="@{[eval $params{NN}]}") : '';
my $opt_TT = defined $params{TT} ? qq(--TT="@{[eval $params{TT}]}") : '';
my $opt_offset = defined $params{offset} ? "--offset=$params{offset}" : '';

system qq`perl ../../load-by-ffmpeg.8.pl --agent="$params{agent}" --url="$url" --title="$params{title}" $opt_sparse $opt_auto_threshold $opt_auto_power    $opt_ext $opt_start $opt_end $opt_dry_run $opt_NN $opt_TT $opt_offset`;


__DATA__

Sec-Ch-Ua: "Chromium";v="136", "Google Chrome";v="136", "Not.A/Brand";v="99"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "macOS"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: identity
Accept-Language: en-US,en;q=0.9
Priority: u=0, i
