This repository was archived by the owner on Jun 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.php
More file actions
218 lines (177 loc) · 7.62 KB
/
Copy pathfunction.php
File metadata and controls
218 lines (177 loc) · 7.62 KB
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
<?php
// get your API key on Google Cloud Console - YouTube Data API v3
$apikey = "YOUR_API_KEY";
// getting json content
function getJSONContent($url)
{
$data = @file_get_contents($url, false);
if ($data === false) {
return null;
}
return json_decode($data, true);
}
// getting channel id
function getChannelId($channelHandle, $apikey)
{
$channelHandle = str_replace(" ", "%20", $channelHandle);
$url = "https://www.googleapis.com/youtube/v3/channels?forHandle=$channelHandle&key=$apikey";
$json = getJSONContent($url);
$channelId = $json['items'][0]['id'] ?? null;
return $channelId;
}
// formatting dates, for example: 2016-05-14T15:43:27Z to 2016/05/14 15:43:27.
function formatDate($date)
{
$date = new DateTime($date);
$date = $date->format('Y/m/d H:i:s'); // change it according to your region
return $date;
}
// receives text, the channel country, and turns it into emoji, maybe not the best idea...
function textToFlagEmoji($countryCode): string
{
$codePoints = array_map(function ($char) {
return 127397 + ord($char);
}, str_split(strtoupper($countryCode)));
return mb_convert_encoding('&#' . implode(';&#', $codePoints) . ';', 'UTF-8', 'HTML-ENTITIES');
}
// channel snippet aka getting channel's avatar, about description and username.
function channelSnippet($channelId, $apikey)
{
$url = "https://www.googleapis.com/youtube/v3/channels?part=snippet&id=$channelId&key=$apikey";
$json = getJSONContent($url);
$username = $json['items'][0]['snippet']['title'];
$description = isset($json['items'][0]['snippet']['description'])
? nl2br($json['items'][0]['snippet']['description'])
: null;
$creationDate = formatDate($json['items'][0]['snippet']['publishedAt']);
$avatarUrl = $json['items'][0]['snippet']['thumbnails']['high']['url'] ?? "./img/noavatar.png";
$avatarUrl = getChannelPictures($channelId, $avatarUrl, $pictureName = "avatar.png"); // Avatar Download
return [
'username' => $username,
'description' => $description,
'avatarUrl' => $avatarUrl,
'creationDate' => $creationDate
];
}
// channel statistics aka getting channel's total number views, subscribers and videos.
function channelStatistics($channelId, $apikey)
{
$url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=$channelId&key=$apikey";
$json = getJSONContent($url);
$totalViews = $json['items'][0]['statistics']['viewCount'];
$hiddenSubCount = $json['items'][0]['statistics']['hiddenSubscriberCount'];
// this is already impossible to do, hiding youtube subscribers, but if one day youtube come back with this idea...
if ($hiddenSubCount == false)
$totalSubscribers = $json['items'][0]['statistics']['subscriberCount'];
$totalVideos = $json['items'][0]['statistics']['videoCount'];
return [
'totalViews' => $totalViews,
'subscribers' => $totalSubscribers,
'totalVideos' => $totalVideos
];
}
// channel branding settings aka getting channel's trailer for people who haven't subscribed yet, country and banner.
function channelbrandingSettings($channelId, $apikey)
{
$url = "https://www.googleapis.com/youtube/v3/channels?part=brandingSettings&id=$channelId&key=$apikey";
$json = getJSONContent($url);
$brandingSettings = $json['items'][0]['brandingSettings']['channel'] ?? null;
$nonSubscriberTrailerID = $brandingSettings['unsubscribedTrailer'] ?? null;
$countryCode = $brandingSettings['country'] ?? null;
$bannerUrl = isset($json['items'][0]['brandingSettings']['image']['bannerExternalUrl'])
? $json['items'][0]['brandingSettings']['image']['bannerExternalUrl'] . "=w2120-fcrop64=1,00000000ffffffff-k-c0xffffffff-no-nd-rj"
: "./img/nobanner.png";
$nonSubscriberTrailer = $nonSubscriberTrailerID ? "https://www.youtube.com/embed/$nonSubscriberTrailerID" : null;
$countryFlag = $countryCode ? textToFlagEmoji($countryCode) : null;
$bannerUrl = getChannelPictures($channelId, $bannerUrl, $pictureName = "banner.png"); // Banner Download
return [
'nonSubscriberTrailer' => $nonSubscriberTrailer,
'countryFlag' => $countryFlag,
'bannerUrl' => $bannerUrl
];
}
// get recent videos, more specifically the last 10 videos, it can be changed up to 50 which I think is the maximum
function getRecentVideos($channelId, $apikey)
{
$url = "https://www.googleapis.com/youtube/v3/search?channelId=$channelId&order=date&part=snippet&type=video&maxResults=10&key=$apikey";
$json = getJSONContent($url);
$videoList = [];
foreach ($json['items'] ?? [] as $item) {
$videoId = $item['id']['videoId'] ?? null;
$title = $item['snippet']['title'] ?? 'Untitled';
$publishDate = $item['snippet']['publishedAt'] ?? 'Unknown';
$publishDate = formatDate($publishDate);
$thumbnail = $item['snippet']['thumbnails']['high']['url'] ?? null;
$videoList[] = [
'title' => $title,
'videoId' => $videoId,
'publishDate' => $publishDate,
'thumbnail' => $thumbnail
];
}
return $videoList;
}
// get video snippet
function videoSnippet($videoId, $apikey)
{
$url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$videoId&key=$apikey";
$json = getJSONContent($url);
$creationDate = formatDate($json['items'][0]['snippet']['publishedAt']);
$channelId = $json['items'][0]['snippet']['channelId'];
$title = $json['items'][0]['snippet']['title'];
$description = $json['items'][0]['snippet']['description'];
$tags = isset($json['items'][0]['snippet']['tags']) ? $json['items'][0]['snippet']['tags'] : [];
$thumbnail = $item['snippet']['thumbnails']['high']['url'] ?? './img/nothumbnail.png';
return [
'creationDate' => $creationDate,
'channelId' => $channelId,
'title' => $title,
'description' => $description,
'tags' => $tags,
'thumbnail' => $thumbnail
];
}
// tries to download the channel avatar and banner to avoid error 403
function getChannelPictures($channelId, $url, $pictureName)
{
$defaultPictures = [
"banner.png" => "./img/nobanner.png",
"avatar.png" => "./img/noavatar.png"
];
if (isset($defaultPictures[$pictureName]) && $url === $defaultPictures[$pictureName])
$url = null;
$channelPath = "channel/$channelId";
if (!is_dir($channelPath))
mkdir($channelPath, 0777, true);
$fullPath = "$channelPath/$pictureName";
if ($url) {
$image = file_get_contents($url);
file_put_contents($fullPath, $image);
return "./channel/$channelId/$pictureName";
}
return $defaultPictures[$pictureName];
}
// check if channel id exists when is changed in url
function checkId($channelId, $apikey)
{
$url = "https://www.googleapis.com/youtube/v3/channels?id=$channelId&key=$apikey";
$json = getJSONContent($url);
$channelId = $json['items'][0]['id'] ?? null;
if ($channelId == null) {
header("Location: index.php");
exit();
}
return $channelId;
}
// check if video id exists when is changed in url
function checkVideoId($videoId, $apikey)
{
$url = "https://www.googleapis.com/youtube/v3/videos?id=$videoId&key=$apikey";
$json = getJSONContent($url);
$videoId = $json['items'][0]['id'] ?? null;
if ($videoId == null) {
header("Location: index.php");
exit();
}
return $videoId;
}