# py-youtube-search

A lightweight, zero-dependency Python library to search YouTube videos programmatically without an API key. This library uses standard Python libraries (`urllib` and `re`) to scrape search results, making it fast, robust, and easy to integrate into any project.

## Features

- **No API Key Required**: Search YouTube directly without setting up Google Cloud projects or billing.
- **Zero External Dependencies**: Built entirely using Python's standard library (`urllib`, `re`). No `requests`, `selenium`, or `beautifulsoup` needed.
- **Advanced Filtering**: Built-in support for duration (Medium 3-20m, Long >20m) and upload date filters (Today, Week, Month, Year).
- **Rich Data Extraction**: Robustly extracts Video ID, Title, Duration, and View Count.

## Installation

You can install the package via pip:

```bash
pip install py-youtube-search
```

## Quick Start

### 1. Basic Search
Fetch the top results for any keyword.

```python
from py_youtube_search import YouTubeSearch

# Search for the top 5 results for "Python tutorials"
search = YouTubeSearch("Python tutorials", limit=5)
videos = search.videos()

for v in videos:
    print(f"Title: {v['title']}")
    print(f"Duration: {v['duration']}")
    print(f"Views: {v['views']}")
    print(f"Link: https://www.youtube.com/watch?v={v['id']}\n")
```

### 2. Advanced Search with Filters
Search for specific types of content, such as long-form tutorials uploaded this week.

```python
from py_youtube_search import YouTubeSearch, Filters

# Search for "LangGraph" videos over 20 minutes long, uploaded this week
# Use the Filters class to access constants
search = YouTubeSearch("LangGraph", sp=Filters.long_this_week, limit=3)
videos = search.videos()

for v in videos:
    print(f"🎥 {v['title']} | ⏱ {v['duration']} | 👁 {v['views']}")
```

## Available Filters

You can access these filters using the `Filters` class (e.g., `Filters.long_today`).

### Duration: Medium (3 - 20 Minutes)
| Filter Attribute | Description |
| :--- | :--- |
| `Filters.medium_today` | Uploaded **Today** |
| `Filters.medium_this_week` | Uploaded **This Week** |
| `Filters.medium_this_month` | Uploaded **This Month** |
| `Filters.medium_this_year` | Uploaded **This Year** |

### Duration: Long (Over 20 Minutes)
| Filter Attribute | Description |
| :--- | :--- |
| `Filters.long_today` | Uploaded **Today** |
| `Filters.long_this_week` | Uploaded **This Week** |
| `Filters.long_this_month` | Uploaded **This Month** |
| `Filters.long_this_year` | Uploaded **This Year** |

## Data Structure

The `videos()` method returns a list of dictionaries with the following structure:

```json
[
  {
    "id": "lDoYisPfcck",
    "title": "Hack the planet! LangGraph AI HackBot Dev & Q/A",
    "duration": "1:05:23",
    "views": "1.2K views",
    "url_suffix": "/watch?v=lDoYisPfcck"
  },
  {
    "id": "5LXLmUsEM20",
    "title": "LangGraph Workflow Patterns - Part 1/10",
    "duration": "42 seconds",
    "views": "500 views",
    "url_suffix": "/watch?v=5LXLmUsEM20"
  }
]
```

## License

This project is licensed under the MIT License - see below for details.

```text
MIT License

Copyright (c) 2026

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
