-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.py
28 lines (22 loc) · 839 Bytes
/
Search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import urllib3
import re
class Search(object):
"""
docstring
"""
def __init__(self, keyword=""):
self.keyword = keyword.replace(" ","+")
self.base_url = "https://www.youtube.com/"
self.query_search = "results?search_query="
self.query_watch = "watch?v="
self.response_regex = r"watch\?v=(\S{11})"
self.http_client = urllib3.PoolManager()
def find(self, keyword=""):
if keyword == "":
keyword = self.keyword
html = self.http_client.request("GET", str(self.base_url+self.query_search+keyword.replace(" ","+")))
video_ids = re.findall(self.response_regex, html.data.decode())
urls_return = []
for id in video_ids:
urls_return.append(str(self.base_url+self.query_watch)+str(id))
return urls_return