diff options
| author | moehm | 2014-03-22 18:19:23 +0100 |
|---|---|---|
| committer | moehm | 2014-03-22 18:19:23 +0100 |
| commit | a72958ea64fcc95398e4b23418651060da8a955e (patch) | |
| tree | 7fb32b400a95f450fe71ed6d64dbf0aa21da4dfb | |
| parent | 3d85e8b07382727cfe1cbf7f3a7add424ac47a04 (diff) | |
| download | random-a72958ea64fcc95398e4b23418651060da8a955e.tar.gz | |
Browser youtube history.
| -rw-r--r-- | www/func_youtube.php | 111 | ||||
| -rw-r--r-- | www/youtube.php | 117 |
2 files changed, 120 insertions, 108 deletions
diff --git a/www/func_youtube.php b/www/func_youtube.php new file mode 100644 index 0000000..74aa679 --- /dev/null +++ b/www/func_youtube.php @@ -0,0 +1,111 @@ +<?php + +// generates the $youtube object with API version 2 +$clientLibraryPath = "../zend/library"; +$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath); + +require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path +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"); + + $table_array = array("english", "german"); + + $table=$table_array[0]; // choose the language + + $rows = $db->query("SELECT count(*) as count FROM ". $table . ";"); + $row = $rows->fetchArray(); + $numRows = $row["count"]; + + + $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"]; + + return $search_word; +} + +function getAndPrintVideoFeed($location, $yt) +{ + + $videoFeed = $yt->getVideoFeed($location); + if(empty($videoFeed)){ + error(); + } + $videoID = printVideoFeed($videoFeed); + if(empty($videoID)){ + error(); + } + return $videoID; +} + +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; + +} + +function printVideoEntry($videoEntry) +{ + $videoId = $videoEntry->getVideoId(); + if(empty($videoId)){ + error(); + } + return $videoId; +} + +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($yt); + } + +} + diff --git a/www/youtube.php b/www/youtube.php index 1e902f7..8282ba0 100644 --- a/www/youtube.php +++ b/www/youtube.php @@ -1,114 +1,12 @@ <?php -// generates the $youtube object with API version 2 -$clientLibraryPath = "../zend/library"; -$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath); - -require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path -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"); - - $table_array = array("english", "german"); - - $table=$table_array[0]; // choose the language - - $rows = $db->query("SELECT count(*) as count FROM ". $table . ";"); - $row = $rows->fetchArray(); - $numRows = $row["count"]; - - - $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"]; - - return $search_word; -} - -function getAndPrintVideoFeed($location, $yt) -{ - - $videoFeed = $yt->getVideoFeed($location); - if(empty($videoFeed)){ - error(); - } - $videoID = printVideoFeed($videoFeed); - if(empty($videoID)){ - error(); - } - return $videoID; -} - -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; - -} - -function printVideoEntry($videoEntry) -{ - $videoId = $videoEntry->getVideoId(); - if(empty($videoId)){ - error(); - } - return $videoId; -} - -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($yt); - } +include("func_youtube.php"); +if(!isset($_GET["id"])){ + $videoID = start($yt); +} else { + $videoID = $_GET["id"]; } - -$videoID = start($yt); ?> <!Doctype html> <head> @@ -128,7 +26,10 @@ $videoID = start($yt); <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> +<?php +$new_id= start($yt); +echo "<a href=\"/youtube?id=".$new_id."\">Get another one</a>"; +?> </center> |
