-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_client.py
83 lines (66 loc) · 2.35 KB
/
youtube_client.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from pytube import Search
from pytube.contrib.search import logger
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
logger.disabled = True
class YouTubeClient:
def __init__(self) -> None:
flow = InstalledAppFlow.from_client_secrets_file(
"client_secret.json",
scopes=["https://www.googleapis.com/auth/youtube.force-ssl"],
)
creds = flow.run_local_server()
self.youtube = build("youtube", "v3", credentials=creds)
def create_playlist(
self, name: str, description: str, privacy_status: str = "private"
):
playlist = (
self.youtube.playlists()
.insert(
part="snippet,status",
body={
"snippet": {
"title": name,
"description": description,
"defaultLanguage": "en",
},
"status": {"privacyStatus": privacy_status},
},
)
.execute()
)
return playlist
def add_song_playlist(self, playlist_id: str, video_id: str):
request = self.youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": playlist_id,
"resourceId": {"kind": "youtube#video", "videoId": video_id},
}
},
)
playlist_item = request.execute()
return playlist_item
def remove_song_playlist(self, playlist_id: str, video_id: str):
request = self.youtube.playlistItems().delete(id=video_id)
response = request.execute()
return response
def search_video(self, query: str):
return Search(query).results[0]
def get_playlist(self, playlist_id):
videos = []
next_page_token = None
while True:
request = self.youtube.playlistItems().list(
part="snippet",
playlistId=playlist_id,
maxResults=50,
pageToken=next_page_token,
)
response = request.execute()
videos.extend(response["items"])
next_page_token = response.get("nextPageToken")
if not next_page_token:
break
return videos