-
Notifications
You must be signed in to change notification settings - Fork 20
/
youtube_video.py
59 lines (47 loc) · 1.79 KB
/
youtube_video.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
# -*- coding: utf-8 -*-
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "client_secret.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
## please see the link: https://developers.google.com/youtube/v3/docs to get more ways to use this API
## search query
# API only allow maxResults in range [0, 50]
request = youtube.search().list(
q='cat',
type="video",
part="id,snippet",
maxResults=50
)
## videoId
# API only allow maxResults in range [0, 50]
request = youtube.videos().list(
part="snippet, contentDetails, recordingDetails, localizations, statistics",
id="Ks-_Mh1QhMc,c0KYU2j0TM4,eIho2S0ZahI"
)
response = request.execute()
print(response)
## get video info
videos = []
for search_result in response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(search_result)
print(videos)
if __name__ == "__main__":
main()