Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Youni's Simple Youtube Statistics Script
- * Shows quantity of subscribers, views and videos on any channel if channel does not hide info
- *
- * Usage: yoursite.com/pathtoscriptfolder/index.php?channel=channel_id
- * where channel_id is part of youtube link like this: UCEU9D6KIShdLeTRyH3IdSvw
- *
- * For using script you need to get your own YoutubeAPI Data v3 with access from browser
- * and fill the constant KEY
- * Also place your default channel id (link) into constant CHANNEL
- *
- * This script puts cached .txt files in its folder so be sure
- * to set up correct access rights, owner and group to folder where script is placed to.
- *
- * Published under license GPL v 3.0
- * Jan 12, 2021
- */
- /* its foss: https://www.youtube.com/channel/UCEU9D6KIShdLeTRyH3IdSvw
- From Youtube Documentation:
- https://developers.google.com/youtube/v3/getting-started
- YoutubeAPI Data v3 key: ---place it here to store it----
- * channel info: https://www.googleapis.com/youtube/v3/channels?part=snippet&id= ID &key= KEY
- //Get videos from channel by YouTube Data API
- $API_key = 'Insert_Your_API_Key';
- $channelID = 'Insert_Channel_ID';
- $maxResults = 10;
- $videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
- $url = 'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$chan.'&maxResults=50&key='.$key;
- echo "url: $url <br>";
- $video_list = json_decode(file_get_contents($url));
- print_r($video_list);
- get stat of video
- https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Q5mHPo2yDG8&key={YOUR_API_KEY}
- its foss linux blog channel UCEU9D6KIShdLeTRyH3IdSvw
- */
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- define('KEY', '...place YoutubeAPI Data V3 Key here to use script...');
- define('CHANNEL', 'UCEU9D6KIShdLeTRyH3IdSvw'); //default: its foss linux blog channel UCEU9D6KIShdLeTRyH3IdSvw
- $CHANNEL = CHANNEL;
- $cache_info = 12000;
- $cache_stat = 180;
- function channel_info($channel = CHANNEL, $data = 'title') {
- global $cache_info;
- $cachefile = 'cache_info_' . $channel . '.txt';
- if ( file_exists($cachefile) && (time() - $cache_info < filemtime($cachefile)) ) {
- $cacheresult = unserialize(file_get_contents($cachefile));
- return $cacheresult[$data];
- } else {
- $json_string = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=snippet&id=' . $channel . '&key=' . KEY);
- $json = json_decode($json_string, true);
- if ( count($json,1) != 0 ) {
- $temp['title'] = $json['items']['0']['snippet']['title'];
- $temp['publishedAt'] = $json['items']['0']['snippet']['publishedAt'];
- $temp['icon'] = $json['items']['0']['snippet']['thumbnails']['default']['url'];
- $result[] = $temp;
- $youtubedata = serialize($temp);
- $fp = fopen($cachefile, 'w');
- fwrite($fp, $youtubedata);
- fclose($fp);
- return $temp[$data];
- } else {
- $result = unserialize(file_get_contents($cachefile));
- return $result[$type];
- }
- }
- }
- function channel_statistics($channel = CHANNEL, $type = 'views') {
- global $cache_stat;
- $cachefile = 'cache_stat_' . $channel . '.txt';
- if ( file_exists($cachefile) && (time() - filemtime($cachefile) < $cache_stat ) ) {
- $cacheresult = unserialize(file_get_contents($cachefile));
- return number_format($cacheresult[$type]);
- } else {
- if ( file_exists($cachefile)) {
- $t = time() - filemtime($cachefile);
- }
- $json_string = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id=' . $channel . '&key=' . KEY);
- $json = json_decode($json_string, true);
- if ( count($json,1) != 0 ) {
- $temp['views'] = $json['items']['0']['statistics']['viewCount'];
- $temp['subscribers'] = $json['items']['0']['statistics']['subscriberCount'];
- $temp['count'] = $json['items']['0']['statistics']['videoCount'];
- $result[] = $temp;
- $youtubedata = serialize($temp);
- if ( file_exists($cachefile)) unlink($cachefile);
- $fp = fopen($cachefile, 'w');
- fwrite($fp, $youtubedata);
- fclose($fp);
- return number_format($temp[$type]);
- } else {
- $result = unserialize(file_get_contents($cachefile));
- return number_format($result[$type]);
- }
- }
- }
- if (isset($_GET['channel']) && (strlen($_GET['channel']) > 0) ) $CHANNEL = $_GET['channel'];
- ?>
- <html><head><title>Channel statistics: <?php echo channel_info($CHANNEL, 'title'); ?></title>
- <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <h2 style="text-align:center;">Channel statistics: <?php echo channel_info($CHANNEL, 'title'); ?></h2>
- <p style="text-align:center;"><?php echo 'Subscribers: <b>' . channel_statistics($CHANNEL, 'subscribers') . '</b>'; ?></p>
- <p style="text-align:center;"><?php echo 'Views: <b>' . channel_statistics($CHANNEL, 'views') . '</b>'; ?></p>
- <p style="text-align:center;"><?php echo 'Videos total: <b>' . channel_statistics($CHANNEL, 'count') . '</b>'; ?></p>
- <p> </p>
- <p style="text-align:center;"><small>Age of a cache:
- <?php $sec = time() - filemtime('cache_stat_' . $CHANNEL . '.txt');
- if ($sec > 60) { $min = intval($sec/60); echo $min . ' minutes '; }
- if ($sec % 60 > 0) { echo $sec % 60 . ' seconds' ; }
- if ($sec < 1) { echo ' just created' ; }
- ?> </small></p>
- <p style="text-align:center;">
- <ul>
- <li><a href="https://studio.youtube.com">Go to your Youtube Studio</a></li>
- <li><a href="https://studio.youtube.com/channel/<?php echo $CHANNEL; ?>">Go to channel's Youtube Studio</a></li>
- <li><a href="https://studio.youtube.com/channel/<?php echo $CHANNEL; ?>/comments/inbox">Go to channel's Youtbue Comments</a></li>
- </ul>
- </p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment