-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudiophonik_gen_youtube.php
executable file
·250 lines (216 loc) · 8.14 KB
/
audiophonik_gen_youtube.php
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
$youtube_boilerplate = <<<HTML
<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>YouTube Player API Sample</title>
<style type="text/css">
#videoContainer {
position:absolute;
top: -1000px;
left: -1000px;
}
#videoDiv {
margin-right: 3px;
}
#videoInfo {
margin-left: 3px;
}
</style>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
google.load("swfobject", "2.1");
</script>
<script type="text/javascript">
/*
* Chromeless player has no controls.
*/
var video_ids = [];
// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
document.getElementById(elmId).innerHTML = value;
}
// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
alert("An error occured of type:" + errorCode);
}
// This function is called when the player changes state
function onPlayerStateChange(newState) {
updateHTML("playerState", newState);
$('#videoTitle').html('');
// Pull an AJAX query
if (ytplayer && newState == 1) {
var current_video_id = ytplayer.getPlaylistIndex();
current_video_id = video_ids[current_video_id];
$.ajax({
url: "http://gdata.youtube.com/feeds/api/videos/" + current_video_id + "?v=2&alt=json",
dataType: "jsonp",
success: function (data) { parseresults(data); }
});
}
}
function parseresults(data) {
$('#videoTitle').html(data.entry.title.\$t);
}
// Display information about the current state of the player
function updatePlayerInfo() {
// Also check that at least one function exists since when IE unloads the
// page, it will destroy the SWF before clearing the interval.
if(ytplayer && ytplayer.getDuration) {
updateHTML("videoDuration", ytplayer.getDuration());
updateHTML("videoCurrentTime", ytplayer.getCurrentTime());
updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
updateHTML("startBytes", ytplayer.getVideoStartBytes());
updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
updateHTML("volume", ytplayer.getVolume());
}
}
// Allow the user to set the volume from 0-100
function setVideoVolume() {
var volume = parseInt(document.getElementById("volumeSetting").value);
if(isNaN(volume) || volume < 0 || volume > 100) {
alert("Please enter a valid volume between 0 and 100.");
}
else if(ytplayer){
ytplayer.setVolume(volume);
}
}
function playVideo() {
if (ytplayer) {
ytplayer.playVideo();
}
}
function pauseVideo() {
if (ytplayer) {
ytplayer.pauseVideo();
}
}
function muteVideo() {
if(ytplayer) {
ytplayer.mute();
}
}
function unMuteVideo() {
if(ytplayer) {
ytplayer.unMute();
}
}
// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("ytPlayer");
// This causes the updatePlayerInfo function to be called every 250ms to
// get fresh data from the player
setInterval(updatePlayerInfo, 250);
updatePlayerInfo();
ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
ytplayer.addEventListener("onError", "onPlayerError");
ytplayer.setPlaybackQuality("hd720");
//Load an initial video into the player
// ytplayer.cueVideoById("ylLzyHk54Z0");
// <REPLACE_WITH_VIDEO_IDS>
}
function prevVideo() {
if (ytplayer) {
ytplayer.previousVideo();
}
}
function nextVideo() {
if (ytplayer) {
ytplayer.nextVideo();
}
}
// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
// Lets Flash from another domain call JavaScript
var params = { allowScriptAccess: "always" };
// The element id of the Flash embed
var atts = { id: "ytPlayer", modestbranding: 1 };
// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
"version=3&enablejsapi=1&playerapiid=player1",
"videoDiv", "480", "360", "9", null, null, params, atts);
}
function _run() {
loadPlayer();
}
google.setOnLoadCallback(_run);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<table>
<tr>
<td id="videoTd"><div id="videoContainer"><div id="videoDiv" style="display: none">Loading...</div></div></td>
<td valign="top">
<div id="videoInfo">
<p>Player state: <span id="playerState">--</span></p>
<p>Current Time: <span id="videoCurrentTime">--:--</span> | Duration: <span id="videoDuration">--:--</span></p>
<p>Bytes Total: <span id="bytesTotal">--</span> | Start Bytes: <span id="startBytes">--</span> | Bytes Loaded: <span id="bytesLoaded">--</span></p>
<p>Controls: <a href="javascript:void(0);" onclick="playVideo();">Play</a> | <a href="javascript:void(0);" onclick="pauseVideo();">Pause</a> | <a href="javascript:void(0);" onclick="muteVideo();">Mute</a> | <a href="javascript:void(0);" onclick="unMuteVideo();">Unmute</a></p>
<p><input id="volumeSetting" type="text" size="3" /> <a href="javascript:void(0)" onclick="setVideoVolume();"><- Set Volume</a> | Volume: <span id="volume">--</span></p>
<p><a href="javascript:void(0)" onclick="nextVideo();">Next</a> | <a href="javascript:void(0)" onclick="prevVideo();">Previous</a></p>
<div id="videoTitle"></div>
</div>
</td></tr>
</table>
</body>
</html>
HTML;
function get_html($url) {
$content = file_get_contents($url);
$next_pg = preg_match('/<a class=\"next\" href=\"([^\"]*)\"/', $content, $next_pg_matches);
if (!$next_pg) {
$next_pg_matches[1] = "";
}
return array($content, $next_pg_matches[1]);
}
$content = "";
$next_pg = TRUE;
$current_pg = 1;
$base_url = "http://audiophonik.tumblr.com";
$prev_video_ids = explode("\n", file_get_contents("videoid.txt"));
$cached_video_ids = FALSE;
$video_ids = array();
$youtube_pattern = '/http:\/\/www.youtube.com\/embed\/(.*)\?/';
$url = $base_url;
while ($next_pg) {
echo "Now retrieving HTML from $url<br />";
$result = get_html($url);
// Parse result and add to array
preg_match_all($youtube_pattern, $result[0], $matches);
$test_ids = array_unique($matches[1]);
$video_ids = array_merge($video_ids, $test_ids);
$video_ids = array_unique($video_ids);
if ($current_pg == 1 and ($test_ids[0] == $prev_video_ids[0])) {
// No reason to parse all the other Tumblr pages; just use the previous list
$cached_video_ids = TRUE;
$video_ids = $prev_video_ids;
$next_pg = FALSE;
}
// If there is no "next page" link, merge the arrays.
// Also merge the arrays if the last video ID we'd stored is detected.
if (!$result[1] or $next_pg == FALSE) {
echo "Ending search and now compiling page.<br />";
$next_pg = FALSE;
}
$url = $base_url . $result[1];
$current_pg++;
}
$str = "video_ids = [";
foreach ($video_ids as $id => $video_id) {
$str .= "'$video_id',";
}
// remove last comma
$str = substr($str, 0, -1);
$str .= "];\n";
$str .= "\tytplayer.loadPlaylist(video_ids);\n";
$output = str_replace("// <REPLACE_WITH_VIDEO_IDS>", $str, $youtube_boilerplate);
echo $output;
if (!$cached_video_ids) {
file_put_contents("videoid.txt", implode("\n", $video_ids));
}