Skip to content

Instantly share code, notes, and snippets.

@nhymxu
Created October 4, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhymxu/11bc54a575cfedc56d1692443c0813be to your computer and use it in GitHub Desktop.
Save nhymxu/11bc54a575cfedc56d1692443c0813be to your computer and use it in GitHub Desktop.
Dead simple youtube playlist randomizer
<?php
/**
* Dead simple youtube playlist randomizer
*
* Detail here: https://dungnt.net/blog/php-youtube-playlist-randomizer
*/
date_default_timezone_set('Asia/Ho_Chi_Minh');
const API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/playlistItems';
const PLAYLIST_ID = 'enter youtube playlist here';
const API_KEY = 'enter google api key here';
const DATA_FILE = 'youtube-playlist-randomizer.json';
const LOG_FILE = 'youtube-playlist-randomizer.log';
$video_ids = []; // format: video_id => video_title
const ERROR_MSG = "Some thing went wrong. Please try again!";
function write_log($msg)
{
$now = date("Y-m-d H:i:s");
file_put_contents(
LOG_FILE,
"[$now] $msg" . PHP_EOL,
FILE_APPEND
);
}
function send_request($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_errno > 0) {
write_log("cURL Error ($curl_errno): $curl_error");
exit(ERROR_MSG);
}
try {
$data = json_decode($response, true);
}
catch (Exception $e) {
write_log($e);
exit(ERROR_MSG);
}
if (isset($data['error']) && !empty($data['error']))
{
write_log("Response error: " . $data['error']['code'] . PHP_EOL . $response);
exit(ERROR_MSG);
}
return $data;
}
function get_playlist_videos($page_token = '')
{
global $video_ids;
$query_url = API_ENDPOINT . '?part=snippet&maxResults=50&playlistId=' . PLAYLIST_ID . '&key=' . API_KEY;
if ($page_token != '') {
$query_url .= "&pageToken={$page_token}";
}
$data = send_request($query_url);
foreach($data['items'] as $item) {
$video_id = $item['snippet']['resourceId']['videoId'];
$video_title = $item['snippet']['title'];
$video_ids[$video_id] = $video_title;
}
if (isset($data['nextPageToken']) && $data['nextPageToken'] != '') {
return get_playlist_videos($data['nextPageToken']);
}
}
function random_video()
{
global $video_ids;
$video_list = array_keys($video_ids);
$video_count = count($video_list);
$random_number = rand(0, $video_count - 1);
return $video_list[$random_number];
}
if (!file_exists(DATA_FILE)) {
get_playlist_videos();
file_put_contents(DATA_FILE, json_encode($video_ids));
} else {
$cache_content = file_get_contents(DATA_FILE);
$video_ids = json_decode($cache_content, true);
}
$redirect_video_id = random_video();
header("Location: https://www.youtube.com/watch?v={$redirect_video_id}&list=" . PLAYLIST_ID);
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment