pafy.py
channel_search(query)
¶
Search channel based on keyword / query provided by user
Source code in mps_youtube/pafy.py
def channel_search(query):
'''
Search channel based on keyword / query provided by user
'''
channelsSearch = ChannelsSearch(query, limit=50, region='US')
return channelsSearch.result()['result']
download_video(ytid, folder)
¶
Given a youtube video id and target folder, this function will download video to that folder
Source code in mps_youtube/pafy.py
def download_video(ytid, folder):
'''
Given a youtube video id and target folder, this function will download video to that folder
'''
ytdl_format_options = {
'outtmpl': os.path.join(folder, '%(title)s-%(id)s.%(ext)s')
}
with yt_dlp.YoutubeDL(ytdl_format_options) as ydl:
ydl.download('https://www.youtube.com/watch?v=%s' % ytid)
return True
extract_video_id(url)
¶
Extract the video id from a url, return video id as str.
Source code in mps_youtube/pafy.py
def extract_video_id(url):
""" Extract the video id from a url, return video id as str. """
idregx = re.compile(r'[\w-]{11}$')
url = str(url).strip()
if idregx.match(url):
return url # ID of video
if '://' not in url:
url = '//' + url
parsedurl = urlparse(url)
if parsedurl.netloc in ('youtube.com', 'www.youtube.com', 'm.youtube.com', 'gaming.youtube.com'):
query = parse_qs(parsedurl.query)
if 'v' in query and idregx.match(query['v'][0]):
return query['v'][0]
elif parsedurl.netloc in ('youtu.be', 'www.youtu.be'):
vidid = parsedurl.path.split('/')[-1] if parsedurl.path else ''
if idregx.match(vidid):
return vidid
err = "Need 11 character video id or the URL of the video. Got %s"
raise ValueError(err % url)
get_playlist(playlist_id)
¶
Get all videos of a playlist identified by playlist_id
Source code in mps_youtube/pafy.py
def get_playlist(playlist_id):
'''
Get all videos of a playlist identified by playlist_id
'''
playlist = Playlist.get('https://www.youtube.com/playlist?list=%s' % playlist_id)
return playlist
get_video_streams(ytid)
¶
given a youtube video id returns different video / audio stream formats'
Source code in mps_youtube/pafy.py
def get_video_streams(ytid):
'''
given a youtube video id returns different video / audio stream formats' \
'''
with yt_dlp.YoutubeDL({'logger':MyLogger()}) as ydl:
info_dict = ydl.extract_info(ytid, download=False)
return [i for i in info_dict['formats'] if i.get('format_note') != 'storyboard']
playlist_search(query)
¶
Returns all playlists having similar names as keyword / query provided
Source code in mps_youtube/pafy.py
def playlist_search(query):
'''
Returns all playlists having similar names as keyword / query provided
'''
playlistsSearch = PlaylistsSearch(query, limit=50)
return playlistsSearch.result()['result']
search_videos(query, pages)
¶
Given a keyword / query this function will return youtube video results against those keywords / query
Source code in mps_youtube/pafy.py
def search_videos(query, pages):
'''
Given a keyword / query this function will return youtube video results against those keywords / query
'''
videosSearch = VideosSearch(query, limit=50)
wdata = videosSearch.result()['result']
for i in range(pages-1):
videosSearch.next()
wdata.extend(videosSearch.result()['result'])
return wdata