diff options
| author | root | 2014-09-13 22:26:58 +0200 |
|---|---|---|
| committer | root | 2014-09-13 22:26:58 +0200 |
| commit | c5639ee890215e4e8e0f544821ea8d285ca58eb8 (patch) | |
| tree | 29f685943c61c4d7ec0e376e485686e985b97065 /func_youtube.php | |
| parent | f8c60cae423fc78ed21d17a9217716ccc1e6dab1 (diff) | |
| download | video-dl-c5639ee890215e4e8e0f544821ea8d285ca58eb8.tar.gz | |
init
Diffstat (limited to 'func_youtube.php')
| -rw-r--r-- | func_youtube.php | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/func_youtube.php b/func_youtube.php new file mode 100644 index 0000000..9affc33 --- /dev/null +++ b/func_youtube.php @@ -0,0 +1,113 @@ +<?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("db/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"]; + + $db->close(); + unset($db); + + 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); + } + +} |
