Adding a script that can pull a list of stream sources

This commit is contained in:
Bradley Bickford 2023-11-19 11:40:01 -05:00
parent 5919bca2ca
commit ee7751cb97

View File

@ -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()