DEV Community

Ichi
Ichi

Posted on • Updated on

Get ALL video information about specific YouTube Channel

I tried to get the information of the all video in a specific channel.

Overview

I wrote it with reference to StackOverflow.
Due to API restrictions, even if I make a request, I can only get 50 requests at a once, so I sent requests many times at regular intervals to get them.
You can get the following information.

  • Title/ID/Summary
  • Thumbnail
  • Post date and time

It may be better to stop trying it indiscriminately because there are restrictions on API calls.

The sample JSON file is uploaded to GitHub.

https://github.com/tomox0115/randomhikakin/blob/main/database/hikakin.json

Get API key

There is a lot of information, so please google around.
Alt Text

Sample code

For the time being, the sample is also uploaded to Gist.

<?php
/*
If you get an error related to the date and time, please specify the default term zone like ↓
date_default_timezone_set("America/Los_Angeles");
*/

//YouTube API v3
$API_KEY = "***************************************";

// Get the channel ID from the user name (skip OK if you know the ID)
function get_user_channel_id($user){
    global $API_KEY;
    $url = 'https://www.googleapis.com/youtube/v3/channels?key=' . $API_KEY . '&part=id&forUsername=';
    return search($user,$url)['items'][0]['id'];
}

// 
function search($searchTerm,$url){
    $url = $url . urlencode($searchTerm);

    $result = file_get_contents($url);

    if($result !== false){
        return json_decode($result, true);
    }

    return false;
}

function push_data($searchResults){
    global $data;
    foreach($searchResults['items'] as $item){
        $data[] = $item;
    }
    return $data;
}

function get_url_for_time_period($channelId, $time){
    global $API_KEY;
    // Date and time type specification
    $publishedAfter = date("Y-m-d\TH:i:sP",strval($time));
    // Specify the period of 60 days
    $publishedBefore_ = $time + (60 * 60 * 24 * 60);
    $publishedBefore = date("Y-m-d\TH:i:sP",$publishedBefore_);
    // Make request URL
    $url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&key=' . $API_KEY . '&maxResults=50&channelId=' . $channelId . '&publishedAfter=' . urlencode($publishedAfter) . '&publishedBefore=' . urlencode($publishedBefore);

    return array("url"=>$url,"utc"=>$publishedBefore_);
}


$start_date = "YYYY-MM-DD"; // Set date
$time = strtotime($start_date);
$username = "*****"; // ex. hikakintv

$channelId = get_user_channel_id($username);

while($time < time()){
    $url = get_url_for_time_period($channelId, $time);
    $searchResults = search("", $url['url']);
    $data = push_data($searchResults);
    $time += 60 * 60 * 24 * 60; // Add 60 days
}

// If you want to display the returned data
echo "<pre>";
var_dump($data);
echo "</pre>";

// If you want to save the array in JSON format
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents($username . ".json" , $json);

// If you want to output the number of videos
print count($data);
Enter fullscreen mode Exit fullscreen mode

JSON that can be acquired

The returned video information is stored in $data as an associative array, so you should output it as JSON.
Below is an example of JSON.
Only the first video information is posted. It will continue forever ...

[
    {
        "kind": "youtube#searchResult",
        "etag": "cRtdkU1jXJ6Abga3q5S490FrsP0",
        "id": {
            "kind": "youtube#video",
            "videoId": "0t8CH5BAgLY"
        },
        "snippet": {
            "publishedAt": "2011-07-20T15:19:55Z",
            "channelId": "UCZf__ehlCEBPop-_sldpBUQ",
            "title": "世界一おいしい飲み物『ピルクル』 - The World Best Juice『Pirukuru』 -",
            "description": "ピルクルに勝る飲み物なし。",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/0t8CH5BAgLY/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/0t8CH5BAgLY/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/0t8CH5BAgLY/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "HikakinTV",
            "liveBroadcastContent": "none",
            "publishTime": "2011-07-20T15:19:55Z"
        }
    },
Enter fullscreen mode Exit fullscreen mode

Please also check the blog and Twitter if you like :D
Twitter @tomox0115
My BLOG

Top comments (2)

Collapse
 
kalashin1 profile image
Kinanee Samson

please provide this in JavaScript for us

Collapse
 
ichii731 profile image
Ichi

sorry. I can't write JS because I've only done it in front-end development and I've never done Node.js.
I think PHP is enough for JSON acquisition only.