Last active
October 14, 2023 23:04
-
-
Save jexp/a77399f610b6f42eb5e27e95403b4954 to your computer and use it in GitHub Desktop.
Read correct YouTube Playlist views from the YT data API with Python (here Neo4j Twitch Stream videos)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# https://developers.google.com/explorer-help/guides/code_samples#python | |
# pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib | |
# python playlist.py | |
import os | |
import sys | |
import json | |
import google_auth_oauthlib.flow | |
from google.oauth2.credentials import Credentials | |
import googleapiclient.discovery | |
import googleapiclient.errors | |
from googleapiclient.discovery import build | |
from googleapiclient.errors import HttpError | |
from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.file import Storage | |
from oauth2client.tools import run_flow | |
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" | |
playlistId = "PL9Hl4pk2FsvXjk0hrerr78pLN-477pDLo" | |
if len(sys.argv)>1: | |
playlistId = ",".join(sys.argv[1:]) | |
api_service_name = "youtube" | |
api_version = "v3" | |
# https://stackoverflow.com/questions/52200589/where-to-download-your-client-secret-file-json-file/52222827 | |
client_secrets_file = "client_secret.json" | |
# https://developers.google.com/docs/api/quickstart/python | |
if os.path.exists('token.json'): | |
credentials = Credentials.from_authorized_user_file('token.json') | |
else: | |
# Get credentials and create an API client | |
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file( | |
client_secrets_file, scopes) | |
credentials = flow.run_local_server() | |
with open('token.json', 'w') as token: | |
token.write(credentials.to_json()) | |
youtube = googleapiclient.discovery.build( | |
api_service_name, api_version, credentials=credentials) | |
nextPageToken = None | |
total = 0 | |
count = 0 | |
while True: | |
request = youtube.playlistItems().list( | |
part="id,contentDetails", | |
playlistId=playlistId, | |
maxResults=25, | |
pageToken = nextPageToken | |
) | |
response = request.execute() | |
nextPageToken = response.get("nextPageToken",None) | |
videos = [item["contentDetails"]["videoId"] for item in response["items"]] | |
request = youtube.videos().list( | |
part="snippet,contentDetails,statistics,status", | |
id=",".join(videos) | |
) | |
response = request.execute() | |
# print(response) | |
# video format: https://developers.google.com/youtube/v3/docs/videos#resource | |
for v in response["items"]: | |
row = [v["id"],v["snippet"]["title"],v["snippet"]["publishedAt"],v["statistics"]["viewCount"],\ | |
v["status"]["privacyStatus"]] | |
total += int(row[3]) | |
print("\t".join(row)) | |
count += len(response["items"]) | |
if (nextPageToken is None): | |
break | |
print("%d videos. Total views: %d" % (count, total), file=sys.stderr) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python Noob :)