Skip to content

Instantly share code, notes, and snippets.

@alecgorge
Created September 4, 2010 05:03
Show Gist options
  • Save alecgorge/564919 to your computer and use it in GitHub Desktop.
Save alecgorge/564919 to your computer and use it in GitHub Desktop.
<pre><?php
function array_insert($array,$pos,$val) {
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
class Episode {
public $name = 0, $season = 0, $episode = 0;
public function __construct ($name, $season, $episode) {
$this->name = $name;
$this->season = (int)$season;
$this->episode = (int)$episode;
}
public function __toString () {
return "Episode ".$this->episode. " named '". $this->name ."' of season ".$this->season."\n";
}
}
function calcMostCommonDiff($orig, $files, $season) {
$mismatches = array();
$inRow = 0;
$firstLength = strlen(reset($orig));
$firstFile = reset($orig);
foreach($files as $file) {
$inRow = 0;
for($i = 0; $i < $firstLength; $i++) {
$thisChar = $firstFile{$i};
if($i >= strlen($file)) continue;
$otherChar = $file{$i};
if($thisChar != $otherChar && is_numeric($thisChar) && is_numeric($otherChar)) {
$inRow++;
$mismatches[] = $i;
if($inRow === 3) {
$removeStart = count($mismatches);
unset($mismatches[$removeStart - 1]);
unset($mismatches[$removeStart - 2]);
unset($mismatches[$removeStart - 3]);
break;
}
}
else {
$inRow = 0;
}
}
}
$count = array();
$len = count($mismatches);
foreach($mismatches as $match) {
$count[$match]++;
}
arsort($count);
$count = array_slice(array_keys($count), 0, 2);
list($pos1, $pos2) = $count;
$ePos = ($pos1 > $pos2 ? $pos1 : $pos2);
$complete_array = array_insert($files, reset(array_keys($orig)), reset($orig));
foreach($complete_array as $file) {
$parts = explode('.', $file);
$extension = array_pop($parts);
$reconstructed = implode('.', $parts);
$frontRemoved = ltrim(substr($reconstructed, $ePos+1), ' .,-[]_');
$backRemoved = preg_replace("/(\[[a-z0-9]+\])/", "", $frontRemoved);
$names[] = trim($backRemoved);
}
foreach($complete_array as $key => $value) {
$objects[] = new Episode($names[$key], $season, substr($value, (int)$count[1], ($count[0]-$count[1])+1));
}
return array($count, $mismatches, $names, $objects);
}
$paths = array(
'D:\Video\The Simpsons\The Simpsons Season 01',
'D:\Video\The Simpsons\The Simpsons Season 07',
'D:\Video\The Simpsons\The Simpsons Season 14',
'D:\Video\The Simpsons\The Simpsons Season 18',
'D:\Video\Family Guy\Season 6',
'D:\Video\Avatar The Last Airbender\Book 3 - Fire'
);
$rawFiles = glob($paths[5].'\*.*');
unset($files);
foreach($rawFiles as $file) {
$files[] = basename($file);
}
for($i = 0; $i < strlen($files[0]); $i++) {
echo " ".$files[0]{$i}." ";
}
echo "\n";
foreach(range(0,strlen($files[0])-1) as $val) {
echo sprintf("%02d ",$val);
}
echo "\n\n";
list($count, $mismatches, $names, $objects) = calcMostCommonDiff(array(0 => $files[0]), array_slice($files, 1), 3);
list($count2, $mismatches2) = calcMostCommonDiff(array(1 => $files[1]), array_merge(array($files[0]), array_slice($files, 2)), 3);
if($count == $count2) {
//$keys = array_keys($count);
echo $count[1] . " (tens place) and " . $count[0] . " (ones place) are episode positions.\n\n";
foreach($objects as $v) {
echo $v;
}
}
else {
var_dump($count, $count2, $mismatches, $mismatches2);
}
?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment