py-youtube-search 0.2.0__py3-none-any.whl → 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py_youtube_search/__init__.py +36 -25
- py_youtube_search-0.2.2.dist-info/METADATA +139 -0
- py_youtube_search-0.2.2.dist-info/RECORD +5 -0
- py_youtube_search-0.2.0.dist-info/METADATA +0 -144
- py_youtube_search-0.2.0.dist-info/RECORD +0 -5
- {py_youtube_search-0.2.0.dist-info → py_youtube_search-0.2.2.dist-info}/WHEEL +0 -0
- {py_youtube_search-0.2.0.dist-info → py_youtube_search-0.2.2.dist-info}/top_level.txt +0 -0
py_youtube_search/__init__.py
CHANGED
|
@@ -16,38 +16,40 @@ class Filters:
|
|
|
16
16
|
long_this_month = "EgYIBBABGAI="
|
|
17
17
|
long_this_year = "EgYIBRABGAI="
|
|
18
18
|
|
|
19
|
+
|
|
19
20
|
class YouTubeSearch:
|
|
20
|
-
def __init__(self
|
|
21
|
+
def __init__(self):
|
|
21
22
|
"""
|
|
22
|
-
Initialize the
|
|
23
|
-
|
|
23
|
+
Initialize the YouTubeSearch client.
|
|
24
|
+
The client is stateless; parameters are passed to the search method.
|
|
24
25
|
"""
|
|
25
|
-
self.keywords = keywords.replace(" ", "+")
|
|
26
|
-
self.sp = sp
|
|
27
|
-
self.limit = limit
|
|
28
|
-
self.source = None
|
|
29
26
|
self.base_url = "https://www.youtube.com/results"
|
|
30
|
-
|
|
31
|
-
async def _fetch_source(self):
|
|
32
|
-
params = {"search_query": self.keywords}
|
|
33
|
-
if self.sp:
|
|
34
|
-
params["sp"] = self.sp
|
|
35
|
-
|
|
36
|
-
headers = {
|
|
27
|
+
self.headers = {
|
|
37
28
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
|
38
29
|
}
|
|
39
30
|
|
|
31
|
+
async def _fetch_source(self, keywords: str, sp: str = None):
|
|
32
|
+
params = {"search_query": keywords.replace(" ", "+")}
|
|
33
|
+
if sp:
|
|
34
|
+
params["sp"] = sp
|
|
35
|
+
|
|
40
36
|
async with aiohttp.ClientSession() as session:
|
|
41
|
-
async with session.get(self.base_url, params=params, headers=headers) as response:
|
|
37
|
+
async with session.get(self.base_url, params=params, headers=self.headers) as response:
|
|
42
38
|
return await response.text()
|
|
43
39
|
|
|
44
|
-
async def search(self):
|
|
40
|
+
async def search(self, keywords: str, sp: str = None, limit: int = 15):
|
|
45
41
|
"""
|
|
46
|
-
Asynchronously fetches and parses the
|
|
47
|
-
|
|
42
|
+
Asynchronously fetches and parses search results for the given keywords.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
keywords (str): The search query.
|
|
46
|
+
sp (str, optional): The filter string (use Filters class). Defaults to None.
|
|
47
|
+
limit (int, optional): Max number of results. Defaults to 15.
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
list: A list of dictionaries containing video details.
|
|
48
51
|
"""
|
|
49
|
-
|
|
50
|
-
self.source = await self._fetch_source()
|
|
52
|
+
source = await self._fetch_source(keywords, sp)
|
|
51
53
|
|
|
52
54
|
# Regex to capture distinct JSON fields for ID, Title, Duration, and Views.
|
|
53
55
|
pattern = (
|
|
@@ -58,11 +60,11 @@ class YouTubeSearch:
|
|
|
58
60
|
r'.+?\"viewCountText\":\{\"simpleText\":\"(?P<views>.+?)\"\}'
|
|
59
61
|
)
|
|
60
62
|
|
|
61
|
-
matches = re.finditer(pattern,
|
|
63
|
+
matches = re.finditer(pattern, source)
|
|
62
64
|
|
|
63
65
|
results = []
|
|
64
66
|
for match in matches:
|
|
65
|
-
if len(results) >=
|
|
67
|
+
if len(results) >= limit:
|
|
66
68
|
break
|
|
67
69
|
|
|
68
70
|
data = match.groupdict()
|
|
@@ -79,7 +81,16 @@ class YouTubeSearch:
|
|
|
79
81
|
# --- Usage Example (Async) ---
|
|
80
82
|
# import asyncio
|
|
81
83
|
# async def main():
|
|
82
|
-
# yt = YouTubeSearch(
|
|
83
|
-
#
|
|
84
|
-
#
|
|
84
|
+
# yt = YouTubeSearch()
|
|
85
|
+
#
|
|
86
|
+
# # Search 1: Long videos about LangGraph
|
|
87
|
+
# print("Searching LangGraph...")
|
|
88
|
+
# videos1 = await yt.search("LangGraph", sp=Filters.long_this_week, limit=5)
|
|
89
|
+
# print(f"Found {len(videos1)} videos")
|
|
90
|
+
#
|
|
91
|
+
# # Search 2: Short Python tutorials (reusing the same instance)
|
|
92
|
+
# print("Searching Python...")
|
|
93
|
+
# videos2 = await yt.search("Python", sp=Filters.medium_today, limit=3)
|
|
94
|
+
# print(f"Found {len(videos2)} videos")
|
|
95
|
+
#
|
|
85
96
|
# asyncio.run(main())
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-youtube-search
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: A lightweight, regex-based YouTube search library without API keys.
|
|
5
|
+
Home-page: https://github.com/VishvaRam/py-youtube-search
|
|
6
|
+
Author: VishvaRam
|
|
7
|
+
Author-email: murthyvishva@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.6
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: aiohttp>=3.8.0
|
|
15
|
+
Dynamic: author
|
|
16
|
+
Dynamic: author-email
|
|
17
|
+
Dynamic: classifier
|
|
18
|
+
Dynamic: description
|
|
19
|
+
Dynamic: description-content-type
|
|
20
|
+
Dynamic: home-page
|
|
21
|
+
Dynamic: license
|
|
22
|
+
Dynamic: requires-dist
|
|
23
|
+
Dynamic: requires-python
|
|
24
|
+
Dynamic: summary
|
|
25
|
+
|
|
26
|
+
# py-youtube-search
|
|
27
|
+
|
|
28
|
+
A lightweight, asynchronous Python library to search YouTube videos programmatically without an API key.
|
|
29
|
+
It scrapes search results using `aiohttp` and `re`, making it fast, robust, and perfect for high-performance applications.
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- **Async Support**: Fully asynchronous using `aiohttp` for non-blocking execution.
|
|
34
|
+
- **Reusable Client**: Create a single instance and run multiple searches with different configurations.
|
|
35
|
+
- **No API Key Required**: Search YouTube directly without setting up Google Cloud projects.
|
|
36
|
+
- **Advanced Filtering**: Built-in support for duration (Medium 3-20m, Long >20m) and upload date filters.
|
|
37
|
+
- **Rich Data Extraction**: Extracts Video ID, Title, Duration, and View Count using optimized regex.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install py-youtube-search
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
### 1. Basic Async Search
|
|
48
|
+
Initialize the client once and run multiple searches.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import asyncio
|
|
52
|
+
from py_youtube_search import YouTubeSearch
|
|
53
|
+
|
|
54
|
+
async def main():
|
|
55
|
+
# 1. Initialize the client (reusable)
|
|
56
|
+
yt = YouTubeSearch()
|
|
57
|
+
|
|
58
|
+
# 2. Run a search
|
|
59
|
+
videos = await yt.search("Python async tutorials", limit=5)
|
|
60
|
+
|
|
61
|
+
for v in videos:
|
|
62
|
+
print(f"Title: {v['title']}")
|
|
63
|
+
print(f"Duration: {v['duration']}")
|
|
64
|
+
print(f"Views: {v['views']}")
|
|
65
|
+
print(f"Link: https://www.youtube.com/watch?v={v['id']}\n")
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
asyncio.run(main())
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 2. Advanced Search with Filters
|
|
72
|
+
Search for specific content, like long-form videos (>20m) uploaded this week.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
import asyncio
|
|
76
|
+
from py_youtube_search import YouTubeSearch, Filters
|
|
77
|
+
|
|
78
|
+
async def main():
|
|
79
|
+
yt = YouTubeSearch()
|
|
80
|
+
|
|
81
|
+
# Search 1: Long videos about LangGraph
|
|
82
|
+
print("Searching for LangGraph...")
|
|
83
|
+
videos = await yt.search("LangGraph", sp=Filters.long_this_week, limit=3)
|
|
84
|
+
|
|
85
|
+
for v in videos:
|
|
86
|
+
print(f"🎥 {v['title']} | ⏱ {v['duration']} | 👁 {v['views']}")
|
|
87
|
+
|
|
88
|
+
# Search 2: Reusing the same client for a different query
|
|
89
|
+
print("\nSearching for Python...")
|
|
90
|
+
videos_py = await yt.search("Python 3.12", sp=Filters.medium_today, limit=3)
|
|
91
|
+
|
|
92
|
+
for v in videos_py:
|
|
93
|
+
print(f"🐍 {v['title']}")
|
|
94
|
+
|
|
95
|
+
if __name__ == "__main__":
|
|
96
|
+
asyncio.run(main())
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Available Filters
|
|
100
|
+
|
|
101
|
+
Pass these constants into the `sp` parameter of the `search()` method.
|
|
102
|
+
|
|
103
|
+
### Duration: Medium (3 - 20 Minutes)
|
|
104
|
+
| Filter Attribute | Description |
|
|
105
|
+
| :--- | :--- |
|
|
106
|
+
| `Filters.medium_today` | Uploaded **Today** |
|
|
107
|
+
| `Filters.medium_this_week` | Uploaded **This Week** |
|
|
108
|
+
| `Filters.medium_this_month` | Uploaded **This Month** |
|
|
109
|
+
| `Filters.medium_this_year` | Uploaded **This Year** |
|
|
110
|
+
|
|
111
|
+
### Duration: Long (Over 20 Minutes)
|
|
112
|
+
| Filter Attribute | Description |
|
|
113
|
+
| :--- | :--- |
|
|
114
|
+
| `Filters.long_today` | Uploaded **Today** |
|
|
115
|
+
| `Filters.long_this_week` | Uploaded **This Week** |
|
|
116
|
+
| `Filters.long_this_month` | Uploaded **This Month** |
|
|
117
|
+
| `Filters.long_this_year` | Uploaded **This Year** |
|
|
118
|
+
|
|
119
|
+
## Data Structure
|
|
120
|
+
|
|
121
|
+
The `.search()` method returns a list of dictionaries:
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
[
|
|
125
|
+
{
|
|
126
|
+
"id": "lDoYisPfcck",
|
|
127
|
+
"title": "Hack the planet! LangGraph AI HackBot Dev & Q/A",
|
|
128
|
+
"duration": "1:05:23",
|
|
129
|
+
"views": "1.2K views",
|
|
130
|
+
"url_suffix": "/watch?v=lDoYisPfcck"
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Dependencies
|
|
136
|
+
- `aiohttp` (for async requests)
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
MIT License. See LICENSE file for details.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
py_youtube_search/__init__.py,sha256=Y_omeJ9q_6gaQOHlsYd0NZnHvmzBSATLtmjtY_TpJAA,3310
|
|
2
|
+
py_youtube_search-0.2.2.dist-info/METADATA,sha256=OJ0-9Eof3gbuDCRPOeR37ZUfnsvQ7pU5Yop3BLzoaGk,4038
|
|
3
|
+
py_youtube_search-0.2.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
4
|
+
py_youtube_search-0.2.2.dist-info/top_level.txt,sha256=4EMqIznKjzwgAcB_78SKaMIzZXpIxWQ4SRLeakY3fzQ,18
|
|
5
|
+
py_youtube_search-0.2.2.dist-info/RECORD,,
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: py-youtube-search
|
|
3
|
-
Version: 0.2.0
|
|
4
|
-
Summary: A lightweight, regex-based YouTube search library without API keys.
|
|
5
|
-
Home-page: https://github.com/VishvaRam/py-youtube-search
|
|
6
|
-
Author: VishvaRam
|
|
7
|
-
Author-email: murthyvishva@gmail.com
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Requires-Python: >=3.6
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
Dynamic: author
|
|
14
|
-
Dynamic: author-email
|
|
15
|
-
Dynamic: classifier
|
|
16
|
-
Dynamic: description
|
|
17
|
-
Dynamic: description-content-type
|
|
18
|
-
Dynamic: home-page
|
|
19
|
-
Dynamic: requires-python
|
|
20
|
-
Dynamic: summary
|
|
21
|
-
|
|
22
|
-
# py-youtube-search
|
|
23
|
-
|
|
24
|
-
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.
|
|
25
|
-
|
|
26
|
-
## Features
|
|
27
|
-
|
|
28
|
-
- **No API Key Required**: Search YouTube directly without setting up Google Cloud projects or billing.
|
|
29
|
-
- **Zero External Dependencies**: Built entirely using Python's standard library (`urllib`, `re`). No `requests`, `selenium`, or `beautifulsoup` needed.
|
|
30
|
-
- **Advanced Filtering**: Built-in support for duration (Medium 3-20m, Long >20m) and upload date filters (Today, Week, Month, Year).
|
|
31
|
-
- **Rich Data Extraction**: Robustly extracts Video ID, Title, Duration, and View Count.
|
|
32
|
-
|
|
33
|
-
## Installation
|
|
34
|
-
|
|
35
|
-
You can install the package via pip:
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
pip install py-youtube-search
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## Quick Start
|
|
42
|
-
|
|
43
|
-
### 1. Basic Search
|
|
44
|
-
Fetch the top results for any keyword.
|
|
45
|
-
|
|
46
|
-
```python
|
|
47
|
-
from py_youtube_search import YouTubeSearch
|
|
48
|
-
|
|
49
|
-
# Search for the top 5 results for "Python tutorials"
|
|
50
|
-
search = YouTubeSearch("Python tutorials", limit=5)
|
|
51
|
-
videos = search.videos()
|
|
52
|
-
|
|
53
|
-
for v in videos:
|
|
54
|
-
print(f"Title: {v['title']}")
|
|
55
|
-
print(f"Duration: {v['duration']}")
|
|
56
|
-
print(f"Views: {v['views']}")
|
|
57
|
-
print(f"Link: https://www.youtube.com/watch?v={v['id']}\n")
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### 2. Advanced Search with Filters
|
|
61
|
-
Search for specific types of content, such as long-form tutorials uploaded this week.
|
|
62
|
-
|
|
63
|
-
```python
|
|
64
|
-
from py_youtube_search import YouTubeSearch, Filters
|
|
65
|
-
|
|
66
|
-
# Search for "LangGraph" videos over 20 minutes long, uploaded this week
|
|
67
|
-
# Use the Filters class to access constants
|
|
68
|
-
search = YouTubeSearch("LangGraph", sp=Filters.long_this_week, limit=3)
|
|
69
|
-
videos = search.videos()
|
|
70
|
-
|
|
71
|
-
for v in videos:
|
|
72
|
-
print(f"🎥 {v['title']} | ⏱ {v['duration']} | 👁 {v['views']}")
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
## Available Filters
|
|
76
|
-
|
|
77
|
-
You can access these filters using the `Filters` class (e.g., `Filters.long_today`).
|
|
78
|
-
|
|
79
|
-
### Duration: Medium (3 - 20 Minutes)
|
|
80
|
-
| Filter Attribute | Description |
|
|
81
|
-
| :--- | :--- |
|
|
82
|
-
| `Filters.medium_today` | Uploaded **Today** |
|
|
83
|
-
| `Filters.medium_this_week` | Uploaded **This Week** |
|
|
84
|
-
| `Filters.medium_this_month` | Uploaded **This Month** |
|
|
85
|
-
| `Filters.medium_this_year` | Uploaded **This Year** |
|
|
86
|
-
|
|
87
|
-
### Duration: Long (Over 20 Minutes)
|
|
88
|
-
| Filter Attribute | Description |
|
|
89
|
-
| :--- | :--- |
|
|
90
|
-
| `Filters.long_today` | Uploaded **Today** |
|
|
91
|
-
| `Filters.long_this_week` | Uploaded **This Week** |
|
|
92
|
-
| `Filters.long_this_month` | Uploaded **This Month** |
|
|
93
|
-
| `Filters.long_this_year` | Uploaded **This Year** |
|
|
94
|
-
|
|
95
|
-
## Data Structure
|
|
96
|
-
|
|
97
|
-
The `videos()` method returns a list of dictionaries with the following structure:
|
|
98
|
-
|
|
99
|
-
```json
|
|
100
|
-
[
|
|
101
|
-
{
|
|
102
|
-
"id": "lDoYisPfcck",
|
|
103
|
-
"title": "Hack the planet! LangGraph AI HackBot Dev & Q/A",
|
|
104
|
-
"duration": "1:05:23",
|
|
105
|
-
"views": "1.2K views",
|
|
106
|
-
"url_suffix": "/watch?v=lDoYisPfcck"
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
"id": "5LXLmUsEM20",
|
|
110
|
-
"title": "LangGraph Workflow Patterns - Part 1/10",
|
|
111
|
-
"duration": "42 seconds",
|
|
112
|
-
"views": "500 views",
|
|
113
|
-
"url_suffix": "/watch?v=5LXLmUsEM20"
|
|
114
|
-
}
|
|
115
|
-
]
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
## License
|
|
119
|
-
|
|
120
|
-
This project is licensed under the MIT License - see below for details.
|
|
121
|
-
|
|
122
|
-
```text
|
|
123
|
-
MIT License
|
|
124
|
-
|
|
125
|
-
Copyright (c) 2026
|
|
126
|
-
|
|
127
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
128
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
129
|
-
in the Software without restriction, including without limitation the rights
|
|
130
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
131
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
132
|
-
furnished to do so, subject to the following conditions:
|
|
133
|
-
|
|
134
|
-
The above copyright notice and this permission notice shall be included in all
|
|
135
|
-
copies or substantial portions of the Software.
|
|
136
|
-
|
|
137
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
138
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
139
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
140
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
141
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
142
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
143
|
-
SOFTWARE.
|
|
144
|
-
```
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
py_youtube_search/__init__.py,sha256=hWJcqytPfRODGIFynNmDrCQm_9P5zWwg4DOcWIyrVog,2769
|
|
2
|
-
py_youtube_search-0.2.0.dist-info/METADATA,sha256=-Z0VAXP6C5HNG4TriRWmPy9ZSrUPKqu9_1SX1dpPOkM,4833
|
|
3
|
-
py_youtube_search-0.2.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
4
|
-
py_youtube_search-0.2.0.dist-info/top_level.txt,sha256=4EMqIznKjzwgAcB_78SKaMIzZXpIxWQ4SRLeakY3fzQ,18
|
|
5
|
-
py_youtube_search-0.2.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|