| 346 | | my ($score, $total); |
| 347 | | foreach (keys %$gscore) |
| 348 | | { |
| 349 | | $score = 10; |
| 350 | | $gscore->{$_} = $score; |
| | 358 | my ($score, $total, $niceness, $granularity, $m); |
| | 359 | |
| | 360 | my $missing = detect_missing_data(); |
| | 361 | |
| | 362 | foreach my $grabber (keys %$gscore) |
| | 363 | { |
| | 364 | # for each slot, say whether we can fill it or not -- that is, |
| | 365 | # whether we support this channel and this day #. |
| | 366 | |
| | 367 | my $hits = 0; |
| | 368 | foreach (@$missing) |
| | 369 | { |
| | 370 | my ($ch, $day) = split(/:/); |
| | 371 | my $result = (can_support_channel($grabber, $ch) |
| | 372 | and |
| | 373 | can_support_day($grabber, $day)); |
| | 374 | $hits += $result; |
| | 375 | # print "$grabber vs $ch:$day: " . ($result ? "OK" : "no") . "\n"; |
| | 376 | } |
| | 377 | $niceness = $grabbers->{$grabber}->{config}->{niceness}; |
| | 378 | unless ($niceness) |
| | 379 | { |
| | 380 | print "WARNING: Grabber $grabber has no niceness support " . |
| | 381 | "specified in config.\n"; |
| | 382 | $niceness = 5; |
| | 383 | } |
| | 384 | $granularity = $grabbers->{$grabber}->{config}->{granularity}; |
| | 385 | unless (defined $granularity) |
| | 386 | { |
| | 387 | print "WARNING: Grabber $grabber has no granularity support " . |
| | 388 | "specified in config.\n"; |
| | 389 | $granularity = ''; |
| | 390 | } |
| | 391 | # TODO: use granularity more intelligently ('c' vs 'd') -- Max. |
| | 392 | $granularity = length ($granularity); |
| | 393 | my $total_channeldays = $days * scalar (keys %$channels); |
| | 394 | $granularity *= int((($total_channeldays - 1 ) / scalar(@$missing))/2); |
| | 395 | |
| | 396 | $score = $hits * ($niceness + $granularity); |
| | 397 | print "Grabber $grabber can fill $hits empty slots with $niceness niceness and $granularity granularity: scoring $score pts.\n"; |
| | 398 | $gscore->{$grabber} = $score; |
| | 402 | } |
| | 403 | |
| | 404 | sub can_support_channel |
| | 405 | { |
| | 406 | my ($grabber, $ch) = @_; |
| | 407 | |
| | 408 | my $channels_supported = $grabbers->{$grabber}->{config}->{channels}; |
| | 409 | unless (defined $channels_supported) |
| | 410 | { |
| | 411 | print "WARNING: Grabber $grabber has no channel support " . |
| | 412 | "specified in config.\n"; |
| | 413 | $channels_supported = ''; |
| | 414 | } |
| | 415 | |
| | 416 | return 1 unless ($channels_supported); # Empty string means we support all |
| | 417 | |
| | 418 | my $match = ($channels_supported =~ /\b$ch\b/); |
| | 419 | # $match ||= 0; |
| | 420 | my $exceptions = ($channels_supported =~/^-/); |
| | 421 | # $exceptions ||= 0; |
| | 422 | # print "M: " . Dumper ($match) . "E:" . Dumper ($exceptions) . "\n"; |
| | 423 | # print "Can $grabber support channel $ch: " . ($match != $exceptions ? 1 : 0) . |
| | 424 | # " (match $match, exceptions: $exceptions).\n"; |
| | 425 | return ($match != $exceptions); |
| | 426 | } |
| | 427 | |
| | 428 | sub can_support_day |
| | 429 | { |
| | 430 | my ($grabber, $day) = @_; |
| | 431 | |
| | 432 | my $days_supported = $grabbers->{$grabber}->{config}->{max_days}; |
| | 433 | unless ($days_supported) |
| | 434 | { |
| | 435 | print "WARNING: Grabber $grabber has no max_days support " . |
| | 436 | "specified in config.\n"; |
| | 437 | $days_supported = 2; |
| | 438 | } |
| | 439 | # print "Can $grabber support day $day: " . ($day <= $days_supported) . ".\n"; |
| | 440 | return $day <= $days_supported; |
| | 441 | } |
| | 442 | |
| | 443 | # |
| | 444 | # Build a little hash of what channel/day data we're currently missing. |
| | 445 | # I think granularity of one day is good for now; could possibly be |
| | 446 | # made more fine-grained if we think grabbers will support that. |
| | 447 | # |
| | 448 | sub detect_missing_data |
| | 449 | { |
| | 450 | my @missing; |
| | 451 | |
| | 452 | my $timeslots_per_day = (24 * 60 * 60) / $timeslot_size; |
| | 453 | |
| | 454 | foreach my $ch (keys %$channels) |
| | 455 | { |
| | 456 | if (defined $channel_data->{$ch}) |
| | 457 | { |
| | 458 | my $slotnum; |
| | 459 | for ($slotnum = 0; $slotnum < $num_timeslots-1; $slotnum++) |
| | 460 | { |
| | 461 | if (!@{$channel_data->{$ch}->{timeslots}}[$slotnum]) |
| | 462 | { |
| | 463 | my $day = int($slotnum / $timeslots_per_day) + 1; |
| | 464 | push @missing, "$ch:$day"; |
| | 465 | $slotnum += $timeslots_per_day - |
| | 466 | ($slotnum % $timeslots_per_day); |
| | 467 | |
| | 468 | } |
| | 469 | } |
| | 470 | } |
| | 471 | else |
| | 472 | { |
| | 473 | for my $i (1 .. $days) |
| | 474 | { |
| | 475 | push (@missing, "$ch:$i"); |
| | 476 | } |
| | 477 | } |
| | 478 | |
| | 479 | } |
| | 480 | |
| | 481 | print "Need data for @missing.\n" if ($debug); |
| | 482 | return \@missing; |