-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
31 lines (25 loc) · 928 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
29
30
31
import yt_dlp
def keyword_search(keyword, num):
search_query = "ytsearch"+str(num)+":"+keyword
# Define options for yt-dlp
ydl_opts = {
'format': 'best', # You can specify the format here
}
# Use yt-dlp to search and retrieve video information
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# Perform the search
info_dict = ydl.extract_info(search_query, download=False)
videos = info_dict.get('entries', [])
# Search results
results = []
for idx, video in enumerate(videos):
info = {}
info['idx'] = idx+1
info['title'] = video['title']
info['uploader'] = video['uploader']
info['url'] = video['webpage_url']
info['thumbnail_url'] = video['thumbnail']
info['video_id'] = video['webpage_url'].split('=')[-1]
results.append(info)
return results
# print(keyword_search('my chemical romance', 3))