Developing a YouTube Subscription Analysis Application with Python
Tags: python, tutorial, opensource, datascience
Published: 2023-08-08 06:06:43

YouTube has grown into a vast platform where users follow various channels, each representing different topics and interests. Managing these subscriptions and understanding what content you are mostly engaged with can be overwhelming. In this article, we'll walk through the creation of a Python application that analyzes a user's subscriptions and visualizes the distribution of topics.

## Step 1: Authenticate with the YouTube API
First, we need to authenticate with the YouTube Data API v3 to access information about a user's subscriptions.

Install the Google Client Library
```bash
pip install --upgrade google-auth-oauthlib google-auth-httplib2 google-api-python-client
```
Authenticate and Save Credentials
```python
import os
import pickle
import google_auth_oauthlib.flow
import googleapiclient.discovery

scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]

def get_authenticated_service():
    credentials_path = "credentials.pickle"
    if os.path.exists(credentials_path):
        with open(credentials_path, "rb") as f:
            credentials = pickle.load(f)
    else:
        flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
            "client_secret.json", scopes)
        credentials = flow.run_console()
        with open(credentials_path, "wb") as f:
            pickle.dump(credentials, f)

    youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
    return youtube
```
Make sure to download the client_secret.json file from your Google Cloud Console.

## Step 2: Retrieve Subscriptions and Analyze Topics
After authentication, we can retrieve subscriptions and count the topics using a defaultdict.

Getting Subscriptions and Topics
```python
from collections import defaultdict

def get_subscriptions(youtube):
    subscriptions = []
    next_page_token = None
    topic_count = defaultdict(int)

    while True:
        response = youtube.subscriptions().list(
            part="snippet",
            mine=True,
            maxResults=50,
            pageToken=next_page_token
        ).execute()

        subscriptions += response["items"]
        next_page_token = response.get("nextPageToken")

        if next_page_token is None:
            break

    for subscription in subscriptions:
        snippet = subscription["snippet"]
        channelId = snippet["resourceId"]["channelId"]
        response = youtube.channels().list(
            part="topicDetails",
            id=channelId
        ).execute()

        items = response.get("items", [])
        if items:
            topicDetails = items[0].get("topicDetails", {})
            topicCategories = topicDetails.get("topicCategories", [])
        else:
            topicCategories = []

        for topic in topicCategories:
            topic_count[topic] += 1

    return topic_count
```
## Step 3: Visualize the Data with Matplotlib
Finally, we visualize the counted topics using Matplotlib.

Plotting the Topics
```python
import matplotlib.pyplot as plt

def plot_topics(topic_count):
    topics, counts = zip(*topic_count.items())
    plt.bar(topics, counts)
    plt.xlabel('Topics')
    plt.ylabel('Counts')
    plt.title('Distribution of Topics in YouTube Subscriptions')
    plt.xticks(rotation='vertical')
    plt.show()
```
Running the Full Code
```python
if __name__ == "__main__":
    youtube = get_authenticated_service()
    topic_count = get_subscriptions(youtube)
    plot_topics(topic_count)
```
## Conclusion
We've created an application that authenticates with the YouTube API, analyzes subscriptions, and visualizes the distribution of topics. This is an excellent starting point for anyone interested in YouTube data analysis.

You can find the full code in the GitHub repository {% embed https://github.com/kwisser/youtube-subscription-analyzer %}.