forked from WKS410/Client-Services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyouTube.py
104 lines (93 loc) · 2.93 KB
/
youTube.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import requests
class YouTubeClient:
"""
Cliente para la API de YouTube
"""
BASE_URL = 'https://www.googleapis.com/youtube/v3'
def __init__(self, api_key):
"""
Inicializa el cliente
"""
self.api_key = api_key
def _request(self, method, endpoint, data=None, params=None):
"""
Realiza una solicitud HTTP a la API
"""
url = self.BASE_URL + endpoint
if not params:
params = {}
params['key'] = self.api_key
response = requests.request(method, url, data=data, params=params)
return response
# Recursos de la API
def get_video_info(self, video_id):
"""
Obtiene información detallada de un video por su ID
"""
endpoint = '/videos'
params = {
'part': 'snippet,contentDetails,statistics',
'id': video_id
}
response = self._request('GET', endpoint, params=params)
return response.json()
def get_channel_info(self, channel_id):
"""
Obtiene información detallada de un canal por su ID
"""
endpoint = '/channels'
params = {
'part': 'snippet,statistics',
'id': channel_id
}
response = self._request('GET', endpoint, params=params)
return response.json()
def get_playlist_info(self, playlist_id):
"""
Obtiene información detallada de una lista de reproducción por su ID
"""
endpoint = '/playlists'
params = {
'part': 'snippet',
'id': playlist_id
}
response = self._request('GET', endpoint, params=params)
return response.json()
def get_video_comments(self, video_id, max_results=20):
"""
Obtiene los comentarios de un video por su ID
"""
endpoint = '/commentThreads'
params = {
'part': 'snippet',
'videoId': video_id,
'maxResults': max_results
}
response = self._request('GET', endpoint, params=params)
return response.json()
def get_channel_videos(self, channel_id, max_results=20):
"""
Obtiene los videos de un canal por su ID
"""
endpoint = '/search'
params = {
'part': 'snippet',
'channelId': channel_id,
'type': 'video',
'maxResults': max_results
}
response = self._request('GET', endpoint, params=params)
return response.json()
def get_playlist_videos(self, playlist_id, max_results=20):
"""
Obtiene los videos de una lista de reproducción por su ID
"""
endpoint = '/playlistItems'
params = {
'part': 'snippet',
'playlistId': playlist_id,
'maxResults': max_results
}
response = self._request('GET', endpoint, params=params)
return response.json()