forked from Benjamin-Loison/YouTube-operational-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlists.php
89 lines (75 loc) · 2.64 KB
/
playlists.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
<?php
header('Content-Type: application/json; charset=UTF-8');
$playlistsTests = [
['part=snippet&id=PL8wZFyWE1ZaI2HE7PYHvpx0_yv4oJjwAZ', 'items/0/snippet/title', '4,000 times the same video'],
['part=statistics&id=PL8wZFyWE1ZaI2HE7PYHvpx0_yv4oJjwAZ', 'items/0/statistics', ['videoCount' => 4_000]],
];
include_once 'common.php';
$realOptions = [
'snippet',
'statistics',
];
// really necessary ?
foreach ($realOptions as $realOption) {
$options[$realOption] = false;
}
if (isset($_GET['part'], $_GET['id'])) {
$part = $_GET['part'];
$parts = explode(',', $part, count($realOptions));
foreach ($parts as $part) {
if (!in_array($part, $realOptions)) {
dieWithJsonMessage("Invalid part $part");
} else {
$options[$part] = true;
}
}
$ids = $_GET['id'];
$realIds = explode(',', $ids);
verifyMultipleIds($realIds);
foreach ($realIds as $realId) {
if (!isPlaylistId($realId)) {
dieWithJsonMessage('Invalid id');
}
}
echo getAPI($realIds);
} else if(!test()) {
dieWithJsonMessage('Required parameters not provided');
}
function getItem($id)
{
global $options;
$result = getJSONFromHTML("https://www.youtube.com/playlist?list=$id", forceLanguage: true);
$item = [
'kind' => 'youtube#playlist',
'etag' => 'NotImplemented'
];
if ($options['snippet']) {
$title = $result['metadata']['playlistMetadataRenderer']['title'];
$item['snippet'] = [
'title' => $title
];
}
if ($options['statistics']) {
$viewCount = $result['sidebar']['playlistSidebarRenderer']['items'][0]['playlistSidebarPrimaryInfoRenderer']['stats'][1]['simpleText'];
$viewCount = getIntFromViewCount($viewCount);
$videoCount = intval(str_replace(',', '', $result['header']['playlistHeaderRenderer']['numVideosText']['runs'][0]['text']));
$item['statistics'] = [
'viewCount' => $viewCount,
'videoCount' => $videoCount
];
}
return $item;
}
function getAPI($ids)
{
$items = [];
foreach ($ids as $id) {
array_push($items, getItem($id));
}
$answer = [
'kind' => 'youtube#playlistListResponse',
'etag' => 'NotImplemented',
'items' => $items
];
return json_encode($answer, JSON_PRETTY_PRINT);
}