Categories: 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…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago