Skip to content

Instantly share code, notes, and snippets.

@JohannSuarez
Created November 29, 2021 02:19
Show Gist options
  • Save JohannSuarez/e98b10ffa54a6e1dc2cf813ba4457feb to your computer and use it in GitHub Desktop.
Save JohannSuarez/e98b10ffa54a6e1dc2cf813ba4457feb to your computer and use it in GitHub Desktop.
Harvest YouTube Playlist Videos
"""
A python script that uses YouTube's API to
harvest a private playlist list.
It grabs video titles, publish dates, uploader information, etc.
You'll need a client_secret.json, which can be obtained here:
https://console.cloud.google.com/
(OAuth 2.0 Client IDs)
"""
# Sample Python code for youtube.playlistItems.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
import json
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"
result_list: list = []
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)
request = youtube.playlistItems().list(
part="snippet",
maxResults=1000,
# Change this to your own playlist.
playlistId="PLPUbnsLOxvidE_IAhdn82YHq2TZwwR-QB"
)
response = request.execute()
# result_list.append(response)
for item in response['items']:
result_list.append(item['snippet']['title'])
print(response)
try:
while(response["nextPageToken"]):
request = youtube.playlistItems().list(
part="snippet",
pageToken=response["nextPageToken"],
maxResults=1000,
# Change this to your own playlist.
playlistId="PLPUbnsLOxvidE_IAhdn82YHq2TZwwR-QB"
)
response = request.execute()
print(response)
# result_list.append(response)
for item in response['items']:
result_list.append(item['snippet']['title'])
except KeyError:
print("End of list.")
with open('result.json', 'w') as fout:
json.dump(result_list, fout)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment