-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_helper.py
55 lines (45 loc) · 1.16 KB
/
youtube_helper.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from pytube import YouTube, Playlist
def get_title(url):
"""
Get the title for a given URL (video or playlist)
Parameters:
url(string): url of the video or playlist
Returns:
title: the title of the video or playlist
"""
# Initialize the channel name
title = ""
# Check if the URL is a playlist or a video
try:
if 'playlist' in url:
# Get playlist data and title
title = Playlist(url).title
else:
# Get video data and title
title = YouTube(url).title
except Exception as e:
pass
# Return the title
return title
def get_channel_name(url):
"""
Get the channel name for a given URL (video or playlist)
Parameters:
url(string): url of the video or playlist
Returns:
channel_name: the name of the channel
"""
# Initialize the channel name
channel_name = ""
# Check if the URL is a playlist or a video
try:
if 'playlist' in url:
# Get playlist data and owner channel name
channel_name = Playlist(url).owner
else:
# Get video data and author channel name
channel_name = YouTube(url).author
except Exception as e:
pass
# Return the channel name
return channel_name