diff options
| author | Horus3 | 2014-03-12 16:01:48 +0100 |
|---|---|---|
| committer | Horus3 | 2014-03-12 16:01:48 +0100 |
| commit | eecb232938a20bd33eb5928e54d7fd3cffea9b2a (patch) | |
| tree | 9dbea05a60d99cf4c34541cfbff5edb366476ecc | |
| parent | 9c65f42d3c406f30c763dd12a5d68dd7850bd7f4 (diff) | |
| download | random-eecb232938a20bd33eb5928e54d7fd3cffea9b2a.tar.gz | |
Bugfixed and error routine.
| -rw-r--r-- | www/youtube.php | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/www/youtube.php b/www/youtube.php index 37654e5..ab8a9c8 100644 --- a/www/youtube.php +++ b/www/youtube.php @@ -1,11 +1,3 @@ -<!Doctype html> -<head> -<title>Random Youtube Video</title> -<link rel='shortcut icon' href='/favicon.ico' type='image/x-icon'> -</head> -<body> -<center> - <?php // generates the $youtube object with API version 2 @@ -17,6 +9,11 @@ Zend_Loader::loadClass('Zend_Gdata_YouTube'); $yt = new Zend_Gdata_YouTube(); $yt->setMajorProtocolVersion(2); +function error(){ + header("Refresh: 0; /youtube"); + exit; +} + function searchterm(){ $db = new SQLite3("../database/dict.db"); @@ -31,6 +28,9 @@ function searchterm(){ $random = mt_rand(1,$numRows); $search_word_db = $db->query("SELECT word FROM " . $table . " WHERE id=" . $random . ";"); + if(empty($search_word_db)){ + error(); + } $search_word_ar = $search_word_db->fetchArray(); $search_word = $search_word_ar["word"]; @@ -41,7 +41,13 @@ function getAndPrintVideoFeed($location, $yt) { $videoFeed = $yt->getVideoFeed($location); + if(empty($videoFeed)){ + error(); + } $videoID = printVideoFeed($videoFeed); + if(empty($videoID)){ + error(); + } return $videoID; } @@ -51,8 +57,14 @@ function printVideoFeed($videoFeed) $res_quant = count($videoFeed); $video = mt_rand(0, $res_quant-1); + if(empty($video)){ + error(); + } $videoId = printVideoEntry($videoFeed[$video]); + if(empty($videoId)){ + error(); + } return $videoId; @@ -61,6 +73,9 @@ function printVideoFeed($videoFeed) function printVideoEntry($videoEntry) { $videoId = $videoEntry->getVideoId(); + if(empty($videoId)){ + error(); + } return $videoId; } @@ -69,25 +84,41 @@ function start($yt){ $searchstring = searchterm(); $location = $yt->newVideoQuery(); + if(empty($location)){ + error(); + } // $location->setOrderBy('viewCount'); $location->setSafeSearch('none'); $location->setVideoQuery($searchstring); + if(empty($location)){ + error(); + } $videoID = getAndPrintVideoFeed($location, $yt); + if(empty($videoID)){ + error(); + } if(fopen("https://gdata.youtube.com/feeds/api/videos/" . $videoID , "r")){ return $videoID; } else { - start(); + start($yt); } } $videoID = start($yt); ?> +<!Doctype html> +<head> +<title>Random Youtube Video</title> +<link rel='shortcut icon' href='/favicon.ico' type='image/x-icon'> +</head> +<body> +<center> <h1>A random youtube video!</h1> <br> -<iframe width="560" height="315" src="https://www.youtube.com/embed/<? echo $videoID ?>" frameborder="0" allowfullscreen></iframe> <br><br> +<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php echo $videoID ?>" frameborder="0" allowfullscreen></iframe> <br><br> <a href="youtube">Get another one</a> |
