-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.php
412 lines (286 loc) · 11.3 KB
/
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* This sample sets and retrieves localized metadata for a video by:
*
* 1. Updating language of the default metadata and setting localized metadata
* for a video via "videos.update" method.
* 2. Getting the localized metadata for a video in a selected language using the
* "videos.list" method and setting the "hl" parameter.
* 3. Listing the localized metadata for a video using the "videos.list" method and
* including "localizations" in the "part" parameter.
*
* @author Ibrahim Ulukaya
*/
$videoId = $_GET['videoId'];
$action = $_GET['action'];
$dateValue = $_GET['date'];
$timeValue = $_GET['time'];
$pageToken = $_GET['pageToken'];
$htmlBody =
<<<END
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://jonthornton.github.io/jquery-timepicker/jquery.timepicker.js"></script>
<link rel="stylesheet" type="text/css" href="https://jonthornton.github.io/jquery-timepicker/jquery.timepicker.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker.standalone.css" />
<script src="http://jonthornton.github.io/Datepair.js/dist/datepair.js"></script>
<script src="http://jonthornton.github.io/Datepair.js/dist/jquery.datepair.js"></script>
<br>
<button type="button" onclick="clearParams()">Reset</button>
<br>
-------------------------------------------------
<br>
<br>
<form id="form2" name="form1" method="GET">
<input type="submit" name="action" value="Get list video" >
</form>
<input type="submit" class="button" name="load more" value="insert" />
<form method="GET" id="testformid">
<div>
</div>
<br>
<div>
List video id: (private video only)
<br>
<textarea form ="testformid" name="videoId" id="taid" cols="35" wrap="soft">$videoId</textarea>
<br><br>
Select public time:
<p id="datepairExample">
<input type="text" name="date" class="date start" />
<input type="text" name="time" class="time start" />
</p>
</div>
<input type="submit" value="Scheduler Pulbic">
</form>
-------------------------------------------------
<br>
<br>
<script>
// initialize input widgets first
$('#datepairExample .time').timepicker({
'disableTextInput':false,
'showDuration': true,
'timeFormat': 'G:i'
});
$('#datepairExample .date').datepicker({
'disableTextInput':false,
'format': 'yyyy-mm-dd',
'autoclose': true
});
$('#datepairExample .time').timepicker('setTime', new Date());
$('#datepairExample .date').datepicker('setDate', new Date());
// initialize datepair
$('#datepairExample').datepair();
function clearParams () {
window.history.pushState({}, "Hide", "youtube.php");
}
function updateVideoList (lists){
$("#taid").val( lists );
}
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'request.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert(response);
});
});
});
</script>
END;
// Call set_include_path() as needed to point to your client library.
set_include_path(get_include_path() . PATH_SEPARATOR . 'google-api-php-client/src');
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
/*
* You can acquire an OAuth 2.0 client ID and client secret from the
* Google Developers Console <https://console.developers.google.com/>
* For more information about using OAuth 2.0 to access Google APIs, please see:
* <https://developers.google.com/youtube/v3/guides/authentication>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$OAUTH2_CLIENT_ID = '1059495052682-pspshs7kbn3u09os8jqd0kvodh29mpcd.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'hOn-jPZN58DNwKEb0xyxo3vg';
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
// $client->setIncludeGrantedScopes(true);
$client->setAccessType("offline");
$client->setApprovalPrompt ("force");
/*
* This OAuth 2.0 access scope allows for full read/write access to the
* authenticated user's account.
*/
$client->setScopes('https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/youtubepartner');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
// This code executes if the user ays tenters an action in the form
// and submits the form. Otherwise, the page displhe form above.
if($client->isAccessTokenExpired()) {
echo "Token expired ";
}
if (isset($_GET['action']) && $_GET['action']) {
try {
getListVideoID($youtube, $htmlBody);
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
} else if (isset($_GET['videoId']) && $_GET['videoId']) {
//get list video Id
$fullDate = $dateValue."T".$timeValue.":00.50Z";
try {
$pieces = explode(",", $videoId);
foreach ($pieces as $key => $value) {
# code...
// echo $value;
setVideoTime($youtube, $value, $fullDate , $htmlBody);
}
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
}
$_SESSION['token'] = $client->getAccessToken();
} else {
// If the user hasn't authorized the app, initiate the OAuth flow
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
$htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
/**
* Returns localized metadata for a video in a selected language.
* If the localized text is not available in the requested language,
* this method will return text in the default language.
*
* @param Google_Service_YouTube $youtube YouTube service object.
* @param string $videoId The videoId parameter instructs the API to return the
* localized metadata for the video specified by the video id.
* @param $htmlBody - html body.
*/
function getListVideoID(Google_Service_YouTube $youtube, &$htmlBody) {
// Call the search.list method to retrieve results matching the specified
// query term.
$searchResponse = $youtube->search->listSearch('id', array(
'maxResults' => '5',
'forMine' => true,
'type' => 'video',
'pageToken' => $pageToken
));
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
$listVideosId = '';
$pageToken = $searchResponse['pageToken'];
foreach ($searchResponse['items'] as $searchResult) {
if ($listVideosId == '') {
$listVideosId = $searchResult['id']['videoId'];
} else{
$listVideosId .= ', ' .$searchResult['id']['videoId'];
}
// $htmlBody .= sprintf('<li> %s</li>', $searchResult['id']['videoId']);
}
if (count($searchResult) > 0) {
getVideoDetail($youtube, $listVideosId, $htmlBody);
}
}
function getData(){
echo "heello";
$htmlBody .= sprintf("get data");
}
/**
* Returns a list of metadata for a video.
*
*/
function getVideoDetail(Google_Service_YouTube $youtube, $videoId, &$htmlBody) {
// Call the YouTube Data API's videos.list method to retrieve videos.
$videos = $youtube->videos->listVideos("id, status, snippet", array(
'id' => $videoId
));
// If $videos is empty, the specified video was not found.
if (empty($videos)) {
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
} else {
// Since the request specified a video ID, the response only
// contains one video resource.
$htmlBody .= "<h3>Video Status: </h3><ul>";
$listPriveVideo = "";
foreach ($videos['items'] as $searchResult) {
$htmlBody .= sprintf('<li>%s -- %s -- %s </li>', $searchResult['id'], $searchResult['snippet']['title'], $searchResult['status']['privacyStatus']);
if ($searchResult['status']['privacyStatus'] == "private") {
if ($listPriveVideo == '') {
$listPriveVideo = $searchResult['id'];
} else{
$listPriveVideo .= ',' .$searchResult['id'];
}
}
}
$htmlBody .= '</ul>';
// $htmlBody = str_replace("video_id", $listPriveVideo , $htmlBody);
$htmlBody .= '<script type="text/javascript"> updateVideoList("'.$listPriveVideo.'");</script>';
}
}
// set public time
function setVideoTime(Google_Service_YouTube $youtube, $videoId, $publicDate, &$htmlBody) {
// Call the YouTube Data API's videos.list method to retrieve videos.
// Call the API's videos.list method to retrieve the video resource.
$listResponse = $youtube->videos->listVideos("status", array('id'=>$videoId));
# array( 'id' => $VIDEO_ID, 'status' => array('privacyStatus' => 'public')));
// If $listResponse is empty, the specified video was not found.
if (empty($listResponse)) {
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
} else {
// Since the request specified a video ID, the response only
// contains one video resource.
$video = $listResponse[0];
$videoStatus = $video['status'];
$videoStatus->privacyStatus = 'private'; #privacyStatus options are public, private, and unlisted
$videoStatus->publishAt = $publicDate;//'2016-11-23T07:29:00.50Z';
$video->setStatus($videoStatus);
$updateResponse = $youtube->videos->update('status', $video);
$htmlBody .= sprintf('<li> Update success for id: %s </li>', $updateResponse['id']);
$htmlBody .= '</ul>';
}
}
?>
<!doctype html>
<html>
<head>
<title>Set and retrieve localized metadata for a video</title>
</head>
<body>
<?=$htmlBody?>
</body>
</html>