From 221cddc2d8f01ba89e1c1075adfe6fb59c1fa109 Mon Sep 17 00:00:00 2001 From: medusa Date: Mon, 22 Apr 2024 02:34:59 +0000 Subject: [PATCH] Update docs/tech_docs/python/python_project.md --- docs/tech_docs/python/python_project.md | 150 +++++++++++++++--------- 1 file changed, 95 insertions(+), 55 deletions(-) diff --git a/docs/tech_docs/python/python_project.md b/docs/tech_docs/python/python_project.md index 74afc03..809a524 100644 --- a/docs/tech_docs/python/python_project.md +++ b/docs/tech_docs/python/python_project.md @@ -1,72 +1,112 @@ +Certainly! Here's a refactored and well-organized guide with additional details, tailored to setting up a Python project using a virtual environment for managing YouTube Music playlist data. This will be provided in Markdown format, ideal for a README file or documentation purposes. + +### Markdown Content for the Guide + +Here is how you can structure your guide in markdown, including script execution, package management, and best practices: + +```markdown # Python Project Setup Guide -```bash -#!/usr/bin/env python3 -``` + +This guide outlines the steps for setting up a Python project to manage YouTube Music playlist data using `ytmusicapi` and SQLite. It includes instructions on creating a virtual environment, managing dependencies, setting up OAuth authentication, initializing a SQLite database, fetching and storing data, and performing data analysis. + +## Table of Contents +1. [Setting Up a Virtual Environment](#setting-up-a-virtual-environment) +2. [Installing Required Packages](#installing-required-packages) +3. [OAuth Authentication Setup](#oauth-authentication-setup) +4. [Database Setup](#database-setup) +5. [Fetching and Storing Data](#fetching-and-storing-data) +6. [Data Analysis](#data-analysis) +7. [Conclusion](#conclusion) + ## Setting Up a Virtual Environment -1. **Create Project Directory** - ```bash - mkdir my_project && cd my_project - ``` +Create and activate a virtual environment to isolate your package installations. -2. **Create Virtual Environment** - ```bash - python3 -m venv venv - ``` +```bash +#!/usr/bin/env python3 +python -m venv venv +source venv/bin/activate # On macOS and Linux +.\venv\Scripts\activate # On Windows +``` -3. **Activate Virtual Environment** - - macOS/Linux: - ```bash - source venv/bin/activate - ``` - - Windows: - ```cmd - .\venv\Scripts\activate - ``` +## Installing Required Packages -4. **Deactivate Virtual Environment** - ```bash - deactivate - ``` +Ensure `ytmusicapi` is included in your `requirements.txt` and install: -## Package Management with pip +```bash +pip install -r requirements.txt +``` -1. **Install a Package** - ```bash - pip install package_name - ``` +## OAuth Authentication Setup -2. **Install Specific Version** - ```bash - pip install package_name==version - ``` +Initialize OAuth to authenticate your access to YouTube Music. -3. **Upgrade a Package** - ```bash - pip install --upgrade package_name - ``` +```bash +ytmusicapi oauth +``` -4. **Using a Requirements File** - - Install packages: - ```bash - pip install -r requirements.txt - ``` - - Freeze current packages to `requirements.txt`: - ```bash - pip freeze > requirements.txt - ``` +Follow the on-screen prompts to authenticate. This process will generate an `oauth.json` file in your project directory. -## Managing Dependencies +## Database Setup -- Create a `requirements.txt` file in your project root. -- List dependencies and versions, e.g., `flask==1.1.2`. -- Use `pip freeze` to generate this file with current environment packages. +Create a SQLite database and define tables to store playlists and track data. + +```python +import sqlite3 +conn = sqlite3.connect('youtube_music.db') +c = conn.cursor() +c.execute(''' +CREATE TABLE IF NOT EXISTS playlists ( + playlist_id TEXT PRIMARY KEY, + title TEXT, + description TEXT, + privacy TEXT, + itemCount INTEGER +) +''') +``` + +## Fetching and Storing Data + +Use `ytmusicapi` to fetch playlist data and store it in your database. + +```python +from ytmusicapi import YTMusic +ytmusic = YTMusic('oauth.json') +playlists = ytmusic.get_library_playlists(limit=100) +# Insert data into the database... +``` + +## Data Analysis + +Analyze your data directly from the SQLite database using SQL queries. + +```python +# Most Popular Artists +c.execute('SELECT artist, COUNT(*) FROM tracks GROUP BY artist ORDER BY COUNT(*) DESC') +popular_artists = c.fetchall() +``` + +## Conclusion + +With this setup, you can manage your YouTube Music playlists and perform data analysis using Python and SQLite effectively. ## Best Practices -- Always activate your virtual environment when working on the project. -- Regularly update your `requirements.txt` to reflect new dependencies. -- Include `venv` in your `.gitignore` file. -- Commit `requirements.txt` to version control. +- **Virtual Environment**: Always activate your virtual environment when working on the project. +- **Dependencies**: Regularly update your `requirements.txt` to reflect new dependencies. +- **Version Control**: Include `venv` in your `.gitignore` file and commit `requirements.txt` to maintain consistency across environments. +``` -This guide provides a streamlined approach to setting up and managing Python projects, ensuring consistency and ease of use across development environments. \ No newline at end of file +### Python Code to Write Markdown to a File + +Here's the Python script to output the above Markdown content into a file named `YT_Music_Guide.md`: + +```python +markdown_content = """[Your Markdown content from above]""" + +with open('YT_Music_Guide.md', 'w') as file: + file.write(markdown_content) +``` + +This script will create a markdown file named `YT_Music_Guide.md` in your project directory. This file can be opened with any text editor or viewed on platforms like GitHub to provide clear documentation for setting up and running your Python project. \ No newline at end of file