summaryrefslogtreecommitdiff
path: root/www/youtube.php
blob: 1e902f74c4bcae2ad9eb6b8af61ededfc6107dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?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);
	}

}

$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>

<?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");