0

I want to create a random video picker in Python, where you input a (YouTube) channel's name, and it picks a random video from this channel. I saw an internet tutorial, which said to use youtube.channels().list(part="contentDetails",forUsername="GoogleDevelopers") and then take the playlist ID from this and call youtube.playlistItems().list(part="snippet",maxResults=50,playlistId="playlistId"). The problem is: how can I take just the playlist ID from youtube.channels().list(), instead of this long thing it normally outputs? This response is stored in a variable and even if there is no way to get just the playlist ID, is there a way to just read the uploads value from the variable?

The normal output looks like this:

{
    'kind': 'youtube#channelListResponse',
    'etag': 'h612UhyviV63eK7y4HMgXE59VnY',
    'pageInfo':
    {
        'totalResults': 1,
        'resultsPerPage': 5
    },
    'items': [
    {
        'kind': 'youtube#channel',
        'etag': 'tjfVDNBL4GkV4fzZBO9NE36KY5o',
        'id': 'UC_x5XG1OV2P6uZZ5FSM9Ttw',
        'contentDetails':
        {
            'relatedPlaylists':
            {
                'likes': '',
                'uploads': 'UU_x5XG1OV2P6uZZ5FSM9Ttw'
            }
        }
    }]
}

Sorry if my English isn't clear and please tell me to provide any further information.

3
  • You can load the response as json and then parse it? It would be somewhat similar to this problem: stackoverflow.com/questions/27189892/…. Not sure if this is the direction you are looking for? Commented Jan 3, 2022 at 14:25
  • @DSteman I tested the link in the accepted answer in the question you recommended, where it checks if the string is a valid Json string. It looks like it is not. Commented Jan 3, 2022 at 17:38
  • Did the answer work for you? Commented Jan 5, 2022 at 8:07

1 Answer 1

0

If x would be your 'normal output':

some_variable = x['items'][0]['contentDetails']['relatedPlaylists']['uploads']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.