diff options
Diffstat (limited to 'www/youtube.php')
| -rw-r--r-- | www/youtube.php | 74 |
1 files changed, 52 insertions, 22 deletions
diff --git a/www/youtube.php b/www/youtube.php index 05d61d6..1e902f7 100644 --- a/www/youtube.php +++ b/www/youtube.php @@ -1,18 +1,4 @@ -<!Doctype html> -<head> -<title>Random YouTube Video</title> -<meta http-equiv='Content-type' content='text/html; charset=utf-8' /> -<link rel='shortcut icon' href='/favicon.ico' type='image/x-icon' /> -<link rel='stylesheet' type='text/css' href='style.css'/> -</head> -<!--body link='#000000' alink='#000000' vlink='#6a6862'> -<body link='#000000' alink='#000000' vlink='#41403d'> -<body link='#000000' alink='#000000' vlink='#2c2b29'--> -<body vlnk='#5b5b5b' vlink='#000000' link='#000000' alink='#9a9696'> -<center> - <?php -include("header.php"); // generates the $youtube object with API version 2 $clientLibraryPath = "../zend/library"; @@ -23,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"); @@ -37,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"]; @@ -47,7 +41,13 @@ function getAndPrintVideoFeed($location, $yt) { $videoFeed = $yt->getVideoFeed($location); + if(empty($videoFeed)){ + error(); + } $videoID = printVideoFeed($videoFeed); + if(empty($videoID)){ + error(); + } return $videoID; } @@ -57,11 +57,16 @@ function printVideoFeed($videoFeed) $res_quant = count($videoFeed); $video = mt_rand(0, $res_quant-1); - if(!empty($video)){ - $videoId = printVideoEntry($videoFeed[$video]); - } else { - header("Refresh: 0; /youtube"); + + if(empty($video)){ + error(); } + + $videoId = printVideoEntry($videoFeed[$video]); + if(empty($videoId)){ + error(); + } + return $videoId; } @@ -69,6 +74,9 @@ function printVideoFeed($videoFeed) function printVideoEntry($videoEntry) { $videoId = $videoEntry->getVideoId(); + if(empty($videoId)){ + error(); + } return $videoId; } @@ -77,29 +85,51 @@ 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> +<meta http-equiv='Content-type' content='text/html; charset=utf-8' /> +<link rel='shortcut icon' href='/favicon.ico' type='image/x-icon' /> +<link rel='stylesheet' type='text/css' href='style.css'/> +</head> +<!--body link='#000000' alink='#000000' vlink='#6a6862'> +<body link='#000000' alink='#000000' vlink='#41403d'> +<body link='#000000' alink='#000000' vlink='#2c2b29'--> +<body vlnk='#5b5b5b' vlink='#000000' link='#000000' alink='#9a9696'> +<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> +<?php include("header.php"); ?> + +<h1>A random youtube video!</h1> <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> </center> <?include("piwik.php"); - |
