Update projects/ytmusic.md
This commit is contained in:
@@ -1,4 +1,107 @@
|
|||||||
Sure, let's start from the beginning, setting up a Python project using a virtual environment to work with the YTMusic API via OAuth authentication. We'll go through everything step-by-step, from creating the virtual environment to running the OAuth setup, and then using the API to interact with YouTube Music.
|
# YouTube Music Data Analysis
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
```python
|
||||||
|
from ytmusicapi import YTMusic
|
||||||
|
import pandas as pd
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
```
|
||||||
|
|
||||||
|
# Initialize YTMusic with OAuth credentials
|
||||||
|
```python
|
||||||
|
ytmusic = YTMusic('oauth.json')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Fetch Data
|
||||||
|
|
||||||
|
### Liked Songs
|
||||||
|
|
||||||
|
```python
|
||||||
|
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
|
||||||
|
|
||||||
|
```python
|
||||||
|
playlists = ytmusic.get_library_playlists(limit=25)
|
||||||
|
playlists_df = pd.DataFrame(playlists)
|
||||||
|
playlists_df.head()
|
||||||
|
```
|
||||||
|
|
||||||
|
### History
|
||||||
|
|
||||||
|
```python
|
||||||
|
history = ytmusic.get_history()
|
||||||
|
history_df = pd.DataFrame(history)
|
||||||
|
history_df.head()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data Visualization
|
||||||
|
|
||||||
|
### Liked Songs by Artist
|
||||||
|
|
||||||
|
```python
|
||||||
|
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
|
||||||
|
|
||||||
|
```python
|
||||||
|
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
|
||||||
|
|
||||||
|
```python
|
||||||
|
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
|
||||||
|
```
|
||||||
|
3. **Create a new notebook or open an existing one and copy the above cells into the notebook.**
|
||||||
|
4. **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
|
### Step 1: Set Up Your Python Virtual Environment
|
||||||
|
|
||||||
@@ -60,7 +163,6 @@ First, ensure you have Python installed on your system. I recommend using Python
|
|||||||
You can create a Python script like `main.py` to start coding with the API:
|
You can create a Python script like `main.py` to start coding with the API:
|
||||||
```python
|
```python
|
||||||
from ytmusicapi import YTMusic
|
from ytmusicapi import YTMusic
|
||||||
|
|
||||||
ytmusic = YTMusic('oauth.json')
|
ytmusic = YTMusic('oauth.json')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user