Quellcode durchsuchen

Cleanup into functions

Zachary Kotlarek vor 13 Jahren
Ursprung
Commit
dc44262c63
1 geänderte Dateien mit 97 neuen und 75 gelöschten Zeilen
  1. 97 75
      tv.php

+ 97 - 75
tv.php

@@ -1,50 +1,83 @@
 <?
-	$MEDIA_PATH='/mnt/media';
-	$TV_PATH=$MEDIA_PATH . '/TV';
-	$MONITORED_CMD='/home/profplump/bin/video/torrentMonitored.pl NULL ' . $MEDIA_PATH;
+# Config
+$MEDIA_PATH    = '/mnt/media';
+$MONITORED_CMD = '/home/profplump/bin/video/torrentMonitored.pl NULL ';
 
-	function isJunk($path) {
-		$path = basename($path);
-		if (preg_match('/^\.+$/', $path)) {
+# True if the input path is junk -- self-links, OS X noise, etc.
+function isJunk($path) {
+	$path = basename($path);
+	
+	# Ignore certain fixed paths
+	foreach (array('.', '..', '.DS_Store') as $value) {
+		if ($path == $value) {
 			return true;
 		}
-		if (preg_match('/^\.\_/', $path)) {
-			return true;
-		}
-		if (preg_match('/^\.DS_Store$/', $path)) {
-			return true;
+	}
+	
+	# Ignore ._ paths
+	if (preg_match('/^\.\_/', $path)) {
+		return true;
+	}
+	
+	# Otherwise the path is good
+	return false;
+}
+
+# Find all the monitored series/seasons from the given command
+function monitoredShows($cmd) {
+	$retval = array();
+	$proc = popen($cmd . ' 2>&1', 'r');
+	$paths = array();
+	while (!feof($proc)) {
+		$str = fread($proc, 4096);
+		$arr = explode("\0", $str);
+		$paths = array_merge($paths, $arr);
+	}
+	pclose($proc);
+
+	# Translates paths to a series/seasons structure
+	foreach ($paths as $path) {
+		preg_match('@/([^\/]+)/Season\s+(\d+)@', $path, $matches);
+		if (!array_key_exists($matches[1], $retval)) {
+			$retval[ $matches[1] ] = array();
 		}
-		return false;
+		$retval[ $matches[1] ][ $matches[2] ] = true;
+	}
+	
+	return $retval;
+}
+
+# Find all shows under the given path
+function allShows($base)	{
+	$retval = array();
+
+	# Look for series folders
+	$tv_dir = opendir($base);
+	if ($tv_dir === FALSE) {
+		die('Unable to opendir(): ' . $path . "\n");
 	}
+	while (false !== ($show = readdir($tv_dir))) {
 
-	$monitored = array();
-	{
-		$proc = popen($MONITORED_CMD . ' 2>&1', 'r');
-		$paths = array();
-		while (!feof($proc)) {
-			$str = fread($proc, 4096);
-			$arr = explode("\0", $str);
-			$paths = array_merge($paths, $arr);
+		# Skip junk
+		if (isJunk($show)) {
+			continue;
 		}
-		pclose($proc);
 
-		foreach ($paths as $path) {
-			preg_match('@/([^\/]+)/Season\s+(\d+)@', $path, $matches);
-			if (!array_key_exists($matches[1], $monitored)) {
-				$monitored[ $matches[1] ] = array();
-			}
-			$monitored[ $matches[1] ][ $matches[2] ] = true;
+		# We only care about directories
+		$show_path = $path . '/' . $show;
+		if (!is_dir($show_path)) {
+			continue;
 		}
-	}
 
-	$shows = array();
-	{
-		# Look for series folders
-		$tv_dir = opendir($TV_PATH);
-		if ($tv_dir === FALSE) {
-			die('Unable to opendir(): ' . $TV_PATH . "\n");
+		# Record the show title
+		$retval[ $show ] = array();
+
+		# Look for season folders
+		$show_dir = opendir($show_path);
+		if ($show_dir === FALSE) {
+			die('Unable to opendir(): ' . $show_path . "\n");
 		}
-		while (false !== ($show = readdir($tv_dir))) {
+		while (false !== ($season = readdir($show_dir))) {
 
 			# Skip junk
 			if (isJunk($show)) {
@@ -52,42 +85,28 @@
 			}
 
 			# We only care about directories
-			$show_path = $TV_PATH . '/' . $show;
-			if (!is_dir($show_path)) {
+			$season_path = $show_path . '/' . $season;
+			if (!is_dir($season_path)) {
 				continue;
 			}
 
-			# Record the show title
-			$shows[ $show ] = array();
-
-			# Look for season folders
-			$show_dir = opendir($show_path);
-			if ($show_dir === FALSE) {
-				die('Unable to opendir(): ' . $show_path . "\n");
-			}
-			while (false !== ($season = readdir($show_dir))) {
-
-				# Skip junk
-				if (isJunk($show)) {
-					continue;
-				}
-
-				# We only care about directories
-				$season_path = $show_path . '/' . $season;
-				if (!is_dir($season_path)) {
-					continue;
-				}
-
-				# Record season numbers
-				if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
-					$shows[ $show ][ $matches[1] ] = true;
-				}
+			# Record season numbers
+			if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
+				$retval[ $show ][ $matches[1] ] = true;
 			}
-			closedir($show_dir);
 		}
-		closedir($tv_dir);
+		closedir($show_dir);
 	}
+	closedir($tv_dir);
+	
+	return $retval;
+}
 
+# Find all shows and all monitored shows
+$shows = allShows($MEDIA_PATH . '/TV');
+$monitored = monitoredShows($MONITORED_CMD . $MEDIA_PATH);
+
+# Generic XHTML 1.1 header
 header('Content-type: text/html; charset=utf-8');
 print <<<ENDOLA
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
@@ -98,23 +117,26 @@ print <<<ENDOLA
 	<title>UberZach TV</title>
 </head>
 <body>
+
 ENDOLA;
 
-	echo '<dl>';
-	foreach ($shows as $show => $seasons) {
-		echo '<dt><a href="?show=' . urlencode($show) . '">' . htmlspecialchars($show) . "</a></dt>\n";
-		foreach ($seasons as $season => $val) {
-			if ($val) {
-				echo '<dd>Season ' . htmlspecialchars($season);
-				if ($monitored[ $show ][ $season ]) {
-					echo ' (monitored)';
-				}
-				echo "</dd>\n";
+# Print a DL of all shows and note the available and monitored seasons
+echo "<dl>\n";
+foreach ($shows as $show => $seasons) {
+	echo '<dt><a href="?show=' . urlencode($show) . '">' . htmlspecialchars($show) . "</a></dt>\n";
+	foreach ($seasons as $season => $val) {
+		if ($val) {
+			echo '<dd>Season ' . htmlspecialchars($season);
+			if ($monitored[ $show ][ $season ]) {
+				echo ' (monitored)';
 			}
+			echo "</dd>\n";
 		}
 	}
-	echo "</dl>\n";
+}
+echo "</dl>\n";
 
+# Generic XHTML 1.1 footer
 print <<<ENDOLA
 </body>
 </html>