Saturday, November 16, 2024
Google search engine
HomeLanguagesYoutube Data API for handling videos | Set-1

Youtube Data API for handling videos | Set-1

Before proceeding further let’s have a look at what we have in store for the videos section. Youtube Data API allows to perform following operations on the video:

  • list
  • insert
  • update
  • rate
  • getRating
  • reportAbuse
  • delete

Let’s discuss how to use Youtube Data API for handling videos.

Please follow the steps below to enable the API and start using it.

  1. Create New Project, Enable API and Create Credentials: In this step we will create a project and will enable the API.
    • Go to Google Developers Console and Click on Sign In in the upper rightmost corner of the page. Sign In using the credentials of the valid Google Account. If you don’t have a google account, setup a account first and then use the details to Sign In on the Google Developers Homepage.
    • Now navigate to the Developer Dashboard and create a new Project.
    • Click on Enable API option.
    • In the search field, search for Youtube Data API and select the Youtube Data API option that comes in the drop down list.

    • You will be redirected to a screen that says information about the Youtube Data API, along with two options : ENABLE and TRY API
    • Click on ENABLE option to get started with the API.
    • In the sidebar under APIs & Services, select Credentials.
    • In the Credentials tab, select the Create credentials drop-down list, and choose API key.
      There are two types of Credentials: API Key and OAuth. OAuth provides you with Client Id and a Secret Key in the form of a .json file. OAuth is generally used where authorization is required like in the case of retrieving liked videos of a user. So for the rest cases where authorization is not required like searching for the videos using a keyword or for searching for the related videos etc, we will be using API Key.
  2. Installation: Google API client for python can be installed using simple pip command:
    pip install --upgrade google-api-python-client

Code for list methods:

  1. List video by Video Id: Below example shows how to retrieve details about a specific video identified by the video Id mentioned in the parameter list.




    # import libraries
    from googleapiclient.discovery import build
    import pprint
      
    # arguments to be passed to build function
    DEVELOPER_KEY = "Your_developer_key"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"
      
    # creating youtube resource object
    # for interacting with API
    youtube = build(YOUTUBE_API_SERVICE_NAME, 
                         YOUTUBE_API_VERSION,
                developerKey = DEVELOPER_KEY)
      
      
    def video_details(video_id):
      
        # Call the videos.list method
        # to retrieve video info
        list_videos_byid = youtube.videos().list(id = video_id,
              part = "id, snippet, contentDetails, statistics",
                                                   ).execute()
      
     # extracting the results from search response
        results = list_videos_byid.get("items", [])
      
        # empty list to store video details
        videos = []
          
        for result in results:
            videos.append("(% s) (% s) (% s) (% s) (% s) (% s)" % (result["snippet"]["title"],
                                                                  result["snippet"]["tags"],
                                                                  result['snippet']['description'],
                                                                  result["snippet"]["publishedAt"],
                                                                  result['contentDetails'], 
                                                                  result["statistics"]))
              
        print("Videos:\n", "\n".join(videos), "\n")
          
    if __name__ == "__main__":
      
        video_id = "vTaxdJ6VYWE"
        video_details(video_id)

    
    

    Output:

  2. List videos by Multiple Video Ids: Below example shows how to retrieve information about multiple videos, identified by the multiple video Ids mentioned in the parameter list.




    # import libraries
    from googleapiclient.discovery import build
    import pprint
      
    # arguments to be passed to build function
    DEVELOPER_KEY = "developer_key"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"
      
    # creating youtube resource 
    # object for interacting with API
    youtube = build(YOUTUBE_API_SERVICE_NAME,
                         YOUTUBE_API_VERSION,
                developerKey = DEVELOPER_KEY)
      
    def multiple_video_details():
      
        # Call the videos.list method
        # to retrieve video info
        list_videos_byid = youtube.videos().list(
                 id = 'Ks-_Mh1QhMc, c0KYU2j0TM4',
          part = "id, snippet, contentDetails, statistics",
                                      ).execute()
      
     # extracting the results from search response
        results = list_videos_byid.get("items", [])
        # empty list to store video details
        videos = []
        n = 1
        for result in results:
            videos.append(" (% s) (% s) (% s) (% s) (% s) (% s) " 
                                  % (n, result["snippet"]["title"],
                                        result["snippet"]["tags"],
                                        result['snippet']['description'],
                                        result["snippet"]["publishedAt"],
                                        result['contentDetails'],
                                        result["statistics"]))
            n = n + 1
              
        print ("Videos:\n", "\n".join(videos), "\n")
          
    if __name__ == "__main__":
        multiple_video_details()

    
    

    Output:

  3. Note: We have used only limited parameters in the above example. There are many other parameters that can be set and if not set then what default value they take can be found from Youtube Videos List Documentation. Please refer to the documentation to have a full understanding of the available parameters.

    Reference: https://developers.google.com/youtube/v3/docs/videos/list

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments