py-youtube-search 0.2.1__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.
@@ -0,0 +1,85 @@
1
+ import re
2
+ import aiohttp
3
+ import asyncio
4
+
5
+ # Filter constants accessible to the user
6
+ class Filters:
7
+ # Duration: 3 - 20 Minutes (Medium)
8
+ medium_today = "EgYIAhABGAU="
9
+ medium_this_week = "EgQIAxGF"
10
+ medium_this_month = "EgYIBBABGAU="
11
+ medium_this_year = "EgYIBRABGAU="
12
+
13
+ # Duration: Over 20 Minutes (Long)
14
+ long_today = "EgYIAhABGAI="
15
+ long_this_week = "EgQIAxAB"
16
+ long_this_month = "EgYIBBABGAI="
17
+ long_this_year = "EgYIBRABGAI="
18
+
19
+ class YouTubeSearch:
20
+ def __init__(self, keywords: str, sp: str = None, limit: int = 15):
21
+ """
22
+ Initialize the search parameters.
23
+ Note: No network request happens here. Call .search() to fetch data.
24
+ """
25
+ self.keywords = keywords.replace(" ", "+")
26
+ self.sp = sp
27
+ self.limit = limit
28
+ self.source = None
29
+ 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 = {
37
+ "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
+ }
39
+
40
+ async with aiohttp.ClientSession() as session:
41
+ async with session.get(self.base_url, params=params, headers=headers) as response:
42
+ return await response.text()
43
+
44
+ async def search(self):
45
+ """
46
+ Asynchronously fetches and parses the search results.
47
+ Returns a list of videos.
48
+ """
49
+ if not self.source:
50
+ self.source = await self._fetch_source()
51
+
52
+ # Regex to capture distinct JSON fields for ID, Title, Duration, and Views.
53
+ pattern = (
54
+ r'\"videoRenderer\":\{'
55
+ r'.+?\"videoId\":\"(?P<id>\S{11})\"'
56
+ r'.+?\"title\":\{\"runs\":\[\{\"text\":\"(?P<title>.+?)\"\}\]'
57
+ r'.+?\"lengthText\":\{.*?\"simpleText\":\"(?P<duration>.+?)\"\}'
58
+ r'.+?\"viewCountText\":\{\"simpleText\":\"(?P<views>.+?)\"\}'
59
+ )
60
+
61
+ matches = re.finditer(pattern, self.source)
62
+
63
+ results = []
64
+ for match in matches:
65
+ if len(results) >= self.limit:
66
+ break
67
+
68
+ data = match.groupdict()
69
+ results.append({
70
+ "id": data["id"],
71
+ "title": data["title"],
72
+ "duration": data["duration"],
73
+ "views": data["views"],
74
+ "url_suffix": f"/watch?v={data['id']}"
75
+ })
76
+
77
+ return results
78
+
79
+ # --- Usage Example (Async) ---
80
+ # import asyncio
81
+ # async def main():
82
+ # yt = YouTubeSearch("LangGraph", sp=Filters.long_this_week)
83
+ # videos = await yt.search()
84
+ # print(videos)
85
+ # asyncio.run(main())
@@ -0,0 +1,127 @@
1
+ Metadata-Version: 2.4
2
+ Name: py-youtube-search
3
+ Version: 0.2.1
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
+
23
+ # py-youtube-search
24
+
25
+ A lightweight, asynchronous Python library to search YouTube videos programmatically without an API key.
26
+ It scrapes search results using `aiohttp` and `re`, making it fast, robust, and perfect for high-performance applications.
27
+
28
+ ## Features
29
+
30
+ - **Async Support**: Fully asynchronous using `aiohttp` for non-blocking execution.
31
+ - **No API Key Required**: Search YouTube directly without setting up Google Cloud projects.
32
+ - **Advanced Filtering**: Built-in support for duration (Medium 3-20m, Long >20m) and upload date filters.
33
+ - **Rich Data Extraction**: Extracts Video ID, Title, Duration, and View Count using optimized regex.
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ pip install py-youtube-search
39
+ ```
40
+
41
+ ## Quick Start
42
+
43
+ ### 1. Basic Async Search
44
+ Fetch the top results for any keyword asynchronously.
45
+
46
+ ```python
47
+ import asyncio
48
+ from py_youtube_search import YouTubeSearch
49
+
50
+ async def main():
51
+ # 1. Initialize the search (no network call yet)
52
+ yt = YouTubeSearch("Python async tutorials", limit=5)
53
+
54
+ # 2. Await the search results
55
+ videos = await yt.search()
56
+
57
+ for v in videos:
58
+ print(f"Title: {v['title']}")
59
+ print(f"Duration: {v['duration']}")
60
+ print(f"Views: {v['views']}")
61
+ print(f"Link: https://www.youtube.com/watch?v={v['id']}\n")
62
+
63
+ if __name__ == "__main__":
64
+ asyncio.run(main())
65
+ ```
66
+
67
+ ### 2. Advanced Search with Filters
68
+ Search for specific content, like long-form videos (>20m) uploaded this week.
69
+
70
+ ```python
71
+ import asyncio
72
+ from py_youtube_search import YouTubeSearch, Filters
73
+
74
+ async def main():
75
+ # Use the Filters class for readable constants
76
+ yt = YouTubeSearch("LangGraph", sp=Filters.long_this_week, limit=3)
77
+
78
+ videos = await yt.search()
79
+
80
+ for v in videos:
81
+ print(f"🎥 {v['title']} | ⏱ {v['duration']} | 👁 {v['views']}")
82
+
83
+ if __name__ == "__main__":
84
+ asyncio.run(main())
85
+ ```
86
+
87
+ ## Available Filters
88
+
89
+ Pass these constants into the `sp` parameter of `YouTubeSearch`.
90
+
91
+ ### Duration: Medium (3 - 20 Minutes)
92
+ | Filter Attribute | Description |
93
+ | :--- | :--- |
94
+ | `Filters.medium_today` | Uploaded **Today** |
95
+ | `Filters.medium_this_week` | Uploaded **This Week** |
96
+ | `Filters.medium_this_month` | Uploaded **This Month** |
97
+ | `Filters.medium_this_year` | Uploaded **This Year** |
98
+
99
+ ### Duration: Long (Over 20 Minutes)
100
+ | Filter Attribute | Description |
101
+ | :--- | :--- |
102
+ | `Filters.long_today` | Uploaded **Today** |
103
+ | `Filters.long_this_week` | Uploaded **This Week** |
104
+ | `Filters.long_this_month` | Uploaded **This Month** |
105
+ | `Filters.long_this_year` | Uploaded **This Year** |
106
+
107
+ ## Data Structure
108
+
109
+ The `.search()` method returns a list of dictionaries:
110
+
111
+ ```json
112
+ [
113
+ {
114
+ "id": "lDoYisPfcck",
115
+ "title": "Hack the planet! LangGraph AI HackBot Dev & Q/A",
116
+ "duration": "1:05:23",
117
+ "views": "1.2K views",
118
+ "url_suffix": "/watch?v=lDoYisPfcck"
119
+ }
120
+ ]
121
+ ```
122
+
123
+ ## Dependencies
124
+ - `aiohttp` (for async requests)
125
+
126
+ ## License
127
+ MIT License. See LICENSE file for details.
@@ -0,0 +1,5 @@
1
+ py_youtube_search/__init__.py,sha256=hWJcqytPfRODGIFynNmDrCQm_9P5zWwg4DOcWIyrVog,2769
2
+ py_youtube_search-0.2.1.dist-info/METADATA,sha256=mr7bxYgCXk7VOF3pe5DNyen4CSg4Kha1CUFppENZwOc,3591
3
+ py_youtube_search-0.2.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
4
+ py_youtube_search-0.2.1.dist-info/top_level.txt,sha256=4EMqIznKjzwgAcB_78SKaMIzZXpIxWQ4SRLeakY3fzQ,18
5
+ py_youtube_search-0.2.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ py_youtube_search