pythonyoutubeyoutube-api

python: get all youtube video urls of a channel


I want to get all video url's of a specific channel. I think json with python or java would be a good choice. I can get the newest video with the following code, but how can I get ALL video links (>500)?

import urllib, json
author = 'Youtube_Username'
inp = urllib.urlopen(r'http://gdata.youtube.com/feeds/api/videos?max-results=1&alt=json&orderby=published&author=' + author)
resp = json.load(inp)
inp.close()
first = resp['feed']['entry'][0]
print first['title'] # video title
print first['link'][0]['href'] #url

Solution

  • Increase max-results from 1 to however many you want, but beware they don't advise grabbing too many in one call and will limit you at 50 (https://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters).

    Instead you could consider grabbing the data down in batches of 25, say, by changing the start-index until none came back.

    EDIT: Here's the code for how I would do it

    import urllib, json
    author = 'Youtube_Username'
    
    foundAll = False
    ind = 1
    videos = []
    while not foundAll:
        inp = urllib.urlopen(r'http://gdata.youtube.com/feeds/api/videos?start-index={0}&max-results=50&alt=json&orderby=published&author={1}'.format( ind, author ) )
        try:
            resp = json.load(inp)
            inp.close()
            returnedVideos = resp['feed']['entry']
            for video in returnedVideos:
                videos.append( video ) 
    
            ind += 50
            print len( videos )
            if ( len( returnedVideos ) < 50 ):
                foundAll = True
        except:
            #catch the case where the number of videos in the channel is a multiple of 50
            print "error"
            foundAll = True
    
    for video in videos:
        print video['title'] # video title
        print video['link'][0]['href'] #url