Get Data From Youtube Videos with Python (tkinter gui)

This code lets you get the data from a video on youtune in terms of views and subscription (and author) of the video. It is not mine, I just added the code to save the json file with the date and a progressive number, in case you save more files in a day and a little graphic interface. It could be useful if you want to take trace of the views on your videos and do some statistics, even if there are a lot of statistics that youtube lets you know, but in this case you can personalize them and have access to them without having to go in youtube.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json
import ast
import json
import os
from urllib.request import Request, urlopen
from datetime import date
import tkinter as tk
# For ignoring SSL certificate errors

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE


def get_data():
    global url
    req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    webpage = urlopen(req).read()

    # Creating a BeautifulSoup object of the html page for easy extraction of data.

    soup = BeautifulSoup(webpage, 'html.parser')
    html = soup.prettify('utf-8')
    video_details = {}
    other_details = {}
    for span in soup.findAll('span',attrs={'class': 'watch-title'}):
        video_details['TITLE'] = span.text.strip()
    for script in soup.findAll('script',attrs={'type': 'application/ld+json'}):
            channelDesctiption = json.loads(script.text.strip())
            video_details['CHANNEL_NAME'] = channelDesctiption['itemListElement'][0]['item']['name']
    for div in soup.findAll('div',attrs={'class': 'watch-view-count'}):
        video_details['NUMBER_OF_VIEWS'] = div.text.strip()
    for button in soup.findAll('button',attrs={'title': 'I like this'}):
        video_details['LIKES'] = button.text.strip()
    for button in soup.findAll('button',attrs={'title': 'I dislike this'}):
        video_details['DISLIKES'] = button.text.strip()
    for span in soup.findAll('span',attrs={'class': 'yt-subscription-button-subscriber-count-branded-horizontal yt-subscriber-count'}):
        video_details['NUMBER_OF_SUBSCRIPTIONS'] = span.text.strip()
    hashtags = []
    for span in soup.findAll('span',attrs={'class': 'standalone-collection-badge-renderer-text'}):
        for a in span.findAll('a',attrs={'class': 'yt-uix-sessionlink'}):
            hashtags.append(a.text.strip())
    video_details['HASH_TAGS'] = hashtags
    with open('output_file.html', 'wb') as file:
        file.write(html)
    today = str(date.today()).replace("-","_")
    counter = 0
    for file in os.listdir():
        if today in file:
            counter += 1
    filename = today + "_" + url.split("/")[-1] + str(counter) + ".json"
    with open(filename, 'w', encoding='utf8') as outfile:
        json.dump(video_details, outfile, ensure_ascii=False,indent=4)
    print ('----------Extraction of data is complete. Check json file.----------')
    with open(filename, 'r', encoding='utf8') as outfile:
        for line in outfile:
            print(line)
    print(f"data saved in {filename}")
    # os.startfile(filename)

def check(event):
    global url
    label["text"] = "Wait... the data will be saved and will appear in the console"
    url = entry.get()
    get_data()
    root.destroy()

# Input from user
root = tk.Tk()
root.title("Youtube Views Tracker")
# v = tk.StringVar()
label = tk.Label(root, text="Write the address of the video and press return")
label['font'] = "Arial 20"
label.pack()
label2 = tk.Label(root, text="Data will be saved in a file and will appear in the console")
label2['font'] = "Arial 20"
label2.pack()
entry = tk.Entry(root)
entry.pack(fill=tk.BOTH, expand=1)
entry.bind("<Return>", check)
entry.focus()
root.mainloop()

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.