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
pip install --upgrade google-auth-oauthlib google-auth-httplib2 google-api-python-client
Authenticate and Save Credentials
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
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
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
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
kwisser / youtube-subscription-analyzer
YouTube Subscriptions Analyzer is a Python script that authenticates with the YouTube API, fetches a user's subscriptions, and visualizes the distribution of topics in a pie chart.
YouTube Subscriptions Analyzer
YouTube Subscriptions Analyzer is a Python script that authenticates with the YouTube API, fetches a user's subscriptions, and visualizes the distribution of topics in a pie chart.
Features
- Retrieves a user's YouTube subscriptions
- Aggregates topics across all subscriptions
- Visualizes the distribution of topics in a pie chart
Requirements
- Python 3.6 or higher
- Google API credentials
- Libraries: google-auth-oauthlib, googleapiclient, matplotlib
Setup
-
Clone the Repository:
git clone https://github.com/kwisser/YoutubeSubscriptions.git cd youtube-subscriptions-analyzer
-
Install Dependencies:
pip install google-auth-oauthlib google-auth-httplib2 google-api-python-client matplotlib
-
Set up Google API credentials:
- Visit the Google Developer Console.
- Create a project and set up the YouTube API v3 credentials.
- Download the
client_secret.json
file and place it in the project's root directory.
Usage
Run the script with:
python main.py
Follow the on-screen instructions to authenticate with your Google account. The script will fetch your YouTube subscriptions and generate a pie chart.
Contributing
If you'd like toβ¦
.
Top comments (0)