فهرست منبع

Prepare for DB conversion

Zachary Kotlarek 9 سال پیش
والد
کامیت
06aac955fe
7فایلهای تغییر یافته به همراه58 افزوده شده و 32 حذف شده
  1. 6 5
      web/includes/config.php
  2. 1 2
      web/includes/display.php
  3. 1 2
      web/includes/series.php
  4. 47 17
      web/includes/tvdb_update.php
  5. 1 2
      web/index.php
  6. 1 2
      web/precache.php
  7. 1 2
      web/seasonMaint.php

+ 6 - 5
web/includes/config.php

@@ -1,7 +1,7 @@
 <?
 
 # Debug output
-define('DEBUG', false);
+define('DEBUG', true);
 
 # Set locale and TZ to make things sane
 define('TZ', 'America/Los_Angeles');
@@ -9,12 +9,13 @@ date_default_timezone_set(TZ);
 setlocale(LC_ALL, 'en_US.UTF8');
 
 # TVDB API
-define('TVDB_URL',     'http://thetvdb.com');
-define('TVDB_API_KEY', '26877109147D171D');
-define('TVDB_DBN',     'pgsql:dbname=tv');
+define('TVDB_URL',           'http://thetvdb.com');
+define('TVDB_API_KEY',       '26877109147D171D');
+define('TVDB_DBN',           'pgsql:dbname=tv');
+define('TVDB_UPDATE_TIMEOUT', 86400 * 7);
 
 ### Config
-$TV_PATH    = '/mnt/media/TV';	# Root path for all TV series
+define('TV_PATH', '/mnt/media/TV');
 $MAX_AGE    = 86400 * 90;	# Consider a series "old" if it has not been updated for this many seconds
 $CACHE_AGE  = 300;
 $CACHE_FILE = '/var/tmp/php/tv_web.cache';

+ 1 - 2
web/includes/display.php

@@ -147,8 +147,7 @@ function printSeries($series) {
 # Print a DL of all series and note the available and monitored seasons
 function printAllSeries() {
 	require 'config.php';
-	global $TV_PATH;
-	$all_series = allSeriesSeasons($TV_PATH);
+	$all_series = allSeriesSeasons(TV_PATH);
 
 	# Header
 	echo '<div data-role="header">';

+ 1 - 2
web/includes/series.php

@@ -7,8 +7,7 @@ function seriesExists($series) {
 }
 
 function seriesPath($series) {
-	global $TV_PATH;
-	return $TV_PATH . '/' . $series;
+	return TV_PATH . '/' . $series;
 }
 
 # Find all series and seasons under a given path

+ 47 - 17
web/includes/tvdb_update.php

@@ -1,20 +1,38 @@
-#!/usr/local/bin/php
 <?php
 require_once('includes/main.php');
 require_once('includes/tvdb/init.php');
 
-# TEST
-if (!update(275557)) {
-	return -1;
+function tvdb_lastModified($id) {
+	# DB init
+	$dbh = new PDO(TVDB_DBN);
+	$select = $dbh->prepare('SELECT modified FROM series WHERE id=:id');
+
+	# Query
+	$select->execute(array('id' => $id));
+	$result = $select->fetch(PDO::FETCH_ASSOC);
+
+	# Cast as a datetime
+	$time = 0;
+	if ($result && is_array($result) && $result['modified']) {
+		$time = strtotime($result['modified']);
+	}
+	return $time;
 }
-return 0;
-# END TEST
 
-function update($id) {
-	return (updateSeries($id) && updateEpisodes($id));
+function tvdb_update($id, $force = false) {
+	if (!$force) {
+		$modified = tvdb_lastModified($id);
+		if (time() - $modified < TVDB_UPDATE_TIMEOUT) {
+			if (DEBUG) {
+				echo 'Skipping due to recent update: ' . $id . "\n";
+			}
+			return true;
+		}
+	}
+	return (tvdb_series($id) && tvdb_episodes($id));
 }
 
-function updateEpisodes($id) {
+function tvdb_episodes($id) {
 
 	# Fetch
 	$tvdb = new Moinax\TvDb\Client(TVDB_URL, TVDB_API_KEY);
@@ -35,22 +53,31 @@ function updateEpisodes($id) {
 	# Update
 	$error = 0;
 	foreach ($episodes['episodes'] as $episode) {
+		$airdate = null;
+		if (is_object($episode->firstAired)) {
+			$airdate = $episode->firstAired->format('c');
+		} else if (DEBUG) {
+			echo 'No airdate for: ' . $id . '::' . $episode->season . ':' . $episode->number . "\n";
+		}
 		$data = array(
 			'id' => $id,
 			'season' => $episode->season,
 			'number' => $episode->number,
-			'airdate' => $episode->firstAired->format('c'), 
+			'airdate' => $airdate,
 			'name' => $episode->name,
 			'desc' => $episode->overview
 		);
-		if (!insertOrUpdate($insert, $update, $data)) {
+		if (!tvdb_insert($insert, $update, $data)) {
+			if (DEBUG) {
+				print_r($dbh->errorInfo());
+			}
 			$err++;
 		}
 	}
 	return ($err == 0);
 }
 
-function updateSeries($id) {
+function tvdb_series($id) {
 
 	# Fetch series and episodes
 	$tvdb = new Moinax\TvDb\Client(TVDB_URL, TVDB_API_KEY);
@@ -76,11 +103,7 @@ function updateSeries($id) {
 		'desc' => $series->overview,
 		'year' => $series->firstAired->format('Y')
 	);
-	return insertOrUpdate($insert, $update, $data);
-}
-
-function insertOrUpdate($insert, $update, $data) {
-	if (!$insert->execute($data) && !$update->execute($data)) {
+	if (!tvdb_insert($insert, $update, $data)) {
 		if (DEBUG) {
 			print_r($dbh->errorInfo());
 		}
@@ -88,4 +111,11 @@ function insertOrUpdate($insert, $update, $data) {
 	}
 	return true;
 }
+
+function tvdb_insert($insert, $update, $data) {
+	if (!$insert->execute($data) && !$update->execute($data)) {
+		return false;
+	}
+	return true;
+}
 ?>

+ 1 - 2
web/index.php

@@ -25,8 +25,7 @@ if (isset($_REQUEST['series'])) {
 		# Force refresh of the series list
 		if (preg_match('/refresh/i', $_REQUEST['series'])) {
 			set_time_limit(0);
-			global $TV_PATH;
-			allSeriesSeasons($TV_PATH, false);
+			allSeriesSeasons(TV_PATH, false);
 		}
 
 		# Always redirect to the main page

+ 1 - 2
web/precache.php

@@ -4,7 +4,6 @@
 set_time_limit(0);
 require_once 'includes/main.php';
 
-global $TV_PATH;
-allSeriesSeasons($TV_PATH, false);
+allSeriesSeasons(TV_PATH, false);
 
 ?>

+ 1 - 2
web/seasonMaint.php

@@ -4,8 +4,7 @@
 set_time_limit(0);
 require_once 'includes/main.php';
 
-global $TV_PATH;
-$all_series = allSeriesSeasons($TV_PATH, false);
+$all_series = allSeriesSeasons(TV_PATH, false);
 $old_series = array();
 $added_seasons = array();
 $multi_seasons = array();