Development

Viewing your YouTube subscribed last upload in PHP

I knocked together some PHP code using the YouTube API  to simply fetch my YouTube subscribed to channels and list the hours/days/weeks/months since last upload.

You can find the code on Gist or in the spoiler at the bottom of this post.

I came up with this as a little challenge to simply list the channels i was subscribed to and the time value since they last uploaded a video, here is a small portion of the output:

As per the PHP code it is formatted nicely even with a link to the channel. All up this mini project took an hour mostly thanks to having YouTube API code lying around from previous workings.

If you want to use this code yourself all you need is your YouTube channel id and a YouTube api key. Add these into the code at lines 4 and 5.

Here is the php code

See the PHP code
diff($ago);
    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;
    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute'
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }
    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

function yt_recent($ch_id)
{
    $data = call_api("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=" . $ch_id . "&maxResults=1&order=date&type=video&key=" . $api_key . "", 1);
    $videoid = $data['items'][0]['id']['videoId'];//returns video id
    $data2 = call_api("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" . $videoid . "&key=" . $api_key . "", 1);
    $date = $data2['items'][0]['snippet']['publishedAt'];
    $fixed = date('Y-m-d', strtotime($date));
    return time_elapsed_string($fixed);
}

$data = call_api("https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&maxResults=50&channelId=" . $your_ch_id . "&key=" . $api_key . "", 1);

foreach ($data['items'] as $record) {
    $ch_id = $record['snippet']['resourceId']['channelId'];
    $name = $record['snippet']['title'];
    $link = "https://youtube.com/channel/" . $ch_id . "";
    echo "$name : " . yt_recent($ch_id) . "

"; }

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago