Files
the_information_nexus/projects/ytmusic.md
2024-05-26 23:55:27 +00:00

5.0 KiB
Raw Blame History

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
  1. Create a new notebook or open an existing one and copy the above cells into the notebook.
  2. 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. Heres how you can set up a virtual environment:

  1. Create a New Directory for Your Project (Optional):

    mkdir yt-music-project
    cd yt-music-project
    
  2. Create a Virtual Environment:

    python -m venv venv
    
  3. Activate the Virtual Environment:

    • On Windows:
      .\venv\Scripts\activate
      
    • On macOS and Linux:
      source venv/bin/activate
      

Step 2: Install Required Packages

  1. Ensure your requirements.txt includes ytmusicapi: You can create a requirements.txt file containing at least:

    ytmusicapi
    

    If you already have a requirements.txt, make sure ytmusicapi is listed.

  2. Install the Required Packages:

    pip install -r requirements.txt
    

Step 3: Set Up OAuth Authentication

  1. Run OAuth Setup: While in your activated virtual environment and your project directory:

    ytmusicapi oauth
    

    Follow 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.json file in your project directory containing the necessary credentials.

Step 4: Initialize YTMusic with OAuth Credentials

  1. Create a Python Script: You can create a Python script like main.py to start coding with the API:
    from ytmusicapi import YTMusic
    ytmusic = YTMusic('oauth.json')
    

Step 5: Test by Creating a Playlist

  1. 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']])
    
  2. 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.