Skip to content

Instantly share code, notes, and snippets.

@eggman
Last active April 4, 2020 05:24
Show Gist options
  • Save eggman/b00f9881ce2cf85ce69cbad9d7990007 to your computer and use it in GitHub Desktop.
Save eggman/b00f9881ce2cf85ce69cbad9d7990007 to your computer and use it in GitHub Desktop.

Get YouTube playlist items

  • python3

prepare

$ pip3 install google-api-client
$ copy > key.json
{ "api_key" : "your api key" }
# -*- coding: utf-8 -*-
# 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 googleapiclient.discovery
import googleapiclient.errors
import json
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
api_service_name = "youtube"
api_version = "v3"
json_open = open('key.json', 'r')
json_load = json.load(json_open)
api_key = json_load['api_key']
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey=api_key)
token=''
while True:
request = youtube.playlistItems().list(
part="snippet",
playlistId="PLTCCfaigD_8rwf7cGbMIJxsBm0iMXPVnr",
maxResults=50,
pageToken=token
)
response = request.execute()
print(response)
# if exist next page
if response.get('nextPageToken'):
token = response['nextPageToken']
else:
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment