فهرست منبع

Bake-in TVDB delays.
Add a global TVDB enable/disable switch. Provide reasonable return values when TVDB functions are called when disabled.
Simplify seasonMaint to use new TVDB logic
Move a few magic numbers to the config.

Zachary Kotlarek 13 سال پیش
والد
کامیت
7a7047665b
3فایلهای تغییر یافته به همراه48 افزوده شده و 18 حذف شده
  1. 12 5
      web/includes/config.php
  2. 32 1
      web/includes/tvdb.php
  3. 4 12
      web/seasonMaint.php

+ 12 - 5
web/includes/config.php

@@ -1,11 +1,18 @@
 <?
 
-$MEDIA_PATH    = '/mnt/media';
-$TV_PATH       = $MEDIA_PATH . '/TV';
+### Config
+$TV_PATH       = '/mnt/media/TV';	# Root path for all TV series
+$MAX_AGE       = 86400 * 90;		# Consider a series "old" if it has not been updated for this many seconds
+
+### TVDB
+$ENABLE_TVDB   = true;		# Master enable/disable flag
+$TVDB_DELAY    = 20;		# Minimum delay between TVDB downloads
+$TVDB_TIMEOUT  = 15;		# Maximum load time for TVDB pages
+$TVDB_LANG_ID  = 7;		# Default language ID for TVDB URLs
+$TVDB_URL      = 'http://thetvdb.com/?tab=series';
+
+### App Config
 $EXISTS_FILES  = array('no_quality_checks', 'more_number_formats', 'skip');
 $CONTENT_FILES = array('must_match', 'search_name');
-$TVDB_URL      = 'http://thetvdb.com/?tab=series';
-$TVDB_LANG_ID  = 7;
-$MAX_AGE       = 86400 * 90;
 
 ?>

+ 32 - 1
web/includes/tvdb.php

@@ -1,6 +1,7 @@
 <?
 
 require 'config.php';
+$LAST_TVDB_DOWNLOAD = 0;
 
 # Parse and input URL for TVDB ID and LID
 function parseTVDBURL($url) {
@@ -36,9 +37,26 @@ function TVDBURL($id, $lid) {
 }
 
 function getTVDBPage($id, $lid) {
+
+	# If TVDB is not enabled return FALSE
+	global $ENABLE_TVDB;
+	if (!$ENABLE_TVDB) {
+		return false;
+	}
+
+	# Sleep between downloads to avoid TVDB bans
+	global $TVDB_DELAY;
+	global $LAST_TVDB_DOWNLOAD;
+	if (time() - $LAST_TVDB_DOWNLOAD < $TVDB_DELAY) {
+		sleep(rand($TVDB_DELAY, 2 * $TVDB_DELAY));
+	}
+	$LAST_TVDB_DOWNLOAD = time();
+
+	# Download with a timeout
+	global $TVDB_TIMEOUT;
 	$url = TVDBURL($id, $lid);
 	$ctx = stream_context_create(array(
-		'http' => array( 'timeout' => 15 )
+		'http' => array( 'timeout' => $TVDB_TIMEOUT )
 	)); 
 	return @file_get_contents($url, 0, $ctx);
 }
@@ -47,6 +65,12 @@ function getTVDBPage($id, $lid) {
 function getTVDBTitle($id, $lid) {
 	$retval = false;
 
+	# If TVDB is not enabled return FALSE
+	global $ENABLE_TVDB;
+	if (!$ENABLE_TVDB) {
+		return false;
+	}
+
 	$page = getTVDBPage($id, $lid);
 	if ($page) {
 		if (preg_match('/\<h1\>([^\>]+)\<\/h1\>/i', $page, $matches)) {
@@ -61,6 +85,13 @@ function getTVDBTitle($id, $lid) {
 function getTVDBSeasons($id, $lid) {
 	$retval = false;
 
+	# If TVDB is not enabled return a fake season list
+	global $ENABLE_TVDB;
+	if (!$ENABLE_TVDB) {
+		return array(1 => true);
+	}
+
+	# Download and parse the series page for a list of seasons
 	$page = getTVDBPage($id, $lid);
 	if ($page) {
 		if (preg_match_all('/class=\"seasonlink\"[^\>]*\>([^\<]+)\<\/a\>/i', $page, $matches)) {

+ 4 - 12
web/seasonMaint.php

@@ -1,7 +1,6 @@
 #!/usr/local/bin/php
 <?
 
-$DEBUG = false;
 set_time_limit(0);
 require_once 'includes/main.php';
 
@@ -12,14 +11,12 @@ $added_seasons = array();
 $multi_seasons = array();
 $empty_seasons = array();
 
+# Set this to "false" for offline testing
+#$ENABLE_TVDB = false;
+
 # Check each series for new seasons listed on TVDB but not locally
 foreach ($all_series as $series => $seasons) {
 
-	# Long wait between series, to avoid TVDB bans
-	if (!$DEBUG) {
-		sleep(rand(20, 40));
-	}
-
 	# Grab the season parameters
 	$flags = readFlags($series);
 
@@ -29,12 +26,7 @@ foreach ($all_series as $series => $seasons) {
 	}
 
 	# Grab the TVDB season list
-	$tvdb_seasons = array();
-	if (!$DEBUG) {
-		$tvdb_seasons = getTVDBSeasons($flags['tvdb-id'], $flags['tvdb-lid']);
-	} else {
-		$tvdb_seasons[1] = true;
-	}
+	$tvdb_seasons = getTVDBSeasons($flags['tvdb-id'], $flags['tvdb-lid']);
 
 	# We only care about "new" seasons -- don't force old seasons into the local tree
 	$tvdb_max = @max(array_keys($tvdb_seasons));