From ee7751cb97b4ac9ee70694567c37e8719f01cfa6 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Sun, 19 Nov 2023 11:40:01 -0500 Subject: [PATCH] Adding a script that can pull a list of stream sources --- get_live_api_stream_sources.py | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 get_live_api_stream_sources.py diff --git a/get_live_api_stream_sources.py b/get_live_api_stream_sources.py new file mode 100644 index 0000000..7a1b2bd --- /dev/null +++ b/get_live_api_stream_sources.py @@ -0,0 +1,67 @@ +from __future__ import print_function + +import datetime +import os.path +import argparse + +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError +from pprint import pprint + +SCOPES = ["https://www.googleapis.com/auth/youtube"] + +def authorize(): + creds = None + + if os.path.exists('token.json'): + creds = Credentials.from_authorized_user_file('token.json', SCOPES) + + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) + creds = flow.run_local_server(port=7384) + + with open('token.json', 'w') as token: + token.write(creds.to_json()) + + return creds + +def construct_youtube_service(creds): + return build('youtube', 'v3', credentials=creds) + +def list_all_stream_sources(youtube_service): + page_token = None + list_of_entries = [] + + while True: + stream_list = youtube_service.liveStreams().list( + part="snippet", + mine=True, + pageToken=page_token + ).execute() + + list_of_entries.extend(stream_list["items"]) + + page_token = stream_list.get("nextPageToken") + + if not page_token: + break + + return list_of_entries + +def main(): + creds = authorize() + + youtube_service = construct_youtube_service(creds) + + pprint(list_all_stream_sources(youtube_service)) + + # pprint([method_name for method_name in dir(youtube_service) if callable(getattr(youtube_service, method_name))]) + +if __name__ == '__main__': + main() \ No newline at end of file