Listing all my shows

Listing all my shows

I have just bought a Chromecast and originally thought this would be an efficient way to cast my shows to my TV without having to remember what I have left to watch but casting video from an embedded media player in a chrome tab seems to be a little CPU intensive. 

Sadly after 2 hours I realized this wasn't going to be what I had intended so I'll use Local Cast or Video Stream to cast files form my PC to the TV but this is still quite useful to me as I have about 75 shows I watch and I delete every episode when I have watched it so keeping track of what I have available to watch is a bit tiresome if I have to use my memory or Windows Explorer to check if I already have an episode. 

So what this does is build an array of directories you have pointed it at and then for each directory build a flat list of files of the types you are interested that can then be used to make a list. I have my shows in a reasonably organised structure but some of the shows have a messy structure within and the files themselves may be a few directories deep so being able to view them all in one page without having to click anything is a nice feature.

Try it if you're interested, it caters for a messy file structure and is available on my GitHub.

<?php
/**
 * @file
 * Lists all the things
 */
 
// Locations where my files and series are
define('SERIES',   'C:/path/to/TV Shows');
define('FILMS',    'C:/path/to/Films');
define('INCOMING', 'C:/path/to/Incoming');
 
// Get the directories I care about
$directories[] = INCOMING;
$directories[] = FILMS;
$directories = array_merge($directories, glob(SERIES . '/*', GLOB_ONLYDIR));
 
// The file types I care about
$types = array(
  '.mkv',
  '.avi',
  '.mp4'
);
 
// For all the directories
foreach($directories as $dir) {
 
  // Make the printable h4 title
  $name = str_replace(SERIES . '/', '', $dir);
  $name = str_replace(FILMS, 'Films', $name);
  $name = str_replace(INCOMING, 'Incoming', $name);
  $array = get_directory($dir);
 
  // If there are files in this directory
  if (!empty($array)) {
    print "<h4>$name</h4>";
 
    // Make a sortable array
    $container = array();
    foreach($array as $directory => $files) {
      if (is_array($files)) {
        foreach($files as $file) {
          if (!empty($types)) {
            $ext = strtolower(mb_substr($file, (4 * -1)));
            if (in_array($ext, $types)) {
              $url = $directory . '/' . $file;
              $container[mb_strtoupper(str_replace(' ', '.', $file)) ] = array(
                'file' => $file,
                'url' => $url,
              );
            }
          }
          else {
            $url = $directory . '/' . $file;
            $container[mb_strtoupper(str_replace(' ', '.', $file)) ] = array(
              'file' => $file,
              'url' => $url,
            );
          }
        }
      }
    }
 
    ksort($container);
    print '<ul>';
    foreach($container as $item) {
      print '<li><a href="file://' . $item['url'] . '">' . $item['file'] . '</a></li>';
    }
    print '</ul>';
  }
}
 
/**
 * Get me all the things
 */
function get_directory($path = '.') {
  $tree = array();
  $temp = array();
  $ignore[] = '.';
  $ignore[] = '..';
  $dh = @opendir($path);
  while (false !== ($file = readdir($dh))) {
    if (!in_array($file, $ignore)) {
      if (!is_dir($path . '/' . $file)) {
        $tree["$path"][] = $file;
      }
      else {
        $temp = get_directory($path . '/' . $file, $ignore);
        if (is_array($temp)) {
          $tree = array_merge($tree, $temp);
        }
      }
    }
  }
  closedir($dh);
 
  return $tree;
}

Comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.