py-youtube-search 0.2.1__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.
@@ -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, keywords: str, sp: str = None, limit: int = 15):
21
+ def __init__(self):
21
22
  """
22
- Initialize the search parameters.
23
- Note: No network request happens here. Call .search() to fetch data.
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 search results.
47
- Returns a list of videos.
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
- if not self.source:
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, self.source)
63
+ matches = re.finditer(pattern, source)
62
64
 
63
65
  results = []
64
66
  for match in matches:
65
- if len(results) >= self.limit:
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("LangGraph", sp=Filters.long_this_week)
83
- # videos = await yt.search()
84
- # print(videos)
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())
@@ -1,25 +1,28 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: py-youtube-search
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: A lightweight, regex-based YouTube search library without API keys.
5
5
  Home-page: https://github.com/VishvaRam/py-youtube-search
6
6
  Author: VishvaRam
7
7
  Author-email: murthyvishva@gmail.com
8
+ License: MIT
8
9
  Classifier: Programming Language :: Python :: 3
9
10
  Classifier: License :: OSI Approved :: MIT License
10
11
  Classifier: Operating System :: OS Independent
11
12
  Requires-Python: >=3.6
12
13
  Description-Content-Type: text/markdown
14
+ Requires-Dist: aiohttp>=3.8.0
13
15
  Dynamic: author
14
16
  Dynamic: author-email
15
17
  Dynamic: classifier
16
18
  Dynamic: description
17
19
  Dynamic: description-content-type
18
20
  Dynamic: home-page
21
+ Dynamic: license
22
+ Dynamic: requires-dist
19
23
  Dynamic: requires-python
20
24
  Dynamic: summary
21
25
 
22
-
23
26
  # py-youtube-search
24
27
 
25
28
  A lightweight, asynchronous Python library to search YouTube videos programmatically without an API key.
@@ -28,6 +31,7 @@ It scrapes search results using `aiohttp` and `re`, making it fast, robust, and
28
31
  ## Features
29
32
 
30
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.
31
35
  - **No API Key Required**: Search YouTube directly without setting up Google Cloud projects.
32
36
  - **Advanced Filtering**: Built-in support for duration (Medium 3-20m, Long >20m) and upload date filters.
33
37
  - **Rich Data Extraction**: Extracts Video ID, Title, Duration, and View Count using optimized regex.
@@ -41,18 +45,18 @@ pip install py-youtube-search
41
45
  ## Quick Start
42
46
 
43
47
  ### 1. Basic Async Search
44
- Fetch the top results for any keyword asynchronously.
48
+ Initialize the client once and run multiple searches.
45
49
 
46
50
  ```python
47
51
  import asyncio
48
52
  from py_youtube_search import YouTubeSearch
49
53
 
50
54
  async def main():
51
- # 1. Initialize the search (no network call yet)
52
- yt = YouTubeSearch("Python async tutorials", limit=5)
55
+ # 1. Initialize the client (reusable)
56
+ yt = YouTubeSearch()
53
57
 
54
- # 2. Await the search results
55
- videos = await yt.search()
58
+ # 2. Run a search
59
+ videos = await yt.search("Python async tutorials", limit=5)
56
60
 
57
61
  for v in videos:
58
62
  print(f"Title: {v['title']}")
@@ -72,21 +76,29 @@ import asyncio
72
76
  from py_youtube_search import YouTubeSearch, Filters
73
77
 
74
78
  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
+ 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)
79
84
 
80
85
  for v in videos:
81
86
  print(f"🎥 {v['title']} | ⏱ {v['duration']} | 👁 {v['views']}")
82
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
+
83
95
  if __name__ == "__main__":
84
96
  asyncio.run(main())
85
97
  ```
86
98
 
87
99
  ## Available Filters
88
100
 
89
- Pass these constants into the `sp` parameter of `YouTubeSearch`.
101
+ Pass these constants into the `sp` parameter of the `search()` method.
90
102
 
91
103
  ### Duration: Medium (3 - 20 Minutes)
92
104
  | Filter Attribute | Description |
@@ -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,5 +0,0 @@
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,,