5.0 KiB
YouTube Music Data Analysis
Setup
from ytmusicapi import YTMusic
import pandas as pd
import matplotlib.pyplot as plt
Initialize YTMusic with OAuth credentials
ytmusic = YTMusic('oauth.json')
Fetch Data
Liked Songs
liked_songs = ytmusic.get_liked_songs(limit=100)
liked_songs_df = pd.DataFrame(liked_songs['tracks'])
liked_songs_df['artists'] = liked_songs_df['artists'].apply(lambda x: x[0]['name'] if x else None)
liked_songs_df.head()
Playlists
playlists = ytmusic.get_library_playlists(limit=25)
playlists_df = pd.DataFrame(playlists)
playlists_df.head()
History
history = ytmusic.get_history()
history_df = pd.DataFrame(history)
history_df.head()
Data Visualization
Liked Songs by Artist
liked_songs_df['artists'].value_counts().plot(kind='bar', figsize=(10, 5))
plt.title('Liked Songs by Artist')
plt.xlabel('Artist')
plt.ylabel('Number of Liked Songs')
plt.show()
History by Title
history_df['title'].value_counts().plot(kind='bar', figsize=(10, 5))
plt.title('History by Title')
plt.xlabel('Title')
plt.ylabel('Number of Plays')
plt.show()
Save Data to CSV
liked_songs_df.to_csv('liked_songs.csv', index=False)
playlists_df.to_csv('playlists.csv', index=False)
history_df.to_csv('history.csv', index=False)
### Full Script Breakdown
1. **Setup:**
- Import necessary libraries (`ytmusicapi`, `pandas`, `matplotlib`).
- Initialize the YTMusic API with OAuth credentials.
2. **Fetch Data:**
- Get the user's liked songs and convert them to a DataFrame.
- Get the user's playlists and convert them to a DataFrame.
- Get the user's history and convert it to a DataFrame.
3. **Data Visualization:**
- Visualize the liked songs by artist using a bar chart.
- Visualize the history by title using a bar chart.
4. **Save Data to CSV:**
- Save the processed DataFrames to CSV files for further analysis or backup.
### How to Use This Notebook
1. **Ensure you have the `oauth.json` file in your project directory, which contains your OAuth credentials for the YTMusic API.**
2. **Start Jupyter Notebook:**
```bash
jupyter notebook
- Create a new notebook or open an existing one and copy the above cells into the notebook.
- Run the cells step by step to fetch, analyze, visualize, and save your YouTube Music data.
This setup will provide you with a comprehensive and interactive data analysis report of your YouTube Music telemetry.
Step 1: Set Up Your Python Virtual Environment
First, ensure you have Python installed on your system. I recommend using Python 3.7 or newer. Here’s how you can set up a virtual environment:
-
Create a New Directory for Your Project (Optional):
mkdir yt-music-project cd yt-music-project -
Create a Virtual Environment:
python -m venv venv -
Activate the Virtual Environment:
- On Windows:
.\venv\Scripts\activate - On macOS and Linux:
source venv/bin/activate
- On Windows:
Step 2: Install Required Packages
-
Ensure your
requirements.txtincludesytmusicapi: You can create arequirements.txtfile containing at least:ytmusicapiIf you already have a
requirements.txt, make sureytmusicapiis listed. -
Install the Required Packages:
pip install -r requirements.txt
Step 3: Set Up OAuth Authentication
-
Run OAuth Setup: While in your activated virtual environment and your project directory:
ytmusicapi oauthFollow the on-screen instructions:
- Visit the URL provided in the command output.
- Log in with your Google account.
- Authorize the application if prompted.
- Copy the provided code back into the terminal.
This will generate an
oauth.jsonfile in your project directory containing the necessary credentials.
Step 4: Initialize YTMusic with OAuth Credentials
- Create a Python Script:
You can create a Python script like
main.pyto start coding with the API:from ytmusicapi import YTMusic ytmusic = YTMusic('oauth.json')
Step 5: Test by Creating a Playlist
-
Write Code to Create a Playlist and Search for Music: Add to your
main.py:# Create a new playlist playlist_id = ytmusic.create_playlist("My Awesome Playlist", "A description of my playlist.") # Search for a song search_results = ytmusic.search("Oasis Wonderwall") # Add the first search result to the new playlist if search_results: ytmusic.add_playlist_items(playlist_id, [search_results[0]['videoId']]) -
Run Your Script:
python main.py
This setup gives you a complete environment to work with the YTMusic API securely and manage your YouTube music data programmatically. You can extend this setup by adding more features, such as handling errors, enhancing functionality, or integrating with other data sources and tools for analysis or backup.