summaryrefslogtreecommitdiff
path: root/www/functions
diff options
context:
space:
mode:
authorroot2014-03-23 00:04:34 +0100
committerroot2014-03-23 00:04:34 +0100
commit293e2d3203d4f473f3239571b5d0237cea26a46b (patch)
tree00d4d35d4f69fb127649f07d8b000bd10fec5191 /www/functions
parentdc083d0685fa36e48f6d9e1ba2895f730a167194 (diff)
downloadrandom-293e2d3203d4f473f3239571b5d0237cea26a46b.tar.gz
Completed youtube history for the browser.
Diffstat (limited to 'www/functions')
-rw-r--r--www/functions/func_puush.php34
-rw-r--r--www/functions/func_youtube.php111
2 files changed, 145 insertions, 0 deletions
diff --git a/www/functions/func_puush.php b/www/functions/func_puush.php
new file mode 100644
index 0000000..472bd7b
--- /dev/null
+++ b/www/functions/func_puush.php
@@ -0,0 +1,34 @@
+<?php
+function get_image($count){
+
+ $puush_array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
+ "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
+ "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4",
+ "5", "6", "7", "8", "9" );
+
+ $length = count($puush_array);
+
+ $id = "";
+ $string = "";
+
+ for ($i=0;$i<4;$i++){
+ $index = mt_rand(0,$length-1);
+ $id = "$id".$puush_array[$index];
+ }
+
+ if(fopen("https://puu.sh/" . $id, "r")){
+ echo "<a href='https://puu.sh/" . $id . "'><img src='https://puu.sh/" .$id . "' alt='" . $id ."' ></a>";
+ return $count;
+ } else {
+ $count = $count + 1;
+ if($count < 100){
+ $count = get_image($count);
+ } else {
+ echo "We tried for " . $count . " times. Now is enough. <br><br>";
+ echo "<a href='/puush'>Try again</a>";
+ exit;
+ }
+ }
+ return $count;
+}
+
diff --git a/www/functions/func_youtube.php b/www/functions/func_youtube.php
new file mode 100644
index 0000000..74aa679
--- /dev/null
+++ b/www/functions/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);
+ }
+
+}
+