· 5 years ago · Jan 12, 2021, 01:22 PM
1<?php
2 /* Youni's Simple Youtube Statistics Script
3 * Shows quantity of subscribers, views and videos on any channel if channel does not hide info
4 *
5 * Usage: yoursite.com/pathtoscriptfolder/index.php?channel=channel_id
6 * where channel_id is part of youtube link like this: UCEU9D6KIShdLeTRyH3IdSvw
7 *
8 * For using script you need to get your own YoutubeAPI Data v3 with access from browser
9 * and fill the constant KEY
10 * Also place your default channel id (link) into constant CHANNEL
11 *
12 * This script puts cached .txt files in its folder so be sure
13 * to set up correct access rights, owner and group to folder where script is placed to.
14 *
15 * Published under license GPL v 3.0
16 * Jan 12, 2021
17*/
18
19/* its foss: https://www.youtube.com/channel/UCEU9D6KIShdLeTRyH3IdSvw
20From Youtube Documentation:
21https://developers.google.com/youtube/v3/getting-started
22YoutubeAPI Data v3 key: ---place it here to store it----
23* channel info: https://www.googleapis.com/youtube/v3/channels?part=snippet&id= ID &key= KEY
24//Get videos from channel by YouTube Data API
25$API_key = 'Insert_Your_API_Key';
26$channelID = 'Insert_Channel_ID';
27$maxResults = 10;
28$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
29$url = 'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$chan.'&maxResults=50&key='.$key;
30echo "url: $url <br>";
31$video_list = json_decode(file_get_contents($url));
32print_r($video_list);
33get stat of video
34https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Q5mHPo2yDG8&key={YOUR_API_KEY}
35its foss linux blog channel UCEU9D6KIShdLeTRyH3IdSvw
36*/
37
38ini_set('display_errors', 1);
39ini_set('display_startup_errors', 1);
40error_reporting(E_ALL);
41
42define('KEY', '...place YoutubeAPI Data V3 Key here to use script...');
43define('CHANNEL', 'UCEU9D6KIShdLeTRyH3IdSvw'); //default: its foss linux blog channel UCEU9D6KIShdLeTRyH3IdSvw
44$CHANNEL = CHANNEL;
45
46$cache_info = 12000;
47$cache_stat = 180;
48
49function channel_info($channel = CHANNEL, $data = 'title') {
50 global $cache_info;
51 $cachefile = 'cache_info_' . $channel . '.txt';
52 if ( file_exists($cachefile) && (time() - $cache_info < filemtime($cachefile)) ) {
53 $cacheresult = unserialize(file_get_contents($cachefile));
54 return $cacheresult[$data];
55 } else {
56 $json_string = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=snippet&id=' . $channel . '&key=' . KEY);
57 $json = json_decode($json_string, true);
58 if ( count($json,1) != 0 ) {
59 $temp['title'] = $json['items']['0']['snippet']['title'];
60 $temp['publishedAt'] = $json['items']['0']['snippet']['publishedAt'];
61 $temp['icon'] = $json['items']['0']['snippet']['thumbnails']['default']['url'];
62
63 $result[] = $temp;
64 $youtubedata = serialize($temp);
65
66 $fp = fopen($cachefile, 'w');
67 fwrite($fp, $youtubedata);
68 fclose($fp);
69 return $temp[$data];
70 } else {
71 $result = unserialize(file_get_contents($cachefile));
72 return $result[$type];
73 }
74 }
75}
76
77
78function channel_statistics($channel = CHANNEL, $type = 'views') {
79 global $cache_stat;
80 $cachefile = 'cache_stat_' . $channel . '.txt';
81 if ( file_exists($cachefile) && (time() - filemtime($cachefile) < $cache_stat ) ) {
82 $cacheresult = unserialize(file_get_contents($cachefile));
83 return number_format($cacheresult[$type]);
84 } else {
85 if ( file_exists($cachefile)) {
86 $t = time() - filemtime($cachefile);
87 }
88 $json_string = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id=' . $channel . '&key=' . KEY);
89 $json = json_decode($json_string, true);
90 if ( count($json,1) != 0 ) {
91 $temp['views'] = $json['items']['0']['statistics']['viewCount'];
92 $temp['subscribers'] = $json['items']['0']['statistics']['subscriberCount'];
93 $temp['count'] = $json['items']['0']['statistics']['videoCount'];
94
95 $result[] = $temp;
96 $youtubedata = serialize($temp);
97
98 if ( file_exists($cachefile)) unlink($cachefile);
99 $fp = fopen($cachefile, 'w');
100 fwrite($fp, $youtubedata);
101 fclose($fp);
102 return number_format($temp[$type]);
103 } else {
104 $result = unserialize(file_get_contents($cachefile));
105 return number_format($result[$type]);
106 }
107 }
108}
109
110if (isset($_GET['channel']) && (strlen($_GET['channel']) > 0) ) $CHANNEL = $_GET['channel'];
111?>
112<html><head><title>Channel statistics: <?php echo channel_info($CHANNEL, 'title'); ?></title>
113<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
114</head>
115<body>
116<h2 style="text-align:center;">Channel statistics: <?php echo channel_info($CHANNEL, 'title'); ?></h2>
117<p style="text-align:center;"><?php echo 'Subscribers: <b>' . channel_statistics($CHANNEL, 'subscribers') . '</b>'; ?></p>
118<p style="text-align:center;"><?php echo 'Views: <b>' . channel_statistics($CHANNEL, 'views') . '</b>'; ?></p>
119<p style="text-align:center;"><?php echo 'Videos total: <b>' . channel_statistics($CHANNEL, 'count') . '</b>'; ?></p>
120<p> </p>
121<p style="text-align:center;"><small>Age of a cache:
122<?php $sec = time() - filemtime('cache_stat_' . $CHANNEL . '.txt');
123if ($sec > 60) { $min = intval($sec/60); echo $min . ' minutes '; }
124if ($sec % 60 > 0) { echo $sec % 60 . ' seconds' ; }
125if ($sec < 1) { echo ' just created' ; }
126?> </small></p>
127
128<p style="text-align:center;">
129<ul>
130 <li><a href="https://studio.youtube.com">Go to your Youtube Studio</a></li>
131 <li><a href="https://studio.youtube.com/channel/<?php echo $CHANNEL; ?>">Go to channel's Youtube Studio</a></li>
132 <li><a href="https://studio.youtube.com/channel/<?php echo $CHANNEL; ?>/comments/inbox">Go to channel's Youtbue Comments</a></li>
133</ul>
134</p>
135
136</body>
137</html>
138
139