pythonyoutube-apiyoutube-data-apiyoutube-livestreaming-api

How to update multiple videos/LiveBroadcasts one by one (using list of their ID's), using YouTube API in Python?


I know that it's impossible to bulk update videos, using YouTube API.

How can I update several videos/LiveBroadcasts in Python by their ID?

I have working script sample, but it applies only on 1st video from list:

###Getting Credentials
***
###GET ID's list OF ALL BROADCASTS  
def main():
    youtube = build('youtube', 'v3', credentials=credentials)
    request = youtube.liveBroadcasts().list(
        part="contentDetails",
        broadcastStatus="all",
        broadcastType="all",
        maxResults=100
        )
    response = request.execute()
    for item in response["items"]:
        response_id=item["id"]
        print(response_id)
###Make all ID of Broadcasts UNLISTED
    request = youtube.liveBroadcasts().update(
            part="status",
            body={
              "id": "{}".format(response_id), #applies only on 1st video ID, first line from "response_id"
              "status": {
                "privacyStatus": "unlisted"
              }
            }
        )
    response = request.execute()
    print(response)    
if __name__ == "__main__":
    main()

print(response_id) is:

1st video ID
2nd video ID
3rd video ID
etc
...

Solution

  • You have an incorrect indentation. You are looking for executing youtube.liveBroadcasts().update function at each iteration in the loop for item in response["items"]: so you should have:

        for item in response["items"]:
            response_id=item["id"]
            print(response_id)
            ###Make all ID of Broadcasts UNLISTED
            request = youtube.liveBroadcasts().update(
                part="status",
                body={
                  "id": "{}".format(response_id), #applies only on 1st video ID, first line from "response_id"
                  "status": {
                    "privacyStatus": "unlisted"
                  }
                }
            )
            response = request.execute()
            print(response) 
    

    Instead of:

        for item in response["items"]:
            response_id=item["id"]
            print(response_id)
    ###Make all ID of Broadcasts UNLISTED
        request = youtube.liveBroadcasts().update(
                part="status",
                body={
                  "id": "{}".format(response_id), #applies only on 1st video ID, first line from "response_id"
                  "status": {
                    "privacyStatus": "unlisted"
                  }
                }
            )
        response = request.execute()
        print(response)    
    

    This is the reason why you have as output the following, as in Python when using a variable assigned in a loop, the last assignment is kept when quitting the loop scope (that's why only the latest video of your list got updated successfully):

    1st video ID
    2nd video ID
    3rd video ID
    etc
    response