Skip to content
Related Articles

Related Articles

Improve Article
GUI to get views, likes, and title of a YouTube video using YouTube API in Python
  • Last Updated : 02 Feb, 2021

Prerequisite: YouTube API, Tkinter

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications.

In this article, we will learn how can we obtain data (like title, views, likes, dislikes, etc) from any YouTube video Using YouTube API in Python.

Below is the GUI to be created:



Step-by-step Approach:

Before proceeding one should properly go through YouTube API in order to install the required modules and enable required functionalities as mentioned in the article.

  • Import required modules.

Python3




# Import Module
from tkinter import *
from googleapiclient.discovery import build
  • We will use the build(), list(), execute() method, it will give Video Details.
  • Inside list method, pass snippet and statistics in part property and in videoId property pass VideoID of VideoURL.

Python3




# creating youtube resource object 
youtube = build('youtube','v3', developerKey="Enter API Key"
  
# retrieve youtube video results 
video_request=youtube.videos().list(
  part='snippet,statistics',
  id="Enter Video ID"
)
  
video_response = video_request.execute()
  • Get Title, views, likes from video_response.

Python3




title = video_response['items'][0]['snippet']['title']
likes = video_response['items'][0]['statistics']['likeCount']
views = video_response['items'][0]['statistics']['viewCount']
  • Create Tkinter Window; Add Buttons, Label, etc..

Python3




# Create Object
root = Tk()
  
# Set Geometry
root.geometry("500x300")
  
# Add Label, Entry Box and Button
Label(root, text="Title, Views, Likes of YouTube Video", fg="blue",
      font=("Helvetica 20 bold"), relief="solid", bg="white").pack(pady=10)
Label(root, text="Enter video URL or ID", font=("10")).pack()
  
video_url = Entry(root, width=40, font=("15"))
video_url.pack(pady=10)
  
Button(root, text="Get Details", font=("Helvetica 15 bold")).pack()
  
details = Label(root, text="")
details.pack(pady=10)
  
# Execute Tkinter
root.mainloop()

Below is the implementation based on the above approach:

Python3




# Import Module
from tkinter import *
from googleapiclient.discovery import build
  
def video_details():
    if "youtube" in video_url.get():
        video_id = video_url.get()[len("https://www.youtube.com/watch?v="):]
    else:
        video_id = video_url.get()
  
    # creating youtube resource object 
    youtube = build('youtube','v3',developerKey='Enter API Key')
  
    # retrieve youtube video results
    video_request=youtube.videos().list(
        part='snippet,statistics',
        id=video_id
    )
  
    video_response = video_request.execute()
  
    title = video_response['items'][0]['snippet']['title']
    likes = video_response['items'][0]['statistics']['likeCount']
    views = video_response['items'][0]['statistics']['viewCount']
  
    details.config(text=f"Title:- {title}\nLikes:- {likes}\nViews:- {views}")
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("500x300")
  
# Add Label, Entry Box and Button
  
Label(root,text="Title, Views, Likes of YouTube Video", fg="blue",
    font=("Helvetica 20 bold"),relief="solid",bg="white").pack(pady=10)
Label(root,text="Enter video URL or ID", font=("10")).pack()
  
video_url = Entry(root,width=40,font=("15"))
video_url.pack(pady=10)
  
Button(root,text="Get Details" ,font=("Helvetica 15 bold"),command=video_details).pack()
  
details = Label(root,text="")
details.pack(pady=10)
  
# Execute Tkinter
root.mainloop()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :