threehappypenguins

modifystreamkey.py

Jul 9th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. from googleapiclient.discovery import build
  2. from google_auth_oauthlib.flow import InstalledAppFlow
  3. from google.auth.transport.requests import Request
  4. from google.oauth2.credentials import Credentials
  5. import os
  6.  
  7. #Set path and filename to read the video id of the PM video.
  8. video_id_text = "/srv/dev-disk-by-uuid-49e4aa1a-279a-45ce-b23e-467c2b2a4162/AppData/jobson/specs/youtube-livestream.spec/video_id.txt"
  9.  
  10. #authentication filepaths and files
  11. ls_token = '/srv/dev-disk-by-uuid-49e4aa1a-279a-45ce-b23e-467c2b2a4162/AppData/youtubelivestream/token.json'
  12. ls_credentials = '/srv/dev-disk-by-uuid-49e4aa1a-279a-45ce-b23e-467c2b2a4162/AppData/youtubelivestream/credentials.json'
  13.  
  14. #Main stream key to change the PM service to after the AM service ends
  15. desired_stream_key = "stream_key_example"
  16.  
  17. # Define the scopes required for accessing the YouTube API
  18. SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
  19.  
  20. # Function to read video IDs from the file
  21. def read_video_ids(filename):
  22. with open(video_id_text, "r") as f:
  23. video_ids = {}
  24. for line in f:
  25. key, value = line.strip().split("=")
  26. video_ids[key.strip()] = value.strip()
  27. return video_ids
  28.  
  29. def get_live_stream_id_by_key(youtube, stream_key):
  30. request = youtube.liveStreams().list(
  31. part="id,cdn",
  32. mine=True,
  33. fields="items(id,cdn/ingestionInfo/streamName)",
  34. maxResults=50
  35. )
  36. response = request.execute()
  37. for item in response.get('items', []):
  38. stream_id = item.get('id')
  39. cdn_info = item.get('cdn', {})
  40. ingestion_info = cdn_info.get('ingestionInfo', {})
  41. if ingestion_info.get('streamName') == stream_key:
  42. return stream_id
  43. return None
  44.  
  45. def main():
  46. # Authenticate using OAuth 2.0
  47. creds = None
  48. if os.path.exists(ls_token):
  49. creds = Credentials.from_authorized_user_file(ls_token)
  50. if not creds or not creds.valid:
  51. if creds and creds.expired and creds.refresh_token:
  52. creds.refresh(Request())
  53. else:
  54. flow = InstalledAppFlow.from_client_secrets_file(
  55. ls_credentials, SCOPES)
  56. creds = flow.run_local_server(port=0)
  57. with open(ls_token, 'w') as token:
  58. token.write(creds.to_json())
  59.  
  60. # Build the YouTube Data API client
  61. youtube = build('youtube', 'v3', credentials=creds)
  62.  
  63. # Read video IDs from the file
  64. video_ids = read_video_ids(video_id_text)
  65.  
  66. # Retrieve video ID 2 from the dictionary
  67. video_id2 = video_ids.get("video_id2")
  68.  
  69. print("Video ID of PM scheduled service:", video_id2)
  70.  
  71. # Retrieve the ID of the live stream associated with the desired stream key
  72. desired_stream_id = get_live_stream_id_by_key(youtube, desired_stream_key)
  73.  
  74. if desired_stream_id:
  75. # Associate the second live video with the existing live stream
  76. youtube.liveBroadcasts().bind(
  77. part="id,contentDetails",
  78. id=video_id2,
  79. streamId=desired_stream_id
  80. ).execute()
  81. print("PM stream key updated successfully.")
  82. else:
  83. print("Livestream not found.")
  84.  
  85. if __name__ == "__main__":
  86. main()
Advertisement
Add Comment
Please, Sign In to add comment