Update projects/ytmusic.md
This commit is contained in:
@@ -1,3 +1,186 @@
|
|||||||
|
To start with connecting to the YouTube Music API and downloading your playlist data using `curl` and storing this information in a `sqlite3` database, we'll break this task into stages. We'll focus on using the YouTube Data API (which supports YouTube Music data) for authentication and data fetching.
|
||||||
|
|
||||||
|
### Stage 1: Setup and API Authentication
|
||||||
|
|
||||||
|
#### 1.1 Create a Project and Enable YouTube Data API
|
||||||
|
|
||||||
|
1. Go to the [Google Cloud Console](https://console.developers.google.com/).
|
||||||
|
2. Create a new project.
|
||||||
|
3. Enable the YouTube Data API v3 for your project.
|
||||||
|
4. Create OAuth 2.0 credentials for your project and download the JSON file.
|
||||||
|
|
||||||
|
#### 1.2 Using `curl` to Connect to the API
|
||||||
|
|
||||||
|
First, you'll need to authenticate with OAuth 2.0. Here is a simple way to get an access token:
|
||||||
|
|
||||||
|
1. **Request User Authorization**
|
||||||
|
|
||||||
|
Open a browser and navigate to the following URL, replacing `YOUR_CLIENT_ID` and `YOUR_REDIRECT_URI` with your OAuth 2.0 Client ID and Redirect URI:
|
||||||
|
```
|
||||||
|
https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/youtube.readonly&access_type=offline&include_granted_scopes=true&response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI
|
||||||
|
```
|
||||||
|
|
||||||
|
After the user grants permission, Google will redirect to the specified `redirect_uri` with a `code` query parameter.
|
||||||
|
|
||||||
|
2. **Exchange Authorization Code for Access Token**
|
||||||
|
|
||||||
|
Use `curl` to exchange the authorization code for an access token:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl \
|
||||||
|
-d "code=YOUR_AUTH_CODE" \
|
||||||
|
-d "client_id=YOUR_CLIENT_ID" \
|
||||||
|
-d "client_secret=YOUR_CLIENT_SECRET" \
|
||||||
|
-d "redirect_uri=YOUR_REDIRECT_URI" \
|
||||||
|
-d "grant_type=authorization_code" \
|
||||||
|
https://oauth2.googleapis.com/token
|
||||||
|
```
|
||||||
|
|
||||||
|
This will return a JSON response with the `access_token` and `refresh_token`.
|
||||||
|
|
||||||
|
#### 1.3 Fetch Playlist Data
|
||||||
|
|
||||||
|
Now that you have the access token, you can fetch your playlists:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl \
|
||||||
|
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
|
||||||
|
"https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stage 2: Store Data in SQLite
|
||||||
|
|
||||||
|
Let's create a Python script to fetch the data using the YouTube Data API and store it in a SQLite database.
|
||||||
|
|
||||||
|
#### 2.1 Install Required Packages
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install requests sqlite3
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2.2 Create Python Script
|
||||||
|
|
||||||
|
Create a script `fetch_and_store.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import requests
|
||||||
|
import sqlite3
|
||||||
|
import json
|
||||||
|
|
||||||
|
# Replace with your actual access token
|
||||||
|
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
|
||||||
|
|
||||||
|
# Fetch playlists
|
||||||
|
response = requests.get(
|
||||||
|
'https://www.googleapis.com/youtube/v3/playlists?part=snippet&mine=true',
|
||||||
|
headers={'Authorization': f'Bearer {ACCESS_TOKEN}'}
|
||||||
|
)
|
||||||
|
playlists = response.json()
|
||||||
|
|
||||||
|
# Connect to SQLite database
|
||||||
|
conn = sqlite3.connect('youtube_music.db')
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
# Create table for playlists
|
||||||
|
c.execute('''
|
||||||
|
CREATE TABLE IF NOT EXISTS playlists (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
title TEXT,
|
||||||
|
description TEXT,
|
||||||
|
published_at TEXT
|
||||||
|
)
|
||||||
|
''')
|
||||||
|
|
||||||
|
# Insert playlists into the database
|
||||||
|
for item in playlists['items']:
|
||||||
|
c.execute('''
|
||||||
|
INSERT OR REPLACE INTO playlists (id, title, description, published_at)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
''', (item['id'], item['snippet']['title'], item['snippet']['description'], item['snippet']['publishedAt']))
|
||||||
|
|
||||||
|
# Commit and close the connection
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
print("Playlists have been successfully saved to the database.")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stage 3: Fetching More Data and Analyzing
|
||||||
|
|
||||||
|
#### 3.1 Fetch Playlist Items
|
||||||
|
|
||||||
|
Update the script to fetch and store playlist items:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Fetch playlist items
|
||||||
|
playlist_id = 'YOUR_PLAYLIST_ID'
|
||||||
|
response = requests.get(
|
||||||
|
f'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={playlist_id}',
|
||||||
|
headers={'Authorization': f'Bearer {ACCESS_TOKEN}'}
|
||||||
|
)
|
||||||
|
playlist_items = response.json()
|
||||||
|
|
||||||
|
# Create table for playlist items
|
||||||
|
c.execute('''
|
||||||
|
CREATE TABLE IF NOT EXISTS playlist_items (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
playlist_id TEXT,
|
||||||
|
title TEXT,
|
||||||
|
description TEXT,
|
||||||
|
published_at TEXT,
|
||||||
|
video_id TEXT
|
||||||
|
)
|
||||||
|
''')
|
||||||
|
|
||||||
|
# Insert playlist items into the database
|
||||||
|
for item in playlist_items['items']:
|
||||||
|
c.execute('''
|
||||||
|
INSERT OR REPLACE INTO playlist_items (id, playlist_id, title, description, published_at, video_id)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
|
''', (item['id'], playlist_id, item['snippet']['title'], item['snippet']['description'], item['snippet']['publishedAt'], item['snippet']['resourceId']['videoId']))
|
||||||
|
|
||||||
|
# Commit and close the connection
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
print("Playlist items have been successfully saved to the database.")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stage 4: Analyzing Data
|
||||||
|
|
||||||
|
You can now analyze the data using SQL queries directly on the SQLite database or by loading the data into a pandas DataFrame for more complex analysis and visualization.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import sqlite3
|
||||||
|
import pandas as pd
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# Connect to SQLite database
|
||||||
|
conn = sqlite3.connect('youtube_music.db')
|
||||||
|
|
||||||
|
# Load playlists into DataFrame
|
||||||
|
playlists_df = pd.read_sql_query("SELECT * FROM playlists", conn)
|
||||||
|
print(playlists_df.head())
|
||||||
|
|
||||||
|
# Load playlist items into DataFrame
|
||||||
|
playlist_items_df = pd.read_sql_query("SELECT * FROM playlist_items", conn)
|
||||||
|
print(playlist_items_df.head())
|
||||||
|
|
||||||
|
# Visualization Example
|
||||||
|
playlist_items_df['title'].value_counts().plot(kind='bar', figsize=(10, 5))
|
||||||
|
plt.title('Playlist Items by Title')
|
||||||
|
plt.xlabel('Title')
|
||||||
|
plt.ylabel('Count')
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
# Close the connection
|
||||||
|
conn.close()
|
||||||
|
```
|
||||||
|
|
||||||
|
This staged approach will help you connect to the YouTube Data API, fetch playlist data, store it in a SQLite database, and perform data analysis.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
# YouTube Music Data Analysis
|
# YouTube Music Data Analysis
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|||||||
Reference in New Issue
Block a user