pythonyoutube-apigdata-api

Detect videos with Youtube playback restriction


I am developing a web service using python and i want to filter out the videos which can not be played outside of the youtube page .

Like on this link [https://www.youtube.com/v/SC3pupLn-_8?version=3&f=videos&app=youtube_gdata] you have to watch video on the youtube page is there is any way of filter which videos belong to the same category. So that i choose only those videos which can be played without any restriction.

import gdata.youtube.service
#------------------------------------------------------------------------------ 

yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = 'YOUR API DEVELOPER KEY'

count=0

def PrintEntryDetails(entry):

    if entry.media.category[0].text == "Movies" :
        global count
        count = count + 1
        if  entry.noembed != None:
            print 'Video embedding not enable: %s' % entry.noembed.text
        else :
            print "entry embedable"

        print 'Video title: %s' % entry.media.title.text
        print 'Video category: %s' % entry.media.category[0].text
        print 'Video published on: %s ' % entry.published.text
        print 'Video description: %s' % entry.media.description.text
        if entry.media.private != None :
            print entry.media.private.text
        else :
            print "Right not found"          
        if entry.media.keywords :
            print 'Video tags: %s' % entry.media.keywords.text
        print 'Video watch page: %s' % entry.media.player.url
        print 'Video flash player URL: %s' % entry.GetSwfUrl()
        print 'Video duration: %s' % entry.media.duration.seconds        
        # For video statistics
        if entry.statistics :       
            print 'Video view count: %s' % entry.statistics.view_count
        # For video rating
        if entry.rating  :    
            print 'Video rating: %s' % entry.rating.average
        # show alternate formats
        for alternate_format in entry.media.content:
            if 'isDefault' not in alternate_format.extension_attributes:
                print 'Alternate format: %s | url: %s ' % (alternate_format.type,
                                                     alternate_format.url)
        # show thumbnails
        for thumbnail in entry.media.thumbnail:
            print 'Thumbnail url: %s' % thumbnail.url        
        print "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    else :
        pass

def PrintVideoFeed(feed):
    counter = 0    
    for entry in feed.entry:        
        PrintEntryDetails(entry)
        counter = counter+1
        #print counter   


def SearchAndPrint():
    max = 20    
    yt_service = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()
    # OrderBy must be one of: published viewCount rating relevance 
    query.orderby = "relevance"
    query.racy = 'include'
    query.author = "tseries"
    query.max_results = 50
    index = 01
    for i in (range(max)):        
        query.start_index = index
        index = index + 50
        query.format = "5"
        feed = yt_service.YouTubeQuery(query)    
        PrintVideoFeed(feed)


SearchAndPrint()
print "**********************************************************"
print "Total Movies"
print count

Solution

  • If I understand your question, you're looking for the app:control/yt:state tag. For example, if a video is restricted to playing on the YouTube site, but you're trying to access it through an embedded URL or through a non-browser, you'll get back something like this:

    <app:control>
        <yt:state name="restricted" reasonCode="limitedSyndication">Syndication of this video was restricted.</yt:state>
    </app:control>
    

    You can see this in your entry object as:

    entry.control.FindExtensions('state')[0].attributes
    

    Which will be:

    {'name': 'restricted', 'reasonCode': 'limitedSyndication'}
    

    Of course you need to make this more robust—control may be None, it may have no state tags, etc. But you get the idea.

    I don't think you can directly search on the presence or absence or particular value of state, but you can use the fields parameter to post-filter the results before retrieving them. The docs actually give the example of returning only "entries that do not restrict playback in any way, which is indicated by the presence of the <yt:state> element":

    entry[not(app:control/yt:state)]
    

    I've left off the (title,media:group) part because you want the default tags, not a limited set.

    For some reason, the fields parameter doesn't always get sent. This may be because, as the docs say, "The fields parameter is currently used for experimental features only." But anyway, you can just retrieve everything and filter on control yourself.